restarting project
This commit is contained in:
150
src/pole_dialogue/mod.rs
Normal file
150
src/pole_dialogue/mod.rs
Normal file
@@ -0,0 +1,150 @@
|
||||
use teloxide::prelude::*;
|
||||
use chrono::Local;
|
||||
use std::cmp::Ordering::Equal;
|
||||
use std::str;
|
||||
mod database;
|
||||
|
||||
fn change_day(last_day: &str) -> bool{
|
||||
last_day.cmp(&Local::now().format("%Y-%m-%d").to_string()) != Equal
|
||||
}
|
||||
|
||||
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, &Local::now().format("%Y-%m-%d").to_string(),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, &Local::now().format("%Y-%m-%d").to_string(),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, &Local::now().format("%Y-%m-%d").to_string(),Rewards::FAIL as i64);
|
||||
}
|
||||
|
||||
fn get_alias(msg: &teloxide::prelude::Message) -> String {
|
||||
match &msg.from().unwrap().username {
|
||||
Some(alias) => format!("@{}",alias),
|
||||
None => format!("{}",&*msg.from().unwrap().first_name),
|
||||
}
|
||||
}
|
||||
|
||||
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(),
|
||||
&Local::now().format("%Y-%m-%d").to_string());
|
||||
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(),
|
||||
&Local::now().format("%Y-%m-%d").to_string());
|
||||
match rw {
|
||||
Rewards::PLATA => ret < (Rewards::PLATA as i64 + Rewards::POLE as i64),
|
||||
Rewards::FAIL => ret < (Rewards::FAIL as i64 + 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"));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user