zone state init

This commit is contained in:
ntr 2018-12-28 16:17:35 +11:00
parent b36559ae9f
commit aa97f82d82
7 changed files with 178 additions and 9 deletions

View File

@ -44,8 +44,6 @@ function calculateTweenParams(sourceSpawn, targetSpawn, account) {
}
function animatePhase(scene, game, resolution, cb) {
console.log(resolution);
// return early for disabled skills
if (resolution.resolution.disable.disabled) return cb();
const group = scene.scene.get('CombatCryps').cryps;
@ -73,7 +71,7 @@ function animatePhase(scene, game, resolution, cb) {
if (moveSourceBattle) scene.tweens.add(moveSourceBattle);
scene.tweens.add(moveTargetBattle);
scene.time.delayedCall(MOVE_CREEP, () => {
return scene.time.delayedCall(MOVE_CREEP, () => {
const isAlly = resolution.target_team_id !== account.id;
// animate animation
const workingSkills = ['Heal', 'Block'];

View File

@ -124,11 +124,11 @@ class MenuGameList extends Phaser.Scene {
boss.on('pointerdown', () => {
const team = cryps.filter(c => c.active).map(c => c.id);
if (team.length === 0) return false;
this.scene.add('Missions', Missions);
this.scene.run('Missions');
this.cleanUp();
// return ws.sendGamePve(team, 'Boss');
// if (team.length === 0) return false;
// this.scene.add('Missions', Missions);
// this.scene.run('Missions');
// this.cleanUp();
return ws.sendZoneCreate();
});

View File

@ -100,6 +100,11 @@ function createSocket(events) {
send({ method: 'item_use', params: { item, target } });
}
function sendZoneCreate() {
send({ method: 'zone_create', params: {} });
}
// -------------
// Incoming
// -------------
@ -157,6 +162,7 @@ function createSocket(events) {
account_create: accountLogin,
account_cryps: accountCryps,
account_items: accountItems,
zone_create: res => console.log(res),
};
// decodes the cbor and
@ -232,6 +238,7 @@ function createSocket(events) {
sendCrypLearn,
sendCrypForget,
sendItemUse,
sendZoneCreate,
connect,
};
}

View File

@ -12,7 +12,7 @@ serde_cbor = "0.9"
tungstenite = "0.6"
bcrypt = "0.2"
petgraph = "0.4"
petgraph = { version = "0.4", features = ["serde-1"] }
dotenv = "0.9.0"
env_logger = "*"

View File

@ -26,6 +26,7 @@ mod passives;
mod rpc;
mod account;
mod item;
mod zone;
use dotenv::dotenv;
use net::{start};

View File

@ -20,6 +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};
use item::{Item, items_list, item_use};
use skill::{Skill};
use zone::{Zone, zone_create};
pub struct Rpc;
@ -69,6 +70,7 @@ impl Rpc {
"game_joinable_list" => Rpc::game_joinable_list(data, &mut tx, account.unwrap(), client),
"game_skill" => Rpc::game_skill(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),
"account_cryps" => Rpc::account_cryps(data, &mut tx, account.unwrap(), client),
"account_items" => Rpc::account_items(data, &mut tx, account.unwrap(), client),
"item_use" => Rpc::item_use(data, &mut tx, account.unwrap(), client),
@ -314,6 +316,18 @@ impl Rpc {
return Ok(cryps_list);
}
fn zone_create(data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut WebSocket<TcpStream>) -> Result<RpcResponse, Error> {
// let _msg = from_slice::<ZoneCreateMsg>(&data).or(Err(err_msg("invalid params")))?;
let response = RpcResponse {
method: "zone_state".to_string(),
params: RpcResult::ZoneState(zone_create(tx, &account)?)
};
return Ok(response);
}
}
@ -334,6 +348,7 @@ pub enum RpcResult {
GameJoinableList(Vec<Game>),
ItemList(Vec<Item>),
ItemUse(()),
ZoneState(Zone),
}
#[derive(Debug,Clone,Serialize,Deserialize)]
@ -504,6 +519,18 @@ pub struct ItemUseParams {
pub target: Uuid,
}
#[derive(Debug,Clone,Serialize,Deserialize)]
struct ZoneCreateMsg {
method: String,
params: (),
}
// #[derive(Debug,Clone,Serialize,Deserialize)]
// pub struct ZoneCreateParams {
// }
// #[cfg(test)]
// mod tests {
// use super::*;

136
server/src/zone.rs Normal file
View File

@ -0,0 +1,136 @@
use uuid::Uuid;
use petgraph::graph::{Graph, UnGraph, NodeIndex};
use petgraph::dot::{Dot, Config};
// Db Commons
use account::Account;
use serde_cbor::{from_slice, to_vec};
use postgres::transaction::Transaction;
use failure::Error;
use failure::err_msg;
use game::Game;
pub type Zone = UnGraph<Encounter, ()>;
#[derive(Debug,Clone,PartialEq,Eq,Hash,PartialOrd,Ord,Serialize,Deserialize)]
pub struct Encounter {
tag: String,
id: Uuid,
game_id: Option<Uuid>,
success: bool,
}
impl Encounter {
fn new(tag: &'static str) -> Encounter {
return Encounter {
tag: tag.to_string(),
id: Uuid::new_v4(),
success: false,
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_join(params: ZoneJoinParams, tx: &mut Transaction, account: &Account) -> Result<UnGraph<Encounter, ()>, Error> {
// let cryps = params.cryp_ids
// .iter()
// .map(|id| cryp_get(tx, *id, account.id))
// .collect::<Result<Vec<Cryp>, Error>>()?;
// if cryps.len() > 3 {
// return Err(err_msg("team size too large (3 max)"));
// }
// // create the game
// let mut game = Game::new();
// // let game_id = game.id;
// game
// .set_pve(true)
// .set_team_num(2)
// .set_team_size(cryps.len());
// // create the mob team
// let mob_team = generate_mob_team(params.mode, &cryps);
// // add the players
// let mut plr_team = Team::new(account.id);
// plr_team
// .set_cryps(cryps);
// game
// .team_add(plr_team)?
// .team_add(mob_team)?;
// game.start();
// // persist
// game_new(&game, tx)?;
// Ok(game)
// }
pub fn zone_create(tx: &mut Transaction, account: &Account) -> Result<UnGraph<Encounter, ()>, Error> {
let id = Uuid::new_v4();
let zone = create_zone_graph();
return Ok(zone);
let bytes = to_vec(&zone)?;
let query = "
INSERT INTO zones (id, data, account)
VALUES ($1, $2, $3)
RETURNING id;
";
let result = tx
.query(query, &[&id, &bytes, &account.id])?;
result.iter().next().ok_or(format_err!("no zone written"))?;
return Ok(zone);
}
#[cfg(test)]
mod tests {
use zone::*;
#[test]
fn create_zone_test() {
let _graph = create_zone_graph();
// good shit;
// let nodes = graph.node_indices().collect::<Vec<NodeIndex>>();
// println!("{:?}", nodes[0]);
// println!("{:?}", graph.node_weight(nodes[0]));
// println!("{:?}", Dot::with_config(&graph, &[Config::EdgeNoLabel]));
}
}