chatcommand_simplegamesetdata.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #include "chatcommand_simplegamesetdata.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. // simplegamesetdata(messageid,gameid,offset,newgamedata)
  11. // Sets the gamedata in a game. Sends a simplegamedata message or an error. Offset is where to save newgamedata within gamedata. Use -1 to replace.
  12. bool chatcommand_simplegamesetdata::processmessage(char first_letter,message *received_message,chatclient *client)
  13. {
  14. if ((first_letter != 's')
  15. || (received_message->actual_message.substr(0,18)!="simplegamesetdata(")) {
  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. biglist_item<simplechatgame *> *gamesearch;
  29. datastring error_message;
  30. datastring *username;
  31. biglist_item<simplechatgameuser *> *usersearch;
  32. datastring currentgamedata;
  33. debug = __LINE__;
  34. method_parameters = received_message->actual_message.substr(18,received_message->actual_message.length-19);
  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.long_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. offset = parameters_parsed.long_parameters[2];
  47. newgamedata = parameters_parsed.string_parameters[3];
  48. debug = __LINE__;
  49. // Look to see if this gameid is already used.
  50. gamesearch = the_websocket->find_simple_game(gameid);
  51. if (gamesearch == nullptr) {
  52. error_message = "That gameid was not found.";
  53. error(client,error_message,messageid);
  54. return true;
  55. }
  56. game = gamesearch->item;
  57. // Look for this client in this game.
  58. usersearch = game->getuser(client);
  59. if (usersearch == nullptr) {
  60. error_message = "You're not in that game.";
  61. error(client,error_message,messageid);
  62. return true;
  63. }
  64. username = usersearch->item->username;
  65. // Set the gamedata. If offset < 0 then replace game data. If offset >= 0 and newgamedata.length > 0 then set part of gamedata based on offset.
  66. debug = __LINE__;
  67. if (offset < 0) {
  68. if (newgamedata.length > 0) {
  69. if (game->gamedata == nullptr) {
  70. game->gamedata = new datablock(newgamedata);
  71. } else {
  72. *(game->gamedata) = newgamedata;
  73. }
  74. } else {
  75. idisposable::dereference((idisposable**)&(game->gamedata));
  76. }
  77. } else {
  78. if (newgamedata.length > 0) {
  79. if (game->gamedata == nullptr) {
  80. game->gamedata = new datablock(newgamedata);
  81. } else {
  82. game->gamedata->alter(newgamedata,offset);
  83. }
  84. }
  85. }
  86. debug = __LINE__;
  87. currentgamedata.clear();
  88. if (game->gamedata != nullptr) {
  89. currentgamedata = *(game->gamedata);
  90. }
  91. // Send a message to all the clients that the gamedata has changed and who changed it.
  92. new_message = simplegamedata(messageid,gameid,*username,currentgamedata);
  93. game->send_message_to_clients(new_message,nullptr);
  94. idisposable::dereference((idisposable**)&new_message);
  95. } else {
  96. debug = __LINE__;
  97. parameters_not_correct(client,received_message);
  98. }
  99. debug = __LINE__;
  100. return true;
  101. }