more zone stuff

This commit is contained in:
ntr 2018-12-29 23:38:42 +11:00
parent eb543fffb6
commit 28bda483f1
6 changed files with 136 additions and 33 deletions

View File

@ -124,7 +124,7 @@ class MenuGameList extends Phaser.Scene {
zones.on('pointerdown', () => { zones.on('pointerdown', () => {
this.scene.add('Missions', Missions, true); this.scene.add('Missions', Missions, true);
return ws.sendZoneCreate(); return ws.sendAccountZone();
}); });

View File

@ -16,16 +16,20 @@ class Missions extends Phaser.Scene {
} }
create() { create() {
const zone = this.registry.get('zone');
if (!zone) return false;
this.scene.manager.add('MissionControls', MissionControls, true); this.scene.manager.add('MissionControls', MissionControls, true);
this.graphics = this.add.graphics(); this.graphics = this.add.graphics();
const nodeData = this.registry.get('zone').nodes; const nodeData = zone.graph.nodes;
this.edgeData = this.registry.get('zone').edges.map(x => x.map(y => nodeData[y])); this.edgeData = zone.graph.edges.map(x => x.map(y => nodeData[y]));
this.cameras.main.setViewport(COMBAT.width() * 0.2, COMBAT.y(), this.cameras.main.setViewport(COMBAT.width() * 0.2, COMBAT.y(),
COMBAT.width() * 0.8, COMBAT.height()); COMBAT.width() * 0.8, COMBAT.height());
this.addNodes(nodeData); this.addNodes(nodeData);
this.drawEdges(nodeData); this.drawEdges(nodeData);
this.addCameraControl(); this.addCameraControl();
this.addEvents(); this.addEvents();
return this;
} }
addNodes(nodeData) { addNodes(nodeData) {

View File

@ -48,6 +48,10 @@ function createSocket(events) {
send({ method: 'account_items', params: {} }); send({ method: 'account_items', params: {} });
} }
function sendAccountZone() {
send({ method: 'account_zone', params: {} });
}
function sendCrypSpawn(name) { function sendCrypSpawn(name) {
send({ method: 'cryp_spawn', params: { name } }); send({ method: 'cryp_spawn', params: { name } });
} }
@ -233,6 +237,7 @@ function createSocket(events) {
sendAccountDemo, sendAccountDemo,
sendAccountCryps, sendAccountCryps,
sendAccountItems, sendAccountItems,
sendAccountZone,
sendGameState, sendGameState,
sendGamePve, sendGamePve,
sendGamePvp, sendGamePvp,

View File

@ -11,6 +11,11 @@ strangle
## NOW ## NOW
* zones
* open w/ item?
* close
* check node joinable
* update on game finish
## SOON ## SOON
* aoe skills * aoe skills

View File

@ -20,7 +20,7 @@ use game::{Game, PveMode, game_state, game_pve, game_pvp, game_join, game_joinab
use account::{Account, account_create, account_login, account_from_token, account_cryps, account_zone}; use account::{Account, account_create, account_login, account_from_token, account_cryps, account_zone};
use item::{Item, items_list, item_use}; use item::{Item, items_list, item_use};
use skill::{Skill}; use skill::{Skill};
use zone::{Zone, zone_create, zone_join}; use zone::{Zone, zone_create, zone_join, zone_close};
pub struct Rpc; pub struct Rpc;
@ -72,6 +72,7 @@ impl Rpc {
"game_target" => Rpc::game_target(data, &mut tx, account.unwrap(), client), "game_target" => Rpc::game_target(data, &mut tx, account.unwrap(), client),
"zone_create" => Rpc::zone_create(data, &mut tx, account.unwrap(), client), "zone_create" => Rpc::zone_create(data, &mut tx, account.unwrap(), client),
"zone_join" => Rpc::zone_join(data, &mut tx, account.unwrap(), client), "zone_join" => Rpc::zone_join(data, &mut tx, account.unwrap(), client),
"zone_close" => Rpc::zone_close(data, &mut tx, account.unwrap(), client),
"account_cryps" => Rpc::account_cryps(data, &mut tx, account.unwrap(), client), "account_cryps" => Rpc::account_cryps(data, &mut tx, account.unwrap(), client),
"account_items" => Rpc::account_items(data, &mut tx, account.unwrap(), client), "account_items" => Rpc::account_items(data, &mut tx, account.unwrap(), client),
"account_zone" => Rpc::account_zone(data, &mut tx, account.unwrap(), client), "account_zone" => Rpc::account_zone(data, &mut tx, account.unwrap(), client),
@ -346,6 +347,18 @@ impl Rpc {
return Ok(response); return Ok(response);
} }
fn zone_close(data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> {
let msg = from_slice::<ZoneCloseMsg>(&data).or(Err(err_msg("invalid params")))?;
let response = RpcResponse {
method: "zone_close".to_string(),
params: RpcResult::ZoneClose(zone_close(msg.params, tx, &account)?)
};
return Ok(response);
}
} }
#[derive(Debug,Clone,Serialize,Deserialize)] #[derive(Debug,Clone,Serialize,Deserialize)]
@ -366,6 +379,7 @@ pub enum RpcResult {
ItemList(Vec<Item>), ItemList(Vec<Item>),
ItemUse(()), ItemUse(()),
ZoneState(Zone), ZoneState(Zone),
ZoneClose(()),
} }
#[derive(Debug,Clone,Serialize,Deserialize)] #[derive(Debug,Clone,Serialize,Deserialize)]
@ -555,6 +569,18 @@ pub struct ZoneJoinParams {
pub cryp_ids: Vec<Uuid>, pub cryp_ids: Vec<Uuid>,
} }
#[derive(Debug,Clone,Serialize,Deserialize)]
struct ZoneCloseMsg {
method: String,
params: ZoneCloseParams,
}
#[derive(Debug,Clone,Serialize,Deserialize)]
pub struct ZoneCloseParams {
pub zone_id: Uuid,
}
// #[cfg(test)] // #[cfg(test)]
// mod tests { // mod tests {
// use super::*; // use super::*;

View File

@ -10,11 +10,12 @@ use failure::Error;
use failure::err_msg; use failure::err_msg;
use game::{Game, PveMode, game_pve_new, game_write}; use game::{Game, PveMode, game_pve_new, game_write};
use rpc::{ZoneJoinParams}; use rpc::{ZoneJoinParams, ZoneCloseParams};
#[derive(Debug,Clone,Serialize,Deserialize)] #[derive(Debug,Clone,Serialize,Deserialize)]
pub struct Zone { pub struct Zone {
id: Uuid, id: Uuid,
active: bool,
graph: UnGraph<Encounter, ()>, graph: UnGraph<Encounter, ()>,
} }
@ -35,31 +36,13 @@ impl Encounter {
game_id: None, game_id: None,
}; };
} }
fn start() -> Encounter {
return Encounter {
tag: "START".to_string(),
success: true,
game_id: None,
};
} }
pub fn create_zone_graph() -> UnGraph<Encounter, ()> {
let mut gr = Graph::new_undirected();
let mut last = gr.add_node(Encounter::new("ZONE0"));
let mut next;
next = gr.add_node(Encounter::new("ZONE1"));
gr.add_edge(last, next, ());
last = next;
next = gr.add_node(Encounter::new("ZONE2"));
gr.add_edge(last, next, ());
last = next;
next = gr.add_node(Encounter::new("ZONE3"));
gr.add_edge(last, next, ());
last = next;
next = gr.add_node(Encounter::new("BOSS"));
gr.add_edge(last, next, ());
// last = next;
return gr;
} }
pub fn zone_delete(tx: &mut Transaction, id: Uuid) -> Result<(), Error> { pub fn zone_delete(tx: &mut Transaction, id: Uuid) -> Result<(), Error> {
@ -116,6 +99,7 @@ pub fn zone_create(tx: &mut Transaction, account: &Account) -> Result<Zone, Erro
let zone = Zone { let zone = Zone {
id, id,
graph, graph,
active: true,
}; };
let bytes = to_vec(&zone)?; let bytes = to_vec(&zone)?;
@ -139,13 +123,13 @@ pub fn zone_update(zone: &Zone, tx: &mut Transaction) -> Result<(), Error> {
let query = " let query = "
UPDATE zones UPDATE zones
SET data = $1 SET data = $1, active = $2
WHERE id = $2 WHERE id = $3
RETURNING id, data; RETURNING id, data;
"; ";
let result = tx let result = tx
.query(query, &[&bytes, &zone.id])?; .query(query, &[&bytes, &zone.active, &zone.id])?;
result.iter().next().ok_or(format_err!("zone {:?} could not be written", zone))?; result.iter().next().ok_or(format_err!("zone {:?} could not be written", zone))?;
@ -175,6 +159,78 @@ pub fn zone_join(params: ZoneJoinParams, tx: &mut Transaction, account: &Account
return Ok(game); return Ok(game);
} }
pub fn zone_close(params: ZoneCloseParams, tx: &mut Transaction, account: &Account) -> Result<(), Error> {
let mut zone = zone_get(tx, params.zone_id)?;
zone.active = false;
zone_update(&zone, tx)?;
return Ok(());
}
pub fn create_zone_graph() -> ZoneGraph {
let mut gr = Graph::new_undirected();
let mut last = gr.add_node(Encounter::start());
let mut next;
next = gr.add_node(Encounter::new("ZONE0"));
gr.add_edge(last, next, ());
last = next;
next = gr.add_node(Encounter::new("ZONE1"));
gr.add_edge(last, next, ());
last = next;
next = gr.add_node(Encounter::new("ZONE2"));
gr.add_edge(last, next, ());
last = next;
next = gr.add_node(Encounter::new("BOSS"));
gr.add_edge(last, next, ());
// last = next;
return gr;
}
pub fn node_joinable(graph: &mut ZoneGraph, target_index: NodeIndex) -> bool {
// early return for already attempted
{
let target_encounter = match graph.node_weight(target_index) {
Some(encounter) => encounter,
None => panic!("{:?} has no weight for {:?}", graph, target_index),
};
println!("{:?}", target_encounter);
if target_encounter.game_id.is_some() {
return false;
}
}
// now check the graph for connectedness
// get all the nodes that have been successfully completed
let mut filtered = graph.clone();
filtered.retain_nodes(|g, i| {
match g.node_weight(i) {
Some(encounter) => encounter.success,
None => panic!("no weight for {:?}", i),
}
});
println!("{:?}", filtered);
// if a node is a neighbour of that graph
// and hasn't been attempted
// it is joinable
for i in filtered.node_indices() {
match graph.neighbors(i).find(|n| *n == target_index) {
Some(_n) => return true,
None => continue,
};
}
return false;
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use zone::*; use zone::*;
@ -189,4 +245,11 @@ mod tests {
// println!("{:?}", Dot::with_config(&graph, &[Config::EdgeNoLabel])); // println!("{:?}", Dot::with_config(&graph, &[Config::EdgeNoLabel]));
} }
#[test]
fn zone_joinable_test() {
let mut graph = create_zone_graph();
assert!(node_joinable(&mut graph, NodeIndex::from(1)));
assert!(!node_joinable(&mut graph, NodeIndex::from(2)));
}
} }