simpify rpc responses
This commit is contained in:
+22
-40
@@ -143,9 +143,7 @@ function createSocket(events) {
|
||||
// -------------
|
||||
// Incoming
|
||||
// -------------
|
||||
function accountLogin(res) {
|
||||
const [struct, login] = res;
|
||||
|
||||
function onAccount(login) {
|
||||
account = login;
|
||||
localStorage.setItem('account', JSON.stringify(login));
|
||||
events.setAccount(login);
|
||||
@@ -153,19 +151,16 @@ function createSocket(events) {
|
||||
sendAccountInstances();
|
||||
}
|
||||
|
||||
function accountInstanceList(res) {
|
||||
const [struct, playerList] = res;
|
||||
events.setAccountInstances(playerList);
|
||||
function onAccountInstances(list) {
|
||||
events.setAccountInstances(list);
|
||||
setTimeout(sendAccountInstances, 5000);
|
||||
}
|
||||
|
||||
function accountConstructs(response) {
|
||||
const [structName, constructs] = response;
|
||||
function onAccountConstructs(constructs) {
|
||||
events.setConstructList(constructs);
|
||||
}
|
||||
|
||||
function gameState(response) {
|
||||
const [structName, game] = response;
|
||||
function onGameState(game) {
|
||||
events.setGame(game);
|
||||
}
|
||||
|
||||
@@ -180,15 +175,6 @@ function createSocket(events) {
|
||||
clearTimeout(gameStateTimeout);
|
||||
}
|
||||
|
||||
function constructSpawn(response) {
|
||||
const [structName, construct] = response;
|
||||
}
|
||||
|
||||
function zoneState(response) {
|
||||
const [structName, zone] = response;
|
||||
events.setZone(zone);
|
||||
}
|
||||
|
||||
let instanceStateTimeout;
|
||||
function startInstanceStateTimeout(id) {
|
||||
clearTimeout(instanceStateTimeout);
|
||||
@@ -196,13 +182,12 @@ function createSocket(events) {
|
||||
return true;
|
||||
}
|
||||
|
||||
function instanceState(response) {
|
||||
const [structName, i] = response;
|
||||
events.setInstance(i);
|
||||
function onInstanceState(instance) {
|
||||
events.setInstance(instance);
|
||||
return true;
|
||||
}
|
||||
|
||||
function instanceList([, list]) {
|
||||
function onOpenInstances(list) {
|
||||
events.setInstanceList(list);
|
||||
return true;
|
||||
}
|
||||
@@ -211,12 +196,11 @@ function createSocket(events) {
|
||||
clearTimeout(instanceStateTimeout);
|
||||
}
|
||||
|
||||
function itemInfo(response) {
|
||||
const [structName, info] = response;
|
||||
function onItemInfo(info) {
|
||||
events.setItemInfo(info);
|
||||
}
|
||||
|
||||
function pong() {
|
||||
function onPong() {
|
||||
events.setPing(Date.now() - ping);
|
||||
setTimeout(sendPing, 1000);
|
||||
}
|
||||
@@ -228,16 +212,14 @@ function createSocket(events) {
|
||||
// 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 = {
|
||||
construct_spawn: constructSpawn,
|
||||
game_state: gameState,
|
||||
account_login: accountLogin,
|
||||
account_create: accountLogin,
|
||||
account_constructs: accountConstructs,
|
||||
account_instances: accountInstanceList,
|
||||
instance_list: instanceList,
|
||||
instance_state: instanceState,
|
||||
item_info: itemInfo,
|
||||
pong,
|
||||
Account: onAccount,
|
||||
AccountConstructs: onAccountConstructs,
|
||||
AccountInstances: onAccountInstances,
|
||||
GameState: onGameState,
|
||||
InstanceState: onInstanceState,
|
||||
ItemInfo: onItemInfo,
|
||||
OpenInstances: onOpenInstances,
|
||||
Pong: onPong,
|
||||
};
|
||||
|
||||
function logout() {
|
||||
@@ -265,14 +247,14 @@ function createSocket(events) {
|
||||
// decode binary msg from server
|
||||
const blob = new Uint8Array(event.data);
|
||||
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
|
||||
if (res.err) return errHandler(res.err);
|
||||
if (!handlers[method]) return errorToast(`${method} handler missing`);
|
||||
return handlers[method](params);
|
||||
if (!handlers[msgType]) return errorToast(`${msgType} handler missing`);
|
||||
return handlers[msgType](params);
|
||||
}
|
||||
|
||||
function connect() {
|
||||
|
||||
Reference in New Issue
Block a user