2018-12-31 20:04:05 +01:00
|
|
|
//
|
2021-01-01 13:57:46 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/telegram/Game.h"
|
|
|
|
|
|
|
|
#include "td/telegram/AnimationsManager.h"
|
|
|
|
#include "td/telegram/ContactsManager.h"
|
2019-04-09 17:38:57 +02:00
|
|
|
#include "td/telegram/Document.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/DocumentsManager.h"
|
2018-09-28 22:57:34 +02:00
|
|
|
#include "td/telegram/misc.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/Photo.h"
|
|
|
|
#include "td/telegram/Td.h"
|
|
|
|
|
|
|
|
#include "td/utils/common.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
|
|
|
|
namespace td {
|
|
|
|
|
2021-09-02 15:49:57 +02:00
|
|
|
Game::Game(Td *td, UserId bot_user_id, tl_object_ptr<telegram_api::game> &&game, FormattedText text,
|
|
|
|
DialogId owner_dialog_id)
|
2018-12-31 20:04:05 +01:00
|
|
|
: Game(td, std::move(game->title_), std::move(game->description_), std::move(game->photo_),
|
|
|
|
std::move(game->document_), owner_dialog_id) {
|
|
|
|
id_ = game->id_;
|
|
|
|
access_hash_ = game->access_hash_;
|
2021-09-02 15:49:57 +02:00
|
|
|
bot_user_id_ = bot_user_id.is_valid() ? bot_user_id : UserId();
|
2018-12-31 20:04:05 +01:00
|
|
|
short_name_ = game->short_name_;
|
2021-09-02 15:49:57 +02:00
|
|
|
text_ = std::move(text);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Game::Game(Td *td, string title, string description, tl_object_ptr<telegram_api::Photo> &&photo,
|
|
|
|
tl_object_ptr<telegram_api::Document> &&document, DialogId owner_dialog_id)
|
|
|
|
: title_(std::move(title)), description_(std::move(description)) {
|
|
|
|
CHECK(td != nullptr);
|
|
|
|
CHECK(photo != nullptr);
|
2019-06-17 01:41:57 +02:00
|
|
|
photo_ = get_photo(td->file_manager_.get(), std::move(photo), owner_dialog_id);
|
2020-06-23 20:04:26 +02:00
|
|
|
if (photo_.is_empty()) {
|
2021-11-10 18:05:41 +01:00
|
|
|
LOG(ERROR) << "Receive empty photo for game " << title_;
|
2019-06-17 01:41:57 +02:00
|
|
|
photo_.id = 0; // to prevent null photo in td_api
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
if (document != nullptr) {
|
|
|
|
int32 document_id = document->get_id();
|
|
|
|
if (document_id == telegram_api::document::ID) {
|
|
|
|
auto parsed_document =
|
|
|
|
td->documents_manager_->on_get_document(move_tl_object_as<telegram_api::document>(document), owner_dialog_id);
|
2019-04-09 17:38:57 +02:00
|
|
|
if (parsed_document.type == Document::Type::Animation) {
|
|
|
|
animation_file_id_ = parsed_document.file_id;
|
|
|
|
} else {
|
|
|
|
LOG(ERROR) << "Receive non-animation document in the game";
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Game::Game(UserId bot_user_id, string short_name) : bot_user_id_(bot_user_id), short_name_(std::move(short_name)) {
|
|
|
|
if (!bot_user_id_.is_valid()) {
|
|
|
|
bot_user_id_ = UserId();
|
|
|
|
}
|
2020-06-23 19:50:16 +02:00
|
|
|
photo_.id = 0; // to prevent null photo in td_api
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-09-02 15:35:56 +02:00
|
|
|
bool Game::is_empty() const {
|
2018-12-31 20:04:05 +01:00
|
|
|
return short_name_.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
UserId Game::get_bot_user_id() const {
|
|
|
|
return bot_user_id_;
|
|
|
|
}
|
|
|
|
|
2019-02-28 19:40:57 +01:00
|
|
|
vector<FileId> Game::get_file_ids(const Td *td) const {
|
2019-01-06 00:15:07 +01:00
|
|
|
auto result = photo_get_file_ids(photo_);
|
2019-08-23 17:22:14 +02:00
|
|
|
Document(Document::Type::Animation, animation_file_id_).append_file_ids(td, result);
|
2019-01-06 00:15:07 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-09-28 01:16:49 +02:00
|
|
|
const FormattedText &Game::get_text() const {
|
2018-04-02 01:03:22 +02:00
|
|
|
return text_;
|
|
|
|
}
|
|
|
|
|
2021-07-22 04:39:16 +02:00
|
|
|
tl_object_ptr<td_api::game> Game::get_game_object(Td *td, bool skip_bot_commands) const {
|
2021-08-28 13:16:29 +02:00
|
|
|
return make_tl_object<td_api::game>(id_, short_name_, title_, get_formatted_text_object(text_, skip_bot_commands, -1),
|
|
|
|
description_, get_photo_object(td->file_manager_.get(), photo_),
|
|
|
|
td->animations_manager_->get_animation_object(animation_file_id_));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-01-16 03:18:06 +01:00
|
|
|
bool Game::has_input_media() const {
|
|
|
|
return bot_user_id_.is_valid();
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
tl_object_ptr<telegram_api::inputMediaGame> Game::get_input_media_game(const Td *td) const {
|
2021-12-16 20:46:03 +01:00
|
|
|
auto r_input_user = td->contacts_manager_->get_input_user(bot_user_id_);
|
|
|
|
CHECK(r_input_user.is_ok());
|
2018-12-31 20:04:05 +01:00
|
|
|
return make_tl_object<telegram_api::inputMediaGame>(
|
2021-12-16 20:46:03 +01:00
|
|
|
make_tl_object<telegram_api::inputGameShortName>(r_input_user.move_as_ok(), short_name_));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(const Game &lhs, const Game &rhs) {
|
|
|
|
return lhs.id_ == rhs.id_ && lhs.access_hash_ == rhs.access_hash_ && lhs.bot_user_id_ == rhs.bot_user_id_ &&
|
|
|
|
lhs.short_name_ == rhs.short_name_ && lhs.title_ == rhs.title_ && lhs.description_ == rhs.description_ &&
|
2018-01-30 18:06:54 +01:00
|
|
|
lhs.photo_ == rhs.photo_ && lhs.animation_file_id_ == rhs.animation_file_id_ && lhs.text_ == rhs.text_;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool operator!=(const Game &lhs, const Game &rhs) {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
StringBuilder &operator<<(StringBuilder &string_builder, const Game &game) {
|
2021-03-15 01:32:14 +01:00
|
|
|
return string_builder << "Game[ID = " << game.id_ << ", access_hash = " << game.access_hash_
|
2018-12-31 20:04:05 +01:00
|
|
|
<< ", bot = " << game.bot_user_id_ << ", short_name = " << game.short_name_
|
|
|
|
<< ", title = " << game.title_ << ", description = " << game.description_
|
|
|
|
<< ", photo = " << game.photo_ << ", animation_file_id = " << game.animation_file_id_ << "]";
|
|
|
|
}
|
|
|
|
|
2018-09-28 22:57:34 +02:00
|
|
|
Result<Game> process_input_message_game(const ContactsManager *contacts_manager,
|
|
|
|
tl_object_ptr<td_api::InputMessageContent> &&input_message_content) {
|
|
|
|
CHECK(input_message_content != nullptr);
|
|
|
|
CHECK(input_message_content->get_id() == td_api::inputMessageGame::ID);
|
|
|
|
auto input_message_game = move_tl_object_as<td_api::inputMessageGame>(input_message_content);
|
|
|
|
|
|
|
|
UserId bot_user_id(input_message_game->bot_user_id_);
|
|
|
|
if (!contacts_manager->have_input_user(bot_user_id)) {
|
|
|
|
return Status::Error(400, "Game owner bot is not accessible");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!clean_input_string(input_message_game->game_short_name_)) {
|
|
|
|
return Status::Error(400, "Game short name must be encoded in UTF-8");
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO validate game_short_name
|
|
|
|
if (input_message_game->game_short_name_.empty()) {
|
|
|
|
return Status::Error(400, "Game short name must be non-empty");
|
|
|
|
}
|
|
|
|
|
|
|
|
return Game(bot_user_id, std::move(input_message_game->game_short_name_));
|
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
} // namespace td
|