Skip to content

Commit

Permalink
09/24 Bug Fixes
Browse files Browse the repository at this point in the history
- Fix collection names
- ownerName is nullable for cores/destroyables/etc.
- Remove usages of plain-text error responder
- pointId is now String
- Dump parsed socket payload on deserialization failure
  • Loading branch information
chatasma committed Sep 24, 2023
1 parent 0dc90f4 commit 3b14a13
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/database/models/death.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl CollectionOwner<Death> for Death {
}

fn get_collection_name() -> &'static str {
"deaths"
"death"
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/database/models/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl CollectionOwner<Level> for Level {
}

fn get_collection_name() -> &'static str {
"levels"
"level"
}
}

Expand Down
10 changes: 5 additions & 5 deletions src/database/models/match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl CollectionOwner<Match> for Match {
}

fn get_collection_name() -> &'static str {
"matches"
"match"
}
}

Expand Down Expand Up @@ -123,7 +123,7 @@ pub struct GoalCollection {
pub struct CoreGoal {
pub id: String,
pub name: String,
pub owner_name: String,
pub owner_name: Option<String>,
pub material: String,
#[serde(default = "default_simpleplayer_set")]
pub contributors: HashSet<SimplePlayer>
Expand All @@ -134,7 +134,7 @@ pub struct CoreGoal {
pub struct DestroyableGoal {
pub id: String,
pub name: String,
pub owner_name: String,
pub owner_name: Option<String>,
pub material: String,
pub block_count: u32,
pub breaks_required: u32,
Expand All @@ -147,15 +147,15 @@ pub struct DestroyableGoal {
pub struct FlagGoal {
pub id: String,
pub name: String,
pub owner_name: String,
pub owner_name: Option<String>,
pub color: String
}

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct WoolGoal {
pub id: String,
pub owner_name: String,
pub owner_name: Option<String>,
pub color: String
}

Expand Down
2 changes: 1 addition & 1 deletion src/database/models/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl Player {

impl CollectionOwner<Player> for Player {
fn get_collection(database: &crate::database::Database) -> &Collection<Player> { &database.players }
fn get_collection_name() -> &'static str { "players" }
fn get_collection_name() -> &'static str { "player" }
}

pub type GamemodeStats = PlayerStats;
Expand Down
2 changes: 1 addition & 1 deletion src/database/models/punishment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ impl CollectionOwner<Punishment> for Punishment {
}

fn get_collection_name() -> &'static str {
"punishments"
"punishment"
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/database/models/rank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl CollectionOwner<Rank> for Rank {
}

fn get_collection_name() -> &'static str {
"ranks"
"rank"
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/database/models/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@ impl CollectionOwner<Session> for Session {
}

fn get_collection_name() -> &'static str {
"sessions"
"session"
}
}
2 changes: 1 addition & 1 deletion src/database/models/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ impl CollectionOwner<Tag> for Tag {
}

fn get_collection_name() -> &'static str {
"tags"
"tag"
}
}
34 changes: 17 additions & 17 deletions src/http/player/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ pub async fn prelogin(
prelogin_req: Json<PlayerPreLoginRequest>,
player_id: &str,
_auth_guard: AuthorizationToken
) -> Result<PlayerPreLoginResponder, ApiError> {
) -> Result<PlayerPreLoginResponder, ApiErrorResponder> {
let data = prelogin_req.0;

if data.player.id != player_id {
return Err(ApiError::Validation(String::from("Player ID in URL does not match body")));
return Err(ApiErrorResponder::validation_error());
};

let ip = hash_ip(&state, &data.ip);
Expand All @@ -43,7 +43,7 @@ pub async fn prelogin(
"action.kind": PunishmentKind::IpBan.to_string()
}, None).await {
Ok(cursor) => Database::consume_cursor_into_owning_vec(cursor).await,
Err(_) => return Err(ApiError::Validation(String::from("Could not load punishments for player")))
Err(_) => return Err(ApiErrorResponder::validation_error_with_message("Could not load punishments for player"))
};
let ip_ban = ip_punishments.first();

Expand Down Expand Up @@ -164,16 +164,16 @@ pub async fn logout(
state: &State<MarsAPIState>,
logout_req: Json<PlayerLogoutRequest>,
_auth_guard: AuthorizationToken
) -> Result<JsonResponder<EmptyResponse>, ApiError> {
) -> Result<JsonResponder<EmptyResponse>, ApiErrorResponder> {
let data = logout_req.0;
let mut player : Player = extract_player_from_url!(&data.player.name, state);
let mut player : Player = async_extract_player_from_url_v2!(&data.player.name, state);
let mut session = if let Some(session) = state.database.find_session_for_player(&player, data.session_id).await {
session
} else {
return Err(ApiError::session_not_found())
return Err(ApiErrorResponder::session_not_found())
};
if !session.is_active() {
return Err(ApiError::session_inactive())
return Err(ApiErrorResponder::session_inactive())
};

let time_millis : u64 = u64::try_from(SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis()).unwrap_or(u64::MAX);
Expand Down Expand Up @@ -203,9 +203,9 @@ pub async fn profile(
state: &State<MarsAPIState>,
player_id: &str,
include_leaderboard_positions: bool
) -> Result<PlayerProfileResponder, ApiError> {
) -> Result<PlayerProfileResponder, ApiErrorResponder> {
let player_id = player_id.to_lowercase();
let player : Player = extract_player_from_url!(&player_id, state);
let player : Player = async_extract_player_from_url_v2!(&player_id, state);
let profile = player.sanitized_copy();
if !include_leaderboard_positions {
return Ok(PlayerProfileResponder::RawProfile(profile))
Expand Down Expand Up @@ -265,11 +265,11 @@ pub async fn issue_punishment(
pun_issue_req: Json<PunishmentIssueRequest>,
_player_id: &str,
auth_guard: AuthorizationToken
) -> Result<JsonResponder<Punishment>, ApiError> {
) -> Result<JsonResponder<Punishment>, ApiErrorResponder> {
let data = pun_issue_req.0;
let punishment_id = Uuid::new_v4().to_string();
let time_millis : u64 = u64::try_from(SystemTime::now().duration_since(UNIX_EPOCH).unwrap().as_millis()).unwrap_or(u64::MAX);
let target_player : Player = extract_player_from_url!(&data.target_name, state);
let target_player : Player = async_extract_player_from_url_v2!(&data.target_name, state);
let punishment = Punishment {
id: punishment_id,
reason: data.reason,
Expand Down Expand Up @@ -302,8 +302,8 @@ pub async fn get_punishments(
state: &State<MarsAPIState>,
player_id: &str,
_auth_guard: AuthorizationToken
) -> Result<JsonResponder<Vec<Punishment>>, ApiError> {
let player : Player = extract_player_from_url!(&player_id, state);
) -> Result<JsonResponder<Vec<Punishment>>, ApiErrorResponder> {
let player : Player = async_extract_player_from_url_v2!(&player_id, state);
Ok(JsonResponder::created(state.database.get_player_punishments(&player).await))
}

Expand All @@ -328,8 +328,8 @@ pub async fn lookup_player(
player_id: &str,
include_alts: bool,
_auth_guard: AuthorizationToken
) -> Result<JsonResponder<PlayerLookupResponse>, ApiError> {
let player : Player = extract_player_from_url!(&player_id, state);
) -> Result<JsonResponder<PlayerLookupResponse>, ApiErrorResponder> {
let player : Player = async_extract_player_from_url_v2!(&player_id, state);
let alts : Vec<PlayerAltResponse> = {
let mut alts : Vec<PlayerAltResponse> = Vec::new();
if include_alts {
Expand All @@ -353,9 +353,9 @@ pub async fn add_player_note(
player_id: &str,
add_note_req: Json<PlayerAddNoteRequest>,
_auth_guard: AuthorizationToken
) -> Result<JsonResponder<Player>, ApiError> {
) -> Result<JsonResponder<Player>, ApiErrorResponder> {
let data = add_note_req.0;
let mut player : Player = extract_player_from_url!(&player_id, state);
let mut player : Player = async_extract_player_from_url_v2!(&player_id, state);
let id = player.notes.iter().max_by_key(|note| note.id).map(|note| note.id).unwrap_or(0) + 1;
let note = StaffNote { id, author: data.author, content: data.content, created_at: get_u64_time_millis() };
let note_clone = note.clone();
Expand Down
2 changes: 1 addition & 1 deletion src/socket/objective/objective_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct CoreLeakData {
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ControlPointCaptureData {
pub point_id: Vec<String>,
pub point_id: String,
pub player_ids: Vec<String>,
pub party_name: String,
}
Expand Down
2 changes: 1 addition & 1 deletion src/socket/player/player_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct PlayerDeathData {

impl PlayerDeathData {
pub fn is_murder(&self) -> bool {
self.attacker.is_some() && self.attacker.is_some() && self.attacker.as_ref().unwrap() == &self.victim
self.attacker.is_some() && self.attacker.as_ref().unwrap() != &self.victim
}

pub fn safe_weapon(&self) -> String {
Expand Down
3 changes: 2 additions & 1 deletion src/socket/socket_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,8 @@ impl SocketRouter {
}

fn parse_data<T: DeserializeOwned>(data: Value) -> T {
serde_json::from_value(data).expect("Socket passed malformed data..")
let debug_res = format!("Socket passed malformed data.. {data:?}");
serde_json::from_value(data).expect(&debug_res)
}

async fn get_match_id(&self) -> String {
Expand Down
16 changes: 16 additions & 0 deletions src/util/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ impl ApiErrorResponder {
)
}

pub fn session_not_found() -> Self {
ApiErrorResponder::create_api_error_responder(
Status::Conflict,
&ApiExceptionType::SessionMissing,
"Session not found"
)
}

pub fn session_inactive() -> Self {
ApiErrorResponder::create_api_error_responder(
Status::Conflict,
&ApiExceptionType::SessionInactive,
"Session inactive"
)
}

pub fn rank_confict() -> Self {
ApiErrorResponder::create_api_error_responder(
Status::Conflict,
Expand Down

0 comments on commit 3b14a13

Please sign in to comment.