refactoring

This commit is contained in:
Victor Martinez Montane 2021-08-24 15:15:55 +02:00
parent 3a06cbc948
commit dcc79bdbf0
8 changed files with 39 additions and 16 deletions

1
Cargo.lock generated
View file

@ -427,6 +427,7 @@ dependencies = [
"log",
"mobc",
"mobc-redis",
"once_cell",
"pretty_env_logger",
"teloxide",
"thiserror",

View file

@ -12,5 +12,6 @@ thiserror = "1.0.x"
anyhow = "1.0.x"
pretty_env_logger = "0.4.0"
tokio = { version = "1.3.0", features = ["rt-multi-thread", "macros"] }
once_cell = "1.8.x"
mobc-redis = "0.7.0"
mobc = "0.7.3"

View file

@ -1,5 +1,6 @@
start - start a game
stop - stop the game
newgame - start a new game
join - join the game
endgame - stop the game
ask - ask someone for cards
status - ask the bot to show the game general status
mystatus - ask the bot to send you your status

View file

@ -16,10 +16,12 @@ use teloxide::utils::command::BotCommand;
parse_with = "split"
)]
pub enum Command {
#[command(description = "start a game")]
Start,
#[command(description = "stop the game")]
Stop,
#[command(description = "start a new game")]
NewGame,
#[command(description = "join the current game")]
Join,
#[command(description = "end the game")]
EndGame,
#[command(description = "ask someone for cards")]
Ask,
#[command(description = "ask the bot to show the game general status")]

View file

@ -4,33 +4,49 @@ mod command;
mod db;
mod errors;
use alias::Cx;
use crate::db::connect;
use alias::{Cx, MobcPool};
use anyhow::Result;
use command::Command;
use dotenv;
use once_cell::sync::OnceCell;
use teloxide::{prelude::*, types::Me};
pub static POOL: OnceCell<MobcPool> = OnceCell::new();
#[tokio::main]
async fn main() {
dotenv::dotenv().ok();
teloxide::enable_logging!();
run().await;
let url = std::env::var("REDIS_URL").expect("REDIS_URL not found");
run(&url).await;
}
async fn run() {
async fn run(redis_url: &str) {
log::info!("Connecting to redis...");
initialize_pool(redis_url).await;
log::info!("Starting bot...");
let redis_url = std::env::var("REDIS_URL").expect("REDIS_URL not found");
let bot = Bot::from_env().auto_send();
let Me { user: bot_user, .. } = bot.get_me().await.unwrap();
let bot_name = bot_user.username.expect("Bots must have usernames");
let (bot, bot_name) = initialize_bot().await;
log::info!("listening...");
teloxide::commands_repl(bot, bot_name, execute).await;
}
async fn initialize_bot() -> (AutoSend<Bot>, String) {
let bot = Bot::from_env().auto_send();
let Me { user: bot_user, .. } = bot.get_me().await.unwrap();
let bot_name = bot_user.username.expect("Bots must have usernames");
(bot, bot_name)
}
async fn initialize_pool(url: &str) {
let pool = connect(url).await.expect("Error connecting to redis");
POOL.set(pool)
.map_err(|_| ())
.expect("Error initializing pool");
}
async fn execute(cx: Cx, command: Command) -> Result<()> {
let pool = POOL.get().unwrap();
match command {
Command::Help => actions::help(&cx).await?,
_ => actions::say_hi(&cx).await?,

0
src/models/game.rs Normal file
View file

2
src/models/mod.rs Normal file
View file

@ -0,0 +1,2 @@
pub mod game;
pub mod player;

0
src/models/player.rs Normal file
View file