Skip to content

Commit

Permalink
libriichi/state: add context for update errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Equim-chan committed Jan 31, 2024
1 parent cf318a7 commit d60b81b
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions libriichi/src/state/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{must_tile, tu8, tuz};
use std::cmp::Ordering;
use std::mem;

use anyhow::{ensure, Result};
use anyhow::{ensure, Context, Result};
use tinyvec::array_vec;

#[derive(Clone, Copy)]
Expand All @@ -34,6 +34,15 @@ impl PlayerState {
&mut self,
event: &Event,
keep_cans_on_announce: bool,
) -> Result<ActionCandidate> {
self.update_inner(event, keep_cans_on_announce)
.with_context(|| format!("on event {event:?}"))
}

fn update_inner(
&mut self,
event: &Event,
keep_cans_on_announce: bool,
) -> Result<ActionCandidate> {
if !keep_cans_on_announce || !event.is_in_game_announce() {
self.last_cans = ActionCandidate {
Expand Down Expand Up @@ -146,7 +155,7 @@ impl PlayerState {
Event::Tsumo { actor, pai } => {
ensure!(
self.tiles_left > 0,
"rule violation: tsumo event but there is no tiles left",
"rule violation: tsumo but no more tiles in yama",
);
self.tiles_left -= 1;
if actor != self.player_id {
Expand Down Expand Up @@ -450,9 +459,9 @@ impl PlayerState {
for t in consumed {
self.witness_tile(t)?;
}
result
.into_iter()
.for_each(|t| self.update_doras_owned(actor_rel, t));
for t in result {
self.update_doras_owned(actor_rel, t);
}
self.can_w_riichi = false;
self.at_ippatsu = false;
return Ok(self.last_cans);
Expand Down

0 comments on commit d60b81b

Please sign in to comment.