less pointers
This commit is contained in:
parent
08e105603c
commit
188db932fb
|
@ -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<TelegramAPI::MessageEntity>& 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<TelegramAPI::MessageEntity>& entites) const;
|
||||
|
||||
Game* messageToGame(const TelegramAPI::Message& msg) const;
|
||||
};
|
43
src/bot.cpp
43
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<TelegramAPI::MessageEntity> 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<TelegramAPI::MessageEntity> 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<TelegramAPI::MessageEntity>& entites) const {
|
||||
std::string Bot::gameToMessage(const Game& g, std::vector<TelegramAPI::MessageEntity>& 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);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue