180 lines
4.9 KiB
Rust
180 lines
4.9 KiB
Rust
use crate::telegram_utils::*;
|
|
use chrono::Local;
|
|
use std::cmp::Ordering::Equal;
|
|
use std::ops::Add;
|
|
use std::str;
|
|
use teloxide::prelude::*;
|
|
mod database;
|
|
|
|
fn change_day(last_day: &str) -> bool {
|
|
last_day.cmp(&get_actual_day()) != Equal
|
|
}
|
|
|
|
fn get_actual_day() -> String {
|
|
Local::now()
|
|
.add(chrono::TimeDelta::hours(2))
|
|
.format("%Y-%m-%d")
|
|
.to_string()
|
|
}
|
|
|
|
fn check_pole(group_id: &str) -> bool {
|
|
let data: database::DatabasePole = database::DatabasePole::get_database();
|
|
let slast_pole = match data.last_pole(group_id) {
|
|
Ok(s) => s,
|
|
Err(_e) => return true,
|
|
};
|
|
change_day(&slast_pole)
|
|
}
|
|
|
|
fn do_pole(group_id: &str, user_id: &str, user_name: &str) {
|
|
let data: database::DatabasePole = database::DatabasePole::get_database();
|
|
data.write_points(
|
|
group_id,
|
|
user_id,
|
|
user_name,
|
|
&get_actual_day(),
|
|
Rewards::POLE as i64,
|
|
);
|
|
}
|
|
|
|
fn do_plata(group_id: &str, user_id: &str, user_name: &str) {
|
|
let data: database::DatabasePole = database::DatabasePole::get_database();
|
|
data.write_points(
|
|
group_id,
|
|
user_id,
|
|
user_name,
|
|
&get_actual_day(),
|
|
Rewards::PLATA as i64,
|
|
);
|
|
}
|
|
|
|
fn do_fail(group_id: &str, user_id: &str, user_name: &str) {
|
|
let data: database::DatabasePole = database::DatabasePole::get_database();
|
|
data.write_points(
|
|
group_id,
|
|
user_id,
|
|
user_name,
|
|
&get_actual_day(),
|
|
Rewards::FAIL as i64,
|
|
);
|
|
}
|
|
|
|
fn check_user_points(msg: &teloxide::prelude::Message, rw: Rewards) -> bool {
|
|
let data: database::DatabasePole = database::DatabasePole::get_database();
|
|
let ret = data.check_user_pole(
|
|
&msg.chat.id.to_string(),
|
|
&msg.from().unwrap().id.to_string(),
|
|
&get_actual_day(),
|
|
);
|
|
check_group_points(msg, rw) && (ret == 0)
|
|
}
|
|
|
|
enum Rewards {
|
|
POLE = 3,
|
|
PLATA = 2,
|
|
FAIL = 1,
|
|
}
|
|
|
|
fn check_group_points(msg: &teloxide::prelude::Message, rw: Rewards) -> bool {
|
|
let data: database::DatabasePole = database::DatabasePole::get_database();
|
|
let ret = data.check_group_points(&msg.chat.id.to_string(), &get_actual_day());
|
|
match rw {
|
|
Rewards::PLATA => ret == (Rewards::POLE as i64),
|
|
Rewards::FAIL => ret == (Rewards::PLATA as i64 + Rewards::POLE as i64),
|
|
_ => false,
|
|
}
|
|
}
|
|
|
|
pub async fn exe_pole(msg: Message, bot: Bot) -> anyhow::Result<()> {
|
|
let text_lower = match msg.text() {
|
|
Some(t) => t.to_lowercase(),
|
|
None => return Ok(()),
|
|
};
|
|
if pole_conditions(msg.clone()) {
|
|
do_pole(
|
|
&msg.chat.id.to_string(),
|
|
&*msg.from().unwrap().id.to_string(),
|
|
&get_alias(&msg),
|
|
);
|
|
bot.send_message(msg.chat.id, format!("{} ha hecho la pole", get_alias(&msg)))
|
|
.await?;
|
|
} else if plata_conditions(msg.clone()) {
|
|
do_plata(
|
|
&msg.chat.id.to_string(),
|
|
&*msg.from().unwrap().id.to_string(),
|
|
&get_alias(&msg),
|
|
);
|
|
bot.send_message(
|
|
msg.chat.id,
|
|
format!("{} ha hecho la plata", get_alias(&msg)),
|
|
)
|
|
.await?;
|
|
} else if bronce_conditions(msg.clone()) {
|
|
do_fail(
|
|
&msg.chat.id.to_string(),
|
|
&*msg.from().unwrap().id.to_string(),
|
|
&get_alias(&msg),
|
|
);
|
|
bot.send_message(msg.chat.id, format!("{} buen fail", get_alias(&msg)))
|
|
.await?;
|
|
}
|
|
return Ok(());
|
|
}
|
|
|
|
fn pole_conditions(msg: Message) -> bool {
|
|
let text_lower = match msg.text() {
|
|
Some(t) => t.to_lowercase(),
|
|
None => return false,
|
|
};
|
|
if text_lower.contains("pole") || text_lower.contains("oro") {
|
|
if check_pole(&msg.chat.id.to_string()) {
|
|
return true;
|
|
}
|
|
}
|
|
false
|
|
}
|
|
|
|
fn plata_conditions(msg: Message) -> bool {
|
|
let text_lower = match msg.text() {
|
|
Some(t) => t.to_lowercase(),
|
|
None => return false,
|
|
};
|
|
if text_lower.contains("plata") || text_lower.contains("subpole") {
|
|
if check_user_points(&msg, Rewards::PLATA) {
|
|
return true;
|
|
}
|
|
}
|
|
false
|
|
}
|
|
|
|
fn bronce_conditions(msg: Message) -> bool {
|
|
let text_lower = match msg.text() {
|
|
Some(t) => t.to_lowercase(),
|
|
None => return false,
|
|
};
|
|
if text_lower.contains("fail") || text_lower.contains("bronce") {
|
|
if check_user_points(&msg, Rewards::FAIL) {
|
|
return true;
|
|
}
|
|
}
|
|
false
|
|
}
|
|
|
|
pub fn get_top(msg: Message, bot: Bot) -> <Bot as Requester>::SendMessage {
|
|
let db = database::DatabasePole::get_database();
|
|
let top = db.get_top_users(&msg.chat.id.0.to_string());
|
|
let mut repl = String::new();
|
|
for u in top {
|
|
repl.push_str(&(u.1 + ": " + &u.0.to_string() + " puntos\n"));
|
|
}
|
|
bot.send_message(msg.chat.id, format!("{}", repl))
|
|
}
|
|
|
|
/*#[cfg(test)]
|
|
#[test]
|
|
fn compare_dates(){
|
|
assert_eq!(false, change_day("2020-01-01"));
|
|
assert_eq!(true, change_day("3025-01-01"));
|
|
}
|
|
*/
|