Skip to content

Commit

Permalink
Merge pull request #291 from dsiekiera/beta
Browse files Browse the repository at this point in the history
Beta
  • Loading branch information
Daniel Siekiera authored Jan 6, 2020
2 parents 71cd981 + 27357a0 commit c03b233
Show file tree
Hide file tree
Showing 5 changed files with 203 additions and 122 deletions.
181 changes: 173 additions & 8 deletions js/web/_main/js/_main.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const FoEproxy = (function () {
return data;
}

// XHR-handler
/** @type {Record<string, undefined|Record<string, undefined|((data: FoE_NETWORK_TYPE, postData: any) => void)[]>>} */
const proxyMap = {};

Expand All @@ -58,6 +59,10 @@ const FoEproxy = (function () {

/** @type {((data: any, requestData: any) => void)[]} */
let proxyRaw = [];

// Websocket-Handler
const wsHandlerMap = {};
let wsRawHandler = [];

const proxy = {
/**
Expand Down Expand Up @@ -149,8 +154,162 @@ const FoEproxy = (function () {

removeRawHandler: function(callback) {
proxyRaw = proxyRaw.filter(c => c !== callback);
},

/**
* Fügt einen datenhandler für Nachrichtedn des WebSockets hinzu.
* @param {string} service Der Servicewert, der in der Nachricht gesetzt sein soll oder 'all'
* @param {string} method Der Methodenwert, der in der Nachricht gesetzt sein soll oder 'all'
* TODO: Genaueren Typ für den Callback definieren
* @param {(data: FoE_NETWORK_TYPE) => void} callback Der Handler, welcher mit der Nachricht aufgerufen werden soll.
*/
addWsHandler: function(service, method, callback) {
// default service and method to 'all'
if (method === undefined) {
// @ts-ignore
callback = service;
service = method = 'all';
} else if (callback === undefined) {
// @ts-ignore
callback = method;
method = 'all';
}

let map = wsHandlerMap[service];
if (!map) {
wsHandlerMap[service] = map = {};
}
let list = map[method];
if (!list) {
map[method] = list = [];
}
if (list.indexOf(callback) !== -1) {
// already registered
return;
}
list.push(callback);
},

removeWsHandler: function(service, method, callback) {
// default service and method to 'all'
if (method === undefined) {
callback = service;
service = method = 'all';
} else if (callback === undefined) {
callback = method;
method = 'all';
}

let map = wsHandlerMap[service];
if (!map) {
return;
}
let list = map[method];
if (!list) {
return;
}
map[method] = list.filter(c => c !== callback);
},

// for raw requests access
addRawWsHandler: function(callback) {
if (wsRawHandler.indexOf(callback) !== -1) {
// already registered
return;
}

wsRawHandler.push(callback);
},

removeRawWsHandler: function(callback) {
wsRawHandler = wsRawHandler.filter(c => c !== callback);
}
};

// ###########################################
// ############## Websocket-Proxy ############
// ###########################################
{
/**
* This function gets the callbacks from wsHandlerMap[service][method] and executes them.
* @param {string} service
* @param {string} method
* @param {FoE_NETWORK_TYPE} data
*/
function _proxyWsAction(service, method, data) {
const map = wsHandlerMap[service];
if (!map) {
return;
}
const list = map[method];
if (!list) {
return;
}
for (let callback of list) {
try {
callback(data);
} catch (e) {
console.error(e);
}
}
}

/**
* This function gets the callbacks from wsHandlerMap[service][method],wsHandlerMap[service]['all'],wsHandlerMap['all'][method] and wsHandlerMap['all']['all'] and executes them.
* @param {string} service
* @param {string} method
* @param {FoE_NETWORK_TYPE} data
*/
function proxyWsAction(service, method, data) {
_proxyWsAction(service, method, data);
_proxyWsAction('all', method, data);
_proxyWsAction(service, 'all', data);
_proxyWsAction('all', 'all', data);
}

const oldWSSend = WebSocket.prototype.send;
/**
* @this {WebSocket}
* @param {MessageEvent} evt
*/
function wsMessageCollector(evt) {
try {
if (evt.data === 'PONG') return;
/** @type {FoE_NETWORK_TYPE[]|FoE_NETWORK_TYPE} */
const data = JSON.parse(evt.data);

// do raw-ws-handlers
for (let callback of wsRawHandler) {
try {
callback(data);
} catch (e) {
console.error(e);
}
}

// do ws-handlers
if (data instanceof Array) {
for (let entry of data) {
proxyWsAction(entry.requestClass, entry.requestMethod, entry);
}
} else if (data.__class__ === "ServerResponse") {
proxyWsAction(data.requestClass, data.requestMethod, data);
}
} catch (e) {
console.error(e);
}
}

WebSocket.prototype.send = function (data) {
oldWSSend.call(this, data);
this.addEventListener('message', wsMessageCollector, {capture: false, passive: true});
this.send = oldWSSend;
};
}

// ###########################################
// ################# XHR-Proxy ###############
// ###########################################

/**
* This function gets the callbacks from proxyMap[service][method] and executes them.
Expand Down Expand Up @@ -268,9 +427,9 @@ const FoEproxy = (function () {

// die Gebäudenamen übernehmen
FoEproxy.addMetaHandler('city_entities', (xhr, postData) => {
BuildingNamesi18n = [];
const BuildingNamesi18n = [];

let j = JSON.parse(xhr.responseText);
const j = JSON.parse(xhr.responseText);

sessionStorage.setItem('BuildingsData', JSON.stringify(j));

Expand Down Expand Up @@ -488,7 +647,7 @@ const FoEproxy = (function () {
// lgUpdateData sammelt die informationen aus mehreren Handlern
let lgUpdateData = null;

FoEproxy.addHandler('GreatBuildingsService', (data, postData) => {
FoEproxy.addHandler('GreatBuildingsService', 'all', (data, postData) => {
let getConstruction = data.requestMethod === 'getConstruction' ? data : null;
let getConstructionRanking = data.requestMethod === 'getConstructionRanking' ? data : null;
let contributeForgePoints = data.requestMethod === 'contributeForgePoints' ? data : null;
Expand Down Expand Up @@ -602,7 +761,9 @@ const FoEproxy = (function () {
// --------------------------------------------------------------------------------------------------
// Tavernenboost wurde gekauft
FoEproxy.addHandler('BoostService', 'addBoost', (data, postData) => {
Tavern.TavernBoost(data.responseData);
if (data.responseData['type'] === 'extra_negotiation_turn') {
Tavern.SetExpireTime(data.responseData['expireTime']);
}
});


Expand Down Expand Up @@ -1384,6 +1545,10 @@ let MainParser = {
{
MainParser.AllBoosts[d[i]['type']] += d[i]['value']
}

if (d[i]['type'] === 'extra_negotiation_turn') {
Tavern.SetExpireTime(d[i]['expireTime']);
}
}
}
},
Expand Down Expand Up @@ -1557,14 +1722,14 @@ let MainParser = {
for(let k in d['teasers']){

if(!d['teasers'].hasOwnProperty(k)){
break;
continue;
}

// prüfen ob es zur ID einen key gibt
let key = MainParser.Conversations.findIndex((obj)=> (obj.id === d['teasers'][k]['id']));

// Konversation gibt es schon
if(key !== undefined){
if(key !== -1){
MainParser.Conversations[key]['title'] = d['teasers'][k]['title'];
}
// ... gibt es noch nicht
Expand All @@ -1581,12 +1746,12 @@ let MainParser = {

for(let k in d){
if(!d.hasOwnProperty(k)){
break;
continue;
}

let key = MainParser.Conversations.findIndex((obj)=> (obj.id === d[k]['id']));

if(key !== undefined) {
if(key !== -1) {
MainParser.Conversations[key]['title'] = d[k]['title'];

} else {
Expand Down
27 changes: 7 additions & 20 deletions js/web/guildfights/js/guildfights.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,28 +73,15 @@ let GildFights = {
init: ()=> {

if(GildFights.InjectionLoaded === false){
WebSocket.prototype._send = WebSocket.prototype.send;
FoEproxy.addWsHandler('GuildBattlegroundService', 'all', data => {
if ($('#LiveGildFighting').length > 0) {

WebSocket.prototype.send = function (data) {

this._send(data);

this.addEventListener('message', function(msg) {
if(msg.data !== 'PONG' && $('#LiveGildFighting').length > 0){
let d = JSON.parse(msg.data)[0];

if(d['requestClass'] === 'GuildBattlegroundService' && d['responseData'][0] !== undefined){
// console.log('msg', d['responseData'][0]);
GildFights.RefreshTable(d['responseData'][0]);
}
if(data.responseData[0] !== undefined){
// console.log('msg', data.responseData[0]);
GildFights.RefreshTable(data.responseData[0]);
}

}, false);

this.send = function (data) {
this._send(data);
};
};
}
});

GildFights.InjectionLoaded = true;
}
Expand Down
Loading

0 comments on commit c03b233

Please sign in to comment.