-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathServer.cs
228 lines (194 loc) · 5.98 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
// Berserker: Multi-platform open source game server.
// Copyright (C) 2011 Berserker Group
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
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 Berserker
{
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 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 Berserker.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
{
Compiler compiler = new Compiler();
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)); //old
HandlePlayerConnection(socket, new ProtocolReceive(socket), new ProtocolSend(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();
}
}
}