Compare commits

...

2 Commits

Author SHA1 Message Date
mrbesen 856b55c8a6
waiting for players 2022-03-15 20:53:45 +01:00
mrbesen 8a69fdc81f
auto join game 2022-03-15 19:57:25 +01:00
1 changed files with 17 additions and 5 deletions

View File

@ -2,6 +2,11 @@
#include <sstream>
static Game::UserInfo userToUserInfo(const TelegramAPI::User& u) {
// + (u.last_name.empty() ? "" : " " + u.last_name) // problems with spaces and unicode in last name
return {u.id, u.first_name, u.username};
}
Bot::Bot(TelegramAPI::Manager* tapi, Conf& conf) : tapi(tapi), conf(conf) {}
Bot::~Bot() {}
@ -15,6 +20,11 @@ bool Bot::handleStart(TelegramAPI::Manager* api, const TelegramAPI::Message& msg
// create a empty game
Game g;
// @GroupAnonymousBot
if(msg.from.id != 1087968824 && !msg.from.is_bot) {
g.addPlayer(userToUserInfo(msg.from));
}
std::vector<TelegramAPI::MessageEntity> entities;
const std::string text = gameToMessage(g, entities);
auto sendmsg = api->sendMessage(msg.chat.id, text, gameToKeyboard(g), entities);
@ -26,11 +36,6 @@ bool Bot::handleMessage(TelegramAPI::Manager* api, TelegramAPI::Message& msg) {
return true;
}
static Game::UserInfo userToUserInfo(const TelegramAPI::User& u) {
// + (u.last_name.empty() ? "" : " " + u.last_name) // problems with spaces and unicode in last name
return {u.id, u.first_name, u.username};
}
#define delret delete g; \
return true
@ -139,6 +144,8 @@ std::string Bot::gameToMessage(const Game& g, std::vector<TelegramAPI::MessageEn
} else {
out << "Winner: " << g.getWinner() << std::endl;
}
} else if(!g.ready()) {
out << "Waiting for players";
} else {
out << "nextturn: " << (g.getNextTurn() ? "X" : "O") << std::endl;
}
@ -199,6 +206,11 @@ Game* Bot::messageToGame(const TelegramAPI::Message& msg) const {
g->addPlayer(info);
}
// game not ready, just stop
if(!g->getPlayerB()) {
return g;
}
// read the next turn value
char nextturn = msg.text.at(msg.text.size()-1);
g->setNextTurn(nextturn == 'X');