camelot-unchained
Version:
Camelot Unchained Client Library
177 lines (170 loc) • 6.93 kB
JavaScript
/**
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
;
var client_1 = require('../../core/client');
var events_1 = require('../../events');
// WARBAND HUB RPC FUNCTION NAMES
var WARBAND_HUB_JOINED = 'warbandJoined';
var WARBAND_HUB_UPDATE = 'warbandUpdate';
var WARBAND_HUB_QUIT = 'warbandQuit';
var WARBAND_HUB_ABANDONED = 'warbandAbandoned';
var WARBAND_HUB_MEMBERJOINED = 'warbandMemberJoined';
var WARBAND_HUB_MEMBERUPDATE = 'warbandMemberUpdate';
var WARBAND_HUB_MEMBERREMOVED = 'warbandMemberRemoved';
var WARBAND_HUB_INVITERECEIVED = 'warbandInviteReceived';
// UI EVENT NAMES
exports.WARBAND_EVENTS_JOINED = 'warbands/joined';
exports.WARBAND_EVENTS_UPDATE = 'warbands/update';
exports.WARBAND_EVENTS_QUIT = 'warbands/quit';
exports.WARBAND_EVENTS_ABANDONED = 'warbands/abandoned';
exports.WARBAND_EVENTS_MEMBERJOINED = 'warbands/memberJoined';
exports.WARBAND_EVENTS_MEMBERUPDATE = 'warbands/memberUpdate';
exports.WARBAND_EVENTS_MEMBERREMOVED = 'warbands/memberRemoved';
exports.WARBAND_EVENTS_INVITERECEIVED = 'warbands/inviteReceived';
/**
* Regisers all events for this hub. RPC calls are converted into UI event calls
* using the UI event emitter system.
*/
var registerEvents = function registerEvents() {
var hub = $.connection.warbandsHub;
/**
* use as a default parameter and it will throw an error if the parameter is not provided to a method
*/
var mandatory = function mandatory() {
throw new Error('Missing Parameter');
};
/**
* wrapper for hub methods which adds logging of every call if client debug mode is enabled
*/
var register = function register(RPCMETHOD, callback) {
hub.on(RPCMETHOD, function () {
for (var _len = arguments.length, params = Array(_len), _key = 0; _key < _len; _key++) {
params[_key] = arguments[_key];
}
if (client_1.default.debug) console.log('warband hub called ' + RPCMETHOD + ' with params:\n' + JSON.stringify(params));
callback.apply(undefined, params);
});
};
/**
* Called when you join a warband.
* -- Name can be an empty string.
*/
register(WARBAND_HUB_JOINED, function () {
var warbandID = arguments.length <= 0 || arguments[0] === undefined ? mandatory() : arguments[0];
var warbandName = arguments[1];
events_1.default.fire(exports.WARBAND_EVENTS_JOINED, { id: warbandID, name: warbandName });
});
/**
* Called when the warband name is changed or removed
* -- No name = this thing is temporary, Has name = this thing is permanent
*/
register(WARBAND_HUB_UPDATE, function (warbandID, warbandName) {
events_1.default.fire(exports.WARBAND_EVENTS_UPDATE, { id: warbandID, name: warbandName });
});
/**
* Called when you quit a warband.
*/
register(WARBAND_HUB_QUIT, function () {
events_1.default.fire(exports.WARBAND_EVENTS_QUIT, null);
});
/**
* Called when you abandon a warband.
*/
register(WARBAND_HUB_ABANDONED, function () {
events_1.default.fire(exports.WARBAND_EVENTS_ABANDONED, null);
});
/**
* Called when a member joins a warband
* -- could be your own character
*/
register(WARBAND_HUB_MEMBERJOINED, function () {
var member = arguments.length <= 0 || arguments[0] === undefined ? mandatory() : arguments[0];
events_1.default.fire(exports.WARBAND_EVENTS_MEMBERJOINED, member);
});
/**
* Called when a member is updated, like health changed for example.
* -- could be your own character
*/
register(WARBAND_HUB_MEMBERUPDATE, function () {
var member = arguments.length <= 0 || arguments[0] === undefined ? mandatory() : arguments[0];
events_1.default.fire(exports.WARBAND_EVENTS_MEMBERUPDATE, member);
});
/**
* Called when a member is removed from your warband.
* -- could be your own character
*/
register(WARBAND_HUB_MEMBERREMOVED, function () {
var characterID = arguments.length <= 0 || arguments[0] === undefined ? mandatory() : arguments[0];
events_1.default.fire(exports.WARBAND_EVENTS_MEMBERREMOVED, characterID);
});
/**
* Called when you receive an invite to join a warband
*/
register(WARBAND_HUB_INVITERECEIVED, function () {
var invite = arguments.length <= 0 || arguments[0] === undefined ? mandatory() : arguments[0];
events_1.default.fire(exports.WARBAND_EVENTS_INVITERECEIVED, invite);
});
};
/**
* Un register all events for this hub.
* **Note** this will un-register all events, so if you've manually registered to an rpc call
* from outside this module, that will also be unregistered. I recommended to just use
* the events systen rather than signalR directly for handling real-time communication.
*/
var unregisterEvents = function unregisterEvents() {
var hub = $.connection.warbandsHub;
hub.off(WARBAND_HUB_JOINED);
hub.off(WARBAND_HUB_UPDATE);
hub.off(WARBAND_HUB_QUIT);
hub.off(WARBAND_HUB_ABANDONED);
hub.off(WARBAND_HUB_MEMBERJOINED);
hub.off(WARBAND_HUB_MEMBERUPDATE);
hub.off(WARBAND_HUB_MEMBERREMOVED);
hub.off(WARBAND_HUB_INVITERECEIVED);
};
/**
* Used to ensure initializeHub is only called once.
*/
var didInitialize = false;
/**
* Initialize the Warband Hub, called internally from the signalR hub registration method
* -- handles client identification for the Warband Hub. client information must exist
* on the cuAPI / client object (loginToken, shardID, characterID) before this method
* is called or identification will fail
*/
var initializeHub = function initializeHub(callback) {
if (didInitialize) return;
var hub = $.connection.warbandsHub;
registerEvents();
$.connection.hub.start().done(function () {
hub.server.identify(client_1.default.loginToken, client_1.default.shardID, client_1.default.characterID).done(function (success) {
if (!success) {
callback(false);
return;
}
// invalidate to force a resend of all data to this client
hub.server.invalidate();
callback(true);
});
});
};
/**
* Re-Initialize the Warband Hub, provide a callback which will indicate whether or not the
* hub has been initialized.
*
* @param {(succeeded: boolean) => any} callback
*/
var reinitializeHub = function reinitializeHub(callback) {
didInitialize = false;
unregisterEvents();
initializeHub(callback);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = {
initializeHub: initializeHub,
reinitializeHub: reinitializeHub,
unregisterEvents: unregisterEvents
};