mnml/client/src/socket.js
2019-02-21 15:02:19 +11:00

314 lines
8.5 KiB
JavaScript

const toast = require('izitoast');
const cbor = require('borc');
const SOCKET_URL = process.env.NODE_ENV === 'production' ? 'wss://cryps.gg/ws' : 'ws://localhost:40000';
function errorToast(err) {
console.error(err);
return toast.error({
title: 'BEEP BOOP',
message: err,
position: 'topRight',
});
}
function createSocket(events) {
let ws;
// handle account auth within the socket itself
// https://www.christian-schneider.net/CrossSiteWebSocketHijacking.html
let account = null;
// -------------
// Outgoing
// -------------
function send(msg) {
console.log('outgoing msg', msg);
msg.token = account && account.token;
ws.send(cbor.encode(msg));
}
function sendAccountLogin(name, password) {
send({ method: 'account_login', params: { name, password } });
}
function sendAccountCreate(name, password) {
send({ method: 'account_create', params: { name, password } });
}
function sendAccountDemo() {
send({ method: 'account_demo', params: {} });
}
function sendAccountCryps() {
send({ method: 'account_cryps', params: {} });
}
function sendAccountZone() {
send({ method: 'account_zone', params: {} });
}
function sendCrypSpawn(name) {
send({ method: 'cryp_spawn', params: { name } });
}
function sendCrypLearn(id, skill) {
send({ method: 'cryp_learn', params: { id, skill } });
}
function sendCrypForget(id, skill) {
send({ method: 'cryp_forget', params: { id, skill } });
}
function sendGameState(id) {
send({ method: 'game_state', params: { id } });
}
function sendGamePve(crypIds, mode) {
send({ method: 'game_pve', params: { cryp_ids: crypIds, mode } });
}
function sendGamePvp(crypIds) {
send({ method: 'game_pvp', params: { cryp_ids: crypIds } });
}
function sendGameJoin(gameId, crypIds) {
send({ method: 'game_join', params: { game_id: gameId, cryp_ids: crypIds } });
}
function sendGameJoinableList() {
send({ method: 'game_joinable_list', params: { } });
}
function sendSpecForget(id, spec) {
send({ method: 'cryp_unspec', params: { id, spec } });
}
function sendPlayerState(instanceId) {
send({ method: 'player_state', params: { instance_id: instanceId } });
}
function sendVboxAccept(instanceId, i) {
send({ method: 'player_vbox_accept', params: { instance_id: instanceId, index: i } });
}
function sendVboxApply(instanceId, crypId, index) {
send({ method: 'player_vbox_apply', params: { instance_id: instanceId, cryp_id: crypId, index } });
}
function sendVboxDiscard(instanceId) {
send({ method: 'player_vbox_discard', params: { instance_id: instanceId } });
}
function sendVboxCombine(instanceId, indices) {
send({ method: 'player_vbox_combine', params: { instance_id: instanceId, indices } });
}
function sendVboxDrop(instanceId, index) {
send({ method: 'player_vbox_drop', params: { instance_id: instanceId, index } });
}
function sendPressR() {
send({ method: 'press_r', params: { } });
}
window.pressR = sendPressR;
function sendGameSkill(gameId, crypId, targetCrypId, skill) {
send({
method: 'game_skill',
params: {
game_id: gameId, cryp_id: crypId, target_cryp_id: targetCrypId, skill,
},
});
events.setActiveSkill(null);
}
function sendGameTarget(gameId, crypId, skillId) {
send({ method: 'game_target', params: { game_id: gameId, cryp_id: crypId, skill_id: skillId } });
events.setActiveSkill(null);
}
function sendZoneCreate() {
send({ method: 'zone_create', params: {} });
}
function sendZoneJoin(zoneId, nodeId, crypIds) {
send({ method: 'zone_join', params: { zone_id: zoneId, node_id: nodeId, cryp_ids: crypIds } });
}
function sendZoneClose(zoneId) {
send({ method: 'zone_close', params: { zone_id: zoneId } });
}
// -------------
// Incoming
// -------------
function accountLogin(res) {
const [struct, login] = res;
account = login;
events.setAccount(login);
sendAccountCryps();
// sendGameJoinableList();
}
function accountCryps(response) {
const [structName, cryps] = response;
events.setCryps(cryps);
}
function gameState(response) {
const [structName, game] = response;
events.setGame(game);
}
function gameJoinableList(response) {
const [structName, gameList] = response;
events.setGameList(gameList);
}
function crypSpawn(response) {
const [structName, cryp] = response;
}
function gamePve(response) {
const [structName, game] = response;
}
function zoneState(response) {
const [structName, zone] = response;
events.setZone(zone);
}
function playerState(response) {
const [structName, player] = response;
events.setVbox(player.vbox);
}
// -------------
// Setup
// -------------
// 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,
cryp_forget: () => true,
cryp_learn: () => true,
game_pve: gamePve,
game_state: gameState,
game_joinable_list: gameJoinableList,
account_login: accountLogin,
account_create: accountLogin,
account_cryps: accountCryps,
zone_create: res => console.log(res),
zone_state: zoneState,
zone_close: res => console.log(res),
player_state: playerState,
};
function errHandler(error) {
switch (error) {
case 'no active zone': return sendZoneCreate();
case 'no cryps selected': return events.errorPrompt('select_cryps');
case 'node requirements not met': return events.errorPrompt('complete_nodes');
case 'cryp at max skills (4)': return events.errorPrompt('max_skills');
default: return errorToast(error);
}
}
// decodes the cbor and
// calls the handlers defined above based on message type
function onMessage(event) {
// decode binary msg from server
const blob = new Uint8Array(event.data);
const res = cbor.decode(blob);
console.log(res);
// check for error and split into response type and data
if (res.err) return errHandler(res.err);
const { method, params } = res;
if (!handlers[method]) return errorToast(`${method} handler missing`);
return handlers[method](params);
}
function connect() {
ws = new WebSocket(SOCKET_URL);
ws.binaryType = 'arraybuffer';
// Connection opened
ws.addEventListener('open', () => {
toast.info({
message: 'connected',
position: 'topRight',
});
if (!account) events.loginPrompt();
if (process.env.NODE_ENV !== 'production') {
send({ method: 'account_login', params: { name: 'ntr', password: 'grepgrepgrep' } });
}
return true;
});
// Listen for messages
ws.addEventListener('message', onMessage);
ws.addEventListener('error', (event) => {
console.error('WebSocket error', event);
// account = null;
// return setTimeout(connect, 5000);
});
ws.addEventListener('close', (event) => {
console.error('WebSocket closed', event);
toast.warning({
message: 'disconnected',
position: 'topRight',
});
return setTimeout(connect, 5000);
});
return ws;
}
return {
sendAccountLogin,
sendAccountCreate,
sendAccountDemo,
sendAccountCryps,
sendAccountZone,
sendGameState,
sendGamePve,
sendGamePvp,
sendGameJoin,
sendGameJoinableList,
sendGameSkill,
sendGameTarget,
sendCrypSpawn,
sendCrypLearn,
sendCrypForget,
sendSpecForget,
sendZoneCreate,
sendZoneJoin,
sendZoneClose,
sendPlayerState,
sendVboxAccept,
sendVboxApply,
sendVboxDrop,
sendVboxCombine,
sendVboxDiscard,
connect,
};
}
module.exports = createSocket;