| 1 |
// Copyright © 2001-2004 Rob Kaper <[email protected]>, |
| 2 |
// 2001 Erik Bourget <[email protected]> |
| 3 |
// 2010-2015 Sylvain Rochet <[email protected]> |
| 4 |
// |
| 5 |
// This program is free software; you can redistribute it and/or |
| 6 |
// modify it under the terms of the GNU General Public License |
| 7 |
// version 2 as published by the Free Software Foundation. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
// General Public License for more details. |
| 13 |
// |
| 14 |
// You should have received a copy of the GNU General Public License |
| 15 |
// along with this program; see the file COPYING. If not, see |
| 16 |
// <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
#include "config.h" |
| 19 |
|
| 20 |
#include <dirent.h> |
| 21 |
#include <netdb.h> |
| 22 |
#include <stdio.h> |
| 23 |
#include <stdarg.h> |
| 24 |
#include <sys/time.h> |
| 25 |
#include <sys/socket.h> |
| 26 |
#include <sys/types.h> |
| 27 |
#include <netinet/in.h> |
| 28 |
#include <syslog.h> |
| 29 |
#include <unistd.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <string.h> |
| 32 |
#include <limits.h> |
| 33 |
#include <utf8.h> |
| 34 |
|
| 35 |
#include <string> |
| 36 |
|
| 37 |
#include "socket.h" |
| 38 |
#include "auction.h" |
| 39 |
#include "event.h" |
| 40 |
#include "game.h" |
| 41 |
#include "gameobject.h" |
| 42 |
#include "gameconfig.h" |
| 43 |
#include "io.h" |
| 44 |
#include "player.h" |
| 45 |
#include "server.h" |
| 46 |
|
| 47 |
#if USE_SYSTEMD_DAEMON |
| 48 |
#include <systemd/sd-daemon.h> |
| 49 |
#endif /* USE_SYSTEMD_DAEMON */ |
| 50 |
|
| 51 |
#define METASERVER_PERIOD 180000 |
| 52 |
|
| 53 |
int main(int argc, char **argv) |
| 54 |
{ |
| 55 |
MonopdServer server(argc, argv); |
| 56 |
return 0; |
| 57 |
} |
| 58 |
|
| 59 |
MonopdServer::MonopdServer(int argc, char **argv) : GameObject(0) |
| 60 |
{ |
| 61 |
m_nextGameId = m_nextPlayerId = 1; |
| 62 |
m_port = 1234; |
| 63 |
m_metaserverHost = "meta.atlanticd.net"; |
| 64 |
m_metaserverPort = 1240; |
| 65 |
m_metaserverIdentity = ""; |
| 66 |
m_useMetaserver = false; |
| 67 |
m_metaserverEvent = 0; |
| 68 |
m_metaserverAddrinfo = NULL; |
| 69 |
m_metaserverBusy = false; |
| 70 |
|
| 71 |
openlog("monopd", LOG_PID, LOG_DAEMON); |
| 72 |
syslog(LOG_NOTICE, "monopd %s started", VERSION); |
| 73 |
|
| 74 |
srand((unsigned int)time(0)); |
| 75 |
loadConfig(); |
| 76 |
loadGameTemplates(); |
| 77 |
|
| 78 |
if (argc > 1) { |
| 79 |
m_port = atoi(argv[1]); |
| 80 |
} |
| 81 |
|
| 82 |
m_listener = new Listener(this, m_port); |
| 83 |
|
| 84 |
// Indicate to systemd that we are ready to answer requests |
| 85 |
updateSystemdStatus(); |
| 86 |
|
| 87 |
if (m_useMetaserver) { |
| 88 |
registerMetaserver(); |
| 89 |
|
| 90 |
// Register Metaserver event |
| 91 |
m_metaserverEvent = newEvent(Event::Metaserver); |
| 92 |
m_metaserverEvent->setLaunchTime(METASERVER_PERIOD); |
| 93 |
m_metaserverEvent->setFrequency(METASERVER_PERIOD); |
| 94 |
} |
| 95 |
|
| 96 |
while(1) { |
| 97 |
// Check for network events |
| 98 |
m_listener->checkActivity(); |
| 99 |
// Check for scheduled events |
| 100 |
processEvents(); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
MonopdServer::~MonopdServer() |
| 105 |
{ |
| 106 |
// We are responsible for the objects allocated |
| 107 |
while (!m_events.empty()) { delete *m_events.begin(); m_events.erase(m_events.begin()); } |
| 108 |
while (!m_games.empty()) { delete *m_games.begin(); m_games.erase(m_games.begin()); } |
| 109 |
while (!m_gameConfigs.empty()) { delete *m_gameConfigs.begin(); m_gameConfigs.erase(m_gameConfigs.begin()); } |
| 110 |
while (!m_players.empty()) { delete *m_players.begin(); m_players.erase(m_players.begin()); } |
| 111 |
} |
| 112 |
|
| 113 |
Event *MonopdServer::newEvent(unsigned int eventType, Game *game, int id) |
| 114 |
{ |
| 115 |
Event *newEvent = new Event(id, (Event::EventType)eventType, game); |
| 116 |
m_events.push_back(newEvent); |
| 117 |
printf("newEvent %d/%d\n", id, (int)m_events.size()); |
| 118 |
return newEvent; |
| 119 |
} |
| 120 |
|
| 121 |
void MonopdServer::delEvent(Event *event) |
| 122 |
{ |
| 123 |
for(std::vector<Event *>::iterator it = m_events.begin(); it != m_events.end() && (*it) ; ++it) |
| 124 |
if (*it == event) |
| 125 |
{ |
| 126 |
printf("delEvent %d/%d\n", event->id(), (int)m_events.size()-1); |
| 127 |
delete event; |
| 128 |
m_events.erase(it); |
| 129 |
break; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
Event *MonopdServer::findEvent(Game *game, unsigned int eventType) |
| 134 |
{ |
| 135 |
Event *event = 0; |
| 136 |
for(std::vector<Event *>::iterator it = m_events.begin(); it != m_events.end() && (event = *it) ; ++it) |
| 137 |
if (event->game() == game && event->type() == eventType) |
| 138 |
return event; |
| 139 |
|
| 140 |
return 0; |
| 141 |
} |
| 142 |
|
| 143 |
Event *MonopdServer::findEvent(Game *game, GameObject *object) |
| 144 |
{ |
| 145 |
for(std::vector<Event *>::iterator it = m_events.begin(); it != m_events.end() && (*it) ; ++it) |
| 146 |
if ( (*it)->game() == game && (*it)->object() == object) |
| 147 |
return (*it); |
| 148 |
|
| 149 |
return 0; |
| 150 |
} |
| 151 |
|
| 152 |
void MonopdServer::newGame(Player *player, const std::string gameType) |
| 153 |
{ |
| 154 |
Game *game = new Game(m_nextGameId++); |
| 155 |
m_games.push_back(game); |
| 156 |
|
| 157 |
game->setProperty("name", (gameType.size() ? gameType : "atlantic"), this); |
| 158 |
game->setProperty("gametype", gameType, this); |
| 159 |
game->setProperty("status", "config", this); |
| 160 |
game->setProperty("turn", 0, this); |
| 161 |
|
| 162 |
// Hardcoded reasonable defaults, which also ensure the entire server is the scope |
| 163 |
game->setProperty("master", -1, this); // addPlayer will set the correct playerid |
| 164 |
game->setProperty("description", "", this); |
| 165 |
game->setProperty("minplayers", 1, this); |
| 166 |
game->setProperty("maxplayers", 1, this); |
| 167 |
game->setProperty("players", 0, this); |
| 168 |
game->setBoolProperty("canbejoined", true, this); |
| 169 |
game->setBoolProperty("canbewatched", false, this); |
| 170 |
|
| 171 |
game->loadGameType(gameType); |
| 172 |
|
| 173 |
if (!game->isValid()) |
| 174 |
{ |
| 175 |
delGame(game, false); |
| 176 |
player->ioWrite("<monopd><msg type=\"error\" value=\"Could not load valid configuration for gametype %s.\"/></monopd>\n", gameType.c_str()); |
| 177 |
m_nextGameId--; |
| 178 |
return; |
| 179 |
} |
| 180 |
|
| 181 |
syslog( LOG_INFO, "new game: id=[%d], type=[%s], games=[%d]", game->id(), gameType.c_str(), (int)m_games.size() ); |
| 182 |
|
| 183 |
game->addPlayer(player, false, true); |
| 184 |
|
| 185 |
updateSystemdStatus(); |
| 186 |
} |
| 187 |
|
| 188 |
void MonopdServer::joinGame(Player *pInput, unsigned int gameId, const bool &spectator) |
| 189 |
{ |
| 190 |
Game *game = findGame(gameId); |
| 191 |
if (!game) |
| 192 |
{ |
| 193 |
pInput->ioError("There is no game with id %d.", gameId); |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
if ( spectator ) |
| 198 |
{ |
| 199 |
GameObject *config = game->findConfigOption( "allowspectators" ); |
| 200 |
if ( config && config->getBoolProperty( "value" ) && game->status() == Game::Run ) |
| 201 |
{ |
| 202 |
game->addPlayer(pInput, true); |
| 203 |
game->ioInfo("%s joins as spectator.", pInput->name().c_str()); |
| 204 |
} |
| 205 |
else |
| 206 |
pInput->ioError("Game %d doesn't allow spectators.", gameId); |
| 207 |
return; |
| 208 |
} |
| 209 |
|
| 210 |
int maxPlayers = game->getIntProperty("maxplayers"); |
| 211 |
if (game->players() >= maxPlayers) |
| 212 |
{ |
| 213 |
pInput->ioError("This game already has the maximum of %d players.", maxPlayers); |
| 214 |
return; |
| 215 |
} |
| 216 |
|
| 217 |
if (game->status() != Game::Config) |
| 218 |
{ |
| 219 |
pInput->ioError("You cannot join game %d, it is already in progress.", game->id()); |
| 220 |
return; |
| 221 |
} |
| 222 |
|
| 223 |
game->addPlayer(pInput); |
| 224 |
} |
| 225 |
|
| 226 |
void MonopdServer::exitGame(Game *game, Player *pInput) |
| 227 |
{ |
| 228 |
game->removePlayer(pInput); |
| 229 |
game->ioInfo("%s left the game.", pInput->name().c_str()); |
| 230 |
if (game->players(true) == 0) { |
| 231 |
delGame(game); |
| 232 |
} |
| 233 |
|
| 234 |
pInput->reset(); |
| 235 |
} |
| 236 |
|
| 237 |
Game *MonopdServer::findGame(int gameId) |
| 238 |
{ |
| 239 |
Game *game = 0; |
| 240 |
for(std::vector<Game *>::iterator it = m_games.begin(); it != m_games.end() && (game = *it) ; ++it) |
| 241 |
if (game->id() == gameId) |
| 242 |
return game; |
| 243 |
|
| 244 |
return 0; |
| 245 |
} |
| 246 |
|
| 247 |
void MonopdServer::delGame(Game *game, bool verbose) |
| 248 |
{ |
| 249 |
if (game->players(true)) { |
| 250 |
return; |
| 251 |
} |
| 252 |
|
| 253 |
if (verbose) |
| 254 |
ioWrite("<monopd><deletegame gameid=\"" + itoa(game->id()) + "\"/></monopd>\n"); |
| 255 |
|
| 256 |
// Remove events from game |
| 257 |
for(std::vector<Event *>::iterator it = m_events.begin(); it != m_events.end() && (*it) ; ) |
| 258 |
{ |
| 259 |
if ( (*it)->game() == game ) |
| 260 |
{ |
| 261 |
delEvent( (*it) ); |
| 262 |
it = m_events.begin(); |
| 263 |
continue; |
| 264 |
} |
| 265 |
++it; |
| 266 |
} |
| 267 |
|
| 268 |
// Remove game |
| 269 |
for(std::vector<Game *>::iterator it = m_games.begin(); it != m_games.end() && (*it) ; ++it) |
| 270 |
if (*it == game) |
| 271 |
{ |
| 272 |
syslog( LOG_INFO, "del game: id=[%d], games=[%d]", game->id(), (int)m_games.size() - 1 ); |
| 273 |
m_games.erase(it); |
| 274 |
break; |
| 275 |
} |
| 276 |
delete game; |
| 277 |
|
| 278 |
updateSystemdStatus(); |
| 279 |
} |
| 280 |
|
| 281 |
void MonopdServer::setGameDescription(Player *pInput, std::string data) |
| 282 |
{ |
| 283 |
// truncate description to 64 characters |
| 284 |
try { |
| 285 |
std::string::iterator it = data.begin(); |
| 286 |
utf8::advance(it, 64, data.end()); |
| 287 |
data.erase(it, data.end()); |
| 288 |
} |
| 289 |
catch (const utf8::not_enough_room &e) { |
| 290 |
} |
| 291 |
|
| 292 |
Game *game = pInput->game(); |
| 293 |
if (pInput == game->master()) |
| 294 |
{ |
| 295 |
game->setProperty("description", data); |
| 296 |
} |
| 297 |
else |
| 298 |
pInput->ioError("Only the master can set the game description!"); |
| 299 |
} |
| 300 |
|
| 301 |
void MonopdServer::reconnectPlayer(Player *pInput, const std::string &cookie) |
| 302 |
{ |
| 303 |
Player *player = findCookie(cookie); |
| 304 |
if (!player) |
| 305 |
{ |
| 306 |
pInput->ioError("Invalid cookie."); |
| 307 |
return; |
| 308 |
} |
| 309 |
|
| 310 |
Game *game = player->game(); |
| 311 |
if (!game) { |
| 312 |
pInput->ioError("Invalid cookie."); |
| 313 |
return; |
| 314 |
} |
| 315 |
|
| 316 |
if (game->status() != Game::Run) { |
| 317 |
pInput->ioError("Game is not running."); |
| 318 |
return; |
| 319 |
} |
| 320 |
|
| 321 |
if (player->socket()) { |
| 322 |
pInput->ioError("Cannot reconnect, target player already has a socket."); |
| 323 |
return; |
| 324 |
} |
| 325 |
|
| 326 |
pInput->ioInfo("Reconnecting."); |
| 327 |
player->setSocket(pInput->socket()); |
| 328 |
player->sendClientMsg(); |
| 329 |
|
| 330 |
game->sendStatus(Game::Init, player); |
| 331 |
game->sendFullUpdate(player); |
| 332 |
game->sendStatus(Game::Run, player); |
| 333 |
player->sendDisplayHistory(); |
| 334 |
|
| 335 |
game->ioInfo("%s reconnected.", player->name().c_str()); |
| 336 |
pInput->setSocket(0); |
| 337 |
delPlayer(pInput); |
| 338 |
} |
| 339 |
|
| 340 |
void MonopdServer::delPlayer(Player *player) |
| 341 |
{ |
| 342 |
Game *game = player->game(); |
| 343 |
if (game) { |
| 344 |
game->removePlayer(player); |
| 345 |
if (game->players(true) == 0) { |
| 346 |
delGame(game); |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
if (player->id() > 0) { |
| 351 |
ioWrite("<monopd><deleteplayer playerid=\"" + itoa(player->id()) + "\"/></monopd>\n"); |
| 352 |
} |
| 353 |
|
| 354 |
for(std::vector<Player *>::iterator it = m_players.begin(); it != m_players.end() && (*it) ; ++it) |
| 355 |
if (*it == player) |
| 356 |
{ |
| 357 |
removeFromScope(player); |
| 358 |
syslog( LOG_INFO, "del player: id=[%d], fd=[%d], name=[%s], players=[%d]", player->id(), player->socket() ? player->socket()->fd() : 0, player->getStringProperty("name").c_str(), (int)m_players.size() - 1 ); |
| 359 |
printf("delPlayer %d/%d\n", player->id(), (int)m_players.size()-1); |
| 360 |
delete player; |
| 361 |
m_players.erase(it); |
| 362 |
player = 0; |
| 363 |
break; |
| 364 |
} |
| 365 |
|
| 366 |
for(std::vector<Player *>::iterator it = m_players.begin(); it != m_players.end() && (player = *it) ; ++it) |
| 367 |
printf(" player %16s %16s game %d bankrupt %d socket fd %d\n", player->name().c_str(), player->getStringProperty("host").c_str(), (player->game() ? player->game()->id() : -1), player->getBoolProperty("bankrupt"), player->socket() ? (int)player->socket()->fd() : -1); |
| 368 |
|
| 369 |
updateSystemdStatus(); |
| 370 |
} |
| 371 |
|
| 372 |
Player *MonopdServer::findPlayer(int playerId) |
| 373 |
{ |
| 374 |
Player *player = 0; |
| 375 |
for(std::vector<Player *>::iterator it = m_players.begin(); it != m_players.end() && (player = *it) ; ++it) |
| 376 |
if (player->id() == playerId) |
| 377 |
return player; |
| 378 |
|
| 379 |
return 0; |
| 380 |
} |
| 381 |
|
| 382 |
Player *MonopdServer::findPlayer(Socket *socket) |
| 383 |
{ |
| 384 |
Player *player = 0; |
| 385 |
for(std::vector<Player *>::iterator it = m_players.begin(); it != m_players.end() && (player = *it) ; ++it) |
| 386 |
if (player->socket() == socket) |
| 387 |
return player; |
| 388 |
|
| 389 |
return 0; |
| 390 |
} |
| 391 |
|
| 392 |
Player *MonopdServer::findPlayer(const std::string &name) |
| 393 |
{ |
| 394 |
Player *player = 0; |
| 395 |
for(std::vector<Player *>::iterator it = m_players.begin(); it != m_players.end() && (player = *it) ; ++it) |
| 396 |
if (player->getStringProperty("name") == name) |
| 397 |
return player; |
| 398 |
|
| 399 |
return 0; |
| 400 |
} |
| 401 |
|
| 402 |
Player *MonopdServer::findCookie(const std::string &cookie) |
| 403 |
{ |
| 404 |
Player *player = 0; |
| 405 |
for(std::vector<Player *>::iterator it = m_players.begin(); it != m_players.end() && (player = *it) ; ++it) |
| 406 |
if (player->getStringProperty("cookie") == cookie) |
| 407 |
return player; |
| 408 |
|
| 409 |
return 0; |
| 410 |
} |
| 411 |
|
| 412 |
GameConfig *MonopdServer::newGameConfig(const std::string id, const std::string name, const std::string description) |
| 413 |
{ |
| 414 |
GameConfig *newGameConfig = new GameConfig(id, name, description); |
| 415 |
m_gameConfigs.push_back(newGameConfig); |
| 416 |
syslog( LOG_INFO, "loaded game configuration: game=[%s]", name.c_str() ); |
| 417 |
return newGameConfig; |
| 418 |
} |
| 419 |
|
| 420 |
void MonopdServer::delGameConfig(GameConfig *gameConfig) |
| 421 |
{ |
| 422 |
for(std::vector<GameConfig *>::iterator it = m_gameConfigs.begin(); it != m_gameConfigs.end() && (*it) ; ++it) |
| 423 |
if (*it == gameConfig) |
| 424 |
{ |
| 425 |
delete gameConfig; |
| 426 |
m_gameConfigs.erase(it); |
| 427 |
break; |
| 428 |
} |
| 429 |
} |
| 430 |
|
| 431 |
void MonopdServer::closedSocket(Socket *socket) |
| 432 |
{ |
| 433 |
Player *pInput = findPlayer(socket); |
| 434 |
|
| 435 |
Game *game = pInput->game(); |
| 436 |
if (!game) { |
| 437 |
delPlayer(pInput); |
| 438 |
sendXMLUpdates(); |
| 439 |
return; |
| 440 |
} |
| 441 |
|
| 442 |
printf("%s socket 0 spec %d, bankrupt %d, gamerun %d\n", pInput->name().c_str(), pInput->getBoolProperty("spectator"), pInput->getBoolProperty("bankrupt"), game->status() == Game::Run ); |
| 443 |
|
| 444 |
// Only remove from game when game not running, or when it's merely a spectator. |
| 445 |
if (game->status() != Game::Run || pInput->getBoolProperty("spectator") || pInput->getBoolProperty("bankrupt")) { |
| 446 |
game->ioInfo("Connection with %s lost.", pInput->name().c_str()); |
| 447 |
printf("exit from game %d: %d\n", game->id(), pInput->id()); |
| 448 |
delPlayer(pInput); |
| 449 |
sendXMLUpdates(); |
| 450 |
return; |
| 451 |
} |
| 452 |
|
| 453 |
printf("may reconnect\n"); |
| 454 |
pInput->setSocket(NULL); |
| 455 |
unsigned int timeout = 180000; |
| 456 |
game->ioInfo("Connection with %s lost. Player has %ds to reconnect until bankruptcy.", pInput->name().c_str(), timeout/1000); |
| 457 |
Event *event = newEvent( Event::PlayerTimeout, game ); |
| 458 |
event->setLaunchTime(timeout); |
| 459 |
event->setObject( dynamic_cast<GameObject *> (pInput) ); |
| 460 |
sendXMLUpdates(); |
| 461 |
} |
| 462 |
|
| 463 |
int MonopdServer::timeleftEvent() |
| 464 |
{ |
| 465 |
int timeleft = INT_MAX; |
| 466 |
struct timeval timenow; |
| 467 |
gettimeofday(&timenow, NULL); |
| 468 |
|
| 469 |
Event *event = NULL; |
| 470 |
for (std::vector<Event *>::iterator it = m_events.begin() ; it != m_events.end() && (event = *it) ; ++it) { |
| 471 |
struct timeval timeres; |
| 472 |
int timems; |
| 473 |
|
| 474 |
if (timercmp(&timenow, event->launchTime(), >)) { |
| 475 |
return 0; |
| 476 |
} |
| 477 |
|
| 478 |
timersub(event->launchTime(), &timenow, &timeres); |
| 479 |
// add 1ms to adjust for precision lost in tv_usec/1000 |
| 480 |
timems = (timeres.tv_sec*1000 + timeres.tv_usec/1000) +1; |
| 481 |
if (timeleft > timems) { |
| 482 |
timeleft = timems; |
| 483 |
} |
| 484 |
} |
| 485 |
if (!event) { |
| 486 |
return -1; |
| 487 |
} |
| 488 |
|
| 489 |
return timeleft; |
| 490 |
} |
| 491 |
|
| 492 |
void MonopdServer::processEvents() |
| 493 |
{ |
| 494 |
struct timeval tv; |
| 495 |
gettimeofday(&tv, NULL); |
| 496 |
|
| 497 |
Event *event = 0; |
| 498 |
for (std::vector<Event *>::iterator it = m_events.begin(); it != m_events.end() && (event = *it);) { |
| 499 |
if (timercmp(event->launchTime(), &tv, >)) { |
| 500 |
++it; // next event |
| 501 |
continue; |
| 502 |
} |
| 503 |
|
| 504 |
Game *game = event->game(); |
| 505 |
Socket *delSocket; |
| 506 |
|
| 507 |
switch(event->type()) |
| 508 |
{ |
| 509 |
case Event::TokenMovementTimeout: |
| 510 |
event->setFrequency(0); |
| 511 |
if (game) { |
| 512 |
game->tokenMovementTimeout(); |
| 513 |
if (game->clientsMoving()) { |
| 514 |
event->setFrequency(1000); |
| 515 |
} |
| 516 |
} |
| 517 |
break; |
| 518 |
case Event::SocketTimeout: |
| 519 |
delSocket = m_listener->findSocket(event->id()); |
| 520 |
if (delSocket) { |
| 521 |
delSocket->setStatus(Socket::Close); |
| 522 |
} |
| 523 |
break; |
| 524 |
case Event::AuctionTimeout: |
| 525 |
if (game) { |
| 526 |
event->setFrequency(game->auctionTimeout()); |
| 527 |
} |
| 528 |
break; |
| 529 |
case Event::Metaserver: |
| 530 |
registerMetaserver(); |
| 531 |
break; |
| 532 |
case Event::PlayerTimeout: |
| 533 |
GameObject *object = event->object(); |
| 534 |
if (!object) |
| 535 |
break; |
| 536 |
|
| 537 |
Player *player; |
| 538 |
player = findPlayer(object->id()); |
| 539 |
if (player) { |
| 540 |
if (player->socket()) { |
| 541 |
break; |
| 542 |
} |
| 543 |
|
| 544 |
Game *game = player->game(); |
| 545 |
if (game->status() == Game::Run) { |
| 546 |
game->ioInfo("%s did not reconnect in time and is now bankrupt.", player->name().c_str()); |
| 547 |
} |
| 548 |
|
| 549 |
// delPlayer() might call delGame() if player is the last player in game, which remove |
| 550 |
// all events from game, therefore we don't know whether delPlayer() is going to |
| 551 |
// remove this event. Remove it before to prevent a double free. |
| 552 |
delEvent(event); |
| 553 |
|
| 554 |
delPlayer(player); |
| 555 |
|
| 556 |
// damn vectors |
| 557 |
it = m_events.begin(); |
| 558 |
continue; // apply to for() loop |
| 559 |
} |
| 560 |
break; |
| 561 |
} |
| 562 |
|
| 563 |
if (event->frequency() == 0) { |
| 564 |
// Delete event from event list |
| 565 |
delEvent(event); |
| 566 |
// damn vectors |
| 567 |
it = m_events.begin(); |
| 568 |
continue; |
| 569 |
} |
| 570 |
|
| 571 |
event->setLaunchTime(event->frequency()); |
| 572 |
++it; // next event |
| 573 |
} |
| 574 |
|
| 575 |
sendXMLUpdates(); |
| 576 |
} |
| 577 |
|
| 578 |
void MonopdServer::registerMetaserver() |
| 579 |
{ |
| 580 |
if (m_metaserverBusy) |
| 581 |
return; |
| 582 |
|
| 583 |
/* Don't ruin games, only try getaddrinfo() if no games are currently running */ |
| 584 |
if (m_metaserverAddrinfo == NULL || m_games.size() == 0) { |
| 585 |
struct addrinfo hints; |
| 586 |
struct addrinfo *result; |
| 587 |
char port_str[6]; |
| 588 |
int r; |
| 589 |
|
| 590 |
memset(&hints, 0, sizeof(struct addrinfo)); |
| 591 |
hints.ai_family = AF_UNSPEC; |
| 592 |
hints.ai_socktype = SOCK_STREAM; |
| 593 |
|
| 594 |
snprintf(port_str, sizeof(port_str), "%d", m_metaserverPort); |
| 595 |
/* |
| 596 |
* getaddrinfo() is synchronous and might block, this is still less worse |
| 597 |
* than a blocking connect(), DNS resolution are less likely to fail. |
| 598 |
* |
| 599 |
* Furthermore, we are only calling getaddrinfo() when no games are |
| 600 |
* currently running. |
| 601 |
*/ |
| 602 |
r = getaddrinfo(m_metaserverHost.c_str(), port_str, &hints, &result); |
| 603 |
if (r != 0) { |
| 604 |
syslog(LOG_INFO, "getaddrinfo() failed: error=[%s]", gai_strerror(r)); |
| 605 |
/* use previous result */ |
| 606 |
} else { |
| 607 |
if (m_metaserverAddrinfo) |
| 608 |
freeaddrinfo(m_metaserverAddrinfo); |
| 609 |
m_metaserverAddrinfo = result; |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
Socket *socket = m_listener->connectSocket(m_metaserverAddrinfo); |
| 614 |
if (socket) |
| 615 |
m_metaserverBusy = true; |
| 616 |
} |
| 617 |
|
| 618 |
void MonopdServer::loadConfig() |
| 619 |
{ |
| 620 |
FILE *f = fopen(MONOPD_CONFIGDIR "/monopd.conf", "r"); |
| 621 |
if (!f) |
| 622 |
{ |
| 623 |
syslog( LOG_WARNING, "cannot open configuration: file=[" MONOPD_CONFIGDIR "/monopd.conf], error=[%s], using default values", strerror(errno)); |
| 624 |
return; |
| 625 |
} |
| 626 |
|
| 627 |
char str[1024], *buf, *ret; |
| 628 |
|
| 629 |
for (ret = fgets(str, sizeof(str), f); ret && !feof(f); ret = fgets(str, sizeof(str), f)) { |
| 630 |
if (str[0] == '#') { |
| 631 |
continue; |
| 632 |
} |
| 633 |
|
| 634 |
if (strstr(str, "=")) { |
| 635 |
buf = strtok(str, "="); |
| 636 |
if (!strcmp(buf, "port")) |
| 637 |
m_port = atoi(strtok(NULL, "\n\0")); |
| 638 |
else if (!strcmp(buf, "metaserverhost")) |
| 639 |
m_metaserverHost = strtok(NULL, "\n\0"); |
| 640 |
else if (!strcmp(buf, "metaserverport")) |
| 641 |
m_metaserverPort = atoi(strtok(NULL, "\n\0")); |
| 642 |
else if (!strcmp(buf, "metaserveridentity")) { |
| 643 |
m_metaserverIdentity = strtok(NULL, "\n\0"); |
| 644 |
m_useMetaserver = true; |
| 645 |
} |
| 646 |
} |
| 647 |
} |
| 648 |
fclose(f); |
| 649 |
} |
| 650 |
|
| 651 |
void MonopdServer::loadGameTemplates() |
| 652 |
{ |
| 653 |
DIR *dirp; |
| 654 |
struct dirent *direntp; |
| 655 |
|
| 656 |
dirp = opendir(MONOPD_DATADIR "/games/"); |
| 657 |
if (!dirp) { |
| 658 |
syslog( LOG_ERR, "cannot open game directory, dir=[" MONOPD_DATADIR "/games/], error=[%s]", strerror(errno) ); |
| 659 |
#if USE_SYSTEMD_DAEMON |
| 660 |
sd_notifyf(1, "STATUS=Failed to start: cannot open game directory, dir=[" MONOPD_DATADIR "/games/], error=[%s]\nERRNO=%d", strerror(errno), errno); |
| 661 |
usleep(100000); |
| 662 |
#endif /* USE_SYSTEMD_DAEMON */ |
| 663 |
exit(-1); |
| 664 |
} |
| 665 |
while ((direntp=readdir(dirp)) != NULL) { |
| 666 |
char str[256], *buf, *ret; |
| 667 |
std::string name, description; |
| 668 |
|
| 669 |
if (!strstr(direntp->d_name, ".conf")) { |
| 670 |
continue; |
| 671 |
} |
| 672 |
|
| 673 |
std::string filename = std::string(MONOPD_DATADIR) + "/games/" + direntp->d_name; |
| 674 |
FILE *f = fopen(filename.c_str(), "r"); |
| 675 |
if (!f) { |
| 676 |
syslog( LOG_WARNING, "cannot open game configuration: file=[%s], error=[%s]", filename.c_str(), strerror(errno) ); |
| 677 |
continue; |
| 678 |
} |
| 679 |
|
| 680 |
for (ret = fgets(str, sizeof(str), f); ret && !feof(f); ret = fgets(str, sizeof(str), f)) { |
| 681 |
if (str[0] == '#') { |
| 682 |
continue; |
| 683 |
} |
| 684 |
|
| 685 |
if (!utf8::is_valid(str, str+strlen(str))) { |
| 686 |
syslog( LOG_WARNING, "cannot open game configuration, file is not proper UTF-8: file=[%s]", filename.c_str() ); |
| 687 |
goto abort; |
| 688 |
} |
| 689 |
|
| 690 |
if (strstr(str, "=")) { |
| 691 |
buf = strtok(str, "="); |
| 692 |
if (!strcmp(buf, "name")) |
| 693 |
name = strtok(NULL, "\n\0"); |
| 694 |
else if (!strcmp(buf, "description")) |
| 695 |
description = strtok(NULL, "\n\0"); |
| 696 |
} |
| 697 |
} |
| 698 |
newGameConfig(strtok(direntp->d_name, "."), (name.size() ? name : strtok(direntp->d_name, ".")), (description.size() ? description : "No description available")); |
| 699 |
abort: |
| 700 |
fclose(f); |
| 701 |
} |
| 702 |
closedir(dirp); |
| 703 |
} |
| 704 |
|
| 705 |
void MonopdServer::welcomeNew(Socket *socket) |
| 706 |
{ |
| 707 |
Player *player = new Player(socket); |
| 708 |
m_players.push_back(player); |
| 709 |
addToScope(player); |
| 710 |
|
| 711 |
player->ioWrite( std::string("<monopd><server host=\"") + m_metaserverIdentity + "\" version=\"" VERSION "\"/></monopd>\n" ); |
| 712 |
sendGameTemplateList(player); |
| 713 |
sendXMLUpdate(player, true, true); // give new player a full update (excluding self) so it knows who's in the lounge |
| 714 |
} |
| 715 |
|
| 716 |
void MonopdServer::welcomeMetaserver(Socket *socket) |
| 717 |
{ |
| 718 |
socket->ioWrite( std::string("REGISTER ") + m_metaserverIdentity + " " + itoa(m_port) + " " + VERSION + "\n"); |
| 719 |
|
| 720 |
/* |
| 721 |
* sockets are not blocking, write() may be delayed and data buffered so wait a little |
| 722 |
* bit before closing the session. Sure this is not perfect, we don't have a close when |
| 723 |
* output buffer empty trigger yet, is it worth complicating this? |
| 724 |
*/ |
| 725 |
Event *socketTimeout = newEvent(Event::SocketTimeout, 0, socket->fd()); |
| 726 |
socketTimeout->setLaunchTime(5000); |
| 727 |
} |
| 728 |
|
| 729 |
void MonopdServer::closedMetaserver(Socket *socket) |
| 730 |
{ |
| 731 |
(void)socket; |
| 732 |
m_metaserverBusy = false; |
| 733 |
} |
| 734 |
|
| 735 |
void MonopdServer::ioWrite(const char *fmt, ...) |
| 736 |
{ |
| 737 |
int n, size = 256; |
| 738 |
char *buf = new char[size]; |
| 739 |
static std::string ioStr; |
| 740 |
va_list arg; |
| 741 |
|
| 742 |
buf[0] = 0; |
| 743 |
|
| 744 |
while (1) |
| 745 |
{ |
| 746 |
va_start(arg, fmt); |
| 747 |
n = vsnprintf(buf, size, fmt, arg); |
| 748 |
va_end(arg); |
| 749 |
|
| 750 |
if (n > -1 && n < size) |
| 751 |
{ |
| 752 |
ioStr = buf; |
| 753 |
delete[] buf; |
| 754 |
ioWrite(ioStr); |
| 755 |
return; |
| 756 |
} |
| 757 |
|
| 758 |
if (n > -1) |
| 759 |
size = n+1; |
| 760 |
else |
| 761 |
size *= 2; |
| 762 |
|
| 763 |
delete[] buf; |
| 764 |
buf = new char[size]; |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
void MonopdServer::ioWrite(const std::string &data, const bool &noGameOnly) |
| 769 |
{ |
| 770 |
Player *player = 0; |
| 771 |
for(std::vector<Player *>::iterator it = m_players.begin(); it != m_players.end() && (player = *it) ; ++it) |
| 772 |
if ( !(noGameOnly && player->game()) ) |
| 773 |
player->ioWrite(data); |
| 774 |
} |
| 775 |
|
| 776 |
void MonopdServer::sendGameTemplateList(Player *pInput) |
| 777 |
{ |
| 778 |
pInput->ioWrite("<monopd>"); |
| 779 |
|
| 780 |
// Supported game types for new games |
| 781 |
GameConfig *gcTmp = 0; |
| 782 |
for(std::vector<GameConfig *>::iterator it = m_gameConfigs.begin() ; it != m_gameConfigs.end() && (gcTmp = *it) ; ++it ) |
| 783 |
pInput->ioWrite("<gameupdate gameid=\"-1\" gametype=\"%s\" name=\"%s\" description=\"%s\"/>", gcTmp->id().c_str(), gcTmp->name().c_str(), gcTmp->description().c_str()); |
| 784 |
|
| 785 |
pInput->ioWrite("</monopd>\n"); |
| 786 |
} |
| 787 |
|
| 788 |
void MonopdServer::updateSystemdStatus() { |
| 789 |
#if USE_SYSTEMD_DAEMON |
| 790 |
/* Update systemd status string */ |
| 791 |
sd_notifyf(0, "READY=1\nSTATUS=Running, %d players, %d games", (int)m_players.size(), (int)m_games.size() ); |
| 792 |
#endif /* USE_SYSTEMD_DAEMON */ |
| 793 |
} |
| 794 |
|
| 795 |
void MonopdServer::processInput(Socket *socket, const std::string data2) |
| 796 |
{ |
| 797 |
char *data = (char*)data2.c_str(); |
| 798 |
Player *pInput = findPlayer(socket); |
| 799 |
Game *game = pInput->game(); |
| 800 |
|
| 801 |
if (!utf8::is_valid(data2.begin(), data2.end())) { |
| 802 |
pInput->ioError("Input is not proper UTF-8"); |
| 803 |
return; |
| 804 |
} |
| 805 |
|
| 806 |
// The following commands are _always_ available. |
| 807 |
if (data[0] == '.') { |
| 808 |
switch (data[1]) { |
| 809 |
|
| 810 |
case 'n': |
| 811 |
// The 'n' name command is available even for non-players. |
| 812 |
// In fact, it's considered to be the protocol handshake. |
| 813 |
setPlayerName(pInput, data2.substr(2)); |
| 814 |
goto sendupdates; |
| 815 |
case 'g': |
| 816 |
switch (data[2]) { |
| 817 |
|
| 818 |
case 'l': |
| 819 |
sendGameTemplateList(pInput); |
| 820 |
goto sendupdates; |
| 821 |
} |
| 822 |
break; |
| 823 |
case 'd': |
| 824 |
if (game) { |
| 825 |
exitGame(game, pInput); |
| 826 |
} |
| 827 |
socket->setStatus(Socket::Close); |
| 828 |
goto sendupdates; |
| 829 |
case 'R': |
| 830 |
if (game) { |
| 831 |
pInput->ioNoSuchCmd("you are already within a game"); |
| 832 |
return; |
| 833 |
} |
| 834 |
reconnectPlayer(pInput, data2.substr(2)); |
| 835 |
goto sendupdates; |
| 836 |
} |
| 837 |
} |
| 838 |
|
| 839 |
if (!pInput->identified()) { |
| 840 |
pInput->ioNoSuchCmd("you are not identified"); |
| 841 |
// The rest of the commands are only available if player is identified. |
| 842 |
return; |
| 843 |
} |
| 844 |
|
| 845 |
if (data[0] != '.') { |
| 846 |
if (utf8::distance(data2.begin(), data2.end()) > 256) { |
| 847 |
pInput->ioError("Chat messages are limited to 256 characters"); |
| 848 |
return; |
| 849 |
} |
| 850 |
|
| 851 |
if (game) { |
| 852 |
game->ioWrite("<monopd><msg type=\"chat\" playerid=\"%d\" author=\"%s\" value=\"%s\"/></monopd>\n", pInput->id(), pInput->name().c_str(), escapeXML(data2).c_str()); |
| 853 |
} else { |
| 854 |
ioWrite("<monopd><msg type=\"chat\" playerid=\"" + itoa(pInput->id()) + "\" author=\"" + pInput->name() + "\" value=\"" + escapeXML(data2) + "\"/></monopd>\n", true); |
| 855 |
} |
| 856 |
return; |
| 857 |
} |
| 858 |
|
| 859 |
// The following commands are always available if player is identified. |
| 860 |
switch (data[1]) { |
| 861 |
|
| 862 |
case 'p': |
| 863 |
switch (data[2]) { |
| 864 |
|
| 865 |
case 'i': |
| 866 |
setPlayerImage(pInput, data2.substr(3)); |
| 867 |
goto sendupdates; |
| 868 |
} |
| 869 |
break; |
| 870 |
} |
| 871 |
|
| 872 |
// Commands available when player is not within a game. |
| 873 |
if (!game) { |
| 874 |
switch (data[1]) { |
| 875 |
|
| 876 |
case 'g': |
| 877 |
switch (data[2]) { |
| 878 |
|
| 879 |
case 'n': |
| 880 |
newGame(pInput, data2.substr(3)); |
| 881 |
goto sendupdates; |
| 882 |
case 'j': |
| 883 |
joinGame(pInput, atol(data2.substr(3).c_str())); |
| 884 |
goto sendupdates; |
| 885 |
case 'S': |
| 886 |
joinGame( pInput, atol(data2.substr(3).c_str()), true ); |
| 887 |
goto sendupdates; |
| 888 |
} |
| 889 |
break; |
| 890 |
} |
| 891 |
|
| 892 |
pInput->ioNoSuchCmd("you are not within a game"); |
| 893 |
// The rest of the commands are only available within a game. |
| 894 |
return; |
| 895 |
} |
| 896 |
|
| 897 |
switch (data[1]) { |
| 898 |
|
| 899 |
case 'f': |
| 900 |
game->sendFullUpdate(pInput); |
| 901 |
goto sendupdates; |
| 902 |
case 'g': |
| 903 |
switch (data[2]) { |
| 904 |
|
| 905 |
// These commands are always available in a running game, no matter what. |
| 906 |
case 'd': |
| 907 |
setGameDescription(pInput, data2.substr(3)); |
| 908 |
goto sendupdates; |
| 909 |
case 'x': |
| 910 |
exitGame(game, pInput); |
| 911 |
goto sendupdates; |
| 912 |
// The following commands have their own availability checks. |
| 913 |
case 'c': |
| 914 |
game->editConfiguration( pInput, data+3 ); |
| 915 |
goto sendupdates; |
| 916 |
case 'k': |
| 917 |
Player *pKick; |
| 918 |
pKick = game->kickPlayer( pInput, atoi(data+3) ); |
| 919 |
if (pKick) { |
| 920 |
exitGame(game, pKick); |
| 921 |
} |
| 922 |
goto sendupdates; |
| 923 |
case 'u': |
| 924 |
game->upgradePlayer( pInput, atoi(data+3) ); |
| 925 |
goto sendupdates; |
| 926 |
case 's': |
| 927 |
game->start(pInput); |
| 928 |
goto sendupdates; |
| 929 |
} |
| 930 |
break; |
| 931 |
} |
| 932 |
|
| 933 |
if (game->status() == Game::End) { |
| 934 |
pInput->ioNoSuchCmd("this game has ended"); |
| 935 |
// The rest of the commands are only available when the game has not ended. |
| 936 |
return; |
| 937 |
} |
| 938 |
|
| 939 |
switch (data[1]) { |
| 940 |
|
| 941 |
case 't': |
| 942 |
game->setTokenLocation(pInput, atoi(data+2)); |
| 943 |
if (!game->clientsMoving()) { |
| 944 |
Event *event = findEvent(game, Event::TokenMovementTimeout); |
| 945 |
if (event) { |
| 946 |
delEvent(event); |
| 947 |
} |
| 948 |
} |
| 949 |
goto sendupdates; |
| 950 |
} |
| 951 |
|
| 952 |
if (pInput->getBoolProperty("spectator") || pInput->getBoolProperty("bankrupt")) { |
| 953 |
pInput->ioNoSuchCmd("you are only a spectator"); |
| 954 |
// The rest of the commands are only available for participating players |
| 955 |
return; |
| 956 |
} |
| 957 |
|
| 958 |
if (game->status() != Game::Run) { |
| 959 |
pInput->ioNoSuchCmd("game is not running"); |
| 960 |
// The rest of the commands are only available if game is running |
| 961 |
return; |
| 962 |
} |
| 963 |
|
| 964 |
switch (data[1]) { |
| 965 |
|
| 966 |
case 'T': |
| 967 |
switch (data[2]) { |
| 968 |
|
| 969 |
case 'c': |
| 970 |
case 'e': |
| 971 |
pInput->updateTradeObject(data+2); |
| 972 |
goto sendupdates; |
| 973 |
case 'm': |
| 974 |
pInput->updateTradeMoney(data+3); |
| 975 |
goto sendupdates; |
| 976 |
case 'n': |
| 977 |
game->newTrade(pInput, atol(data2.substr(3).c_str())); |
| 978 |
goto sendupdates; |
| 979 |
case 'a': |
| 980 |
game->acceptTrade(pInput, data+3); |
| 981 |
goto sendupdates; |
| 982 |
case 'r': |
| 983 |
game->rejectTrade(pInput, atol(data2.substr(3).c_str())); |
| 984 |
goto sendupdates; |
| 985 |
} |
| 986 |
break; |
| 987 |
// From the official rules: "may buy and erect at any time" |
| 988 |
case 'h': |
| 989 |
switch (data[2]) { |
| 990 |
|
| 991 |
case 'b': |
| 992 |
pInput->buyHouse(atoi(data+3)); |
| 993 |
goto sendupdates; |
| 994 |
case 's': |
| 995 |
pInput->sellHouse(atoi(data+3)); |
| 996 |
goto sendupdates; |
| 997 |
} |
| 998 |
break; |
| 999 |
// From official rules: "Unimproved properties can be mortgaged |
| 1000 |
// through the Bank at any time" |
| 1001 |
// Selling estates is not officially supported, but it makes most |
| 1002 |
// sense here. |
| 1003 |
case 'e': |
| 1004 |
switch (data[2]) { |
| 1005 |
|
| 1006 |
case 'm': |
| 1007 |
pInput->mortgageEstate(atoi(data+3)); |
| 1008 |
goto sendupdates; |
| 1009 |
case 's': |
| 1010 |
pInput->sellEstate(atoi(data+3)); |
| 1011 |
goto sendupdates; |
| 1012 |
} |
| 1013 |
break; |
| 1014 |
} |
| 1015 |
|
| 1016 |
if (game->clientsMoving()) { |
| 1017 |
pInput->ioNoSuchCmd("other players are still moving"); |
| 1018 |
// The rest of the commands are only available when no clients are moving |
| 1019 |
return; |
| 1020 |
} |
| 1021 |
|
| 1022 |
// If we're in a tax dialog, we don't accept too many commands. |
| 1023 |
if (game->pausedForDialog()) { |
| 1024 |
switch (data[1]) { |
| 1025 |
|
| 1026 |
case 'T': |
| 1027 |
switch (data[2]) { |
| 1028 |
|
| 1029 |
case '$': |
| 1030 |
pInput->payTax(); |
| 1031 |
goto sendupdates; |
| 1032 |
case '%': |
| 1033 |
pInput->payTax(true); |
| 1034 |
goto sendupdates; |
| 1035 |
} |
| 1036 |
break; |
| 1037 |
} |
| 1038 |
|
| 1039 |
// The rest of the commands are not available during a tax dialog |
| 1040 |
pInput->ioNoSuchCmd("a tax dialog is in progress"); |
| 1041 |
return; |
| 1042 |
} |
| 1043 |
|
| 1044 |
// Declaring bankruptcy is only possible when a player is in debt. |
| 1045 |
if (game->debts()) { |
| 1046 |
if (game->findDebt(pInput)) { |
| 1047 |
switch (data[1]) { |
| 1048 |
|
| 1049 |
case 'D': |
| 1050 |
game->declareBankrupt(pInput); |
| 1051 |
goto sendupdates; |
| 1052 |
case 'p': |
| 1053 |
game->solveDebts(pInput, true); |
| 1054 |
goto sendupdates; |
| 1055 |
} |
| 1056 |
} |
| 1057 |
|
| 1058 |
pInput->ioNoSuchCmd("there are debts to be settled"); |
| 1059 |
// The rest of the commands are only available when there |
| 1060 |
// are no debts to be settled. |
| 1061 |
return; |
| 1062 |
} |
| 1063 |
|
| 1064 |
// Auctions restrict movement and stuff. |
| 1065 |
if (game->auction()) { |
| 1066 |
switch (data[1]) { |
| 1067 |
|
| 1068 |
case 'a': |
| 1069 |
switch (data[2]) { |
| 1070 |
|
| 1071 |
case 'b': |
| 1072 |
if (!game->bidInAuction(pInput, data+3)) { |
| 1073 |
Event *event; |
| 1074 |
event = findEvent(game, Event::AuctionTimeout); |
| 1075 |
if (!event) |
| 1076 |
event = newEvent(Event::AuctionTimeout, game); |
| 1077 |
event->setLaunchTime(4000); |
| 1078 |
} |
| 1079 |
goto sendupdates; |
| 1080 |
} |
| 1081 |
break; |
| 1082 |
} |
| 1083 |
|
| 1084 |
pInput->ioNoSuchCmd("an auction is in progress."); |
| 1085 |
// The rest of the commands are only available when there |
| 1086 |
// is no auction in progress |
| 1087 |
return; |
| 1088 |
} |
| 1089 |
|
| 1090 |
if (!pInput->getBoolProperty("hasturn")) { |
| 1091 |
pInput->ioNoSuchCmd("this is not your turn"); |
| 1092 |
// The rest of the commands are only available when it's the player's turn |
| 1093 |
return; |
| 1094 |
} |
| 1095 |
|
| 1096 |
switch (data[1]) { |
| 1097 |
|
| 1098 |
case 'E': |
| 1099 |
pInput->endTurn(true); |
| 1100 |
goto sendupdates; |
| 1101 |
} |
| 1102 |
|
| 1103 |
if (pInput->getBoolProperty("can_buyestate")) { |
| 1104 |
Event *event; |
| 1105 |
|
| 1106 |
switch (data[1]) { |
| 1107 |
|
| 1108 |
case 'e': |
| 1109 |
switch (data[2]) { |
| 1110 |
|
| 1111 |
case 'b': |
| 1112 |
pInput->buyEstate(); |
| 1113 |
goto sendupdates; |
| 1114 |
case 'a': |
| 1115 |
game->newAuction(pInput); |
| 1116 |
// A AuctionTimeout event may exist if a player disconnected |
| 1117 |
// while an auction was in progress, destroy previous event if necessary |
| 1118 |
event = findEvent(game, Event::AuctionTimeout); |
| 1119 |
if (event) { |
| 1120 |
delEvent(event); |
| 1121 |
} |
| 1122 |
goto sendupdates; |
| 1123 |
} |
| 1124 |
break; |
| 1125 |
} |
| 1126 |
} |
| 1127 |
|
| 1128 |
if (pInput->getBoolProperty("jailed")) { |
| 1129 |
|
| 1130 |
switch (data[1]) { |
| 1131 |
|
| 1132 |
case 'j': |
| 1133 |
switch (data[2]) { |
| 1134 |
|
| 1135 |
case 'c': |
| 1136 |
pInput->useJailCard(); |
| 1137 |
goto sendupdates; |
| 1138 |
case 'p': |
| 1139 |
pInput->payJail(); |
| 1140 |
goto sendupdates; |
| 1141 |
case 'r': |
| 1142 |
pInput->rollJail(); |
| 1143 |
goto sendupdates; |
| 1144 |
} |
| 1145 |
break; |
| 1146 |
} |
| 1147 |
} |
| 1148 |
|
| 1149 |
if (pInput->getBoolProperty("can_roll")) { |
| 1150 |
|
| 1151 |
switch (data[1]) { |
| 1152 |
|
| 1153 |
case 'r': |
| 1154 |
pInput->rollDice(); |
| 1155 |
// A TokenMovementTimeout event may exist if a player disconnected |
| 1156 |
// while moving, destroy previous event if necessary |
| 1157 |
Event *event = findEvent(game, Event::TokenMovementTimeout); |
| 1158 |
if (event) { |
| 1159 |
delEvent(event); |
| 1160 |
} |
| 1161 |
event = newEvent(Event::TokenMovementTimeout, game); |
| 1162 |
event->setLaunchTime(10000); |
| 1163 |
goto sendupdates; |
| 1164 |
} |
| 1165 |
} |
| 1166 |
|
| 1167 |
pInput->ioNoSuchCmd(); |
| 1168 |
return; |
| 1169 |
|
| 1170 |
sendupdates: |
| 1171 |
sendXMLUpdates(); |
| 1172 |
} |
| 1173 |
|
| 1174 |
void MonopdServer::sendXMLUpdates() |
| 1175 |
{ |
| 1176 |
// Send XML to all players |
| 1177 |
for(std::vector<Player *>::iterator it = m_players.begin(); it != m_players.end() && (*it) ; ++it) |
| 1178 |
sendXMLUpdate((*it)); |
| 1179 |
|
| 1180 |
// Reset propertiesChanged for all player and game objects |
| 1181 |
// TODO: cache whether they changed above and reuse here? |
| 1182 |
for(std::vector<Player *>::iterator it = m_players.begin(); it != m_players.end() && (*it) ; ++it) |
| 1183 |
(*it)->unsetPropertiesChanged(); |
| 1184 |
for(std::vector<Game *>::iterator it = m_games.begin(); it != m_games.end() && (*it) ; ++it) |
| 1185 |
{ |
| 1186 |
(*it)->unsetPropertiesChanged(); |
| 1187 |
|
| 1188 |
// Let games handle resets for its own objects .. |
| 1189 |
(*it)->unsetChildProperties(); |
| 1190 |
} |
| 1191 |
} |
| 1192 |
|
| 1193 |
void MonopdServer::sendXMLUpdate(Player *pOutput, bool fullUpdate, bool excludeSelf) |
| 1194 |
{ |
| 1195 |
bool updateEmpty = true; |
| 1196 |
|
| 1197 |
// Send updates *about* all games .. |
| 1198 |
Game *gUpdate = 0; |
| 1199 |
for(std::vector<Game *>::iterator uit = m_games.begin(); uit != m_games.end() && (gUpdate = *uit) ; ++uit) |
| 1200 |
{ |
| 1201 |
// .. but only when changed (and in property scope) |
| 1202 |
std::string updateXML = gUpdate->oldXMLUpdate(pOutput, fullUpdate); |
| 1203 |
if (updateXML.size()) |
| 1204 |
{ |
| 1205 |
if (updateEmpty) |
| 1206 |
{ |
| 1207 |
pOutput->ioWrite("<monopd>"); |
| 1208 |
updateEmpty = false; |
| 1209 |
} |
| 1210 |
pOutput->ioWrite("%s", updateXML.c_str()); |
| 1211 |
} |
| 1212 |
} |
| 1213 |
|
| 1214 |
// Let game handle updates *about* all of its objects .. |
| 1215 |
if (Game *game = pOutput->game()) |
| 1216 |
updateEmpty = game->sendChildXMLUpdate(pOutput, updateEmpty); |
| 1217 |
|
| 1218 |
// Send updates *about* all players .. |
| 1219 |
Player *pUpdate = 0; |
| 1220 |
for(std::vector<Player *>::iterator uit = m_players.begin(); uit != m_players.end() && (pUpdate = *uit) ; ++uit) |
| 1221 |
{ |
| 1222 |
// .. but only when changed (and in property scope) |
| 1223 |
if (!excludeSelf || pUpdate != pOutput) |
| 1224 |
{ |
| 1225 |
std::string updateXML = pUpdate->oldXMLUpdate(pOutput, fullUpdate); |
| 1226 |
if (updateXML.size()) |
| 1227 |
{ |
| 1228 |
if (updateEmpty) |
| 1229 |
{ |
| 1230 |
pOutput->ioWrite("<monopd>"); |
| 1231 |
updateEmpty = false; |
| 1232 |
} |
| 1233 |
pOutput->ioWrite("%s", updateXML.c_str()); |
| 1234 |
} |
| 1235 |
} |
| 1236 |
} |
| 1237 |
|
| 1238 |
if (!updateEmpty) |
| 1239 |
pOutput->ioWrite("</monopd>\n"); |
| 1240 |
} |
| 1241 |
|
| 1242 |
void MonopdServer::setPlayerName(Player *player, std::string name) { |
| 1243 |
|
| 1244 |
// truncate player name to 16 characters |
| 1245 |
if (name.size()) { |
| 1246 |
try { |
| 1247 |
std::string::iterator it = name.begin(); |
| 1248 |
utf8::advance(it, 16, name.end()); |
| 1249 |
name.erase(it, name.end()); |
| 1250 |
} |
| 1251 |
catch (const utf8::not_enough_room &e) { |
| 1252 |
} |
| 1253 |
} else { |
| 1254 |
name = "anonymous"; |
| 1255 |
} |
| 1256 |
|
| 1257 |
std::string useName = name; |
| 1258 |
int i=1; |
| 1259 |
while (findPlayer(useName)) { |
| 1260 |
useName = name + itoa(++i); |
| 1261 |
} |
| 1262 |
|
| 1263 |
player->setProperty("name", useName, this); |
| 1264 |
if (player->identified()) { |
| 1265 |
return; |
| 1266 |
} |
| 1267 |
|
| 1268 |
player->setProperty("game", -1, this); |
| 1269 |
player->setProperty("host", player->socket()->ipAddr(), this); |
| 1270 |
player->identify(m_nextPlayerId++); |
| 1271 |
player->sendClientMsg(); |
| 1272 |
|
| 1273 |
syslog(LOG_INFO, "new player: id=[%d], fd=[%d], name=[%s], players=[%d]", player->id(), player->socket()->fd(), name.c_str(), (int)m_players.size()); |
| 1274 |
player = 0; |
| 1275 |
for (std::vector<Player *>::iterator it = m_players.begin(); it != m_players.end() && (player = *it) ; ++it) { |
| 1276 |
printf(" player %16s %16s game %d bankrupt %d socket fd %d\n", player->name().c_str(), player->getStringProperty("host").c_str(), (player->game() ? player->game()->id() : -1), player->getBoolProperty("bankrupt"), player->socket() ? (int)player->socket()->fd() : -1); |
| 1277 |
} |
| 1278 |
|
| 1279 |
updateSystemdStatus(); |
| 1280 |
} |
| 1281 |
|
| 1282 |
void MonopdServer::setPlayerImage(Player *player, std::string image) { |
| 1283 |
// truncate image name to 32 characters |
| 1284 |
try { |
| 1285 |
std::string::iterator it = image.begin(); |
| 1286 |
utf8::advance(it, 32, image.end()); |
| 1287 |
image.erase(it, image.end()); |
| 1288 |
} |
| 1289 |
catch (const utf8::not_enough_room &e) { |
| 1290 |
} |
| 1291 |
|
| 1292 |
player->setProperty("image", image, this); |
| 1293 |
} |