[Game] Remove instances of using unwrap without handling the panics

This commit is contained in:
CosminPerRam 2023-09-06 13:37:48 +03:00
parent d97bd68ada
commit 7cfecbfff9
2 changed files with 9 additions and 9 deletions

View file

@ -8,7 +8,7 @@ Games:
Protocols:
- Quake 2: Fixed a bug where the version tag wouldn't always be present.
- The Ship: Fixed some instances of using `unwrap` without handling the panics.
- The Ship: Removed instances of using `unwrap` without handling the panics.
Crate:
- Rich errors, capturing backtrace is done on `RUST_BACKTRACE=1`.

View file

@ -25,14 +25,14 @@ pub struct TheShipPlayer {
}
impl TheShipPlayer {
pub fn new_from_valve_player(player: &ServerPlayer) -> Self {
Self {
pub fn new_from_valve_player(player: &ServerPlayer) -> GDResult<Self> {
Ok(Self {
name: player.name.clone(),
score: player.score,
duration: player.duration,
deaths: player.deaths.unwrap(), // TODO! Err here!
money: player.money.unwrap(), // TODO! Err here!
}
deaths: player.deaths.ok_or(PacketBad)?,
money: player.money.ok_or(PacketBad)?,
})
}
}
@ -105,10 +105,10 @@ impl Response {
players_online: response.info.players_online,
players: response
.players
.unwrap()
.ok_or(PacketBad)?
.iter()
.map(TheShipPlayer::new_from_valve_player)
.collect(),
.collect::<GDResult<Vec<TheShipPlayer>>>()?,
players_maximum: response.info.players_maximum,
players_bots: response.info.players_bots,
server_type: response.info.server_type,
@ -119,7 +119,7 @@ impl Response {
tv_port,
tv_name,
keywords,
rules: response.rules.unwrap(),
rules: response.rules.ok_or(PacketBad)?,
mode: the_unwrapped_ship.mode,
witnesses: the_unwrapped_ship.witnesses,
duration: the_unwrapped_ship.duration,