tennis scoring
This commit is contained in:
parent
d1950fd838
commit
edec9baf81
@ -101,13 +101,14 @@ pub struct Instance {
|
|||||||
|
|
||||||
open: bool,
|
open: bool,
|
||||||
max_players: usize,
|
max_players: usize,
|
||||||
max_rounds: usize,
|
|
||||||
password: Option<String>,
|
password: Option<String>,
|
||||||
time_control: TimeControl,
|
time_control: TimeControl,
|
||||||
|
|
||||||
phase: InstancePhase,
|
phase: InstancePhase,
|
||||||
phase_end: Option<DateTime<Utc>>,
|
phase_end: Option<DateTime<Utc>>,
|
||||||
phase_start: DateTime<Utc>,
|
phase_start: DateTime<Utc>,
|
||||||
|
|
||||||
|
winner: Option<Uuid>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Instance {
|
impl Instance {
|
||||||
@ -119,12 +120,12 @@ impl Instance {
|
|||||||
phase: InstancePhase::Lobby,
|
phase: InstancePhase::Lobby,
|
||||||
open: true,
|
open: true,
|
||||||
max_players: 2,
|
max_players: 2,
|
||||||
max_rounds: 5,
|
|
||||||
name: String::new(),
|
name: String::new(),
|
||||||
time_control: TimeControl::Standard,
|
time_control: TimeControl::Standard,
|
||||||
password: None,
|
password: None,
|
||||||
phase_start: Utc::now(),
|
phase_start: Utc::now(),
|
||||||
phase_end: Some(TimeControl::Standard.lobby_timeout()),
|
phase_end: Some(TimeControl::Standard.lobby_timeout()),
|
||||||
|
winner: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,12 +137,12 @@ impl Instance {
|
|||||||
phase: InstancePhase::InProgress,
|
phase: InstancePhase::InProgress,
|
||||||
open: false,
|
open: false,
|
||||||
max_players: 0,
|
max_players: 0,
|
||||||
max_rounds: 5,
|
|
||||||
time_control: TimeControl::Standard,
|
time_control: TimeControl::Standard,
|
||||||
name: "Global Matchmaking".to_string(),
|
name: "Global Matchmaking".to_string(),
|
||||||
password: None,
|
password: None,
|
||||||
phase_start: Utc::now(),
|
phase_start: Utc::now(),
|
||||||
phase_end: Some(TimeControl::Standard.lobby_timeout()),
|
phase_end: Some(TimeControl::Standard.lobby_timeout()),
|
||||||
|
winner: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -198,11 +199,6 @@ impl Instance {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_max_rounds(mut self, rounds: usize) -> Instance {
|
|
||||||
self.max_rounds = rounds;
|
|
||||||
self
|
|
||||||
}
|
|
||||||
|
|
||||||
fn add_bots(mut self) -> Instance {
|
fn add_bots(mut self) -> Instance {
|
||||||
self.open = false;
|
self.open = false;
|
||||||
iter::repeat_with(|| {
|
iter::repeat_with(|| {
|
||||||
@ -354,12 +350,34 @@ impl Instance {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
fn finish_condition(&self) -> bool {
|
fn finish_condition(&mut self) -> bool {
|
||||||
// boN standard
|
if self.rounds.len() < 4 {
|
||||||
// OR condition is for forfeitures
|
return false;
|
||||||
self.players.iter()
|
}
|
||||||
.any(|p| p.wins as usize >= self.max_rounds / 2 + 1)
|
|
||||||
|| self.rounds.len() == self.max_rounds
|
for player in self.players.iter() {
|
||||||
|
let score_to_beat = self.players.iter()
|
||||||
|
.filter(|p| p.id != player.id)
|
||||||
|
.max_by_key(|p| p.wins)
|
||||||
|
.unwrap().wins;
|
||||||
|
|
||||||
|
if player.wins >= 4 && player.wins >= score_to_beat + 2 {
|
||||||
|
self.winner = Some(player.id);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// both players afk
|
||||||
|
if self.players.iter().all(|p| p.wins == 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// boN
|
||||||
|
// self.players.iter()
|
||||||
|
// .any(|p| p.wins as usize >= self.max_rounds / 2 + 1)
|
||||||
|
// || self.rounds.len() == self.max_rounds
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn finish(&mut self) -> &mut Instance {
|
pub fn finish(&mut self) -> &mut Instance {
|
||||||
@ -698,7 +716,6 @@ pub fn instance_new(params: InstanceLobbyParams, tx: &mut Transaction, account:
|
|||||||
let mut instance = match params.pve {
|
let mut instance = match params.pve {
|
||||||
true => Instance::new()
|
true => Instance::new()
|
||||||
.set_time_control(TimeControl::Practice)
|
.set_time_control(TimeControl::Practice)
|
||||||
.set_max_rounds(10)
|
|
||||||
.set_name(params.name)?
|
.set_name(params.name)?
|
||||||
.add_bots(),
|
.add_bots(),
|
||||||
|
|
||||||
@ -785,7 +802,8 @@ mod tests {
|
|||||||
instance.player_ready(player_account).unwrap();
|
instance.player_ready(player_account).unwrap();
|
||||||
|
|
||||||
assert_eq!(instance.phase, InstancePhase::Finished);
|
assert_eq!(instance.phase, InstancePhase::Finished);
|
||||||
assert!(instance.players.iter().any(|p| p.wins as usize == instance.max_rounds / 2 + 1));
|
println!("{:#?}", instance);
|
||||||
|
// assert!(instance.players.iter().any(|p| p.wins as usize == instance.max_rounds / 2 + 1));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user