Merge branch 'master' into horizontal-vbox

This commit is contained in:
ntr 2019-05-14 14:02:08 +10:00
commit c4ec531798
6 changed files with 45 additions and 21 deletions

View File

@ -30,7 +30,7 @@ class InstanceCreateForm extends Component {
this.sendInstanceNew = sendInstanceNew.bind(this);
this.nameChange = this.nameChange.bind(this);
this.nameInput = this.nameInput.bind(this);
this.playersChange = this.playersChange.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
}
@ -39,7 +39,7 @@ class InstanceCreateForm extends Component {
this.setState({ players: Number(event.target.value) });
}
nameChange(event) {
nameInput(event) {
this.setState({ name: event.target.value });
}
@ -63,7 +63,7 @@ class InstanceCreateForm extends Component {
disabled={disabled}
value={this.state.name}
placeholder="name"
onChange={this.nameChange}
onInput={this.nameInput}
/>
<label htmlFor="playerSelect">players</label>
<select id="playerSelect"

View File

@ -7,12 +7,13 @@ class SpawnButton extends Component {
this.state = { value: null, enabled: false };
this.handleChange = this.handleChange.bind(this);
this.handleInput = this.handleInput.bind(this);
this.handleSubmit = this.handleSubmit.bind(this);
this.enable = this.enable.bind(this);
}
handleChange(event) {
handleInput(event) {
console.log(event.target.value);
this.setState({ value: event.target.value });
}
@ -41,7 +42,7 @@ class SpawnButton extends Component {
disabled={!this.state.enabled}
value={this.state.value}
placeholder="name"
onChange={this.handleChange}
onInput={this.handleInput}
/>
<button
className="login-btn"

View File

@ -28,14 +28,18 @@ function createSocket(events) {
// -------------
// Outgoing
// -------------
let ping = Date.now();
function send(msg) {
console.log('outgoing msg', msg);
ping = Date.now();
if (msg.method !== 'ping') console.log('outgoing msg', msg);
msg.token = account && account.token && account.token;
ws.send(cbor.encode(msg));
}
let ping;
function sendPing() {
ping = Date.now();
send({ method: 'ping', params: {} });
}
function sendAccountLogin(name, password) {
send({ method: 'account_login', params: { name, password } });
}
@ -230,6 +234,11 @@ function createSocket(events) {
events.setVboxInfo(info);
}
function pong() {
events.setPing(Date.now() - ping);
setTimeout(sendPing, 1000);
}
// -------------
// Setup
// -------------
@ -248,6 +257,7 @@ function createSocket(events) {
zone_close: res => console.log(res),
instance_state: instanceState,
vbox_info: vboxInfo,
pong,
};
function logout() {
@ -277,8 +287,7 @@ function createSocket(events) {
const res = cbor.decode(blob);
const { method, params } = res;
console.log(res);
events.setPing(Date.now() - ping);
if (method !== 'pong' ) console.log(res);
// check for error and split into response type and data
if (res.err) return errHandler(res.err);
@ -303,6 +312,8 @@ function createSocket(events) {
sendAccountCryps();
}
sendPing();
return true;
});

View File

@ -568,12 +568,12 @@ impl Game {
return self;
}
info!("upkeep beginning: {:} vs {:}", self.players[0].name, self.players[1].name);
if !self.phase_timed_out() {
return self;
}
info!("upkeep game: {:} vs {:}", self.players[0].name, self.players[1].name);
for player in self.players.iter_mut() {
if !player.ready {
player.set_ready(true);

View File

@ -1,7 +1,7 @@
use failure::{Error, err_msg};
use tungstenite::Message;
use tungstenite::protocol::WebSocket;
use tungstenite::server::accept;
use tungstenite::error::Error;
use tungstenite::Message::Binary;
use std::net::{TcpListener, TcpStream};
use serde_cbor::{to_vec};
@ -35,18 +35,19 @@ struct RpcErrorResponse {
err: String
}
fn receive(db: Db, rpc: &Rpc, msg: Message, client: &mut WebSocket<TcpStream>) -> Result<(), Error> {
fn receive(db: Db, rpc: &Rpc, msg: Message, client: &mut WebSocket<TcpStream>) -> Result<String, Error> {
match rpc.receive(msg, &db, client) {
Ok(reply) => {
let response = to_vec(&reply)
.expect("failed to serialize response");
client.write_message(Binary(response))
client.write_message(Binary(response))?;
return Ok(reply.method);
},
Err(e) => {
info!("{:?}", e);
let response = to_vec(&RpcErrorResponse { err: e.to_string() })
.expect("failed to serialize error response");
client.write_message(Binary(response))
client.write_message(Binary(response))?;
return Err(err_msg(e));
}
}
}
@ -120,8 +121,13 @@ pub fn start() {
let begin = Instant::now();
let db_connection = db.get().expect("unable to get db connection");
match receive(db_connection, &rpc, msg, &mut websocket) {
Ok(_r) => info!("response sent. total duration: {:?}", begin.elapsed()),
Err(e) => info!("{:?}", e),
Ok(method) => {
match method.as_ref() {
"pong" => (),
_ => info!("response sent. total duration: {:?}", begin.elapsed()),
}
},
Err(e) => warn!("{:?}", e),
}
},
// connection is closed

View File

@ -35,6 +35,10 @@ impl Rpc {
// cast the msg to this type to receive method name
match from_slice::<RpcMessage>(&data) {
Ok(v) => {
if v.method == "ping" {
return Ok(RpcResponse { method: "pong".to_string(), params: RpcResult::Pong(()) });
}
info!("message method: {:?}", v.method);
let mut tx = db.transaction()?;
@ -382,7 +386,7 @@ impl Rpc {
#[derive(Debug,Clone,Serialize,Deserialize)]
pub struct RpcResponse {
method: String,
pub method: String,
params: RpcResult,
}
@ -402,6 +406,8 @@ pub enum RpcResult {
InstanceList(Vec<Instance>),
InstanceState(Instance),
Pong(()),
}
#[derive(Debug,Clone,Serialize,Deserialize)]