From 188db932fbd69b53380fc989104895920c833ece Mon Sep 17 00:00:00 2001 From: mrbesen Date: Mon, 28 Feb 2022 13:48:28 +0100 Subject: [PATCH] less pointers --- include/bot.h | 6 +++--- src/bot.cpp | 43 +++++++++++++++++++++---------------------- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/include/bot.h b/include/bot.h index 7a028c8..f4b98e5 100644 --- a/include/bot.h +++ b/include/bot.h @@ -24,9 +24,9 @@ private: TelegramAPI::Manager* tapi; Conf& conf; - void updateGame(int64_t chatid, uint64_t msgid, const Game* g); - TelegramAPI::InlineKeyboard gameToKeyboard(const Game*) const; - std::string gameToMessage(const Game*, std::vector& entites) const; + void updateGame(int64_t chatid, uint64_t msgid, const Game& g); + TelegramAPI::InlineKeyboard gameToKeyboard(const Game&) const; + std::string gameToMessage(const Game&, std::vector& entites) const; Game* messageToGame(const TelegramAPI::Message& msg) const; }; \ No newline at end of file diff --git a/src/bot.cpp b/src/bot.cpp index cc38a6c..3c4fe1e 100644 --- a/src/bot.cpp +++ b/src/bot.cpp @@ -13,12 +13,11 @@ bool Bot::handleStart(TelegramAPI::Manager* api, const TelegramAPI::Message& msg } // create a empty game - Game* g = new Game(); + Game g; std::vector entities; const std::string text = gameToMessage(g, entities); auto sendmsg = api->sendMessage(msg.chat.id, text, gameToKeyboard(g), entities); - delete g; return true; } @@ -50,7 +49,7 @@ bool Bot::handleCallback(TelegramAPI::Manager* api, TelegramAPI::CallbackQuery& Game::UserInfo newuser = userToUserInfo(clbq.from); if(g->addPlayer(newuser)) { api->answerCallbackQuery(clbq.id, "You joined the game"); - updateGame(chatid, msgid, g); + updateGame(chatid, msgid, *g); delret; } @@ -71,7 +70,7 @@ bool Bot::handleCallback(TelegramAPI::Manager* api, TelegramAPI::CallbackQuery& if(g->turn(x, y, player)) { api->answerCallbackQuery(clbq.id, "ok"); - updateGame(chatid, msgid, g); + updateGame(chatid, msgid, *g); delret; } api->answerCallbackQuery(clbq.id, "Error - this is not your turn"); @@ -87,28 +86,28 @@ void Bot::stop() { } -void Bot::updateGame(int64_t chatid, uint64_t msgid, const Game* g) { +void Bot::updateGame(int64_t chatid, uint64_t msgid, const Game& g) { std::vector entities; const std::string text = gameToMessage(g, entities); tapi->editMessageText(chatid, msgid, text, gameToKeyboard(g), entities); } -TelegramAPI::InlineKeyboard Bot::gameToKeyboard(const Game* g) const { +TelegramAPI::InlineKeyboard Bot::gameToKeyboard(const Game& g) const { static const std::string text[3] = {" ", "X", "O"}; TelegramAPI::InlineKeyboard kb; - for(uint_fast8_t row = 0; row < g->getSize(); row++) { - for(uint_fast8_t col = 0; col < g->getSize(); col++) { - Game::SYM s = g->getPos(col, row); + for(uint_fast8_t row = 0; row < g.getSize(); row++) { + for(uint_fast8_t col = 0; col < g.getSize(); col++) { + Game::SYM s = g.getPos(col, row); const std::string clb = std::to_string(col) + " " + std::to_string(row); auto btn = TelegramAPI::InlineButton::createCallback(text[(int) s], clb); kb.addButton(btn, row); } } - if(!g->ready() && !g->done()) { + if(!g.ready() && !g.done()) { // missing player - kb.addButton(TelegramAPI::InlineButton::createCallback("Join Game", "join"), g->getSize()); + kb.addButton(TelegramAPI::InlineButton::createCallback("Join Game", "join"), g.getSize()); } return kb; @@ -130,28 +129,28 @@ static TelegramAPI::MessageEntity userInfoToEntity(const Game::UserInfo& userinf return ent; } -std::string Bot::gameToMessage(const Game* g, std::vector& entites) const { +std::string Bot::gameToMessage(const Game& g, std::vector& entites) const { std::ostringstream out; - out << "X: " << g->getPlayerA().getUsernameOrName() << std::endl; - out << "O: " << g->getPlayerB().getUsernameOrName() << std::endl; - if(g->done()) { - if(g->getWinner().empty()) { + out << "X: " << g.getPlayerA().getUsernameOrName() << std::endl; + out << "O: " << g.getPlayerB().getUsernameOrName() << std::endl; + if(g.done()) { + if(g.getWinner().empty()) { out << "Draw" << std::endl; } else { - out << "Winner: " << g->getWinner() << std::endl; + out << "Winner: " << g.getWinner() << std::endl; } } else { - out << "nextturn: " << (g->getNextTurn() ? "X" : "O") << std::endl; + out << "nextturn: " << (g.getNextTurn() ? "X" : "O") << std::endl; } //create entities - if(g->getPlayerA()) { - TelegramAPI::MessageEntity a = userInfoToEntity(g->getPlayerA()); + if(g.getPlayerA()) { + TelegramAPI::MessageEntity a = userInfoToEntity(g.getPlayerA()); a.offset = 3; entites.push_back(a); - if(g->getPlayerB()) { - TelegramAPI::MessageEntity b = userInfoToEntity(g->getPlayerB()); + if(g.getPlayerB()) { + TelegramAPI::MessageEntity b = userInfoToEntity(g.getPlayerB()); b.offset = 7 + a.length; // ("x: " + "\n" + "O: ").size() = 7 entites.push_back(b); }