Skip to content

Commit

Permalink
Fixes correspondence takebacks
Browse files Browse the repository at this point in the history
  • Loading branch information
klautcomputing committed Jul 25, 2024
1 parent 1ada35b commit 65ce39a
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ codegen-backend = "cranelift"
[profile.db-dev]
codegen-backend = "cranelift"
[build]
rustflags = ["-Z", "threads=1","--cfg=web_sys_unstable_apis"]
rustflags = ["-Z", "threads=8","--cfg=web_sys_unstable_apis"]
2 changes: 1 addition & 1 deletion apis/src/components/atoms/simple_switch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn SimpleSwitch(
<SwitchRoot
checked
on_checked_change
attr:disabled = disabled
attr:disabled=disabled
attr:class="disabled:opacity-25 disabled:cursor-not-allowed disabled:hover:bg-transparent items-center transition w-8 h-4 bg-white rounded-full relative focus:shadow-black data-[state=checked]:bg-orange-twilight outline-none cursor-default"
>
<SwitchThumb attr:class="hover:bg-pillbug-teal bg-button-dawn dark:bg-button-twilight block w-4 h-4 rounded-full shadow-md transition-transform duration-100 translate-x-0.5 will-change-transform data-[state=checked]:translate-x-[19px]"/>
Expand Down
12 changes: 10 additions & 2 deletions apis/src/components/organisms/time_select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
use leptix_primitives::components::radio_group::{RadioGroupItem, RadioGroupRoot};
use leptos::*;
use leptos_icons::*;
use shared_types::{GameSpeed, TimeMode};
use shared_types::{CorrespondenceMode, GameSpeed, TimeMode};

#[component]
pub fn TimeSelect(
Expand Down Expand Up @@ -98,8 +98,16 @@ pub fn TimeSelect(
required=true
attr:class="flex flex-row gap-2 p-2"
default_value="dpm"
on_value_change=move |v| corresp_selected.set(v)
on_value_change=move |v| {
if v == "dpm" {
time_signals.corr_mode.set(CorrespondenceMode::DaysPerMove)
} else if v == "tte" {
time_signals.corr_mode.set(CorrespondenceMode::TotalTimeEach)
}
corresp_selected.set(v);
}
>

<RadioGroupItem value="dpm" attr:class=radio_style>
"Per move"
</RadioGroupItem>
Expand Down
7 changes: 6 additions & 1 deletion apis/src/pages/challenge_create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,12 @@ pub fn ChallengeCreate(
/>
</div>
<div class="flex gap-1 p-1">
Casual <SimpleSwitch checked=params.rated optional_action=add_expansions disabled=untimed_no_rated/> Rated
Casual
<SimpleSwitch
checked=params.rated
optional_action=add_expansions
disabled=untimed_no_rated
/> Rated
</div>
<div class="flex gap-1 p-1">
Base <SimpleSwitch checked=params.with_expansions optional_action=make_unrated/> MLP
Expand Down
137 changes: 103 additions & 34 deletions db/src/models/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,9 @@ impl Game {
fn get_correspondence_time_info(&self, state: &State) -> Result<TimeInfo, DbError> {
let mut time_info = TimeInfo::new(state.game_status.clone());
if self.turn < 2 && self.game_start == GameStart::Moves.to_string() {
if self.turn == 0 {
time_info.new_game_status = GameStatus::NotStarted;
};
time_info.white_time_left = self.white_time_left;
time_info.black_time_left = self.black_time_left;
} else {
Expand Down Expand Up @@ -736,61 +739,127 @@ impl Game {
.await?)
}

fn get_takeback_time_correspondence(&self, popped: i32) -> (Option<i64>, Option<i64>) {
// For TotalTimeEach increment: None, base: Some
if self.time_increment.is_none() {
return self.get_takeback_time_realtime(popped);
}

// For DaysPerMove increment: Some and base: None
let mut black_time = self.black_time_left;
let mut white_time = self.white_time_left;

if self.turn % 2 == 0 {
black_time = self
.time_increment
.map(|t| t as i64 * NANOS_IN_SECOND as i64);
} else {
white_time = self
.time_increment
.map(|t| t as i64 * NANOS_IN_SECOND as i64);
}

if popped == 2 {
if self.turn % 2 == 0 {
white_time = self
.time_increment
.map(|t| t as i64 * NANOS_IN_SECOND as i64);
} else {
black_time = self
.time_increment
.map(|t| t as i64 * NANOS_IN_SECOND as i64);
}
}

(white_time, black_time)
}

fn get_takeback_time_realtime(&self, popped: i32) -> (Option<i64>, Option<i64>) {
let past_turn = self.turn - popped;
let mut times = self.move_times.clone();
let mut black_time = self.black_time_left;
let mut white_time = self.white_time_left;

if self.turn % 2 == 0 {
black_time = times.pop().unwrap_or(Some(0));
} else {
white_time = times.pop().unwrap_or(Some(0));
}

if popped == 2 {
if self.turn % 2 == 0 {
white_time = times.pop().unwrap_or(Some(0));
} else {
black_time = times.pop().unwrap_or(Some(0));
}
}

if past_turn > 1 {
if self.turn % 2 == 0 {
black_time = Some(
black_time.unwrap_or(0)
- self.time_increment.unwrap_or(0) as i64 * NANOS_IN_SECOND as i64,
);
} else {
white_time = Some(
white_time.unwrap_or(0)
- self.time_increment.unwrap_or(0) as i64 * NANOS_IN_SECOND as i64,
);
}
if popped == 2 {
if self.turn % 2 == 0 {
white_time = Some(
white_time.unwrap_or(0)
- self.time_increment.unwrap_or(0) as i64 * NANOS_IN_SECOND as i64,
);
} else {
black_time = Some(
black_time.unwrap_or(0)
- self.time_increment.unwrap_or(0) as i64 * NANOS_IN_SECOND as i64,
);
}
}
}

(white_time, black_time)
}

fn get_takeback_time(&self, popped: i32) -> Result<(Option<i64>, Option<i64>), DbError> {
match TimeMode::from_str(&self.time_mode)? {
TimeMode::Untimed => Ok((None, None)),
TimeMode::Correspondence => Ok(self.get_takeback_time_correspondence(popped)),
TimeMode::RealTime => Ok(self.get_takeback_time_realtime(popped)),
}
}

pub async fn accept_takeback(
&self,
game_control: &GameControl,
conn: &mut DbConn<'_>,
) -> Result<Game, DbError> {
let game_control_string = format!("{}. {game_control};", self.turn);
let mut moves = self.history.split_terminator(';').collect::<Vec<_>>();
let mut white_time = self.white_time_left;
let mut black_time = self.black_time_left;
let mut popped = 0_i32;
let mut new_move_times = self.move_times.clone();
let mut popped = 0;

if let (Some(Some(time)), Some(a_move)) = (new_move_times.pop(), moves.pop()) {
if let Some(a_move) = moves.pop() {
new_move_times.pop();
popped += 1;
if new_move_times.len() % 2 == 0 {
if self.turn - popped > 2 {
white_time = Some(
time - self.time_increment.unwrap_or(0) as i64 * NANOS_IN_SECOND as i64,
);
} else {
white_time = Some(time);
}
} else if self.turn - popped > 2 {
black_time =
Some(time - self.time_increment.unwrap_or(0) as i64 * NANOS_IN_SECOND as i64);
} else {
white_time = Some(time);
}
if a_move.trim() == "pass" {
new_move_times.pop();
moves.pop();
new_move_times.pop();
popped += 1;
if new_move_times.len() % 2 == 0 {
if self.turn - popped > 2 {
white_time = Some(
time - self.time_increment.unwrap_or(0) as i64 * NANOS_IN_SECOND as i64,
);
} else {
white_time = Some(time);
}
} else if self.turn - popped > 2 {
black_time = Some(
time - self.time_increment.unwrap_or(0) as i64 * NANOS_IN_SECOND as i64,
);
} else {
white_time = Some(time);
}
}
}

if popped == 0 {
return Err(DbError::InvalidInput {
info: String::from("Takeback failed, no moves to pop"),
error: String::from("Popped = 0"),
});
}

let (white_time, black_time) = self.get_takeback_time(popped)?;
let mut new_history = moves.join(";");
if !new_history.is_empty() {
new_history.push(';');
Expand Down

0 comments on commit 65ce39a

Please sign in to comment.