YAPOG  0.0.1
Yet Another Pokemon Online Game
Server.cpp
Go to the documentation of this file.
30 
31 #include "Server/Server.hpp"
32 #include "Server/ClientSession.hpp"
33 #include "World/Map/MapReader.hpp"
34 #include "World/Map/Map.hpp"
35 #include "World/Map/Player.hpp"
36 #include "World/Map/PlayerReader.hpp"
37 #include "World/Map/NPC.hpp"
38 #include "World/Map/NPCReader.hpp"
39 #include "World/Map/Teleporter.hpp"
40 
41 namespace yse
42 {
43  const bool Server::DEFAULT_RUNNING_STATE = false;
44  const yap::Int16 Server::DEFAULT_PORT = 8008;
45 
46  const float Server::DEFAULT_WORLD_UPDATE_RATE = 100.0f;
47 
49  : isRunning_ (DEFAULT_RUNNING_STATE)
50  , socket_ ()
51  , socketMutex_ ()
52  , listeningThread_ ([&] () { HandleListening (); })
53  , port_ (DEFAULT_PORT)
54  , clients_ ()
55  , clientsMutex_ ()
56  , world_ ()
57  , worldMutex_ ()
63  , databaseManager_ ()
65  {
66  }
67 
68  void Server::Init ()
69  {
70  InitRandom ();
71 
72 #ifndef YAPOG_WIN
73  InitContentManager (yap::Path ("../Content/"));
74 #else
75  InitContentManager (yap::Path ("../../Content/"));
76 #endif // YAPOG_WIN
77 
80 
82 
84 
85  LoadMaps ();
86 
87  if (!socket_.Listen (port_))
89  "Failed to listen port `" +
91  "'.");
92  }
93 
95  {
96  isRunning_ = true;
97 
99 
101 
102  while (isRunning_)
103  {
106 
107  {
108  yap::Lock lock (clientsMutex_);
109 
110  clients_.Refresh ();
111  clients_.ServerTick (dt);
112  }
113 
114  {
115  yap::Lock lock (worldMutex_);
116 
117  world_.Update (dt);
118  }
119 
120  yap::Time sleepTime = yap::Time (1.0f / DEFAULT_WORLD_UPDATE_RATE) - dt;
121  if (sleepTime.GetValue () > 0.0f)
122  yap::Thread::Sleep (sleepTime);
123  }
124 
125  Dispose ();
126  }
127 
128  void Server::Stop ()
129  {
130  isRunning_ = false;
131  }
132 
134  {
135  clients_.Dispose ();
136 
137  socket_.Close ();
138  }
139 
141  {
142  {
143  yap::Lock lock (worldMutex_);
144  client->GetUser ().SetWorld (&world_);
145  }
146 
147  {
150  }
151 
152  client->Init ();
153 
154  {
155  yap::Lock lock (clientsMutex_);
156  clients_.AddClient (client);
157  }
158  }
159 
161  {
162  while (isRunning_)
163  {
164  ClientSession* client = new ClientSession ();
165 
166  if (!socket_.Accept (client->GetSocket ()))
167  YAPOG_THROW("Failed to accept client `" +
168  client->GetSocket ().GetRemoteAddress () +
169  "'.");
170 
172  "Client connected: `" +
173  client->GetSocket ().GetRemoteAddress () +
174  "'.");
175 
176  AddClient (client);
177  }
178  }
179 
181  {
182  yap::RandomHelper::Init (time (nullptr));
183  }
184 
185  void Server::InitContentManager (const yap::Path& contentRootPath)
186  {
187  contentManager_.Init (contentRootPath);
188  }
189 
191  {
193  "Map",
195  yap::Path ("Map"), "Map"));
196 
198  "NPC",
200  yap::Path ("NPC"),
201  "NPC"));
202 
204  "DestructibleObject",
208  yap::Path ("DestructibleObject"),
209  "DestructibleObject"));
210 
212  "Player",
214  yap::Path ("Player"),
215  "Player"));
216 
218  "MapElement",
220  yap::Path ("MapElement"),
221  "MapElement"));
222 
224  "OpenBattleSpawnerArea",
228  yap::Path ("OpenBattleSpawnerArea"),
229  "OpenBattleSpawnerArea"));
230 
232  "Teleporter",
234  yap::Path ("Teleporter"),
235  "Teleporter"));
236 
238  "PokemonInfo",
240  yap::Path ("Pokemon/Pokemon"),
241  "PokemonInfo"));
242 
244  "NatureInfo",
246  yap::Path ("Pokemon/Nature"),
247  "Nature"));
248 
250  "TypeInfo",
252  yap::Path ("Pokemon/Types"),
253  "Type"));
254 
256  "SkillInfo",
258  yap::Path ("Pokemon/Skills"),
259  "Skill"));
260  }
261 
263  {
264  worldObjectStateFactory_.AddState ("Inactive", "Inactive");
265  worldObjectStateFactory_.AddState ("Moving", "Moving");
266  }
267 
269  {
272  }
273 
275  {
276  world_.LoadMaps ();
277  }
278 } // namespace yse