chatcommand_simplegamechat.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #include "chatcommand_simplegamechat.h"
  2. #include "websocket.h"
  3. #include "datastring.h"
  4. #include "parameters.h"
  5. #include "message.h"
  6. #include "Debug.h"
  7. #include <inttypes.h>
  8. #include "simplechatgame.h"
  9. // simplegamechat(messageid,gameid,username,message)
  10. // Sends a chat message to usename in a simple game. Sends a simplegamechat message to the user and a success message or an error message to the caller.
  11. bool chatcommand_simplegamechat::processmessage(char first_letter,message *received_message,chatclient *client)
  12. {
  13. if ((first_letter != 's')
  14. || (received_message->actual_message.substr(0,15)!="simplegamechat(")) {
  15. return false;
  16. }
  17. Debug debug(__FILE__,__func__,__LINE__);
  18. message *new_message;
  19. datastring method_parameters;
  20. parameters parameters_parsed;
  21. bool parameter_success = true;
  22. int64_t messageid;
  23. datastring gameid;
  24. datastring usernameto;
  25. datastring chatmessage;
  26. simplechatgame *game;
  27. datastring error_message;
  28. datastring *usernamefrom;
  29. chatclient *chatclientto;
  30. biglist_item<simplechatgameuser *> *usersearchfrom;
  31. biglist_item<simplechatgameuser *> *usersearchto;
  32. biglist_item<simplechatgame *> *gamesearch;
  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. usernameto = parameters_parsed.string_parameters[2];
  47. chatmessage = 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. debug = __LINE__;
  58. // Look for this user in this game.
  59. usersearchfrom = game->getuser(client);
  60. if (usersearchfrom == nullptr) {
  61. error_message = "You're not in that game.";
  62. error(client,error_message,messageid);
  63. return true;
  64. }
  65. usernamefrom = usersearchfrom->item->username;
  66. // Look for usernameto if it's specified.
  67. if (usernameto.length == 0) {
  68. // They didn't specify a usernameto, so send the message to all the users in the game.
  69. new_message = simplegamechat(messageid,gameid,*usernamefrom,chatmessage);
  70. game->send_message_to_clients(new_message,nullptr);
  71. idisposable::dereference((idisposable**)&new_message);
  72. } else {
  73. usersearchto = game->getuser(usernameto);
  74. if (usersearchto == nullptr) {
  75. error_message = "That user is not in that game.";
  76. error(client,error_message,messageid);
  77. return true;
  78. }
  79. // Try to send a message to that user.
  80. chatclientto = usersearchto->item->client;
  81. if (chatclientto == nullptr) {
  82. error_message = "That user is currently disconnected.";
  83. error(client,error_message,messageid);
  84. } else {
  85. new_message = simplegamechat(messageid,gameid,*usernamefrom,chatmessage);
  86. client->add_message(new_message);
  87. client->callback_on_writable();
  88. chatclientto->add_message(new_message);
  89. chatclientto->callback_on_writable();
  90. idisposable::dereference((idisposable**)&new_message);
  91. }
  92. }
  93. } else {
  94. debug = __LINE__;
  95. parameters_not_correct(client,received_message);
  96. }
  97. debug = __LINE__;
  98. return true;
  99. }