Compare commits
2 Commits
188db932fb
...
856b55c8a6
Author | SHA1 | Date |
---|---|---|
mrbesen | 856b55c8a6 | |
mrbesen | 8a69fdc81f |
22
src/bot.cpp
22
src/bot.cpp
|
@ -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');
|
||||
|
|
Loading…
Reference in New Issue