use crate::client::BotClient;
use crate::error::Result;
use rustigram_types::games::GameHighScore;
use serde::Serialize;
use std::future::{Future, IntoFuture};
use std::pin::Pin;
macro_rules! impl_into_future {
($builder:ident, $return_ty:ty, $method:literal) => {
impl IntoFuture for $builder {
type Output = Result<$return_ty>;
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send>>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move { self.client.post_json($method, &self.params).await })
}
}
};
}
#[derive(Serialize)]
struct SetGameScoreParams {
user_id: i64,
score: u32,
#[serde(skip_serializing_if = "Option::is_none")]
force: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
disable_edit_message: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
chat_id: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
message_id: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
inline_message_id: Option<String>,
}
pub struct SetGameScore {
client: BotClient,
params: SetGameScoreParams,
}
impl SetGameScore {
pub(crate) fn new(client: BotClient, user_id: i64, score: u32) -> Self {
Self {
client,
params: SetGameScoreParams {
user_id,
score,
force: None,
disable_edit_message: None,
chat_id: None,
message_id: None,
inline_message_id: None,
},
}
}
pub fn force(mut self, v: bool) -> Self {
self.params.force = Some(v);
self
}
pub fn disable_edit_message(mut self, v: bool) -> Self {
self.params.disable_edit_message = Some(v);
self
}
pub fn chat_message(mut self, chat_id: i64, message_id: i64) -> Self {
self.params.chat_id = Some(chat_id);
self.params.message_id = Some(message_id);
self
}
pub fn inline_message_id(mut self, id: impl Into<String>) -> Self {
self.params.inline_message_id = Some(id.into());
self
}
}
impl_into_future!(SetGameScore, serde_json::Value, "setGameScore");
#[derive(Serialize)]
struct GetGameHighScoresParams {
user_id: i64,
#[serde(skip_serializing_if = "Option::is_none")]
chat_id: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
message_id: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
inline_message_id: Option<String>,
}
pub struct GetGameHighScores {
client: BotClient,
params: GetGameHighScoresParams,
}
impl GetGameHighScores {
pub(crate) fn new(client: BotClient, user_id: i64) -> Self {
Self {
client,
params: GetGameHighScoresParams {
user_id,
chat_id: None,
message_id: None,
inline_message_id: None,
},
}
}
pub fn chat_message(mut self, chat_id: i64, message_id: i64) -> Self {
self.params.chat_id = Some(chat_id);
self.params.message_id = Some(message_id);
self
}
pub fn inline_message_id(mut self, id: impl Into<String>) -> Self {
self.params.inline_message_id = Some(id.into());
self
}
}
impl_into_future!(GetGameHighScores, Vec<GameHighScore>, "getGameHighScores");