2021-09-07 09:02:44 +02:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
2021-09-07 09:02:44 +02: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/GameManager.h"
|
|
|
|
|
2021-09-08 15:50:03 +02:00
|
|
|
#include "td/telegram/AccessRights.h"
|
2021-09-07 09:45:01 +02:00
|
|
|
#include "td/telegram/AuthManager.h"
|
2022-02-05 21:28:43 +01:00
|
|
|
#include "td/telegram/ChainId.h"
|
2021-09-07 09:45:01 +02:00
|
|
|
#include "td/telegram/DialogId.h"
|
2024-01-03 21:07:50 +01:00
|
|
|
#include "td/telegram/DialogManager.h"
|
2021-09-07 09:45:01 +02:00
|
|
|
#include "td/telegram/Global.h"
|
|
|
|
#include "td/telegram/InlineQueriesManager.h"
|
|
|
|
#include "td/telegram/MessageId.h"
|
|
|
|
#include "td/telegram/MessagesManager.h"
|
|
|
|
#include "td/telegram/net/DcId.h"
|
|
|
|
#include "td/telegram/net/NetQueryCreator.h"
|
|
|
|
#include "td/telegram/Td.h"
|
2023-07-01 13:53:04 +02:00
|
|
|
#include "td/telegram/telegram_api.h"
|
2021-09-07 09:45:01 +02:00
|
|
|
#include "td/telegram/UpdatesManager.h"
|
2024-04-02 02:52:34 +02:00
|
|
|
#include "td/telegram/UserManager.h"
|
2021-09-07 09:45:01 +02:00
|
|
|
|
|
|
|
#include "td/utils/buffer.h"
|
2021-09-08 15:50:03 +02:00
|
|
|
#include "td/utils/logging.h"
|
2021-09-07 09:45:01 +02:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
|
2021-09-07 09:02:44 +02:00
|
|
|
namespace td {
|
|
|
|
|
2022-02-03 22:46:10 +01:00
|
|
|
class SetGameScoreQuery final : public Td::ResultHandler {
|
2021-09-07 09:45:01 +02:00
|
|
|
Promise<Unit> promise_;
|
|
|
|
DialogId dialog_id_;
|
|
|
|
|
|
|
|
public:
|
2022-02-03 22:46:10 +01:00
|
|
|
explicit SetGameScoreQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
2021-09-07 09:45:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void send(DialogId dialog_id, MessageId message_id, bool edit_message,
|
2022-02-05 21:28:43 +01:00
|
|
|
tl_object_ptr<telegram_api::InputUser> input_user, int32 score, bool force) {
|
2021-09-07 09:45:01 +02:00
|
|
|
int32 flags = 0;
|
|
|
|
if (edit_message) {
|
|
|
|
flags |= telegram_api::messages_setGameScore::EDIT_MESSAGE_MASK;
|
|
|
|
}
|
|
|
|
if (force) {
|
|
|
|
flags |= telegram_api::messages_setGameScore::FORCE_MASK;
|
|
|
|
}
|
|
|
|
|
|
|
|
dialog_id_ = dialog_id;
|
|
|
|
|
2024-01-03 21:07:50 +01:00
|
|
|
auto input_peer = td_->dialog_manager_->get_input_peer(dialog_id, AccessRights::Edit);
|
2021-09-07 09:45:01 +02:00
|
|
|
if (input_peer == nullptr) {
|
2022-02-03 22:46:10 +01:00
|
|
|
return on_error(Status::Error(400, "Can't access the chat"));
|
2021-09-07 09:45:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CHECK(input_user != nullptr);
|
2022-02-01 17:34:14 +01:00
|
|
|
send_query(G()->net_query_creator().create(
|
2021-09-07 09:45:01 +02:00
|
|
|
telegram_api::messages_setGameScore(flags, false /*ignored*/, false /*ignored*/, std::move(input_peer),
|
2022-02-01 17:34:14 +01:00
|
|
|
message_id.get_server_message_id().get(), std::move(input_user), score),
|
2022-02-05 21:28:43 +01:00
|
|
|
{{dialog_id}}));
|
2021-09-07 09:45:01 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2021-09-07 09:45:01 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_setGameScore>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2021-09-07 09:45:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
auto ptr = result_ptr.move_as_ok();
|
|
|
|
LOG(INFO) << "Receive result for SetGameScore: " << to_string(ptr);
|
2021-11-08 13:20:38 +01:00
|
|
|
td_->updates_manager_->on_get_updates(std::move(ptr), std::move(promise_));
|
2021-09-07 09:45:01 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2022-02-03 22:46:10 +01:00
|
|
|
LOG(INFO) << "Receive error for SetGameScoreQuery: " << status;
|
2024-01-03 21:07:50 +01:00
|
|
|
td_->dialog_manager_->on_get_dialog_error(dialog_id_, status, "SetGameScoreQuery");
|
2021-09-07 09:45:01 +02:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class SetInlineGameScoreQuery final : public Td::ResultHandler {
|
|
|
|
Promise<Unit> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit SetInlineGameScoreQuery(Promise<Unit> &&promise) : promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
2021-09-03 11:27:59 +02:00
|
|
|
void send(tl_object_ptr<telegram_api::InputBotInlineMessageID> input_bot_inline_message_id, bool edit_message,
|
2021-09-07 09:45:01 +02:00
|
|
|
tl_object_ptr<telegram_api::InputUser> input_user, int32 score, bool force) {
|
|
|
|
CHECK(input_bot_inline_message_id != nullptr);
|
|
|
|
CHECK(input_user != nullptr);
|
|
|
|
|
|
|
|
int32 flags = 0;
|
|
|
|
if (edit_message) {
|
|
|
|
flags |= telegram_api::messages_setInlineGameScore::EDIT_MESSAGE_MASK;
|
|
|
|
}
|
|
|
|
if (force) {
|
|
|
|
flags |= telegram_api::messages_setInlineGameScore::FORCE_MASK;
|
|
|
|
}
|
|
|
|
|
2021-09-03 11:27:59 +02:00
|
|
|
auto dc_id = DcId::internal(InlineQueriesManager::get_inline_message_dc_id(input_bot_inline_message_id));
|
2021-09-07 09:45:01 +02:00
|
|
|
send_query(G()->net_query_creator().create(
|
|
|
|
telegram_api::messages_setInlineGameScore(flags, false /*ignored*/, false /*ignored*/,
|
|
|
|
std::move(input_bot_inline_message_id), std::move(input_user), score),
|
2022-02-01 16:51:20 +01:00
|
|
|
{}, dc_id));
|
2021-09-07 09:45:01 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2021-09-07 09:45:01 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_setInlineGameScore>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2021-09-07 09:45:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
LOG_IF(ERROR, !result_ptr.ok()) << "Receive false in result of setInlineGameScore";
|
|
|
|
|
|
|
|
promise_.set_value(Unit());
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2021-09-07 09:45:01 +02:00
|
|
|
LOG(INFO) << "Receive error for SetInlineGameScoreQuery: " << status;
|
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class GetGameHighScoresQuery final : public Td::ResultHandler {
|
|
|
|
Promise<td_api::object_ptr<td_api::gameHighScores>> promise_;
|
|
|
|
DialogId dialog_id_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit GetGameHighScoresQuery(Promise<td_api::object_ptr<td_api::gameHighScores>> &&promise)
|
|
|
|
: promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void send(DialogId dialog_id, MessageId message_id, tl_object_ptr<telegram_api::InputUser> input_user) {
|
|
|
|
dialog_id_ = dialog_id;
|
|
|
|
|
2024-01-03 21:07:50 +01:00
|
|
|
auto input_peer = td_->dialog_manager_->get_input_peer(dialog_id, AccessRights::Read);
|
2021-09-07 09:45:01 +02:00
|
|
|
CHECK(input_peer != nullptr);
|
|
|
|
|
|
|
|
CHECK(input_user != nullptr);
|
|
|
|
send_query(G()->net_query_creator().create(telegram_api::messages_getGameHighScores(
|
|
|
|
std::move(input_peer), message_id.get_server_message_id().get(), std::move(input_user))));
|
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2021-09-07 09:45:01 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getGameHighScores>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2021-09-07 09:45:01 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 13:20:38 +01:00
|
|
|
promise_.set_value(td_->game_manager_->get_game_high_scores_object(result_ptr.move_as_ok()));
|
2021-09-07 09:45:01 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2024-01-03 21:07:50 +01:00
|
|
|
td_->dialog_manager_->on_get_dialog_error(dialog_id_, status, "GetGameHighScoresQuery");
|
2021-09-07 09:45:01 +02:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class GetInlineGameHighScoresQuery final : public Td::ResultHandler {
|
|
|
|
Promise<td_api::object_ptr<td_api::gameHighScores>> promise_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
explicit GetInlineGameHighScoresQuery(Promise<td_api::object_ptr<td_api::gameHighScores>> &&promise)
|
|
|
|
: promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
2021-09-03 11:27:59 +02:00
|
|
|
void send(tl_object_ptr<telegram_api::InputBotInlineMessageID> input_bot_inline_message_id,
|
2021-09-07 09:45:01 +02:00
|
|
|
tl_object_ptr<telegram_api::InputUser> input_user) {
|
|
|
|
CHECK(input_bot_inline_message_id != nullptr);
|
|
|
|
CHECK(input_user != nullptr);
|
|
|
|
|
2021-09-03 11:27:59 +02:00
|
|
|
auto dc_id = DcId::internal(InlineQueriesManager::get_inline_message_dc_id(input_bot_inline_message_id));
|
2021-09-07 09:45:01 +02:00
|
|
|
send_query(G()->net_query_creator().create(
|
|
|
|
telegram_api::messages_getInlineGameHighScores(std::move(input_bot_inline_message_id), std::move(input_user)),
|
2022-02-01 16:51:20 +01:00
|
|
|
{}, dc_id));
|
2021-09-07 09:45:01 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_result(BufferSlice packet) final {
|
2021-09-07 09:45:01 +02:00
|
|
|
auto result_ptr = fetch_result<telegram_api::messages_getInlineGameHighScores>(packet);
|
|
|
|
if (result_ptr.is_error()) {
|
2021-11-08 12:19:57 +01:00
|
|
|
return on_error(result_ptr.move_as_error());
|
2021-09-07 09:45:01 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 13:20:38 +01:00
|
|
|
promise_.set_value(td_->game_manager_->get_game_high_scores_object(result_ptr.move_as_ok()));
|
2021-09-07 09:45:01 +02:00
|
|
|
}
|
|
|
|
|
2021-11-08 12:19:57 +01:00
|
|
|
void on_error(Status status) final {
|
2021-09-07 09:45:01 +02:00
|
|
|
promise_.set_error(std::move(status));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-09-07 09:02:44 +02:00
|
|
|
GameManager::GameManager(Td *td, ActorShared<> parent) : td_(td), parent_(std::move(parent)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
GameManager::~GameManager() = default;
|
|
|
|
|
|
|
|
void GameManager::tear_down() {
|
|
|
|
parent_.reset();
|
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
void GameManager::set_game_score(MessageFullId message_full_id, bool edit_message, UserId user_id, int32 score,
|
2021-09-07 11:05:44 +02:00
|
|
|
bool force, Promise<td_api::object_ptr<td_api::message>> &&promise) {
|
2021-09-07 09:45:01 +02:00
|
|
|
CHECK(td_->auth_manager_->is_bot());
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
if (!td_->messages_manager_->have_message_force(message_full_id, "set_game_score")) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Message not found"));
|
2021-09-07 09:45:01 +02:00
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
auto dialog_id = message_full_id.get_dialog_id();
|
2024-01-03 21:07:50 +01:00
|
|
|
if (!td_->dialog_manager_->have_input_peer(dialog_id, AccessRights::Edit)) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Can't access the chat"));
|
2021-09-07 09:45:01 +02:00
|
|
|
}
|
|
|
|
|
2024-04-02 02:52:34 +02:00
|
|
|
TRY_RESULT_PROMISE(promise, input_user, td_->user_manager_->get_input_user(user_id));
|
2021-09-07 09:45:01 +02:00
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
if (!td_->messages_manager_->can_set_game_score(message_full_id)) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Game score can't be set"));
|
2021-09-07 09:45:01 +02:00
|
|
|
}
|
|
|
|
|
2021-09-07 11:05:44 +02:00
|
|
|
auto query_promise = PromiseCreator::lambda(
|
2023-09-21 18:11:17 +02:00
|
|
|
[actor_id = actor_id(this), message_full_id, promise = std::move(promise)](Result<Unit> &&result) mutable {
|
2021-09-07 11:05:44 +02:00
|
|
|
if (result.is_error()) {
|
|
|
|
return promise.set_error(result.move_as_error());
|
|
|
|
}
|
2023-09-21 18:11:17 +02:00
|
|
|
send_closure(actor_id, &GameManager::on_set_game_score, message_full_id, std::move(promise));
|
2021-09-07 11:05:44 +02:00
|
|
|
});
|
2022-02-03 22:46:10 +01:00
|
|
|
td_->create_handler<SetGameScoreQuery>(std::move(query_promise))
|
2023-09-21 18:11:17 +02:00
|
|
|
->send(dialog_id, message_full_id.get_message_id(), edit_message, std::move(input_user), score, force);
|
2021-09-07 09:45:01 +02:00
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
void GameManager::on_set_game_score(MessageFullId message_full_id,
|
2021-09-07 11:05:44 +02:00
|
|
|
Promise<td_api::object_ptr<td_api::message>> &&promise) {
|
2023-09-21 18:11:17 +02:00
|
|
|
promise.set_value(td_->messages_manager_->get_message_object(message_full_id, "on_set_game_score"));
|
2021-09-07 11:05:44 +02:00
|
|
|
}
|
|
|
|
|
2021-09-07 09:45:01 +02:00
|
|
|
void GameManager::set_inline_game_score(const string &inline_message_id, bool edit_message, UserId user_id, int32 score,
|
|
|
|
bool force, Promise<Unit> &&promise) {
|
|
|
|
CHECK(td_->auth_manager_->is_bot());
|
|
|
|
|
|
|
|
auto input_bot_inline_message_id = td_->inline_queries_manager_->get_input_bot_inline_message_id(inline_message_id);
|
|
|
|
if (input_bot_inline_message_id == nullptr) {
|
|
|
|
return promise.set_error(Status::Error(400, "Invalid inline message identifier specified"));
|
|
|
|
}
|
|
|
|
|
2024-04-02 02:52:34 +02:00
|
|
|
TRY_RESULT_PROMISE(promise, input_user, td_->user_manager_->get_input_user(user_id));
|
2021-09-07 09:45:01 +02:00
|
|
|
|
|
|
|
td_->create_handler<SetInlineGameScoreQuery>(std::move(promise))
|
2023-03-22 15:39:56 +01:00
|
|
|
->send(std::move(input_bot_inline_message_id), edit_message, std::move(input_user), score, force);
|
2021-09-07 09:45:01 +02:00
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
void GameManager::get_game_high_scores(MessageFullId message_full_id, UserId user_id,
|
2021-09-07 09:45:01 +02:00
|
|
|
Promise<td_api::object_ptr<td_api::gameHighScores>> &&promise) {
|
|
|
|
CHECK(td_->auth_manager_->is_bot());
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
if (!td_->messages_manager_->have_message_force(message_full_id, "get_game_high_scores")) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Message not found"));
|
2021-09-07 09:45:01 +02:00
|
|
|
}
|
|
|
|
|
2023-09-21 18:11:17 +02:00
|
|
|
auto dialog_id = message_full_id.get_dialog_id();
|
2024-01-03 21:07:50 +01:00
|
|
|
if (!td_->dialog_manager_->have_input_peer(dialog_id, AccessRights::Read)) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Can't access the chat"));
|
2021-09-07 09:45:01 +02:00
|
|
|
}
|
2023-09-21 18:11:17 +02:00
|
|
|
auto message_id = message_full_id.get_message_id();
|
2023-03-22 13:05:05 +01:00
|
|
|
if (message_id.is_scheduled() || !message_id.is_server() || dialog_id.get_type() == DialogType::SecretChat) {
|
2021-09-24 09:59:51 +02:00
|
|
|
return promise.set_error(Status::Error(400, "Wrong message identifier specified"));
|
2021-09-07 09:45:01 +02:00
|
|
|
}
|
|
|
|
|
2024-04-02 02:52:34 +02:00
|
|
|
TRY_RESULT_PROMISE(promise, input_user, td_->user_manager_->get_input_user(user_id));
|
2021-09-07 09:45:01 +02:00
|
|
|
|
2023-03-22 15:39:56 +01:00
|
|
|
td_->create_handler<GetGameHighScoresQuery>(std::move(promise))->send(dialog_id, message_id, std::move(input_user));
|
2021-09-07 09:45:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void GameManager::get_inline_game_high_scores(const string &inline_message_id, UserId user_id,
|
|
|
|
Promise<td_api::object_ptr<td_api::gameHighScores>> &&promise) {
|
|
|
|
CHECK(td_->auth_manager_->is_bot());
|
|
|
|
|
|
|
|
auto input_bot_inline_message_id = td_->inline_queries_manager_->get_input_bot_inline_message_id(inline_message_id);
|
|
|
|
if (input_bot_inline_message_id == nullptr) {
|
|
|
|
return promise.set_error(Status::Error(400, "Invalid inline message identifier specified"));
|
|
|
|
}
|
|
|
|
|
2024-04-02 02:52:34 +02:00
|
|
|
TRY_RESULT_PROMISE(promise, input_user, td_->user_manager_->get_input_user(user_id));
|
2021-09-07 09:45:01 +02:00
|
|
|
|
|
|
|
td_->create_handler<GetInlineGameHighScoresQuery>(std::move(promise))
|
2023-03-22 15:39:56 +01:00
|
|
|
->send(std::move(input_bot_inline_message_id), std::move(input_user));
|
2021-09-07 09:45:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::gameHighScores> GameManager::get_game_high_scores_object(
|
|
|
|
telegram_api::object_ptr<telegram_api::messages_highScores> &&high_scores) {
|
2024-04-02 02:52:34 +02:00
|
|
|
td_->user_manager_->on_get_users(std::move(high_scores->users_), "get_game_high_scores_object");
|
2021-09-07 09:45:01 +02:00
|
|
|
|
|
|
|
auto result = td_api::make_object<td_api::gameHighScores>();
|
|
|
|
for (const auto &high_score : high_scores->scores_) {
|
|
|
|
int32 position = high_score->pos_;
|
|
|
|
UserId user_id(high_score->user_id_);
|
|
|
|
int32 score = high_score->score_;
|
|
|
|
if (position <= 0 || !user_id.is_valid() || score < 0) {
|
|
|
|
LOG(ERROR) << "Receive wrong " << to_string(high_score);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
result->scores_.push_back(make_tl_object<td_api::gameHighScore>(
|
2024-04-02 02:52:34 +02:00
|
|
|
position, td_->user_manager_->get_user_id_object(user_id, "get_game_high_scores_object"), score));
|
2021-09-07 09:45:01 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2021-09-07 09:02:44 +02:00
|
|
|
} // namespace td
|