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) {
registry.set('player', player);
if (!registry.get('inMenu')) {
setMenu();
setCryps(player.cryps);

View File

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

View File

@ -141,9 +141,12 @@ function createSocket(events) {
}
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
@ -295,6 +298,7 @@ function createSocket(events) {
sendZoneJoin,
sendZoneClose,
sendInstanceJoin,
sendInstanceReady,
sendPlayerCrypsSet,
sendPlayerState,
sendVboxAccept,

View File

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

View File

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