diff --git a/client/src/components/cryp.container.js b/client/src/components/cryp.container.js
index c858c658..bc899048 100644
--- a/client/src/components/cryp.container.js
+++ b/client/src/components/cryp.container.js
@@ -1,10 +1,16 @@
const { connect } = require('preact-redux');
-const InvStats = require('./invstats');
+const CrypPanel = require('./cryp.panel');
-// Add the incident from state as a property
const addState = connect(
- state => ({ cryp: state.cryp })
+ function receiveState(state) {
+ const { ws, cryp } = state;
+ function sendCombatPve() {
+ return ws.sendCombatPve(cryp.id);
+ }
+
+ return { cryp, sendCombatPve };
+ }
);
-module.exports = addState(InvStats);
+module.exports = addState(CrypPanel);
diff --git a/client/src/components/invstats.jsx b/client/src/components/cryp.panel.jsx
similarity index 58%
rename from client/src/components/invstats.jsx
rename to client/src/components/cryp.panel.jsx
index 81bc4c81..6de8e6af 100755
--- a/client/src/components/invstats.jsx
+++ b/client/src/components/cryp.panel.jsx
@@ -1,54 +1,60 @@
const preact = require('preact');
-function InvStats({ cryp }) {
+function CrypPanel({ cryp, sendCombatPve }) {
if (!cryp) return
not ready
;
return (
-
+
{cryp.name}
The big boy
-
+
-
+
Stat Area
{JSON.stringify(cryp.def)}
{JSON.stringify(cryp.dmg)}
{JSON.stringify(cryp.hp)}
{JSON.stringify(cryp.stam)}
-
+
+
);
@@ -57,4 +63,4 @@ function InvStats({ cryp }) {
// that gets put into the dom
}
-module.exports = InvStats;
+module.exports = CrypPanel;
diff --git a/client/src/components/login.container.jsx b/client/src/components/login.container.jsx
index e28077c6..d934a447 100644
--- a/client/src/components/login.container.jsx
+++ b/client/src/components/login.container.jsx
@@ -10,7 +10,7 @@ const addState = connect(
}
return { account: state.account, submitLogin };
- },
+ }
);
module.exports = addState(Login);
diff --git a/client/src/main.jsx b/client/src/main.jsx
index 89403831..15c676a4 100644
--- a/client/src/main.jsx
+++ b/client/src/main.jsx
@@ -51,4 +51,4 @@ const Main = () => (
preact.render(, document.body);
-// fizzyText('cryps.gg');
+fizzyText('cryps.gg');
diff --git a/client/src/socket.jsx b/client/src/socket.jsx
index bba555a8..2f14975b 100644
--- a/client/src/socket.jsx
+++ b/client/src/socket.jsx
@@ -23,11 +23,9 @@ function createSocket(store) {
// https://www.christian-schneider.net/CrossSiteWebSocketHijacking.html
let account = null;
- function send(msg) {
- msg.token = account && account.token;
- ws.send(cbor.encode(msg));
- }
-
+ // -------------
+ // Incoming
+ // -------------
function accountLogin(res) {
const [struct, login] = res;
@@ -43,14 +41,37 @@ function createSocket(store) {
return store.dispatch(actions.setCryp(cryp));
}
+ function combatPve(response) {
+ const [structName, battle] = response;
+ console.log('got a new battle', battle);
+ }
+
+
+ // -------------
+ // Outgoing
+ // -------------
+ function send(msg) {
+ msg.token = account && account.token;
+ ws.send(cbor.encode(msg));
+ }
+
function sendAccountLogin(name, password) {
send({ method: 'account_login', params: { name, password } });
}
+ function sendCombatPve(id) {
+ send({ method: 'combat_pve', params: { id } });
+ }
+
+
+ // -------------
+ // Handling
+ // -------------
// when the server sends a reply it will have one of these message types
// this object wraps the reply types to a function
const handlers = {
cryp_spawn: crypSpawn,
+ combat_pve: combatPve,
account_login: accountLogin,
account_create: accountLogin,
};
@@ -66,6 +87,7 @@ function createSocket(store) {
// check for error and split into response type and data
if (res.err) return errorToast(res.err);
const { method, params } = res;
+ if (!handlers[method]) return errorToast(`${method} handler missing`);
return handlers[method](params);
}
@@ -90,6 +112,7 @@ function createSocket(store) {
return {
sendAccountLogin,
+ sendCombatPve,
};
}
diff --git a/server/WORKLOG.md b/server/WORKLOG.md
index 9a0a8b25..c633109d 100755
--- a/server/WORKLOG.md
+++ b/server/WORKLOG.md
@@ -32,4 +32,7 @@ physical, magic, pure dmg?
items give skills
gem td style attr combinations
- stoney + spikey = jagged
\ No newline at end of file
+ stoney + spikey = jagged
+
+ slimey
+ ghostly
\ No newline at end of file
diff --git a/server/src/combat.rs b/server/src/combat.rs
index c03d2f60..03c0ccf6 100755
--- a/server/src/combat.rs
+++ b/server/src/combat.rs
@@ -1,5 +1,6 @@
+use ws::{Sender};
use rand::prelude::*;
-use serde_cbor::{from_slice};
+use serde_cbor::{from_slice, to_vec};
// Db Commons
use failure::Error;
@@ -78,7 +79,12 @@ pub fn keep_levelling(mut c: Cryp) -> Cryp {
fn generate_mob(plr: &Cryp) -> Cryp {
let mut rng = thread_rng();
- let mob_lvl: u8 = rng.gen_range(1, plr.lvl);
+
+ // rng panics on min == max
+ let mob_lvl: u8 = match plr.lvl {
+ 1 => 1,
+ _ => rng.gen_range(1, plr.lvl)
+ };
return Cryp::new()
.named(&"bamboo basher".to_string())
@@ -87,15 +93,18 @@ fn generate_mob(plr: &Cryp) -> Cryp {
}
-pub fn pve(params: CombatPveParams, db: Db, account: Account) -> Result {
+pub fn pve(params: CombatPveParams, db: Db, account: Account, send: F) -> Result
+where F: Fn(&Battle) -> Result<(), Error>
+ {
let query = "
SELECT *
FROM cryps
- WHERE id = $1;
+ WHERE id = $1
+ AND account = $2;
";
let result = db
- .query(query, &[¶ms.id])?;
+ .query(query, &[¶ms.id, &account.id])?;
let returned = match result.iter().next() {
Some(row) => row,
@@ -107,7 +116,20 @@ pub fn pve(params: CombatPveParams, db: Db, account: Account) -> Result(&cryp_bytes)?;
let mob = generate_mob(&plr);
- return Ok(Battle::new(&plr, &mob));
+ let mut battle = Battle::new(&plr, &mob);
+
+ loop {
+ battle.next();
+
+ match send(&battle) {
+ Err(e) => break Err(err_msg("could not reply")),
+ _ => (),
+ };
+
+ if battle.finished() {
+ break Ok(battle)
+ }
+ }
}
diff --git a/server/src/net.rs b/server/src/net.rs
index 57ebce38..7bb9b458 100755
--- a/server/src/net.rs
+++ b/server/src/net.rs
@@ -32,7 +32,7 @@ impl Handler for Server {
fn on_message(&mut self, msg: Message) -> Result<()> {
let db = self.db.get().expect("unable to get db connection");
- match self.rpc.receive(msg, db) {
+ match self.rpc.receive(msg, db, &self.out) {
Ok(reply) => {
let response = to_vec(&reply)
.expect("failed to serialize response");
diff --git a/server/src/rpc.rs b/server/src/rpc.rs
index d3831dc1..85e8acbe 100755
--- a/server/src/rpc.rs
+++ b/server/src/rpc.rs
@@ -1,5 +1,5 @@
-use ws::{Message};
-use serde_cbor::{from_slice};
+use ws::{Message, Sender};
+use serde_cbor::{from_slice, to_vec};
use uuid::Uuid;
use failure::Error;
use failure::err_msg;
@@ -10,10 +10,35 @@ use battle::{Battle};
use combat::{pve};
use account::{Account, create, login, from_token};
+// struct Replier where T: Fn(RpcResponse) {
+// send: T,
+// msg: RpcResponse,
+// }
+
+// impl Replier where T: Fn(RpcResponse) {
+// fn new(send: T) -> Replier {
+// Replier {
+// send,
+// msg: None,
+// }
+// }
+
+// fn msg(&mut self, arg: u32) -> u32 {
+// match self.msg {
+// Some(v) => v,
+// None => {
+// let v = (self.calculation)(arg);
+// self.msg = Some(v);
+// v
+// },
+// }
+// }
+// }
+
pub struct Rpc;
impl Rpc {
- pub fn receive(&self, msg: Message, db: Db) -> Result {
+ pub fn receive(&self, msg: Message, db: Db, out: &Sender) -> Result {
// consume the ws data into bytes
let data = msg.into_data();
@@ -46,12 +71,24 @@ impl Rpc {
}
},
"combat_pve" => {
+ let send = |b: &Battle| -> Result<(), Error> {
+ let reply = RpcResponse {
+ method: "combat_pve".to_string(),
+ params: RpcResult::Pve(b.clone())
+ };
+ let response = to_vec(&reply)?;
+ match out.send(response) {
+ Ok(()) => Ok(()),
+ Err(e) => Err(err_msg(e))
+ }
+ };
+
match from_slice::(&data) {
Ok(v) => {
match account {
Some(u) => Ok(RpcResponse {
method: v.method,
- params: RpcResult::Pve(pve(v.params, db, u)?)
+ params: RpcResult::Pve(pve(v.params, db, u, send)?)
}),
None => Err(err_msg("auth required")),
}