start the refactor
This commit is contained in:
@@ -1,17 +1,20 @@
|
||||
use teloxide::prelude::*;
|
||||
use chrono::Local;
|
||||
use std::ops::Add;
|
||||
use std::cmp::Ordering::Equal;
|
||||
use std::str;
|
||||
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{
|
||||
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()
|
||||
Local::now()
|
||||
.add(chrono::TimeDelta::hours(2))
|
||||
.format("%Y-%m-%d")
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn check_pole(group_id: &str) -> bool {
|
||||
@@ -23,26 +26,46 @@ fn check_pole(group_id: &str) -> bool {
|
||||
change_day(&slast_pole)
|
||||
}
|
||||
|
||||
fn do_pole(group_id: &str, user_id: &str, user_name: &str){
|
||||
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);
|
||||
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){
|
||||
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);
|
||||
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){
|
||||
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);
|
||||
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{
|
||||
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(),
|
||||
let ret = data.check_user_pole(
|
||||
&msg.chat.id.to_string(),
|
||||
&msg.from().unwrap().id.to_string(),
|
||||
&get_actual_day());
|
||||
&get_actual_day(),
|
||||
);
|
||||
check_group_points(msg, rw) && (ret == 0)
|
||||
}
|
||||
|
||||
@@ -54,44 +77,52 @@ enum Rewards {
|
||||
|
||||
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());
|
||||
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,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn exe_pole(
|
||||
msg: Message,
|
||||
bot: Bot,
|
||||
) -> anyhow::Result<()>{
|
||||
let text_lower = match msg.text(){
|
||||
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(),
|
||||
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?;
|
||||
&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?;
|
||||
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?;
|
||||
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(){
|
||||
fn pole_conditions(msg: Message) -> bool {
|
||||
let text_lower = match msg.text() {
|
||||
Some(t) => t.to_lowercase(),
|
||||
None => return false,
|
||||
};
|
||||
@@ -103,21 +134,21 @@ fn pole_conditions(msg: Message) -> bool{
|
||||
false
|
||||
}
|
||||
|
||||
fn plata_conditions(msg: Message) -> bool{
|
||||
let text_lower = match msg.text(){
|
||||
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) {
|
||||
if check_user_points(&msg, Rewards::PLATA) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
fn bronce_conditions(msg: Message) -> bool{
|
||||
let text_lower = match msg.text(){
|
||||
fn bronce_conditions(msg: Message) -> bool {
|
||||
let text_lower = match msg.text() {
|
||||
Some(t) => t.to_lowercase(),
|
||||
None => return false,
|
||||
};
|
||||
@@ -129,7 +160,7 @@ fn bronce_conditions(msg: Message) -> bool{
|
||||
false
|
||||
}
|
||||
|
||||
pub fn get_top(msg: Message, bot: Bot) -> <Bot as Requester>::SendMessage{
|
||||
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();
|
||||
@@ -139,11 +170,10 @@ pub fn get_top(msg: Message, bot: Bot) -> <Bot as Requester>::SendMessage{
|
||||
bot.send_message(msg.chat.id, format!("{}", repl))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
/*#[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