123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- #include "chatcommand_startsimplegame.h"
- #include "websocket.h"
- #include "datastring.h"
- #include "parameters.h"
- #include "stringbuilder.h"
- #include "message.h"
- #include "Debug.h"
- #include <inttypes.h>
- #include "simplechatgame.h"
- // startsimplegame(messageid,gameid,username,gamedata)
- // Starts a simple game.
- // Returns simplegame(messageid,gameid) or error(message,messageid);
- bool chatcommand_startsimplegame::processmessage(char first_letter,message *received_message,chatclient *client)
- {
- if ((first_letter != 's')
- || (received_message->actual_message.substr(0,16)!="startsimplegame(")) {
- return false;
- }
-
- Debug debug(__FILE__,__func__,__LINE__);
- message *new_message;
- datastring method_parameters;
- parameters parameters_parsed;
- bool parameter_success = true;
- int64_t messageid;
- datastring gameid;
- datastring username;
- datastring gamedata;
- simplechatgame *game;
- datastring error_message;
- biglist_item<simplechatgame *> *game_search;
- biglist_item<simplechatgameuser *> *user;
-
-
- debug = __LINE__;
- method_parameters = received_message->actual_message.substr(16,received_message->actual_message.length-17);
- method_parameters.null_terminate(); // Used to comply with snprintf.
- parameters_parsed.long_parameter(method_parameters,parameter_success);
- parameters_parsed.string_parameter(method_parameters,parameter_success);
- parameters_parsed.string_parameter(method_parameters,parameter_success);
- parameters_parsed.string_parameter(method_parameters,parameter_success);
-
- debug = __LINE__;
- if (parameter_success) {
- // Give the parameters nice names.
- debug = __LINE__;
- messageid = parameters_parsed.long_parameters[0];
- gameid = parameters_parsed.string_parameters[1];
- username = parameters_parsed.string_parameters[2];
- gamedata = parameters_parsed.string_parameters[3];
-
- debug = __LINE__;
- // Look to see if this gameid is already used.
- do { // loop only once
- // Look for this game.
- if (username.length == 0) {
- error_message = "A username is required.";
- error(client,error_message,messageid);
- return true;
- }
- game_search = the_websocket->find_simple_game(gameid);
- debug = __LINE__;
- if (game_search != nullptr) {
- error_message = "That gameid is already used.";
- error(client,error_message,messageid);
- return true;
- }
- debug = __LINE__;
- // Create a new game object.
- game = new simplechatgame();
- if (game == nullptr) {
- break;
- }
- debug = __LINE__;
- game->gameid = new datablock(gameid);
- if (game->gameid == nullptr) {
- delete game;
- break;
- }
- if (gamedata.length > 0) {
- game->gamedata = new datablock(gamedata);
- if (game->gamedata == nullptr) {
- delete game;
- break;
- }
- }
- user = game->add_user(client,username);
- if (user == nullptr) {
- delete game;
- break;
- }
- // Add the game to the list of games.
- game_search = the_websocket->add_simple_game(game);
- if (game_search == nullptr) {
- delete game;
- break;
- }
- debug = __LINE__;
- new_message = simplegamestarted(messageid,gameid);
- client->push_message(&new_message);
- return true;
- } while (false);
- error_message = "Out of memory.";
- error(client,error_message,messageid);
- } else {
- debug = __LINE__;
- parameters_not_correct(client,received_message);
- }
- debug = __LINE__;
- return true;
- }
|