instance game

This commit is contained in:
ntr 2019-02-28 17:45:26 +11:00
parent 3c5f37f520
commit 7589ae84d4
6 changed files with 55 additions and 31 deletions

View File

@ -42,6 +42,7 @@ function registerEvents(registry, events, tutorial) {
} }
function setPlayer(player) { function setPlayer(player) {
registry.set('player', player);
if (!registry.get('inMenu')) { if (!registry.get('inMenu')) {
setMenu(); setMenu();
setCryps(player.cryps); setCryps(player.cryps);

View File

@ -16,7 +16,8 @@ class MenuNavigation extends Phaser.Scene {
} }
create() { create() {
// const ws = this.registry.get('ws'); const ws = this.registry.get('ws');
const player = this.registry.get('player');
this.cameras.main.setViewport(X, Y, WIDTH, HEIGHT); this.cameras.main.setViewport(X, Y, WIDTH, HEIGHT);
const ready = this.add const ready = this.add
@ -27,18 +28,19 @@ class MenuNavigation extends Phaser.Scene {
.text(ready.getCenter().x, ready.getCenter().y, 'Ready', TEXT.HEADER) .text(ready.getCenter().x, ready.getCenter().y, 'Ready', TEXT.HEADER)
.setOrigin(0.5, 0.5); .setOrigin(0.5, 0.5);
ready.on('pointerdown', () => { ready.on('pointerdown', () => {
// Put player in game console.log(player);
ws.sendInstanceReady(player.instance);
}); });
const leave = this.add // const leave = this.add
.rectangle(BTN_WIDTH * 3, BTN_HEIGHT, BTN_WIDTH, BTN_HEIGHT, 0x440000) // .rectangle(BTN_WIDTH * 3, BTN_HEIGHT, BTN_WIDTH, BTN_HEIGHT, 0x440000)
.setInteractive() // .setInteractive()
.setOrigin(0) // .setOrigin(0)
.on('pointerdown', () => this.registry.set('home', true)); // .on('pointerdown', () => this.registry.set('home', true));
this.add // this.add
.text(leave.getCenter().x, leave.getCenter().y, 'Leave', TEXT.HEADER) // .text(leave.getCenter().x, leave.getCenter().y, 'Leave', TEXT.HEADER)
.setOrigin(0.5, 0.5); // .setOrigin(0.5, 0.5);
} }
cleanUp() { cleanUp() {

View File

@ -141,9 +141,12 @@ function createSocket(events) {
} }
function sendInstanceJoin(cryps) { function sendInstanceJoin(cryps) {
send({ method: 'instance_join', params: { cryp_ids: cryps } }); send({ method: 'instance_join', params: { cryp_ids: cryps, pve: true } });
} }
function sendInstanceReady(instanceId) {
send({ method: 'instance_ready', params: { instance_id: instanceId } });
}
// ------------- // -------------
// Incoming // Incoming
@ -295,6 +298,7 @@ function createSocket(events) {
sendZoneJoin, sendZoneJoin,
sendZoneClose, sendZoneClose,
sendInstanceJoin, sendInstanceJoin,
sendInstanceReady,
sendPlayerCrypsSet, sendPlayerCrypsSet,
sendPlayerState, sendPlayerState,
sendVboxAccept, sendVboxAccept,

View File

@ -521,7 +521,7 @@ impl Game {
self self
} }
fn finished(&self) -> bool { pub fn finished(&self) -> bool {
self.teams.iter().any(|t| t.cryps.iter().all(|c| c.is_ko())) self.teams.iter().any(|t| t.cryps.iter().all(|c| c.is_ko()))
} }

View File

@ -11,7 +11,7 @@ use std::iter;
use rpc::{InstanceJoinParams, InstanceReadyParams}; use rpc::{InstanceJoinParams, InstanceReadyParams};
use account::Account; use account::Account;
use player::{Player, player_create, player_get}; use player::{Player, player_create, player_get, player_update};
use cryp::{Cryp, cryp_get}; use cryp::{Cryp, cryp_get};
use mob::{instance_mobs}; use mob::{instance_mobs};
use game::{Game, Team, game_get, game_instance_new, game_instance_join}; use game::{Game, Team, game_get, game_instance_new, game_instance_join};
@ -70,12 +70,18 @@ impl Instance {
self self
} }
fn update_player(mut self, player: Player) -> Result<Instance, Error> { fn player_ready(mut self, mut player: Player) -> Result<Instance, Error> {
if self.phase != InstancePhase::Vbox {
panic!("instance not in vbox phase");
}
let i = self.players let i = self.players
.iter() .iter()
.position(|p| p.id == player.id) .position(|p| p.id == player.id)
.ok_or(err_msg("player_id not found"))?; .ok_or(err_msg("player_id not found"))?;
player.set_ready(true);
self.players[i] = player; self.players[i] = player;
Ok(self) Ok(self)
@ -189,6 +195,8 @@ impl Instance {
game.start(); game.start();
assert!(game.finished());
round.game_id = Some(game.id); round.game_id = Some(game.id);
round.outcome = Some((game.winner().unwrap().id, Uuid::new_v4())); round.outcome = Some((game.winner().unwrap().id, Uuid::new_v4()));
} }
@ -229,15 +237,22 @@ impl Instance {
self self
} }
fn next_game_id(&self, player: &Player) -> Option<Uuid> { fn next_game_id(&mut self, player: &Player) -> Uuid {
let current_round = self.rounds.len() - 1; let current_round = self.rounds.len() - 1;
let next_round = self.rounds[current_round] let next_round = self.rounds[current_round]
.iter() .iter_mut()
.find(|g| g.player_ids.contains(&player.id)) .find(|g| g.player_ids.contains(&player.id))
.unwrap(); .unwrap();
return next_round.game_id; match next_round.game_id {
Some(id) => return id,
None => {
let id = Uuid::new_v4();
next_round.game_id = Some(id);
return id;
}
}
} }
} }
@ -343,6 +358,11 @@ pub fn instance_join(params: InstanceJoinParams, tx: &mut Transaction, account:
player_create(tx, &player, account)?; player_create(tx, &player, account)?;
instance.add_player(player.clone()); instance.add_player(player.clone());
if instance.can_start() {
instance.start();
}
instance_write(instance, tx)?; instance_write(instance, tx)?;
return Ok(player); return Ok(player);
@ -351,21 +371,18 @@ pub fn instance_join(params: InstanceJoinParams, tx: &mut Transaction, account:
pub fn instance_ready(params: InstanceReadyParams, tx: &mut Transaction, account: &Account) -> Result<Game, Error> { pub fn instance_ready(params: InstanceReadyParams, tx: &mut Transaction, account: &Account) -> Result<Game, Error> {
let player = player_get(tx, account.id, params.instance_id)?; let player = player_get(tx, account.id, params.instance_id)?;
let instance = instance_get(tx, params.instance_id)? let mut instance = instance_get(tx, params.instance_id)?
.update_player(player.clone())?; .player_ready(player.clone())?;
let game_id = instance.next_game_id(&player); let game_id = instance.next_game_id(&player);
let game = match game_id { let game = match game_get(tx, game_id) {
Some(id) => game_instance_join(tx, player, id)?, Ok(_g) => game_instance_join(tx, player.clone(), game_id)?,
None => { Err(_) => game_instance_new(tx, player.clone(), instance.pve)?,
let game = game_instance_new(tx, player, instance.pve)?;
// instance.
game
},
}; };
instance_write(instance, tx)?; instance_write(instance, tx)?;
player_update(tx, player)?;
return Ok(game); return Ok(game);
} }
@ -388,10 +405,8 @@ mod tests {
instance.start(); instance.start();
assert_eq!(instance.rounds[0].len(), 8); assert_eq!(instance.rounds[0].len(), 8);
{ let player = instance.players.clone().into_iter().find(|p| p.account == player_id).unwrap();
let player = instance.players.iter_mut().find(|p| p.account == player_id).unwrap(); let mut instance = instance.player_ready(player).unwrap();
player.set_ready(true);
}
assert!(instance.vbox_phase_finished()); assert!(instance.vbox_phase_finished());
instance.games_phase_start(); instance.games_phase_start();

View File

@ -78,6 +78,8 @@ impl Rpc {
// "zone_close" => Rpc::zone_close(data, &mut tx, account.unwrap(), client), // "zone_close" => Rpc::zone_close(data, &mut tx, account.unwrap(), client),
"instance_join" => Rpc::instance_join(data, &mut tx, account.unwrap(), client), "instance_join" => Rpc::instance_join(data, &mut tx, account.unwrap(), client),
"instance_ready" => Rpc::instance_ready(data, &mut tx, account.unwrap(), client),
"player_state" => Rpc::player_state(data, &mut tx, account.unwrap(), client), "player_state" => Rpc::player_state(data, &mut tx, account.unwrap(), client),
"player_cryps_set" => Rpc::player_cryps_set(data, &mut tx, account.unwrap(), client), "player_cryps_set" => Rpc::player_cryps_set(data, &mut tx, account.unwrap(), client),
"player_vbox_accept" => Rpc::player_vbox_accept(data, &mut tx, account.unwrap(), client), "player_vbox_accept" => Rpc::player_vbox_accept(data, &mut tx, account.unwrap(), client),
@ -277,7 +279,7 @@ impl Rpc {
let msg = from_slice::<InstanceReadyMsg>(&data).or(Err(err_msg("invalid params")))?; let msg = from_slice::<InstanceReadyMsg>(&data).or(Err(err_msg("invalid params")))?;
let response = RpcResponse { let response = RpcResponse {
method: "instance_state".to_string(), method: "game_state".to_string(),
params: RpcResult::GameState(instance_ready(msg.params, tx, &account)?) params: RpcResult::GameState(instance_ready(msg.params, tx, &account)?)
}; };