chatcommand_simplegameusersetdata.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "chatcommand_simplegameusersetdata.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. // simplegameusersetdata(messageid,gameid,userdata)
  11. // Sets the data for a user in a game. Returns a success message or an error message.
  12. bool chatcommand_simplegameusersetdata::processmessage(char first_letter,message *received_message,chatclient *client)
  13. {
  14. if ((first_letter != 's')
  15. || (received_message->actual_message.substr(0,22)!="simplegameusersetdata(")) {
  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. datastring username;
  26. datastring userdata;
  27. simplechatgame *game;
  28. biglist_item<simplechatgame *> *gamesearch;
  29. datastring error_message;
  30. biglist_item<simplechatgameuser *> *usersearch;
  31. simplechatgameuser *user;
  32. debug = __LINE__;
  33. method_parameters = received_message->actual_message.substr(22,received_message->actual_message.length-23);
  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. userdata = parameters_parsed.string_parameters[3];
  47. printf("simplegameusersetdata(");
  48. gameid.print();
  49. printf(",");
  50. username.print();
  51. printf(",");
  52. userdata.print();
  53. printf(")\n");
  54. debug = __LINE__;
  55. // Look to see if this gameid is already used.
  56. gamesearch = the_websocket->find_simple_game(gameid);
  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. debug = __LINE__;
  64. // Look for this client in this game.
  65. usersearch = game->getuser(client);
  66. debug = __LINE__;
  67. if (usersearch == nullptr) {
  68. error_message = "You're not in that game.";
  69. error(client,error_message,messageid);
  70. return true;
  71. }
  72. username = usersearch->item->username == nullptr ? datastring::anemptystring() : *(usersearch->item->username);
  73. debug = __LINE__;
  74. if ((userdata == nullptr) || (userdata.length == 0)) {
  75. debug = __LINE__;
  76. if (usersearch->item->userdata != nullptr) {
  77. idisposable::dereference((idisposable **)&(usersearch->item->userdata));
  78. }
  79. debug = __LINE__;
  80. } else {
  81. debug = __LINE__;
  82. if ((usersearch->item->userdata != nullptr) && (usersearch->item->userdata->capacity < userdata.length)) {
  83. idisposable::dereference((idisposable **)&(usersearch->item->userdata));
  84. }
  85. debug = __LINE__;
  86. if (usersearch->item->userdata == nullptr) {
  87. debug = __LINE__;
  88. datablock *userdata2 = new datablock(userdata);
  89. debug = __LINE__;
  90. usersearch->item->userdata = userdata2;
  91. debug = __LINE__;
  92. if (usersearch->item->userdata == nullptr) {
  93. error_message = "Out of memory.";
  94. error(client,error_message,messageid);
  95. return true;
  96. }
  97. debug = __LINE__;
  98. } else {
  99. debug = __LINE__;
  100. *(usersearch->item->userdata) == userdata;
  101. }
  102. debug = __LINE__;
  103. }
  104. debug = __LINE__;
  105. if (username.length > 0)
  106. {
  107. // Send a message to the other clients that the user data changed for this user.
  108. new_message = simplegameuserdata(messageid,gameid,username,userdata);
  109. debug = __LINE__;
  110. biglist_iterator<simplechatgameuser *> userloop(&(game->users));
  111. debug = __LINE__;
  112. while(!userloop.eof()) {
  113. debug = __LINE__;
  114. user = userloop.item;
  115. debug = __LINE__;
  116. if ((user->client != nullptr) && (user->client != client)) {
  117. user->client->add_message(new_message);
  118. user->client->callback_on_writable();
  119. }
  120. debug = __LINE__;
  121. userloop.movenext();
  122. }
  123. debug = __LINE__;
  124. idisposable::dereference((idisposable**)&new_message);
  125. }
  126. debug = __LINE__;
  127. // Send a success message to the client that changed the data.
  128. success_message(client,messageid);
  129. } else {
  130. debug = __LINE__;
  131. parameters_not_correct(client,received_message);
  132. }
  133. debug = __LINE__;
  134. return true;
  135. }