chatcommand_leavesimplegame.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. #include "chatcommand_leavesimplegame.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. // leavesimplegame(messageid,gameid,offset,newgamedata)
  11. // Leaves a simple game. Returns a simplegameleave message or an error. Offset is where to save newgamedata within gamedata. Use -1 to replace.
  12. bool chatcommand_leavesimplegame::processmessage(char first_letter,message *received_message,chatclient *client)
  13. {
  14. if ((first_letter != 'l')
  15. || (received_message->actual_message.substr(0,16)!="leavesimplegame(")) {
  16. return false;
  17. }
  18. Debug debug(__FILE__,__func__,__LINE__);
  19. message *new_message;
  20. datastring method_parameters;
  21. parameters parameters_parsed;
  22. bool parameter_success = true;
  23. int64_t messageid;
  24. datastring gameid;
  25. int64_t offset;
  26. datastring newgamedata;
  27. simplechatgame *game;
  28. datastring error_message;
  29. biglist_item<simplechatgame *> *gamesearch;
  30. biglist_item<simplechatgameuser *> *user;
  31. datablock *username;
  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.long_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. offset = parameters_parsed.long_parameters[2];
  46. newgamedata = parameters_parsed.string_parameters[3];
  47. debug = __LINE__;
  48. // Look to see if this gameid is already used.
  49. gamesearch = the_websocket->find_simple_game(gameid);
  50. if (gamesearch == nullptr) {
  51. error_message = "That gameid was not found.";
  52. error(client,error_message,messageid);
  53. return true;
  54. }
  55. debug = __LINE__;
  56. game = gamesearch->item;
  57. debug = __LINE__;
  58. // Look for this user in this game.
  59. user = game->getuser(client);
  60. if (user == nullptr) {
  61. error_message = "You're not in that game.";
  62. error(client,error_message,messageid);
  63. return true;
  64. }
  65. debug = __LINE__;
  66. username = user->item->username == nullptr ? nullptr : user->item->username->clone();
  67. debug = __LINE__;
  68. // Change gamedata.
  69. if (offset < 0) {
  70. if (game->gamedata == nullptr) {
  71. game->gamedata = new datablock(newgamedata);
  72. } else {
  73. *(game->gamedata) = newgamedata;
  74. }
  75. } else {
  76. if (newgamedata.length > 0) {
  77. if (game->gamedata == nullptr) {
  78. } else {
  79. game->gamedata->alter(newgamedata,offset);
  80. }
  81. }
  82. }
  83. debug = __LINE__;
  84. // Remove this user from this game.
  85. idisposable::dereference((idisposable**)&(user->item));
  86. user->used = false;
  87. debug = __LINE__;
  88. // Send a message to all the users that this user left the game.
  89. if (username == nullptr) {
  90. game->send_user_list_to_clients();
  91. new_message = simplegameleave(messageid,gameid,datastring::anemptystring(),game->gamedata == nullptr ? datastring::anemptystring() : *(game->gamedata));
  92. // Send a message to the client that is leaving the game.
  93. client->push_message(&new_message);
  94. } else {
  95. new_message = simplegameleave(messageid,gameid,*username,game->gamedata == nullptr ? datastring::anemptystring() : *(game->gamedata));
  96. idisposable::dereference((idisposable**)&username);
  97. game->send_message_to_clients(new_message,nullptr);
  98. if (game->usercount(true) == 0) {
  99. // This game has no connected users. Delete the game.
  100. gamesearch->used = false;
  101. idisposable::dereference((idisposable**)&game);
  102. }
  103. // Send a message to the client that is leaving the game.
  104. client->push_message(&new_message);
  105. }
  106. } else {
  107. debug = __LINE__;
  108. parameters_not_correct(client,received_message);
  109. }
  110. debug = __LINE__;
  111. return true;
  112. }