simpify rpc responses
This commit is contained in:
parent
859ff36771
commit
5c6e587044
@ -143,9 +143,7 @@ function createSocket(events) {
|
|||||||
// -------------
|
// -------------
|
||||||
// Incoming
|
// Incoming
|
||||||
// -------------
|
// -------------
|
||||||
function accountLogin(res) {
|
function onAccount(login) {
|
||||||
const [struct, login] = res;
|
|
||||||
|
|
||||||
account = login;
|
account = login;
|
||||||
localStorage.setItem('account', JSON.stringify(login));
|
localStorage.setItem('account', JSON.stringify(login));
|
||||||
events.setAccount(login);
|
events.setAccount(login);
|
||||||
@ -153,19 +151,16 @@ function createSocket(events) {
|
|||||||
sendAccountInstances();
|
sendAccountInstances();
|
||||||
}
|
}
|
||||||
|
|
||||||
function accountInstanceList(res) {
|
function onAccountInstances(list) {
|
||||||
const [struct, playerList] = res;
|
events.setAccountInstances(list);
|
||||||
events.setAccountInstances(playerList);
|
|
||||||
setTimeout(sendAccountInstances, 5000);
|
setTimeout(sendAccountInstances, 5000);
|
||||||
}
|
}
|
||||||
|
|
||||||
function accountConstructs(response) {
|
function onAccountConstructs(constructs) {
|
||||||
const [structName, constructs] = response;
|
|
||||||
events.setConstructList(constructs);
|
events.setConstructList(constructs);
|
||||||
}
|
}
|
||||||
|
|
||||||
function gameState(response) {
|
function onGameState(game) {
|
||||||
const [structName, game] = response;
|
|
||||||
events.setGame(game);
|
events.setGame(game);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -180,15 +175,6 @@ function createSocket(events) {
|
|||||||
clearTimeout(gameStateTimeout);
|
clearTimeout(gameStateTimeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
function constructSpawn(response) {
|
|
||||||
const [structName, construct] = response;
|
|
||||||
}
|
|
||||||
|
|
||||||
function zoneState(response) {
|
|
||||||
const [structName, zone] = response;
|
|
||||||
events.setZone(zone);
|
|
||||||
}
|
|
||||||
|
|
||||||
let instanceStateTimeout;
|
let instanceStateTimeout;
|
||||||
function startInstanceStateTimeout(id) {
|
function startInstanceStateTimeout(id) {
|
||||||
clearTimeout(instanceStateTimeout);
|
clearTimeout(instanceStateTimeout);
|
||||||
@ -196,13 +182,12 @@ function createSocket(events) {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function instanceState(response) {
|
function onInstanceState(instance) {
|
||||||
const [structName, i] = response;
|
events.setInstance(instance);
|
||||||
events.setInstance(i);
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function instanceList([, list]) {
|
function onOpenInstances(list) {
|
||||||
events.setInstanceList(list);
|
events.setInstanceList(list);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@ -211,12 +196,11 @@ function createSocket(events) {
|
|||||||
clearTimeout(instanceStateTimeout);
|
clearTimeout(instanceStateTimeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
function itemInfo(response) {
|
function onItemInfo(info) {
|
||||||
const [structName, info] = response;
|
|
||||||
events.setItemInfo(info);
|
events.setItemInfo(info);
|
||||||
}
|
}
|
||||||
|
|
||||||
function pong() {
|
function onPong() {
|
||||||
events.setPing(Date.now() - ping);
|
events.setPing(Date.now() - ping);
|
||||||
setTimeout(sendPing, 1000);
|
setTimeout(sendPing, 1000);
|
||||||
}
|
}
|
||||||
@ -228,16 +212,14 @@ function createSocket(events) {
|
|||||||
// when the server sends a reply it will have one of these message types
|
// when the server sends a reply it will have one of these message types
|
||||||
// this object wraps the reply types to a function
|
// this object wraps the reply types to a function
|
||||||
const handlers = {
|
const handlers = {
|
||||||
construct_spawn: constructSpawn,
|
Account: onAccount,
|
||||||
game_state: gameState,
|
AccountConstructs: onAccountConstructs,
|
||||||
account_login: accountLogin,
|
AccountInstances: onAccountInstances,
|
||||||
account_create: accountLogin,
|
GameState: onGameState,
|
||||||
account_constructs: accountConstructs,
|
InstanceState: onInstanceState,
|
||||||
account_instances: accountInstanceList,
|
ItemInfo: onItemInfo,
|
||||||
instance_list: instanceList,
|
OpenInstances: onOpenInstances,
|
||||||
instance_state: instanceState,
|
Pong: onPong,
|
||||||
item_info: itemInfo,
|
|
||||||
pong,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
function logout() {
|
function logout() {
|
||||||
@ -265,14 +247,14 @@ function createSocket(events) {
|
|||||||
// decode binary msg from server
|
// decode binary msg from server
|
||||||
const blob = new Uint8Array(event.data);
|
const blob = new Uint8Array(event.data);
|
||||||
const res = cbor.decode(blob);
|
const res = cbor.decode(blob);
|
||||||
const { method, params } = res;
|
const [msgType, params] = res;
|
||||||
|
|
||||||
if (method !== 'pong' ) console.log(res);
|
if (msgType !== 'pong') console.log(res);
|
||||||
|
|
||||||
// check for error and split into response type and data
|
// check for error and split into response type and data
|
||||||
if (res.err) return errHandler(res.err);
|
if (res.err) return errHandler(res.err);
|
||||||
if (!handlers[method]) return errorToast(`${method} handler missing`);
|
if (!handlers[msgType]) return errorToast(`${msgType} handler missing`);
|
||||||
return handlers[method](params);
|
return handlers[msgType](params);
|
||||||
}
|
}
|
||||||
|
|
||||||
function connect() {
|
function connect() {
|
||||||
|
|||||||
@ -17,15 +17,14 @@ use instance::{Instance, instance_state, instance_list, instance_new, instance_r
|
|||||||
use vbox::{vbox_accept, vbox_apply, vbox_discard, vbox_combine, vbox_reclaim, vbox_unequip};
|
use vbox::{vbox_accept, vbox_apply, vbox_discard, vbox_combine, vbox_reclaim, vbox_unequip};
|
||||||
use item::{Item, ItemInfoCtr, item_info};
|
use item::{Item, ItemInfoCtr, item_info};
|
||||||
|
|
||||||
|
|
||||||
type MnmlWs = ws::WebsocketContext<MnmlSocket>;
|
type MnmlWs = ws::WebsocketContext<MnmlSocket>;
|
||||||
|
|
||||||
pub fn receive(data: Vec<u8>, db: &Db, client: &mut MnmlWs, begin: Instant) -> Result<RpcResponse, Error> {
|
pub fn receive(data: Vec<u8>, db: &Db, _client: &mut MnmlWs, begin: Instant) -> Result<RpcResult, Error> {
|
||||||
// cast the data to this type to receive method name
|
// cast the msg to this type to receive method name
|
||||||
match from_slice::<RpcMessage>(&data) {
|
match from_slice::<RpcMessage>(&data) {
|
||||||
Ok(v) => {
|
Ok(v) => {
|
||||||
if v.method == "ping" {
|
if v.method == "ping" {
|
||||||
return Ok(RpcResponse { method: "pong".to_string(), params: RpcResult::Pong(()) });
|
return Ok(RpcResult::Pong(()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut tx = db.transaction()?;
|
let mut tx = db.transaction()?;
|
||||||
@ -56,33 +55,33 @@ pub fn receive(data: Vec<u8>, db: &Db, client: &mut MnmlWs, begin: Instant) -> R
|
|||||||
// match on that to determine what fn to call
|
// match on that to determine what fn to call
|
||||||
let response = match v.method.as_ref() {
|
let response = match v.method.as_ref() {
|
||||||
// NO AUTH
|
// NO AUTH
|
||||||
"account_create" => handle_account_create(data, &mut tx, client),
|
"account_create" => handle_account_create(data, &mut tx),
|
||||||
"account_login" => handle_account_login(data, &mut tx, client),
|
"account_login" => handle_account_login(data, &mut tx),
|
||||||
"item_info" => Ok(RpcResponse { method: "item_info".to_string(), params: RpcResult::ItemInfo(item_info()) }),
|
"item_info" => Ok(RpcResult::ItemInfo(item_info())),
|
||||||
|
|
||||||
// AUTH METHODS
|
// AUTH METHODS
|
||||||
"account_constructs" => handle_account_constructs(data, &mut tx, account.unwrap(), client),
|
"account_constructs" => handle_account_constructs(data, &mut tx, account.unwrap()),
|
||||||
"account_instances" => handle_account_instances(data, &mut tx, account.unwrap(), client),
|
"account_instances" => handle_account_instances(data, &mut tx, account.unwrap()),
|
||||||
|
|
||||||
"construct_spawn" => handle_construct_spawn(data, &mut tx, account.unwrap(), client),
|
"construct_spawn" => handle_construct_spawn(data, &mut tx, account.unwrap()),
|
||||||
"construct_delete" => handle_construct_delete(data, &mut tx, account.unwrap(), client),
|
"construct_delete" => handle_construct_delete(data, &mut tx, account.unwrap()),
|
||||||
|
|
||||||
"game_state" => handle_game_state(data, &mut tx, account.unwrap(), client),
|
"game_state" => handle_game_state(data, &mut tx, account.unwrap()),
|
||||||
"game_skill" => handle_game_skill(data, &mut tx, account.unwrap(), client),
|
"game_skill" => handle_game_skill(data, &mut tx, account.unwrap()),
|
||||||
"game_ready" => handle_game_ready(data, &mut tx, account.unwrap(), client),
|
"game_ready" => handle_game_ready(data, &mut tx, account.unwrap()),
|
||||||
|
|
||||||
"instance_list" => handle_instance_list(data, &mut tx, account.unwrap(), client),
|
"instance_list" => handle_instance_list(data, &mut tx, account.unwrap()),
|
||||||
"instance_join" => handle_instance_join(data, &mut tx, account.unwrap(), client),
|
"instance_join" => handle_instance_join(data, &mut tx, account.unwrap()),
|
||||||
"instance_ready" => handle_instance_ready(data, &mut tx, account.unwrap(), client),
|
"instance_ready" => handle_instance_ready(data, &mut tx, account.unwrap()),
|
||||||
"instance_new" => handle_instance_new(data, &mut tx, account.unwrap(), client),
|
"instance_new" => handle_instance_new(data, &mut tx, account.unwrap()),
|
||||||
"instance_state" => handle_instance_state(data, &mut tx, account.unwrap(), client),
|
"instance_state" => handle_instance_state(data, &mut tx, account.unwrap()),
|
||||||
|
|
||||||
"vbox_accept" => handle_vbox_accept(data, &mut tx, account.unwrap(), client),
|
"vbox_accept" => handle_vbox_accept(data, &mut tx, account.unwrap()),
|
||||||
"vbox_apply" => handle_vbox_apply(data, &mut tx, account.unwrap(), client),
|
"vbox_apply" => handle_vbox_apply(data, &mut tx, account.unwrap()),
|
||||||
"vbox_combine" => handle_vbox_combine(data, &mut tx, account.unwrap(), client),
|
"vbox_combine" => handle_vbox_combine(data, &mut tx, account.unwrap()),
|
||||||
"vbox_discard" => handle_vbox_discard(data, &mut tx, account.unwrap(), client),
|
"vbox_discard" => handle_vbox_discard(data, &mut tx, account.unwrap()),
|
||||||
"vbox_reclaim" => handle_vbox_reclaim(data, &mut tx, account.unwrap(), client),
|
"vbox_reclaim" => handle_vbox_reclaim(data, &mut tx, account.unwrap()),
|
||||||
"vbox_unequip" => handle_vbox_unequip(data, &mut tx, account.unwrap(), client),
|
"vbox_unequip" => handle_vbox_unequip(data, &mut tx, account.unwrap()),
|
||||||
|
|
||||||
_ => Err(format_err!("unknown method - {:?}", v.method)),
|
_ => Err(format_err!("unknown method - {:?}", v.method)),
|
||||||
};
|
};
|
||||||
@ -100,18 +99,12 @@ pub fn receive(data: Vec<u8>, db: &Db, client: &mut MnmlWs, begin: Instant) -> R
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_game_state(data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result<RpcResponse, Error> {
|
fn handle_game_state(data: Vec<u8>, tx: &mut Transaction, account: Account) -> Result<RpcResult, Error> {
|
||||||
let msg = from_slice::<GameStateMsg>(&data).or(Err(err_msg("invalid params")))?;
|
let msg = from_slice::<GameStateMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||||
|
return Ok(RpcResult::GameState(game_state(msg.params, tx, &account)?));
|
||||||
let game_response = RpcResponse {
|
|
||||||
method: "game_state".to_string(),
|
|
||||||
params: RpcResult::GameState(game_state(msg.params, tx, &account)?)
|
|
||||||
};
|
|
||||||
|
|
||||||
return Ok(game_response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// fn handle_game_pve(data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result<RpcResponse, Error> {
|
// fn handle_game_pve(data: Vec<u8>, tx: &mut Transaction, account: Account) -> Result<RpcResult, Error> {
|
||||||
// let msg = from_slice::<GamePveMsg>(&data).or(Err(err_msg("invalid params")))?;
|
// let msg = from_slice::<GamePveMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||||
|
|
||||||
// let game_response = RpcResponse {
|
// let game_response = RpcResponse {
|
||||||
@ -122,250 +115,107 @@ fn handle_game_state(data: Vec<u8>, tx: &mut Transaction, account: Account, _cli
|
|||||||
// return Ok(game_response);
|
// return Ok(game_response);
|
||||||
// }
|
// }
|
||||||
|
|
||||||
fn handle_game_skill(data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result<RpcResponse, Error> {
|
fn handle_game_skill(data: Vec<u8>, tx: &mut Transaction, account: Account) -> Result<RpcResult, Error> {
|
||||||
let msg = from_slice::<GameSkillMsg>(&data).or(Err(err_msg("invalid params")))?;
|
let msg = from_slice::<GameSkillMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||||
|
Ok(RpcResult::GameState(game_skill(msg.params, tx, &account)?))
|
||||||
let game_response = RpcResponse {
|
|
||||||
method: "game_state".to_string(),
|
|
||||||
params: RpcResult::GameState(game_skill(msg.params, tx, &account)?)
|
|
||||||
};
|
|
||||||
|
|
||||||
return Ok(game_response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_game_ready(data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result<RpcResponse, Error> {
|
fn handle_game_ready(data: Vec<u8>, tx: &mut Transaction, account: Account) -> Result<RpcResult, Error> {
|
||||||
let msg = from_slice::<GameStateMsg>(&data).or(Err(err_msg("invalid params")))?;
|
let msg = from_slice::<GameStateMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||||
|
Ok(RpcResult::GameState(game_ready(msg.params, tx, &account)?))
|
||||||
let game_response = RpcResponse {
|
|
||||||
method: "game_state".to_string(),
|
|
||||||
params: RpcResult::GameState(game_ready(msg.params, tx, &account)?)
|
|
||||||
};
|
|
||||||
|
|
||||||
return Ok(game_response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn handle_construct_spawn(data: Vec<u8>, tx: &mut Transaction, account: Account, client: &mut MnmlWs) -> Result<RpcResponse, Error> {
|
fn handle_construct_spawn(data: Vec<u8>, tx: &mut Transaction, account: Account) -> Result<RpcResult, Error> {
|
||||||
let msg = from_slice::<ConstructSpawnMsg>(&data).or(Err(err_msg("invalid params")))?;
|
let msg = from_slice::<ConstructSpawnMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||||
|
|
||||||
construct_spawn(msg.params, tx, &account)?;
|
construct_spawn(msg.params, tx, &account)?;
|
||||||
|
Ok(RpcResult::AccountConstructs(account_constructs(tx, &account)?))
|
||||||
let construct_list = RpcResponse {
|
|
||||||
method: "account_constructs".to_string(),
|
|
||||||
params: RpcResult::ConstructList(account_constructs(tx, &account)?)
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(construct_list)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_construct_delete(data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result<RpcResponse, Error> {
|
fn handle_construct_delete(data: Vec<u8>, tx: &mut Transaction, account: Account) -> Result<RpcResult, Error> {
|
||||||
let msg = from_slice::<ConstructDeleteMsg>(&data).or(Err(err_msg("invalid params")))?;
|
let msg = from_slice::<ConstructDeleteMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||||
|
|
||||||
construct_delete(tx, msg.params.id, account.id)?;
|
construct_delete(tx, msg.params.id, account.id)?;
|
||||||
|
Ok(RpcResult::AccountConstructs(account_constructs(tx, &account)?))
|
||||||
let construct_list = RpcResponse {
|
|
||||||
method: "account_constructs".to_string(),
|
|
||||||
params: RpcResult::ConstructList(account_constructs(tx, &account)?)
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(construct_list)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn handle_account_create(data: Vec<u8>, tx: &mut Transaction, _client: &mut MnmlWs) -> Result<RpcResponse, Error> {
|
fn handle_account_create(data: Vec<u8>, tx: &mut Transaction) -> Result<RpcResult, Error> {
|
||||||
let msg = from_slice::<AccountCreateMsg>(&data).or(Err(err_msg("invalid params")))?;
|
let msg = from_slice::<AccountCreateMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||||
|
|
||||||
let account = account_create(msg.params, tx)?;
|
let account = account_create(msg.params, tx)?;
|
||||||
|
Ok(RpcResult::Account(account))
|
||||||
Ok(RpcResponse {
|
|
||||||
method: "account_create".to_string(),
|
|
||||||
params: RpcResult::Account(account)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_account_login(data: Vec<u8>, tx: &mut Transaction, _client: &mut MnmlWs) -> Result<RpcResponse, Error> {
|
fn handle_account_login(data: Vec<u8>, tx: &mut Transaction) -> Result<RpcResult, Error> {
|
||||||
match from_slice::<AccountLoginMsg>(&data) {
|
let msg = from_slice::<AccountLoginMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||||
Ok(v) => Ok(RpcResponse {
|
Ok(RpcResult::Account(account_login(msg.params, tx)?))
|
||||||
method: v.method,
|
|
||||||
params: RpcResult::Account(account_login(v.params, tx)?)
|
|
||||||
}),
|
|
||||||
Err(_e) => Err(err_msg("invalid params")),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// fn handle_account_demo(_data: Vec<u8>, tx: &mut Transaction, _client: &mut MnmlWs) -> Result<RpcResponse, Error> {
|
fn handle_account_constructs(_data: Vec<u8>, tx: &mut Transaction, account: Account) -> Result<RpcResult, Error> {
|
||||||
// let mut rng = thread_rng();
|
Ok(RpcResult::AccountConstructs(account_constructs(tx, &account)?))
|
||||||
|
|
||||||
// let acc_name: String = iter::repeat(()).map(|()| rng.sample(Alphanumeric)).take(8).collect();
|
|
||||||
|
|
||||||
// let account = account_create(AccountCreateParams { name: acc_name, password: "grepgrepgrep".to_string() }, tx)?;
|
|
||||||
|
|
||||||
// let name: String = iter::repeat(()).map(|()| rng.sample(Alphanumeric)).take(8).collect();
|
|
||||||
// construct_spawn(ConstructSpawnParams { name }, tx, &account)?;
|
|
||||||
|
|
||||||
// let name: String = iter::repeat(()).map(|()| rng.sample(Alphanumeric)).take(8).collect();
|
|
||||||
// construct_spawn(ConstructSpawnParams { name }, tx, &account)?;
|
|
||||||
|
|
||||||
// let name: String = iter::repeat(()).map(|()| rng.sample(Alphanumeric)).take(8).collect();
|
|
||||||
// construct_spawn(ConstructSpawnParams { name }, tx, &account)?;
|
|
||||||
|
|
||||||
// let res = RpcResponse {
|
|
||||||
// method: "account_create".to_string(),
|
|
||||||
// params: RpcResult::Account(account),
|
|
||||||
// };
|
|
||||||
|
|
||||||
// return Ok(res);
|
|
||||||
// }
|
|
||||||
|
|
||||||
|
|
||||||
fn handle_account_constructs(_data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result<RpcResponse, Error> {
|
|
||||||
Ok(RpcResponse {
|
|
||||||
method: "account_constructs".to_string(),
|
|
||||||
params: RpcResult::ConstructList(account_constructs(tx, &account)?)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_account_instances(_data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result<RpcResponse, Error> {
|
fn handle_account_instances(_data: Vec<u8>, tx: &mut Transaction, account: Account) -> Result<RpcResult, Error> {
|
||||||
Ok(RpcResponse {
|
Ok(RpcResult::AccountInstances(account_instances(tx, &account)?))
|
||||||
method: "account_instances".to_string(),
|
|
||||||
params: RpcResult::InstanceList(account_instances(tx, &account)?)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_instance_new(data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result<RpcResponse, Error> {
|
fn handle_instance_new(data: Vec<u8>, tx: &mut Transaction, account: Account) -> Result<RpcResult, Error> {
|
||||||
let msg = from_slice::<InstanceLobbyMsg>(&data).or(Err(err_msg("invalid params")))?;
|
let msg = from_slice::<InstanceLobbyMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||||
|
Ok(RpcResult::InstanceState(instance_new(msg.params, tx, &account)?))
|
||||||
let response = RpcResponse {
|
|
||||||
method: "instance_state".to_string(),
|
|
||||||
params: RpcResult::InstanceState(instance_new(msg.params, tx, &account)?)
|
|
||||||
};
|
|
||||||
|
|
||||||
return Ok(response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_instance_ready(data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result<RpcResponse, Error> {
|
fn handle_instance_ready(data: Vec<u8>, tx: &mut Transaction, account: Account) -> Result<RpcResult, Error> {
|
||||||
let msg = from_slice::<InstanceReadyMsg>(&data).or(Err(err_msg("invalid params")))?;
|
let msg = from_slice::<InstanceReadyMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||||
|
Ok(RpcResult::InstanceState(instance_ready(msg.params, tx, &account)?))
|
||||||
let response = RpcResponse {
|
|
||||||
method: "instance_state".to_string(),
|
|
||||||
params: RpcResult::InstanceState(instance_ready(msg.params, tx, &account)?)
|
|
||||||
};
|
|
||||||
|
|
||||||
return Ok(response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_instance_join(data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result<RpcResponse, Error> {
|
fn handle_instance_join(data: Vec<u8>, tx: &mut Transaction, account: Account) -> Result<RpcResult, Error> {
|
||||||
let msg = from_slice::<InstanceJoinMsg>(&data).or(Err(err_msg("invalid params")))?;
|
let msg = from_slice::<InstanceJoinMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||||
|
Ok(RpcResult::InstanceState(instance_join(msg.params, tx, &account)?))
|
||||||
let response = RpcResponse {
|
|
||||||
method: "instance_state".to_string(),
|
|
||||||
params: RpcResult::InstanceState(instance_join(msg.params, tx, &account)?)
|
|
||||||
};
|
|
||||||
|
|
||||||
return Ok(response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_instance_list(_data: Vec<u8>, tx: &mut Transaction, _account: Account, _client: &mut MnmlWs) -> Result<RpcResponse, Error> {
|
fn handle_instance_list(_data: Vec<u8>, tx: &mut Transaction, _account: Account) -> Result<RpcResult, Error> {
|
||||||
let response = RpcResponse {
|
Ok(RpcResult::OpenInstances(instance_list(tx)?))
|
||||||
method: "instance_list".to_string(),
|
|
||||||
params: RpcResult::InstanceList(instance_list(tx)?)
|
|
||||||
};
|
|
||||||
|
|
||||||
return Ok(response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// fn handle_instance_ready(data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result<RpcResponse, Error> {
|
fn handle_instance_state(data: Vec<u8>, tx: &mut Transaction, account: Account) -> Result<RpcResult, Error> {
|
||||||
// let msg = from_slice::<InstanceReadyMsg>(&data).or(Err(err_msg("invalid params")))?;
|
|
||||||
|
|
||||||
// let response = RpcResponse {
|
|
||||||
// method: "game_state".to_string(),
|
|
||||||
// params: RpcResult::GameState(instance_ready(msg.params, tx, &account)?)
|
|
||||||
// };
|
|
||||||
|
|
||||||
// return Ok(response);
|
|
||||||
// }
|
|
||||||
|
|
||||||
fn handle_instance_state(data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result<RpcResponse, Error> {
|
|
||||||
let msg = from_slice::<InstanceStateMsg>(&data).or(Err(err_msg("invalid params")))?;
|
let msg = from_slice::<InstanceStateMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||||
match instance_state(msg.params, tx, &account)? {
|
match instance_state(msg.params, tx, &account)? {
|
||||||
RpcResult::GameState(p) => Ok(RpcResponse {
|
RpcResult::GameState(p) => Ok(RpcResult::GameState(p)),
|
||||||
method: "game_state".to_string(),
|
RpcResult::InstanceState(p) => Ok(RpcResult::InstanceState(p)),
|
||||||
params: RpcResult::GameState(p),
|
|
||||||
}),
|
|
||||||
RpcResult::InstanceState(p) => Ok(RpcResponse {
|
|
||||||
method: "instance_state".to_string(),
|
|
||||||
params: RpcResult::InstanceState(p),
|
|
||||||
}),
|
|
||||||
_ => Err(err_msg("unhandled instance state"))
|
_ => Err(err_msg("unhandled instance state"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_vbox_accept(data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result<RpcResponse, Error> {
|
fn handle_vbox_accept(data: Vec<u8>, tx: &mut Transaction, account: Account) -> Result<RpcResult, Error> {
|
||||||
let msg = from_slice::<VboxAcceptMsg>(&data).or(Err(err_msg("invalid params")))?;
|
let msg = from_slice::<VboxAcceptMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||||
|
Ok(RpcResult::InstanceState(vbox_accept(msg.params, tx, &account)?))
|
||||||
let response = RpcResponse {
|
|
||||||
method: "instance_state".to_string(),
|
|
||||||
params: RpcResult::InstanceState(vbox_accept(msg.params, tx, &account)?)
|
|
||||||
};
|
|
||||||
|
|
||||||
return Ok(response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_vbox_discard(data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result<RpcResponse, Error> {
|
fn handle_vbox_discard(data: Vec<u8>, tx: &mut Transaction, account: Account) -> Result<RpcResult, Error> {
|
||||||
let msg = from_slice::<VboxDiscardMsg>(&data).or(Err(err_msg("invalid params")))?;
|
let msg = from_slice::<VboxDiscardMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||||
|
Ok(RpcResult::InstanceState(vbox_discard(msg.params, tx, &account)?))
|
||||||
let response = RpcResponse {
|
|
||||||
method: "instance_state".to_string(),
|
|
||||||
params: RpcResult::InstanceState(vbox_discard(msg.params, tx, &account)?)
|
|
||||||
};
|
|
||||||
|
|
||||||
return Ok(response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn handle_vbox_combine(data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result<RpcResponse, Error> {
|
fn handle_vbox_combine(data: Vec<u8>, tx: &mut Transaction, account: Account) -> Result<RpcResult, Error> {
|
||||||
let msg = from_slice::<VboxCombineMsg>(&data).or(Err(err_msg("invalid params")))?;
|
let msg = from_slice::<VboxCombineMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||||
|
Ok(RpcResult::InstanceState(vbox_combine(msg.params, tx, &account)?))
|
||||||
let response = RpcResponse {
|
|
||||||
method: "instance_state".to_string(),
|
|
||||||
params: RpcResult::InstanceState(vbox_combine(msg.params, tx, &account)?)
|
|
||||||
};
|
|
||||||
|
|
||||||
return Ok(response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_vbox_apply(data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result<RpcResponse, Error> {
|
fn handle_vbox_apply(data: Vec<u8>, tx: &mut Transaction, account: Account) -> Result<RpcResult, Error> {
|
||||||
let msg = from_slice::<VboxApplyMsg>(&data).or(Err(err_msg("invalid params")))?;
|
let msg = from_slice::<VboxApplyMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||||
|
Ok(RpcResult::InstanceState(vbox_apply(msg.params, tx, &account)?))
|
||||||
let response = RpcResponse {
|
|
||||||
method: "instance_state".to_string(),
|
|
||||||
params: RpcResult::InstanceState(vbox_apply(msg.params, tx, &account)?)
|
|
||||||
};
|
|
||||||
|
|
||||||
return Ok(response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_vbox_reclaim(data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result<RpcResponse, Error> {
|
fn handle_vbox_reclaim(data: Vec<u8>, tx: &mut Transaction, account: Account) -> Result<RpcResult, Error> {
|
||||||
let msg = from_slice::<VboxReclaimMsg>(&data).or(Err(err_msg("invalid params")))?;
|
let msg = from_slice::<VboxReclaimMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||||
|
Ok(RpcResult::InstanceState(vbox_reclaim(msg.params, tx, &account)?))
|
||||||
let response = RpcResponse {
|
|
||||||
method: "instance_state".to_string(),
|
|
||||||
params: RpcResult::InstanceState(vbox_reclaim(msg.params, tx, &account)?)
|
|
||||||
};
|
|
||||||
|
|
||||||
return Ok(response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn handle_vbox_unequip(data: Vec<u8>, tx: &mut Transaction, account: Account, _client: &mut MnmlWs) -> Result<RpcResponse, Error> {
|
fn handle_vbox_unequip(data: Vec<u8>, tx: &mut Transaction, account: Account) -> Result<RpcResult, Error> {
|
||||||
let msg = from_slice::<VboxUnequipMsg>(&data).or(Err(err_msg("invalid params")))?;
|
let msg = from_slice::<VboxUnequipMsg>(&data).or(Err(err_msg("invalid params")))?;
|
||||||
|
Ok(RpcResult::InstanceState(vbox_unequip(msg.params, tx, &account)?))
|
||||||
let response = RpcResponse {
|
|
||||||
method: "instance_state".to_string(),
|
|
||||||
params: RpcResult::InstanceState(vbox_unequip(msg.params, tx, &account)?)
|
|
||||||
};
|
|
||||||
|
|
||||||
return Ok(response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||||
@ -373,24 +223,15 @@ pub struct RpcErrorResponse {
|
|||||||
pub err: String
|
pub err: String
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
|
||||||
pub struct RpcResponse {
|
|
||||||
pub method: String,
|
|
||||||
params: RpcResult,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug,Clone,Serialize,Deserialize)]
|
#[derive(Debug,Clone,Serialize,Deserialize)]
|
||||||
pub enum RpcResult {
|
pub enum RpcResult {
|
||||||
ConstructSpawn(Construct),
|
|
||||||
ConstructForget(Construct),
|
|
||||||
ConstructLearn(Construct),
|
|
||||||
ConstructUnspec(Construct),
|
|
||||||
Account(Account),
|
Account(Account),
|
||||||
ConstructList(Vec<Construct>),
|
AccountConstructs(Vec<Construct>),
|
||||||
|
AccountInstances(Vec<Instance>),
|
||||||
GameState(Game),
|
GameState(Game),
|
||||||
ItemInfo(ItemInfoCtr),
|
ItemInfo(ItemInfoCtr),
|
||||||
|
|
||||||
InstanceList(Vec<Instance>),
|
OpenInstances(Vec<Instance>),
|
||||||
InstanceState(Instance),
|
InstanceState(Instance),
|
||||||
|
|
||||||
Pong(()),
|
Pong(()),
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user