write game abilities to db

This commit is contained in:
ntr 2018-10-20 17:06:44 +11:00
parent 69e1a3fcea
commit 69fe6e8f55
7 changed files with 83 additions and 30 deletions

View File

@ -5,8 +5,8 @@ const CrypList = require('./cryp.list');
const addState = connect( const addState = connect(
function receiveState(state) { function receiveState(state) {
const { ws, cryps, activeItem } = state; const { ws, cryps, activeItem } = state;
function sendCombatPve(crypId) { function sendGamePve(crypId) {
return ws.sendCombatPve(crypId); return ws.sendGamePve(crypId);
} }
function sendItemUse(targetId) { function sendItemUse(targetId) {
@ -16,7 +16,7 @@ const addState = connect(
return false; return false;
} }
return { cryps, sendCombatPve, activeItem, sendItemUse }; return { cryps, sendGamePve, activeItem, sendItemUse };
} }
); );

View File

@ -3,7 +3,7 @@ const preact = require('preact');
const { stringSort } = require('./../utils'); const { stringSort } = require('./../utils');
const nameSort = stringSort('name'); const nameSort = stringSort('name');
function CrypList({ cryps, activeItem, sendCombatPve, sendItemUse }) { function CrypList({ cryps, activeItem, sendGamePve, sendItemUse }) {
if (!cryps) return <div>not ready</div>; if (!cryps) return <div>not ready</div>;
const crypPanels = cryps.sort(nameSort).map(cryp => ( const crypPanels = cryps.sort(nameSort).map(cryp => (
@ -35,7 +35,7 @@ function CrypList({ cryps, activeItem, sendCombatPve, sendItemUse }) {
className="button is-dark" className="button is-dark"
type="submit" type="submit"
disabled={cryp.hp.value === 0} disabled={cryp.hp.value === 0}
onClick={() => sendCombatPve(cryp.id)}> onClick={() => sendGamePve(cryp.id)}>
Start PVE Start PVE
</button> </button>
</div> </div>

View File

@ -4,12 +4,12 @@ const Game = require('./game');
const addState = connect( const addState = connect(
function receiveState(state) { function receiveState(state) {
const { game, account } = state; const { ws, game, account } = state;
// function sendCombatPve(crypId) { function sendGameAbility(crypId, targetTeamId, ability) {
// return ws.sendCombatPve(crypId); return ws.sendGameAbility(game.id, crypId, targetTeamId, ability);
// } }
return { game }; return { game, account, sendGameAbility };
} }
); );

View File

@ -2,16 +2,34 @@ const preact = require('preact');
function GamePanel({ game, sendGameAbility, account }) { function GamePanel({ game, sendGameAbility, account }) {
if (!game) return <div>...</div>; if (!game) return <div>...</div>;
return (
<div> const otherTeams = game.teams.filter(t => t.id !== account.id);
<div>{JSON.stringify(game)}</div>
const playerTeam = game.teams.find(t => t.id === account.id);
const playerCryps = playerTeam.cryps.map(c => (
<div key={c.id} >
<div>{c.name}</div>
<button <button
className="button is-dark is-fullwidth" className="button is-dark is-fullwidth"
type="submit" type="submit"
onClick={() => sendCrypSpawn(name)}> onClick={() => sendGameAbility(c.id, otherTeams[0].id, 'Attack')}>
Spawn 👾 Attack
</button> </button>
</div> </div>
));
return (
<div>
<div>
<h1>them</h1>
<div>{JSON.stringify(otherTeams)}</div>
</div>
<div>
<h1>us</h1>
{playerCryps}
</div>
</div>
) )
// return ( // return (
// <div className=""> // <div className="">

View File

@ -24,7 +24,7 @@ function createSocket(store) {
// Connection opened // Connection opened
ws.addEventListener('open', function wsOpen(event) { ws.addEventListener('open', function wsOpen(event) {
//send({ method: 'account_login', params: { name: 'ntr', password: 'grepgrepgrep' } }); send({ method: 'account_login', params: { name: 'ntr', password: 'grepgrepgrep' } });
}); });
// Listen for messages // Listen for messages
@ -79,7 +79,7 @@ function createSocket(store) {
console.log('got a new cryp', cryp); console.log('got a new cryp', cryp);
} }
function combatPve(response) { function gamePve(response) {
const [structName, game] = response; const [structName, game] = response;
console.log('got a new game', game); console.log('got a new game', game);
} }
@ -109,10 +109,15 @@ function createSocket(store) {
send({ method: 'cryp_spawn', params: { name } }); send({ method: 'cryp_spawn', params: { name } });
} }
function sendCombatPve(id) { function sendGamePve(id) {
send({ method: 'combat_pve', params: { id } }); send({ method: 'game_pve', params: { id } });
} }
function sendGameAbility(gameId, crypId, targetTeamId, ability) {
send({ method: 'game_ability', params: { game_id: gameId, cryp_id: crypId, target_team_id: targetTeamId, ability } });
}
function sendItemUse(item, target) { function sendItemUse(item, target) {
console.log(item, target); console.log(item, target);
send({ method: 'item_use', params: { item, target } }); send({ method: 'item_use', params: { item, target } });
@ -127,7 +132,7 @@ function createSocket(store) {
// this object wraps the reply types to a function // this object wraps the reply types to a function
const handlers = { const handlers = {
cryp_spawn: crypSpawn, cryp_spawn: crypSpawn,
combat_pve: combatPve, game_pve: gamePve,
game_state: gameState, game_state: gameState,
account_login: accountLogin, account_login: accountLogin,
account_create: accountLogin, account_create: accountLogin,
@ -153,7 +158,8 @@ function createSocket(store) {
return { return {
sendAccountLogin, sendAccountLogin,
sendAccountRegister, sendAccountRegister,
sendCombatPve, sendGamePve,
sendGameAbility,
sendCrypSpawn, sendCrypSpawn,
sendItemUse, sendItemUse,
connect, connect,

View File

@ -1,12 +1,13 @@
exports.up = async knex => { exports.up = async knex => {
knex.schema.createTable('games', table => { await knex.schema.createTable('games', table => {
table.uuid('id').primary(); table.uuid('id').primary();
table.timestamps();
table.binary('data').notNullable();
table.index('id'); table.index('id');
table.timestamps();
table.binary('data').notNullable();
}); });
knex.schema.createTable('players', table => { await knex.schema.createTable('players', table => {
table.uuid('id').primary(); table.uuid('id').primary();
table.index('id'); table.index('id');

View File

@ -363,12 +363,12 @@ pub fn game_new(game: Game, tx: &mut Transaction) -> Result<Game, Error> {
let query = " let query = "
INSERT INTO games (id, data) INSERT INTO games (id, data)
VALUES ($1, $2, $3) VALUES ($1, $2)
RETURNING id, account; RETURNING id;
"; ";
let result = tx let result = tx
.query(query, &[&game_bytes, &game.id])?; .query(query, &[&game.id, &game_bytes])?;
let _returned = result.iter().next().expect("no row returned"); let _returned = result.iter().next().expect("no row returned");
@ -377,6 +377,34 @@ pub fn game_new(game: Game, tx: &mut Transaction) -> Result<Game, Error> {
return Ok(game); return Ok(game);
} }
pub fn players_write(game: Game, tx: &mut Transaction) -> Result<Game, Error> {
let game_bytes = to_vec(&game)?;
for team in &game.teams {
// pve
if !team.id.is_nil() {
for cryp in &team.cryps {
let id = Uuid::new_v4();
let query = "
INSERT INTO players (id, game, cryp, account)
VALUES ($1, $2, $3, $4)
RETURNING id, account;
";
let result = tx
.query(query, &[&id, &game.id, &cryp.id, &team.id])?;
let _returned = result.iter().next().expect("no row written");
println!("wrote player entry game:{:?} cryp:{:?} account:{:?}", game.id, cryp.id, team.id);
}
}
}
return Ok(game);
}
pub fn game_write(game: Game, tx: &mut Transaction) -> Result<Game, Error> { pub fn game_write(game: Game, tx: &mut Transaction) -> Result<Game, Error> {
let game_bytes = to_vec(&game)?; let game_bytes = to_vec(&game)?;
@ -390,7 +418,7 @@ pub fn game_write(game: Game, tx: &mut Transaction) -> Result<Game, Error> {
let result = tx let result = tx
.query(query, &[&game_bytes, &game.id])?; .query(query, &[&game_bytes, &game.id])?;
let _returned = result.iter().next().expect("no row returned"); let returned = result.iter().next().expect("no row returned");
println!("{:?} wrote game", game.id); println!("{:?} wrote game", game.id);
@ -461,7 +489,7 @@ pub fn game_pve(params: GamePveParams, tx: &mut Transaction, account: &Account)
game.start(); game.start();
Ok(game) return game_new(game, tx);
} }
#[cfg(test)] #[cfg(test)]