Changeset View
Changeset View
Standalone View
Standalone View
source/tools/rdb/src/connection.rs
//! An assortment of structs/enums which aren't db models. | |||||
//! | |||||
//! All types that we write to clients should be placed here. | |||||
use crate::filebase::Filebase; | |||||
use crate::models::{PublicReplayReviewStatus, Replay, ReplayId, UserId}; | |||||
use crate::DbConn; | |||||
use std::net::SocketAddr; | |||||
pub struct ConnState { | |||||
pub username: Option<String>, | |||||
pub user_id: Option<UserId>, | |||||
pub peer_ip: SocketAddr, | |||||
} | |||||
impl ConnState { | |||||
pub fn new(peer_ip: SocketAddr) -> Self { | |||||
ConnState { | |||||
username: None, | |||||
user_id: None, | |||||
peer_ip, | |||||
} | |||||
} | |||||
} | |||||
#[derive(Copy, Clone, Debug, Serialize, Deserialize)] | |||||
pub enum PlayersType { | |||||
Singleplayer, | |||||
Multiplayer, | |||||
Both, | |||||
} | |||||
#[derive(Copy, Clone, Debug, Serialize, Deserialize)] | |||||
pub enum RatedType { | |||||
Rated, | |||||
Unrated, | |||||
Both, | |||||
} | |||||
#[derive(Copy, Clone, Debug, Serialize, Deserialize)] | |||||
pub enum SortMethod { | |||||
Time, | |||||
Players, | |||||
MapName, | |||||
Size, | |||||
Population, | |||||
Duration, | |||||
Id, | |||||
Submitter, | |||||
Title, | |||||
SubmissionDate, | |||||
} | |||||
#[derive(Debug, Clone, Serialize, Deserialize)] | |||||
pub struct PublicUser { | |||||
pub id: UserId, | |||||
pub username: String, | |||||
} | |||||
#[derive(Clone, Debug, Serialize, Deserialize)] | |||||
pub struct ReplayData { | |||||
id: ReplayId, | |||||
submitter_id: UserId, | |||||
title: Option<String>, | |||||
msg_body: Option<String>, | |||||
submission_date: Option<u64>, | |||||
review_status: PublicReplayReviewStatus, | |||||
metadata: Option<String>, | |||||
replay_data_full: Option<String>, | |||||
replay_data_edge_lines: Option<String>, | |||||
} | |||||
impl ReplayData { | |||||
fn none_bool<T>(b: bool, t: T) -> Option<T> { | |||||
if b { | |||||
Some(t) | |||||
} else { | |||||
None | |||||
} | |||||
} | |||||
pub fn new( | |||||
db_conn: &DbConn, | |||||
conn_state: &ConnState, | |||||
replay: &Replay, | |||||
get_replay_title: bool, | |||||
get_replay_msg: bool, | |||||
get_metadata: bool, | |||||
get_replay_data_full: bool, | |||||
get_replay_data_edge_lines: bool, | |||||
get_replay_submission_date: bool, | |||||
) -> ReplayData { | |||||
ReplayData { | |||||
id: replay.ddb_id, | |||||
submitter_id: replay.ddb_submitter_id, | |||||
title: Self::none_bool(get_replay_title, replay.ddb_title.clone()), | |||||
msg_body: Self::none_bool(get_replay_msg, replay.ddb_msg_body.clone()), | |||||
submission_date: Self::none_bool(get_replay_submission_date, replay.ddb_submission_date.clone()), | |||||
review_status: replay | |||||
.ddb_review_status | |||||
.accessible_status(&db_conn, conn_state.username.as_ref().unwrap()), | |||||
metadata: Self::none_bool(get_metadata, Filebase::get_metadata(replay.ddb_id)), | |||||
replay_data_full: Self::none_bool(get_replay_data_full, Filebase::get_replay_data_full(replay.ddb_id)), | |||||
replay_data_edge_lines: Self::none_bool(get_replay_data_edge_lines, Filebase::get_replay_data_edge_lines(replay.ddb_id)), | |||||
} | |||||
} | |||||
} | |||||
#[derive(Clone, Debug, Serialize, Deserialize)] | |||||
pub enum Msg { | |||||
Error { | |||||
msg: String, | |||||
}, | |||||
Success, | |||||
Login { | |||||
username: String, | |||||
password: String, | |||||
version: u64, | |||||
}, | |||||
Register { | |||||
username: String, | |||||
password: String, | |||||
version: u64, | |||||
}, | |||||
QueryForReplayList { | |||||
offset: u64, | |||||
numb: u64, | |||||
replay_id: Option<u64>, | |||||
sort_method: SortMethod, | |||||
dir_accending: bool, | |||||
players_type: PlayersType, | |||||
rated_type: RatedType, | |||||
timestamp_day: Option<String>, | |||||
submitter: Option<String>, | |||||
submitter_id: Option<u64>, | |||||
players: Option<String>, | |||||
map_name: Option<String>, | |||||
size_min: Option<u16>, | |||||
size_max: Option<u16>, | |||||
population_min: Option<u16>, | |||||
population_max: Option<u16>, | |||||
duration_min: Option<u32>, | |||||
duration_max: Option<u32>, | |||||
victory_condition: Option<String>, | |||||
mods: Vec<Vec<String>>, | |||||
engine_version: String, | |||||
get_replay_title: bool, | |||||
get_replay_msg: bool, | |||||
get_metadata: bool, | |||||
get_replay_data_full: bool, | |||||
get_replay_data_edge_lines: bool, | |||||
get_replay_submission_date: bool, | |||||
}, | |||||
QueryForReplayDatas { | |||||
replay_ids: Vec<ReplayId>, | |||||
get_replay_title: bool, | |||||
get_replay_msg: bool, | |||||
get_metadata: bool, | |||||
get_replay_data_full: bool, | |||||
get_replay_data_edge_lines: bool, | |||||
get_replay_submission_date: bool, | |||||
}, | |||||
ReplayDatas { | |||||
replay_datas: Vec<ReplayData>, | |||||
}, | |||||
QueryForUserData { | |||||
user_ids: Vec<UserId>, | |||||
usernames: Vec<String>, | |||||
}, | |||||
UserDatas { | |||||
user_datas: Vec<PublicUser>, | |||||
}, | |||||
} |
Wildfire Games · Phabricator