Implemented the `create_room` command to allow users to create new chat rooms with customizable settings such as privacy, age restrictions, and ownership. Enhanced room management by introducing functions to mark rooms as occupied or possibly empty, and added cleanup logic for stale temporary rooms. Updated the `RoomMeta` structure to include new fields for room creation timestamps and temporary status, ensuring better room lifecycle management.
102 lines
3.1 KiB
Rust
102 lines
3.1 KiB
Rust
use serde::{Deserialize, Serialize};
|
|
use serde_json::Value;
|
|
use std::collections::{HashMap, HashSet};
|
|
use std::sync::Arc;
|
|
use tokio::sync::mpsc;
|
|
use tokio_postgres::Client as PgClient;
|
|
|
|
pub(crate) type ClientId = u64;
|
|
|
|
#[derive(Clone)]
|
|
pub(crate) struct ClientConn {
|
|
pub(crate) user_name: String,
|
|
pub(crate) room: String,
|
|
pub(crate) color: Option<String>,
|
|
pub(crate) token: Option<String>,
|
|
pub(crate) falukant_user_id: Option<i32>,
|
|
pub(crate) chat_user_id: Option<i32>,
|
|
pub(crate) gender_id: Option<i32>,
|
|
pub(crate) age: Option<i32>,
|
|
pub(crate) rights: HashSet<String>,
|
|
pub(crate) right_type_ids: HashSet<i32>,
|
|
pub(crate) logged_in: bool,
|
|
pub(crate) tx: mpsc::UnboundedSender<String>,
|
|
}
|
|
|
|
#[derive(Default)]
|
|
pub(crate) struct ChatState {
|
|
pub(crate) clients: HashMap<ClientId, ClientConn>,
|
|
pub(crate) rooms: HashMap<String, HashSet<ClientId>>,
|
|
pub(crate) tokens: HashMap<String, ClientId>,
|
|
pub(crate) logged_in_names: HashSet<String>,
|
|
pub(crate) dice_games: HashMap<String, DiceGame>,
|
|
pub(crate) room_meta: HashMap<String, RoomMeta>,
|
|
}
|
|
|
|
#[derive(Clone, Default)]
|
|
pub(crate) struct ServerConfig {
|
|
pub(crate) allowed_users: Option<HashSet<String>>,
|
|
pub(crate) db_client: Option<Arc<PgClient>>,
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub(crate) struct UserProfile {
|
|
pub(crate) display_name: String,
|
|
pub(crate) color: Option<String>,
|
|
pub(crate) falukant_user_id: Option<i32>,
|
|
pub(crate) chat_user_id: Option<i32>,
|
|
pub(crate) gender_id: Option<i32>,
|
|
pub(crate) age: Option<i32>,
|
|
pub(crate) rights: HashSet<String>,
|
|
pub(crate) right_type_ids: HashSet<i32>,
|
|
}
|
|
|
|
#[derive(Debug, Deserialize)]
|
|
pub(crate) struct Command {
|
|
#[serde(rename = "type")]
|
|
pub(crate) cmd_type: Value,
|
|
pub(crate) token: Option<String>,
|
|
pub(crate) name: Option<String>,
|
|
pub(crate) room: Option<String>,
|
|
pub(crate) message: Option<String>,
|
|
pub(crate) value: Option<Value>,
|
|
pub(crate) password: Option<String>,
|
|
pub(crate) rounds: Option<i32>,
|
|
pub(crate) to: Option<String>,
|
|
#[serde(rename = "userName")]
|
|
pub(crate) user_name: Option<String>,
|
|
}
|
|
|
|
#[derive(Debug, Serialize)]
|
|
pub(crate) struct RoomInfo {
|
|
pub(crate) name: String,
|
|
pub(crate) users: usize,
|
|
}
|
|
|
|
#[derive(Clone, Default)]
|
|
pub(crate) struct DiceGame {
|
|
pub(crate) running: bool,
|
|
pub(crate) current_round: i32,
|
|
pub(crate) total_rounds: i32,
|
|
pub(crate) rolled_this_round: HashMap<ClientId, i32>,
|
|
pub(crate) total_scores: HashMap<ClientId, i32>,
|
|
}
|
|
|
|
#[allow(dead_code)]
|
|
#[derive(Clone, Debug, Default)]
|
|
pub(crate) struct RoomMeta {
|
|
pub(crate) name: String,
|
|
pub(crate) password: Option<String>,
|
|
pub(crate) gender_restriction_id: Option<i32>,
|
|
pub(crate) required_user_right_id: Option<i32>,
|
|
pub(crate) min_age: Option<i32>,
|
|
pub(crate) max_age: Option<i32>,
|
|
pub(crate) is_public: bool,
|
|
pub(crate) owner_id: Option<i32>,
|
|
pub(crate) room_type_id: Option<i32>,
|
|
pub(crate) friends_of_owner_only: bool,
|
|
pub(crate) is_temporary: bool,
|
|
pub(crate) created_at_unix: Option<i64>,
|
|
pub(crate) empty_since_unix: Option<i64>,
|
|
}
|