chatcommand_joinsimplegame.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "chatcommand_joinsimplegame.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. // joinsimplegame(messageid,gameid,username,gamedata)
  11. // Joins a simple chat game. Appends gamedata to the game's gamedata.
  12. // Returns a simplegamejoined message or an error.
  13. bool chatcommand_joinsimplegame::processmessage(char first_letter,message *received_message,chatclient *client)
  14. {
  15. if ((first_letter != 'j')
  16. || (received_message->actual_message.substr(0,15)!="joinsimplegame(")) {
  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. datastring currentgamedata;
  29. simplechatgame *game;
  30. datastring error_message;
  31. biglist_item<simplechatgame *> *gamesearch;
  32. biglist_item<simplechatgameuser *> *usersearch;
  33. debug = __LINE__;
  34. method_parameters = received_message->actual_message.substr(15,received_message->actual_message.length-16);
  35. method_parameters.null_terminate(); // Used to comply with snprintf.
  36. parameters_parsed.long_parameter(method_parameters,parameter_success);
  37. parameters_parsed.string_parameter(method_parameters,parameter_success);
  38. parameters_parsed.string_parameter(method_parameters,parameter_success);
  39. parameters_parsed.string_parameter(method_parameters,parameter_success);
  40. debug = __LINE__;
  41. if (parameter_success) {
  42. // Give the parameters nice names.
  43. debug = __LINE__;
  44. messageid = parameters_parsed.long_parameters[0];
  45. gameid = parameters_parsed.string_parameters[1];
  46. username = parameters_parsed.string_parameters[2];
  47. gamedata = parameters_parsed.string_parameters[3];
  48. debug = __LINE__;
  49. if (username.length == 0) {
  50. error_message = "A username is required.";
  51. error(client,error_message,messageid);
  52. return true;
  53. }
  54. // Look to see if this gameid is already used.
  55. gamesearch = the_websocket->find_simple_game(gameid);
  56. debug = __LINE__;
  57. if (gamesearch == nullptr) {
  58. error_message = "That gameid was not found.";
  59. error(client,error_message,messageid);
  60. return true;
  61. }
  62. game = gamesearch->item;
  63. // Look to see if this user is already there.
  64. usersearch = game->getuser(username);
  65. if (usersearch == nullptr) {
  66. // This is a new user.
  67. if (game->add_client(client,username)) {
  68. if (gamedata.length > 0) {
  69. *(game->gamedata) += gamedata;
  70. }
  71. } else {
  72. error_message = "Out of memory joining game.";
  73. error(client,error_message,messageid);
  74. return true;
  75. }
  76. } else {
  77. // This user is already there.
  78. // They probably lost the connection.
  79. // Change the client connection.
  80. // This is insecure from hacking in that someone else can steal the client connection for that user and that game.
  81. // Fixing this would require some sort of password.
  82. usersearch->item->client = client;
  83. }
  84. debug = __LINE__;
  85. currentgamedata.clear();
  86. if (game->gamedata != nullptr) {
  87. currentgamedata = *(game->gamedata);
  88. }
  89. new_message = simplegamejoined(messageid,gameid,currentgamedata,&(game->users));
  90. game->send_message_to_clients(new_message,nullptr);
  91. idisposable::dereference((idisposable**)&new_message);
  92. debug = __LINE__;
  93. } else {
  94. debug = __LINE__;
  95. parameters_not_correct(client,received_message);
  96. }
  97. debug = __LINE__;
  98. return true;
  99. }