forked from aperia/cyclops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.cs
298 lines (249 loc) · 6.8 KB
/
server.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net.Sockets;
using System.Net;
using System.Threading; // not used?
namespace Cyclops
{
class Server
{
static void Main(string[] args)
{
new Server().Run(args);
}
private GameWorld world;
private TcpListener listener;
// TODO: Move to enums.cs or constants.cs
private const ushort ProtoServerOld = 0x0101;
private const ushort ProtoServerNew = 0x0201;
private const ushort ProtoPlayerOld = 0x0000;
private const ushort ProtoPlayerNew = 0x020A;
private const string InvalidNameOrPassword = "Invalid username or password.";
public void Run(string[] args)
{
// TODO: Print version information
#if DEBUG
// TODO: Print "DEBUGGING ON"-message
Log.WriteDebug("Debugging on.");
#endif
// Load configuration
// Remove configuration asm/
if (Directory.Exists("asm"))
Directory.Delete("asm", true);
string configFile = "config.cs";
// Read config file in command line argument
if (args.Length > 0)
{
if (!File.Exists(args[0]))
{
Console.WriteLine("Usage: mono cyclops.exe [config file]");
Console.WriteLine();
return;
}
else
{
configFile = args[0];
}
}
if (!File.Exists(configFile))
{
// TODO: Create new template config file
configFile = "config.cs";
}
Log.WriteBegin("Loading config file (" + configFile + ")...");
try
{
DynamicCompile compiler = new DynamicCompile();
Dictionary<string, string> values = (Dictionary<string, string>) compiler.Compile(configFile, null);
Config.Load(values);
}
catch (Exception e)
{
Log.WriteError(e.ToString());
return;
}
Log.WriteEnd();
// TODO: Write load-message
Log.WriteBegin("Loading items...");
Item.LoadItems();
Log.WriteEnd();
// TODO: Write load-message
Log.WriteBegin("Loading spells...");
Spell.Load();
Log.WriteEnd();
// TODO: Write load-message
Log.WriteBegin("Loading monsters...");
Monster.Load();
Log.WriteEnd();
// TODO: Write load-message
Log.WriteBegin("Loading commands...");
Command.Load();
Log.WriteEnd();
// TODO: Write load-message
Log.WriteBegin("Loading map...");
Map map = Map.Load();
world = new GameWorld(map);
Log.WriteEnd();
// TODO: Write load-message
Log.WriteBegin("Loading non-person characters...");
NPC.Load();
List<NPC> allNPCs = NPC.GetAllNPCs();
foreach (NPC npc in allNPCs)
world.SendAddNPC(npc, npc.CurrentPosition);
Log.WriteEnd();
// TODO: Write load-message
Log.WriteBegin("Loading monster spawns...");
Respawn.Load(world);
Log.WriteEnd();
// TODO: Write load-message (listener)
Log.WriteBegin("Starting TCP listener...");
listener = new TcpListener(IPAddress.Any, Config.GetPort());
listener.Start();
Log.WriteEnd();
Log.WriteLine("Server is now running.");
// TODO: Write message: SERVER NOW RUNNING
AcceptConnections();
}
/// <summary>
/// Accept incoming connection requests.
/// </summary>
private void AcceptConnections()
{
// TODO: Remove!
Player player = new Player(null);
player.Name = "Ivan";
player.Password = Hash.GetSha256("Ivan");
player.CurrentPosition = new Position(32369, 32241, 7);
player.MaxCapacity = 420; //TODO: Verify if correct
player.MagicLevel = 1;
player.MaxHP = 150;
player.CurrentHP = 150;
player.MaxMana = 0;
player.CurrentMana = 0;
player.CurrentVocation = Vocation.NONE;
player.BaseSpeed = 220;
player.SavePlayer();
while (true)
{
#if DEBUG
// TODO: Write message here ("accept connections")
#endif
HandleConnection();
}
}
private void HandleConnection()
{
Socket socket = listener.AcceptSocket();
HandlePlayerConnection(socket, new ProtocolReceive65(socket), new ProtocolSend65(socket));
}
private void HandlePlayerConnection(Socket socket, ProtocolReceive protocolReceive, ProtocolSend protocolSend)
{
LoginInfo playerLogin = protocolReceive.HandlePlayerLogin(socket);
GameWorld localWorld = world;
#if DEBUG
// TODO: Write message here ("Logging in (name/password-hash)...")
#endif
// TODO: Kick current player and let this one log in
if (localWorld.IsPlayerOnline(playerLogin.GetUsername()))
{
protocolSend.Reset();
protocolSend.AddSorryBox("A player with this name is already online.");
protocolSend.MarkSocketAsClosed();
protocolSend.WriteToSocket();
#if DEBUG
// TODO: Write message here ("Player is already online")
#endif
return;
}
Player player = new Player(protocolSend);
if (!player.LoadPlayer(playerLogin))
{
protocolSend.Reset();
protocolSend.AddSorryBox("A player with this name is already online.");
protocolSend.MarkSocketAsClosed();
protocolSend.WriteToSocket();
#if DEBUG
// TODO: Write message here ("Invalid username or password.")
#endif
return;
}
localWorld.SendAddPlayer(player, player.CurrentPosition);
protocolReceive.StartReceiving(world, player);
#if DEBUG
// TODO: Write message here ("Logged in (name)")
#endif
// TODO?: Threading
//new PlayerThread(player, localWorld, protoReceive).StartThread();
}
}
}
/*
namespace Cyclops
{
class Server
{
static void Main(string[] args)
{
// Print distribution information
Tracer.Println("Cyclops 1.0");
Tracer.Println("");
#if DEBUG
Tracer.Println("Debugging: On!");
#endif
// Load configuration
// Remove configuration asm/
if (Directory.Exists("asm"))
Directory.Delete("asm", true);
string configFile = "config.cs";
// Read config file in command line argument
if (args.Length > 0)
{
if (!File.Exists(args[0]))
{
Tracer.Println("Usage: mono cyclops.exe [config file]");
Tracer.Println("");
Pause();
return;
}
else
{
configFile = args[0];
}
}
if (!File.Exists(configFile))
{
// TODO: Create new template config file
configFile = "config.cs";
}
Tracer.Print("Loading configuration [" + configFile + "]: ");
try
{
DynamicCompile compiler = new DynamicCompile();
Dictionary<string, string> values = (Dictionary<string, string>) compiler.Compile(configFile, null);
Config.Load(values);
}
catch (Exception e)
{
Tracer.Println("Failed!");
Tracer.Println("");
Tracer.Println(e.ToString());
Pause();
return;
}
Tracer.Println("Done!");
// Start server
new Server().Start();
Pause();
return;
}
private static void Pause()
{
Tracer.Println("Press any key to continue... ");
Console.ReadLine();
}
}
}
*/