chatcommand_startsimplegame.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #include "chatcommand_startsimplegame.h"
  2. #include "websocket.h"
  3. #include "datastring.h"
  4. #include "parameters.h"
  5. #include "stringbuilder.h"
  6. #include "message.h"
  7. #include "Debug.h"
  8. #include <inttypes.h>
  9. #include "simplechatgame.h"
  10. // startsimplegame(messageid,gameid,username,gamedata)
  11. // Starts a simple game.
  12. // Returns simplegame(messageid,gameid) or error(message,messageid);
  13. bool chatcommand_startsimplegame::processmessage(char first_letter,message *received_message,chatclient *client)
  14. {
  15. if ((first_letter != 's')
  16. || (received_message->actual_message.substr(0,16)!="startsimplegame(")) {
  17. return false;
  18. }
  19. Debug debug(__FILE__,__func__,__LINE__);
  20. message *new_message;
  21. datastring method_parameters;
  22. parameters parameters_parsed;
  23. bool parameter_success = true;
  24. int64_t messageid;
  25. datastring gameid;
  26. datastring username;
  27. datastring gamedata;
  28. simplechatgame *game;
  29. datastring error_message;
  30. biglist_item<simplechatgame *> *game_search;
  31. biglist_item<simplechatgameuser *> *user;
  32. debug = __LINE__;
  33. method_parameters = received_message->actual_message.substr(16,received_message->actual_message.length-17);
  34. method_parameters.null_terminate(); // Used to comply with snprintf.
  35. parameters_parsed.long_parameter(method_parameters,parameter_success);
  36. parameters_parsed.string_parameter(method_parameters,parameter_success);
  37. parameters_parsed.string_parameter(method_parameters,parameter_success);
  38. parameters_parsed.string_parameter(method_parameters,parameter_success);
  39. debug = __LINE__;
  40. if (parameter_success) {
  41. // Give the parameters nice names.
  42. debug = __LINE__;
  43. messageid = parameters_parsed.long_parameters[0];
  44. gameid = parameters_parsed.string_parameters[1];
  45. username = parameters_parsed.string_parameters[2];
  46. gamedata = parameters_parsed.string_parameters[3];
  47. debug = __LINE__;
  48. // Look to see if this gameid is already used.
  49. do { // loop only once
  50. // Look for this game.
  51. if (username.length == 0) {
  52. error_message = "A username is required.";
  53. error(client,error_message,messageid);
  54. return true;
  55. }
  56. game_search = the_websocket->find_simple_game(gameid);
  57. debug = __LINE__;
  58. if (game_search != nullptr) {
  59. error_message = "That gameid is already used.";
  60. error(client,error_message,messageid);
  61. return true;
  62. }
  63. debug = __LINE__;
  64. // Create a new game object.
  65. game = new simplechatgame();
  66. if (game == nullptr) {
  67. break;
  68. }
  69. debug = __LINE__;
  70. game->gameid = new datablock(gameid);
  71. if (game->gameid == nullptr) {
  72. delete game;
  73. break;
  74. }
  75. if (gamedata.length > 0) {
  76. game->gamedata = new datablock(gamedata);
  77. if (game->gamedata == nullptr) {
  78. delete game;
  79. break;
  80. }
  81. }
  82. user = game->add_user(client,username);
  83. if (user == nullptr) {
  84. delete game;
  85. break;
  86. }
  87. // Add the game to the list of games.
  88. game_search = the_websocket->add_simple_game(game);
  89. if (game_search == nullptr) {
  90. delete game;
  91. break;
  92. }
  93. debug = __LINE__;
  94. new_message = simplegamestarted(messageid,gameid);
  95. client->push_message(&new_message);
  96. return true;
  97. } while (false);
  98. error_message = "Out of memory.";
  99. error(client,error_message,messageid);
  100. } else {
  101. debug = __LINE__;
  102. parameters_not_correct(client,received_message);
  103. }
  104. debug = __LINE__;
  105. return true;
  106. }