less pointers
This commit is contained in:
parent
08e105603c
commit
188db932fb
|
@ -24,9 +24,9 @@ private:
|
||||||
TelegramAPI::Manager* tapi;
|
TelegramAPI::Manager* tapi;
|
||||||
Conf& conf;
|
Conf& conf;
|
||||||
|
|
||||||
void updateGame(int64_t chatid, uint64_t msgid, const Game* g);
|
void updateGame(int64_t chatid, uint64_t msgid, const Game& g);
|
||||||
TelegramAPI::InlineKeyboard gameToKeyboard(const Game*) const;
|
TelegramAPI::InlineKeyboard gameToKeyboard(const Game&) const;
|
||||||
std::string gameToMessage(const Game*, std::vector<TelegramAPI::MessageEntity>& entites) const;
|
std::string gameToMessage(const Game&, std::vector<TelegramAPI::MessageEntity>& entites) const;
|
||||||
|
|
||||||
Game* messageToGame(const TelegramAPI::Message& msg) 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
|
// create a empty game
|
||||||
Game* g = new Game();
|
Game g;
|
||||||
|
|
||||||
std::vector<TelegramAPI::MessageEntity> entities;
|
std::vector<TelegramAPI::MessageEntity> entities;
|
||||||
const std::string text = gameToMessage(g, entities);
|
const std::string text = gameToMessage(g, entities);
|
||||||
auto sendmsg = api->sendMessage(msg.chat.id, text, gameToKeyboard(g), entities);
|
auto sendmsg = api->sendMessage(msg.chat.id, text, gameToKeyboard(g), entities);
|
||||||
delete g;
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -50,7 +49,7 @@ bool Bot::handleCallback(TelegramAPI::Manager* api, TelegramAPI::CallbackQuery&
|
||||||
Game::UserInfo newuser = userToUserInfo(clbq.from);
|
Game::UserInfo newuser = userToUserInfo(clbq.from);
|
||||||
if(g->addPlayer(newuser)) {
|
if(g->addPlayer(newuser)) {
|
||||||
api->answerCallbackQuery(clbq.id, "You joined the game");
|
api->answerCallbackQuery(clbq.id, "You joined the game");
|
||||||
updateGame(chatid, msgid, g);
|
updateGame(chatid, msgid, *g);
|
||||||
delret;
|
delret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -71,7 +70,7 @@ bool Bot::handleCallback(TelegramAPI::Manager* api, TelegramAPI::CallbackQuery&
|
||||||
|
|
||||||
if(g->turn(x, y, player)) {
|
if(g->turn(x, y, player)) {
|
||||||
api->answerCallbackQuery(clbq.id, "ok");
|
api->answerCallbackQuery(clbq.id, "ok");
|
||||||
updateGame(chatid, msgid, g);
|
updateGame(chatid, msgid, *g);
|
||||||
delret;
|
delret;
|
||||||
}
|
}
|
||||||
api->answerCallbackQuery(clbq.id, "Error - this is not your turn");
|
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;
|
std::vector<TelegramAPI::MessageEntity> entities;
|
||||||
const std::string text = gameToMessage(g, entities);
|
const std::string text = gameToMessage(g, entities);
|
||||||
tapi->editMessageText(chatid, msgid, text, gameToKeyboard(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"};
|
static const std::string text[3] = {" ", "X", "O"};
|
||||||
|
|
||||||
TelegramAPI::InlineKeyboard kb;
|
TelegramAPI::InlineKeyboard kb;
|
||||||
for(uint_fast8_t row = 0; row < g->getSize(); row++) {
|
for(uint_fast8_t row = 0; row < g.getSize(); row++) {
|
||||||
for(uint_fast8_t col = 0; col < g->getSize(); col++) {
|
for(uint_fast8_t col = 0; col < g.getSize(); col++) {
|
||||||
Game::SYM s = g->getPos(col, row);
|
Game::SYM s = g.getPos(col, row);
|
||||||
const std::string clb = std::to_string(col) + " " + std::to_string(row);
|
const std::string clb = std::to_string(col) + " " + std::to_string(row);
|
||||||
auto btn = TelegramAPI::InlineButton::createCallback(text[(int) s], clb);
|
auto btn = TelegramAPI::InlineButton::createCallback(text[(int) s], clb);
|
||||||
kb.addButton(btn, row);
|
kb.addButton(btn, row);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!g->ready() && !g->done()) {
|
if(!g.ready() && !g.done()) {
|
||||||
// missing player
|
// missing player
|
||||||
kb.addButton(TelegramAPI::InlineButton::createCallback("Join Game", "join"), g->getSize());
|
kb.addButton(TelegramAPI::InlineButton::createCallback("Join Game", "join"), g.getSize());
|
||||||
}
|
}
|
||||||
|
|
||||||
return kb;
|
return kb;
|
||||||
|
@ -130,28 +129,28 @@ static TelegramAPI::MessageEntity userInfoToEntity(const Game::UserInfo& userinf
|
||||||
return ent;
|
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;
|
std::ostringstream out;
|
||||||
out << "X: " << g->getPlayerA().getUsernameOrName() << std::endl;
|
out << "X: " << g.getPlayerA().getUsernameOrName() << std::endl;
|
||||||
out << "O: " << g->getPlayerB().getUsernameOrName() << std::endl;
|
out << "O: " << g.getPlayerB().getUsernameOrName() << std::endl;
|
||||||
if(g->done()) {
|
if(g.done()) {
|
||||||
if(g->getWinner().empty()) {
|
if(g.getWinner().empty()) {
|
||||||
out << "Draw" << std::endl;
|
out << "Draw" << std::endl;
|
||||||
} else {
|
} else {
|
||||||
out << "Winner: " << g->getWinner() << std::endl;
|
out << "Winner: " << g.getWinner() << std::endl;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
out << "nextturn: " << (g->getNextTurn() ? "X" : "O") << std::endl;
|
out << "nextturn: " << (g.getNextTurn() ? "X" : "O") << std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
//create entities
|
//create entities
|
||||||
if(g->getPlayerA()) {
|
if(g.getPlayerA()) {
|
||||||
TelegramAPI::MessageEntity a = userInfoToEntity(g->getPlayerA());
|
TelegramAPI::MessageEntity a = userInfoToEntity(g.getPlayerA());
|
||||||
a.offset = 3;
|
a.offset = 3;
|
||||||
entites.push_back(a);
|
entites.push_back(a);
|
||||||
|
|
||||||
if(g->getPlayerB()) {
|
if(g.getPlayerB()) {
|
||||||
TelegramAPI::MessageEntity b = userInfoToEntity(g->getPlayerB());
|
TelegramAPI::MessageEntity b = userInfoToEntity(g.getPlayerB());
|
||||||
b.offset = 7 + a.length; // ("x: " + "\n" + "O: ").size() = 7
|
b.offset = 7 + a.length; // ("x: " + "\n" + "O: ").size() = 7
|
||||||
entites.push_back(b);
|
entites.push_back(b);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue