aboutsummaryrefslogtreecommitdiffhomepage
path: root/td/telegram/Game.cpp
diff options
context:
space:
mode:
authorArseny Smirnov <arseny30@gmail.com>2018-12-31 22:04:05 +0300
committerArseny Smirnov <arseny30@gmail.com>2017-12-31 23:08:40 +0300
commit71d03f39c364367a8a7c51f783a41099297de826 (patch)
tree4c0c57f9ddb87f46d58192e1ebfd2c40f6d2c315 /td/telegram/Game.cpp
Project import generated by Copybara.
GitOrigin-RevId: 318483224ad6164d9966f731d60cde37039bb2d4
Diffstat (limited to 'td/telegram/Game.cpp')
-rw-r--r--td/telegram/Game.cpp123
1 files changed, 123 insertions, 0 deletions
diff --git a/td/telegram/Game.cpp b/td/telegram/Game.cpp
new file mode 100644
index 000000000..6efb3b28e
--- /dev/null
+++ b/td/telegram/Game.cpp
@@ -0,0 +1,123 @@
+//
+// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2017
+//
+// 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/td_api.h"
+#include "td/telegram/telegram_api.h"
+
+#include "td/telegram/AnimationsManager.h"
+#include "td/telegram/ContactsManager.h"
+#include "td/telegram/DocumentsManager.h"
+#include "td/telegram/Photo.h"
+#include "td/telegram/Td.h"
+
+#include "td/utils/common.h"
+#include "td/utils/logging.h"
+
+namespace td {
+
+Game::Game(Td *td, tl_object_ptr<telegram_api::game> &&game, DialogId owner_dialog_id)
+ : 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_;
+ short_name_ = game->short_name_;
+}
+
+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);
+ if (photo->get_id() == telegram_api::photo::ID) {
+ photo_ = get_photo(td->file_manager_.get(), move_tl_object_as<telegram_api::photo>(photo), owner_dialog_id);
+ }
+ 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);
+ switch (parsed_document.first) {
+ case DocumentsManager::DocumentType::Animation:
+ animation_file_id_ = parsed_document.second;
+ break;
+ case DocumentsManager::DocumentType::Audio:
+ case DocumentsManager::DocumentType::General:
+ case DocumentsManager::DocumentType::Sticker:
+ case DocumentsManager::DocumentType::Video:
+ case DocumentsManager::DocumentType::VideoNote:
+ case DocumentsManager::DocumentType::VoiceNote:
+ case DocumentsManager::DocumentType::Unknown:
+ LOG(ERROR) << "Receive non-animation document in the game";
+ break;
+ default:
+ UNREACHABLE();
+ }
+ }
+ }
+}
+
+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();
+ }
+}
+
+bool Game::empty() const {
+ return short_name_.empty();
+}
+
+void Game::set_bot_user_id(UserId bot_user_id) {
+ if (bot_user_id.is_valid()) {
+ bot_user_id_ = bot_user_id;
+ } else {
+ bot_user_id_ = UserId();
+ }
+}
+
+UserId Game::get_bot_user_id() const {
+ return bot_user_id_;
+}
+
+void Game::set_message_text(string text, vector<MessageEntity> &&entities) {
+ text_ = std::move(text);
+ entities_ = std::move(entities);
+}
+
+tl_object_ptr<td_api::game> Game::get_game_object(const Td *td) const {
+ return make_tl_object<td_api::game>(
+ id_, short_name_, title_, text_, get_text_entities_object(entities_), description_,
+ get_photo_object(td->file_manager_.get(), &photo_),
+ td->animations_manager_->get_animation_object(animation_file_id_, "get_game_object"));
+}
+
+tl_object_ptr<telegram_api::inputMediaGame> Game::get_input_media_game(const Td *td) const {
+ auto input_user = td->contacts_manager_->get_input_user(bot_user_id_);
+ CHECK(input_user != nullptr);
+ return make_tl_object<telegram_api::inputMediaGame>(
+ make_tl_object<telegram_api::inputGameShortName>(std::move(input_user), short_name_));
+}
+
+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_ &&
+ lhs.photo_ == rhs.photo_ && lhs.animation_file_id_ == rhs.animation_file_id_ && lhs.text_ == rhs.text_ &&
+ lhs.entities_ == rhs.entities_;
+}
+
+bool operator!=(const Game &lhs, const Game &rhs) {
+ return !(lhs == rhs);
+}
+
+StringBuilder &operator<<(StringBuilder &string_builder, const Game &game) {
+ return string_builder << "Game[id = " << game.id_ << ", access_hash = " << game.access_hash_
+ << ", 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_ << "]";
+}
+
+} // namespace td