rejoin games

This commit is contained in:
ntr 2018-11-22 18:39:08 +11:00
parent 557925801b
commit 3b33ac517c
7 changed files with 83 additions and 40 deletions

View File

@ -5,7 +5,7 @@
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"start": "parcel index.html --port 40080 --no-hmr --no-source-maps", "start": "parcel index.html --port 40080 --no-hmr --no-source-maps",
"build": "rm -rf dist && parcel build index.html", "build": "rm -rf dist && parcel build --no-source-maps index.html",
"lint": "eslint --fix src/", "lint": "eslint --fix src/",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },

View File

@ -7,5 +7,6 @@ const game = renderCryps();
const events = registerEvents(game.registry, game.events); const events = registerEvents(game.registry, game.events);
const ws = createSocket(events); const ws = createSocket(events);
events.setWs(ws); events.setWs(ws);
events.setGameList([]);
ws.connect(); ws.connect();

View File

@ -18,6 +18,7 @@ const gameListWidth = () => Math.floor(window.innerWidth * 0.2);
const gameListHeight = () => Math.floor(window.innerHeight / 10); const gameListHeight = () => Math.floor(window.innerHeight / 10);
const gameListX = () => crypListWidth(); const gameListX = () => crypListWidth();
const gameListY = i => menuHeight() + (gameListHeight() * i); const gameListY = i => menuHeight() + (gameListHeight() * i);
const gameListRowY = i => menuHeight() + (gameListHeight() * (i + 2));
const statsWidth = () => Math.floor(window.innerWidth - crypListWidth() - gameListWidth()); const statsWidth = () => Math.floor(window.innerWidth - crypListWidth() - gameListWidth());
const statsHeight = () => window.innerHeight - menuHeight(); const statsHeight = () => window.innerHeight - menuHeight();
@ -82,6 +83,7 @@ module.exports = {
y: gameListY, y: gameListY,
width: gameListWidth, width: gameListWidth,
height: gameListHeight, height: gameListHeight,
rowY: gameListRowY,
}, },
}, },

View File

@ -15,14 +15,23 @@ class CrypList extends Phaser.Scene {
return true; return true;
} }
updateData(parent, key, data) { updateData(parent, key) {
if (key === 'cryps') { const UPDATE_KEYS = ['gameList', 'cryps'];
this.renderList(data); if (UPDATE_KEYS.includes(key)) {
this.renderList();
this.renderGameList();
} }
if (key === 'game' && this.scene.isActive()) {
this.scene.switch('Combat');
}
return true; return true;
} }
renderList(cryps) { renderList() {
const cryps = this.registry.get('cryps');
// your cryps // your cryps
if (this.CrypRows) { if (this.CrypRows) {
this.CrypRows.cleanup(); this.CrypRows.cleanup();
@ -35,10 +44,19 @@ class CrypList extends Phaser.Scene {
const cryp = cryps.find(c => c.id === this.CrypPage.id); const cryp = cryps.find(c => c.id === this.CrypPage.id);
this.displaySkills(cryp); this.displaySkills(cryp);
} }
}
renderGameList() {
const ws = this.registry.get('ws');
const cryps = this.registry.get('cryps');
const gameList = this.registry.get('gameList');
if (this.gameList) { if (this.gameList) {
this.gameList.cleanup();
this.gameList.destroy(true); this.gameList.destroy(true);
} }
this.gameList = new GameList(this, cryps);
this.gameList = new GameList({ list: this, ws, cryps, gameList });
} }
displaySkills(cryp) { displaySkills(cryp) {

View File

@ -7,55 +7,65 @@ const {
} = require('./constants'); } = require('./constants');
class GameList extends Phaser.GameObjects.Group { class GameList extends Phaser.GameObjects.Group {
constructor(list, cryps) { constructor(args) {
super(list); super(list);
// this.keyboard = list.input.keyboard;
const ws = list.registry.get('ws'); const { list, ws, cryps, gameList } = args;
const games = [
'PVE',
'PVP',
];
const X = GAME_LIST.x(); const X = GAME_LIST.x();
const WIDTH = GAME_LIST.width(); const WIDTH = GAME_LIST.width();
const HEIGHT = GAME_LIST.height(); const HEIGHT = GAME_LIST.height();
const TEXT_MARGIN = 24;
const pvp = list.add const pvp = list.add
.rectangle(X, GAME_LIST.y(0), WIDTH, HEIGHT, 0x440000) .rectangle(X, GAME_LIST.y(0), WIDTH, HEIGHT, 0x440000)
.setInteractive() .setInteractive()
.setOrigin(0); .setOrigin(0);
this this.add(list.add.text(pvp.getCenter().x, pvp.getCenter().y, 'NEW', TEXT.HEADER));
.add(list.add.text(pvp.getCenter().x, pvp.getCenter().y, 'NEW', TEXT.HEADER));
const pve = list.add const pve = list.add
.rectangle(X, GAME_LIST.y(1), Math.floor(WIDTH / 2), HEIGHT, 0x004400) .rectangle(X, GAME_LIST.y(1), Math.floor(WIDTH / 2), HEIGHT, 0x004400)
.setInteractive() .setInteractive()
.setOrigin(0); .setOrigin(0);
this this.add(list.add.text(pve.getCenter().x, pve.getCenter().y, 'PVE', TEXT.HEADER));
.add(list.add.text(pve.getCenter().x, pve.getCenter().y, 'PVE', TEXT.HEADER));
const refresh = list.add const refresh = list.add
.rectangle(X + Math.floor(WIDTH / 2), GAME_LIST.y(1), Math.floor(WIDTH / 2), HEIGHT, 0x000044) .rectangle(X + Math.floor(WIDTH / 2), GAME_LIST.y(1), Math.floor(WIDTH / 2), HEIGHT, 0x000044)
.setInteractive() .setInteractive()
.setOrigin(0); .setOrigin(0);
this this.add(list.add.text(refresh.getCenter().x, refresh.getCenter().y, 'REFRESH', TEXT.HEADER));
.add(list.add.text(refresh.getCenter().x, refresh.getCenter().y, 'REFRESH', TEXT.HEADER));
const gameRow = (game, i) => {
const GAME_X = GAME_LIST.x();
const GAME_Y = GAME_LIST.rowY(i);
const gameBox = list.add
.rectangle(GAME_X, GAME_Y, WIDTH, HEIGHT, 0x111111)
.setInteractive()
.setOrigin(0);
this.add(list.add.text(GAME_X, GAME_Y, game.id, TEXT.NORMAL));
this.add(list.add.text(GAME_X, GAME_Y + TEXT_MARGIN, `${game.team_size}v${game.team_size}`, TEXT.NORMAL));
gameBox.on('pointerdown', () => {
const team = cryps.filter(c => c.active).map(c => c.id);
ws.sendGameJoin(game.id, team);
});
};
gameList.forEach(gameRow);
pvp.on('pointerdown', () => { pvp.on('pointerdown', () => {
const team = cryps.filter(c => c.active).map(c => c.id); const team = cryps.filter(c => c.active).map(c => c.id);
list.scene.switch('Combat');
return ws.sendGamePvp(team); return ws.sendGamePvp(team);
}); });
pve.on('pointerdown', () => { pve.on('pointerdown', () => {
const team = cryps.filter(c => c.active).map(c => c.id); const team = cryps.filter(c => c.active).map(c => c.id);
list.scene.switch('Combat');
return ws.sendGamePve(team); return ws.sendGamePve(team);
}); });

View File

@ -5,11 +5,9 @@ exports.up = async knex => {
table.timestamps(); table.timestamps();
table.binary('data').notNullable(); table.binary('data').notNullable();
table.boolean('open') table.boolean('joinable')
.defaultTo(true) .defaultTo(true)
.notNullable(); .notNullable();
// table.boolean('active').notNullable();
}); });
await knex.schema.createTable('players', table => { await knex.schema.createTable('players', table => {

View File

@ -98,7 +98,11 @@ impl Game {
self self
} }
fn add_team(&mut self, team: Team) -> Result<&mut Game, Error> { fn already_joined(&self, team_id: Uuid) -> bool {
self.teams.iter().any(|t| t.id == team_id)
}
fn team_add(&mut self, team: Team) -> Result<&mut Game, Error> {
if self.teams.len() == self.team_num { if self.teams.len() == self.team_num {
return Err(err_msg("maximum number of teams")); return Err(err_msg("maximum number of teams"));
} }
@ -154,6 +158,10 @@ impl Game {
self self
} }
fn joinable(&self) -> bool {
self.phase == Phase::Start
}
fn can_start(&self) -> bool { fn can_start(&self) -> bool {
self.teams.len() == self.team_num self.teams.len() == self.team_num
} }
@ -497,13 +505,13 @@ pub fn game_new(game: &Game, tx: &mut Transaction) -> Result<(), Error> {
let game_bytes = to_vec(&game)?; let game_bytes = to_vec(&game)?;
let query = " let query = "
INSERT INTO games (id, data) INSERT INTO games (id, data, joinable)
VALUES ($1, $2) VALUES ($1, $2, $3)
RETURNING id; RETURNING id;
"; ";
let result = tx let result = tx
.query(query, &[&game.id, &game_bytes])?; .query(query, &[&game.id, &game.joinable(), &game_bytes])?;
result.iter().next().ok_or(format_err!("no game written"))?; result.iter().next().ok_or(format_err!("no game written"))?;
@ -568,13 +576,13 @@ pub fn game_update(game: &Game, tx: &mut Transaction) -> Result<(), Error> {
let query = " let query = "
UPDATE games UPDATE games
SET data = $1 SET (data = $1, joinable = $2)
WHERE id = $2 WHERE id = $3
RETURNING id, data; RETURNING id, data;
"; ";
let result = tx let result = tx
.query(query, &[&game_bytes, &game.id])?; .query(query, &[&game_bytes, &game.joinable(), &game.id])?;
result.iter().next().ok_or(format_err!("game {:?} could not be written", game))?; result.iter().next().ok_or(format_err!("game {:?} could not be written", game))?;
@ -634,8 +642,8 @@ pub fn game_pve(params: GamePveParams, tx: &mut Transaction, account: &Account)
.set_cryps(mobs); .set_cryps(mobs);
game game
.add_team(plr_team)? .team_add(plr_team)?
.add_team(mob_team)?; .team_add(mob_team)?;
game.start(); game.start();
@ -665,7 +673,7 @@ pub fn game_pvp(params: GamePvpParams, tx: &mut Transaction, account: &Account)
let mut team = Team::new(account.id); let mut team = Team::new(account.id);
team.set_cryps(cryps); team.set_cryps(cryps);
game.add_team(team)?; game.team_add(team)?;
// persist // persist
game_new(&game, tx)?; game_new(&game, tx)?;
@ -676,6 +684,13 @@ pub fn game_pvp(params: GamePvpParams, tx: &mut Transaction, account: &Account)
pub fn game_join(params: GameJoinParams, tx: &mut Transaction, account: &Account) -> Result<Game, Error> { pub fn game_join(params: GameJoinParams, tx: &mut Transaction, account: &Account) -> Result<Game, Error> {
let mut game = game_get(tx, params.game_id)?; let mut game = game_get(tx, params.game_id)?;
// rejoining a game from the FE list
if game.already_joined(account.id) {
return Ok(game);
}
// ok actually adding a new team
let game_id = game.id; let game_id = game.id;
let cryps = params.cryp_ids let cryps = params.cryp_ids
@ -689,8 +704,7 @@ pub fn game_join(params: GameJoinParams, tx: &mut Transaction, account: &Account
let mut team = Team::new(account.id); let mut team = Team::new(account.id);
team.set_cryps(cryps); team.set_cryps(cryps);
game.team_add(team)?;
game.add_team(team)?;
if game.can_start() { if game.can_start() {
game.start(); game.start();
@ -706,7 +720,7 @@ pub fn game_joinable_list(tx: &mut Transaction, _account: &Account) -> Result<Ve
let query = " let query = "
SELECT games.data SELECT games.data
FROM games FROM games
WHERE open; WHERE joinable;
"; ";
let result = tx let result = tx
@ -771,8 +785,8 @@ mod tests {
.set_cryps(vec![y]); .set_cryps(vec![y]);
game game
.add_team(x_team).unwrap() .team_add(x_team).unwrap()
.add_team(y_team).unwrap(); .team_add(y_team).unwrap();
assert!(game.can_start()); assert!(game.can_start());