time control

This commit is contained in:
ntr 2019-06-08 15:53:34 +10:00
parent a3d596a9b2
commit be427f2271
4 changed files with 72 additions and 46 deletions

View File

@ -57,7 +57,7 @@ function Instance(args) {
} }
function ScoreBoard() { function ScoreBoard() {
if (instance.phase !== 'Lobby') return null; if (instance.phase === 'InProgress') return null;
const players = instance.players.map((p, i) => { const players = instance.players.map((p, i) => {
const pText = playerText(p); const pText = playerText(p);

View File

@ -56,6 +56,8 @@ class InstanceCreateForm extends Component {
return ( return (
<div class={classes}> <div class={classes}>
<form> <form>
<fieldset>
<legend>New Instance</legend>
<label>instance name</label> <label>instance name</label>
<input <input
class="login-input" class="login-input"
@ -65,7 +67,7 @@ class InstanceCreateForm extends Component {
placeholder="game name" placeholder="game name"
onInput={this.nameInput} onInput={this.nameInput}
/> />
<label htmlFor="pveSelect">vs CPU</label> <label htmlFor="pveSelect">Practice Mode - Bo10, vs CPU, 2x turn time</label>
<input id="pveSelect" <input id="pveSelect"
type="checkbox" type="checkbox"
disabled={disabled} disabled={disabled}
@ -73,6 +75,7 @@ class InstanceCreateForm extends Component {
onChange={this.pveChange} onChange={this.pveChange}
> >
</input> </input>
</fieldset>
</form> </form>
<button <button
onClick={this.handleSubmit} onClick={this.handleSubmit}

View File

@ -16,7 +16,7 @@ use rpc::{GameStateParams, GameSkillParams};
use construct::{Construct}; use construct::{Construct};
use skill::{Skill, Effect, Cast, Resolution, Event, resolution_steps}; use skill::{Skill, Effect, Cast, Resolution, Event, resolution_steps};
use player::{Player}; use player::{Player};
use instance::{instance_game_finished}; use instance::{TimeControl, instance_game_finished};
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)] #[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
pub enum Phase { pub enum Phase {
@ -36,6 +36,7 @@ pub struct Game {
pub stack: Vec<Cast>, pub stack: Vec<Cast>,
pub resolved: Vec<Resolution>, pub resolved: Vec<Resolution>,
pub instance: Option<Uuid>, pub instance: Option<Uuid>,
time_control: TimeControl,
phase_end: DateTime<Utc>, phase_end: DateTime<Utc>,
phase_start: DateTime<Utc>, phase_start: DateTime<Utc>,
} }
@ -51,11 +52,17 @@ impl Game {
stack: vec![], stack: vec![],
resolved: vec![], resolved: vec![],
instance: None, instance: None,
time_control: TimeControl::Standard,
phase_end: Utc::now(), phase_end: Utc::now(),
phase_start: Utc::now(), phase_start: Utc::now(),
}; };
} }
pub fn set_time_control(&mut self, tc: TimeControl) -> &mut Game {
self.time_control = tc;
self
}
pub fn set_player_num(&mut self, size: usize) -> &mut Game { pub fn set_player_num(&mut self, size: usize) -> &mut Game {
self.player_num = size; self.player_num = size;
self self
@ -150,10 +157,10 @@ impl Game {
} }
fn skill_phase_start(mut self, num_resolutions: usize) -> Game { fn skill_phase_start(mut self, num_resolutions: usize) -> Game {
let resolution_animation_ms = num_resolutions * 2500; let resolution_animation_ms = num_resolutions as i64 * 2500;
let phase_add_time_ms = 60000 + resolution_animation_ms; let phase_add_time_ms = self.time_control.game_time_seconds() * 1000 + resolution_animation_ms;
self.phase_start = Utc::now() self.phase_start = Utc::now()
.checked_add_signed(Duration::milliseconds(resolution_animation_ms as i64)) .checked_add_signed(Duration::milliseconds(resolution_animation_ms))
.expect("could not set phase start"); .expect("could not set phase start");
self.phase_end = Utc::now() self.phase_end = Utc::now()
.checked_add_signed(Duration::milliseconds(phase_add_time_ms as i64)) .checked_add_signed(Duration::milliseconds(phase_add_time_ms as i64))

View File

@ -30,12 +30,6 @@ enum InstancePhase {
Finished, Finished,
} }
#[derive(Debug,Clone,Copy,PartialEq,Serialize,Deserialize)]
enum Format {
Standard,
RoundRobin,
}
#[derive(Debug,Clone,Serialize,Deserialize)] #[derive(Debug,Clone,Serialize,Deserialize)]
struct Round { struct Round {
game_id: Option<Uuid>, game_id: Option<Uuid>,
@ -48,6 +42,28 @@ impl Round {
} }
} }
#[derive(Debug,Clone,Copy,Serialize,Deserialize)]
pub enum TimeControl {
Standard,
Practice,
}
impl TimeControl {
fn vbox_time_seconds(&self) -> i64 {
match self {
TimeControl::Standard => 120,
TimeControl::Practice => 240,
}
}
pub fn game_time_seconds(&self) -> i64 {
match self {
TimeControl::Standard => 60,
TimeControl::Practice => 120,
}
}
}
#[derive(Debug,Clone,Serialize,Deserialize)] #[derive(Debug,Clone,Serialize,Deserialize)]
pub struct Instance { pub struct Instance {
id: Uuid, id: Uuid,
@ -60,12 +76,11 @@ pub struct Instance {
max_players: usize, max_players: usize,
max_rounds: usize, max_rounds: usize,
password: Option<String>, password: Option<String>,
time_control: TimeControl,
phase: InstancePhase, phase: InstancePhase,
phase_end: DateTime<Utc>, phase_end: DateTime<Utc>,
phase_start: DateTime<Utc>, phase_start: DateTime<Utc>,
format: Format,
} }
impl Instance { impl Instance {
@ -79,12 +94,12 @@ impl Instance {
max_players: 2, max_players: 2,
max_rounds: 5, max_rounds: 5,
name: String::new(), name: String::new(),
time_control: TimeControl::Standard,
password: None, password: None,
phase_start: Utc::now(), phase_start: Utc::now(),
phase_end: Utc::now() phase_end: Utc::now()
.checked_add_signed(Duration::minutes(5)) .checked_add_signed(Duration::minutes(5))
.expect("could not set phase end"), .expect("could not set phase end"),
format: Format::Standard,
} }
} }
@ -97,12 +112,11 @@ impl Instance {
open: false, open: false,
max_players: 0, max_players: 0,
max_rounds: 5, max_rounds: 5,
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: Utc::now(), phase_end: Utc::now(),
format: Format::Standard,
} }
} }
@ -151,8 +165,13 @@ impl Instance {
Ok(self) Ok(self)
} }
fn set_format(mut self, format: Format) -> Instance { fn set_time_control(mut self, tc: TimeControl) -> Instance {
self.format = format; self.time_control = tc;
self
}
fn set_max_rounds(mut self, rounds: usize) -> Instance {
self.max_rounds = rounds;
self self
} }
@ -265,6 +284,7 @@ impl Instance {
game game
.set_player_num(2) .set_player_num(2)
.set_player_constructs(3) .set_player_constructs(3)
.set_time_control(self.time_control)
.set_instance(self.id); .set_instance(self.id);
for player in self.players.clone().into_iter() { for player in self.players.clone().into_iter() {
@ -293,7 +313,7 @@ impl Instance {
self.phase = InstancePhase::InProgress; self.phase = InstancePhase::InProgress;
self.phase_start = Utc::now(); self.phase_start = Utc::now();
self.phase_end = Utc::now() self.phase_end = Utc::now()
.checked_add_signed(Duration::seconds(120)) .checked_add_signed(Duration::seconds(self.time_control.vbox_time_seconds()))
.expect("could not set phase end"); .expect("could not set phase end");
@ -309,16 +329,11 @@ impl Instance {
} }
fn finish_condition(&self) -> bool { fn finish_condition(&self) -> bool {
match self.format { // boN standard
// bo5 standard
// OR condition is for forfeitures // OR condition is for forfeitures
Format::Standard => self.players.iter()
self.players.iter().any(|p| p.wins as usize >= self.max_rounds / 2 + 1) .any(|p| p.wins as usize >= self.max_rounds / 2 + 1)
|| self.rounds.len() == self.max_rounds, || self.rounds.len() == self.max_rounds
// everybody plays each other once
Format::RoundRobin => self.rounds.len() == self.players.len() - 1,
}
} }
fn finish(&mut self) -> &mut Instance { fn finish(&mut self) -> &mut Instance {
@ -625,6 +640,7 @@ pub fn instances_need_upkeep(tx: &mut Transaction) -> Result<Vec<Instance>, Erro
pub fn instance_new(params: InstanceLobbyParams, tx: &mut Transaction, account: &Account) -> Result<Instance, Error> { pub fn instance_new(params: InstanceLobbyParams, tx: &mut Transaction, account: &Account) -> Result<Instance, Error> {
let mut instance = match params.pve { let mut instance = match params.pve {
true => Instance::new() true => Instance::new()
.set_time_control(TimeControl::Practice)
.set_name(params.name)? .set_name(params.name)?
.add_bots(), .add_bots(),