Commit c1712536 authored by Roque's avatar Roque

[WEB-WORKER] [WIP] Drone simulator refactor

- handle game finish
- handle web worker erros
- update flight log
- cleanup
parent b9ba1073
......@@ -61,14 +61,12 @@ var GameManager = /** @class */ (function () {
GameManager.prototype.run = function () {
var gadget = this;
return gadget._init();
};
GameManager.prototype.event = function (event) {
var _this = this;
//TODO
console.log("[GM] Event. this._camera:", this._camera);
console.log("[GM] Event. event:", event);
return gadget._init()
.push(function () {
//TODO return result
gadget._final_score = "fake-result 000";
return gadget._final_score;
});
};
GameManager.prototype.update = function () {
......@@ -90,7 +88,11 @@ var GameManager = /** @class */ (function () {
_this.ongoing_update_promise = null;
triggerUpdateIfPossible();
})
.push(undefined, _this.finish_deferred.reject.bind(_this.finish_deferred));
.push(undefined, function(error) {
console.log("ERROR on update:", error);
console.log("rejecting finish_deferred promise...");
_this.finish_deferred.reject.bind(_this.finish_deferred);
});
}
}
triggerUpdateIfPossible();
......@@ -202,12 +204,23 @@ var GameManager = /** @class */ (function () {
});
return queue
//TODO finish
/*.push(function () {
if (_this._allDroneAreOut()) {
.push(function () {
if (_this._timeOut()) {
console.log("TIMEOUT!");
return _this._finish();
}
})*/;
});
};
GameManager.prototype._timeOut = function () {
var seconds = Math.floor(this._game_duration / 1000);
return this._totalTime - seconds <= 0;
};
GameManager.prototype._finish = function () {
console.log("Simulation finished");
this._canUpdate = false;
return this.finish_deferred.resolve();
};
GameManager.prototype._dispose = function () {
......@@ -392,7 +405,6 @@ var GameManager = /** @class */ (function () {
// Timing
this._game_duration = 0;
this._totalTime = GAMEPARAMETERS.gameTime;
this._canUpdate = true;
return new RSVP.Queue()
.push(function () {
......@@ -408,34 +420,7 @@ var GameManager = /** @class */ (function () {
return RSVP.all(promise_list);
})
.push(function () {
//The loop is handle from the outside (webworker)
//_this._scene.registerBeforeRender(function () { console.log("loop"); });
/*_this._scene.registerBeforeRender(function () {
// To increase the game speed, increase this value
_this._max_step_animation_frame = 10;
// time delta means that drone are updated every virtual second
// This is fixed and must not be modified
// otherwise, it will lead to different scenario results
// (as drone calculations may be triggered less often)
var TIME_DELTA = 1000 / 60, i;
// init the value on the first step
_this.waiting_update_count = _this._max_step_animation_frame;
function triggerUpdateIfPossible() {
if ((_this._canUpdate) && (_this.ongoing_update_promise === null) && (0 < _this.waiting_update_count)) {
_this.ongoing_update_promise = _this._update(TIME_DELTA, (_this.waiting_update_count === 1))
.push(function () {
_this.waiting_update_count -= 1;
_this.ongoing_update_promise = null;
triggerUpdateIfPossible();
})
.push(undefined, _this.finish_deferred.reject.bind(_this.finish_deferred));
}
}
triggerUpdateIfPossible();
});*/
//TODO solving promise so game-start finishes and web worker can update
_this.finish_deferred.resolve();
_this._canUpdate = true;
return _this.finish_deferred.promise;
});
};
......
......@@ -10,11 +10,8 @@
<key> <string>_Access_contents_information_Permission</string> </key>
<value>
<tuple>
<string>Anonymous</string>
<string>Assignee</string>
<string>Assignor</string>
<string>Associate</string>
<string>Auditor</string>
<string>Manager</string>
</tuple>
</value>
......@@ -52,11 +49,8 @@
<key> <string>_View_Permission</string> </key>
<value>
<tuple>
<string>Anonymous</string>
<string>Assignee</string>
<string>Assignor</string>
<string>Associate</string>
<string>Auditor</string>
<string>Manager</string>
</tuple>
</value>
......@@ -169,7 +163,7 @@
<dictionary>
<item>
<key> <string>action</string> </key>
<value> <string>publish_alive</string> </value>
<value> <string>hide</string> </value>
</item>
<item>
<key> <string>actor</string> </key>
......@@ -195,7 +189,7 @@
</tuple>
<state>
<tuple>
<float>1662555757.05</float>
<float>1664290777.68</float>
<string>UTC</string>
</tuple>
</state>
......@@ -204,7 +198,7 @@
</item>
<item>
<key> <string>validation_state</string> </key>
<value> <string>published_alive</string> </value>
<value> <string>hidden</string> </value>
</item>
</dictionary>
</list>
......@@ -244,7 +238,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>1003.14332.42567.56439</string> </value>
<value> <string>1003.14747.6204.5393</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -262,7 +256,7 @@
</tuple>
<state>
<tuple>
<float>1664202073.56</float>
<float>1664290605.07</float>
<string>UTC</string>
</tuple>
</state>
......
......@@ -212,8 +212,6 @@ var runGame, updateGame, eventGame, game_manager_instance;
game_parameters_json.randomSpawn.rightTeam.position.y = destination[1];
return game_parameters_json;
}
console.log('runGame', canvas);
game_parameters_json = processLog(game_parameters_json, log);
if (!game_manager_instance) {
game_manager_instance = new GameManager(canvas, script,
......@@ -223,11 +221,9 @@ var runGame, updateGame, eventGame, game_manager_instance;
};
updateGame = function () {
return game_manager_instance.update();
};
eventGame = function (event) {
return game_manager_instance.event(event);
if (game_manager_instance) {
return game_manager_instance.update();
}
};
/*// Resize canvas on window resize
......
......@@ -244,7 +244,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>1003.8464.37017.2594</string> </value>
<value> <string>1003.14747.21196.46916</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -262,7 +262,7 @@
</tuple>
<state>
<tuple>
<float>1663849971.49</float>
<float>1664291458.36</float>
<string>UTC</string>
</tuple>
</state>
......
......@@ -7,51 +7,15 @@
// game.js
(function (RSVP, requestAnimationFrame, cancelAnimationFrame) {
"use strict";
console.log('game');
// Events props to send to worker
const mouseEventFields = new Set([
'altKey',
'bubbles',
'button',
'buttons',
'cancelBubble',
'cancelable',
'clientX',
'clientY',
'composed',
'ctrlKey',
'defaultPrevented',
'detail',
'eventPhase',
'fromElement',
'isTrusted',
'layerX',
'layerY',
'metaKey',
'movementX',
'movementY',
'offsetX',
'offsetY',
'pageX',
'pageY',
'relatedTarget',
'returnValue',
'screenX',
'screenY',
'shiftKey',
'timeStamp',
'type',
'which',
'x',
'y',
'wheelDelta',
'wheelDeltaX',
'wheelDeltaY',
'deltaX',
'deltaY',
'deltaZ',
'deltaMode',
const mouseEventFields = new Set(['altKey', 'bubbles', 'button', 'buttons',
'cancelBubble', 'cancelable', 'clientX', 'clientY', 'composed', 'ctrlKey',
'defaultPrevented', 'detail', 'eventPhase', 'fromElement', 'isTrusted',
'layerX', 'layerY', 'metaKey', 'movementX', 'movementY', 'offsetX', 'pageX',
'offsetY', 'pageY', 'relatedTarget', 'returnValue', 'screenX', 'screenY',
'shiftKey', 'timeStamp', 'type', 'which', 'x', 'wheelDelta', 'wheelDeltaX',
'wheelDeltaY', 'y', 'deltaX', 'deltaY', 'deltaZ', 'deltaMode',
]);
//////////////////////////////////////////
......@@ -172,18 +136,8 @@
worker.onmessage = workerToMain;
// Always quit the game when the worker callback usage is over
// to prevent trying to call pause
//context.quit();
return message_error_handler_defer.promise;
/*options.canvas_original.addEventListener("mousewheel", (evt) => {
console.log("[MAIN] canvas mousewheel. event:", evt);
const eventClone = cloneEvent(evt);
worker.postMessage({
type: 'mousewheel',
eventClone: eventClone
});
});*/
function workerToMain(evt) {
switch (evt.data.type) {
case 'loaded':
......@@ -206,6 +160,10 @@
case 'updated':
return update_defer.resolve('updated');
break;
case 'finished':
console.log('GAME: finished');
return context.quit();
break;
case 'event':
bindEvent(evt.data);
break;
......@@ -215,6 +173,9 @@
case 'canvasStyle':
options.canvas_original.style[evt.data.name] = evt.data.value;
break;
case 'error':
message_error_handler_defer.reject(evt.data.error);
break;
default:
message_error_handler_defer.reject(
new Error('Unsupported message ' + JSON.stringify(evt.data))
......@@ -260,10 +221,8 @@
// We can`t pass original event to the worker
const eventClone = cloneEvent(e);
if (eventClone.type === "pointerout") {
console.log("ignoring pointerout event");
return;
}
console.log("[MAIN][LISTENER] event(cloned)-target:", data.eventName, data.targetName);
worker.postMessage({
type: 'event',
targetName: data.targetName,
......@@ -302,7 +261,6 @@
// droneaailefixe.js
(function () {
"use strict";
//console.log('droneaailefixe');
}());
// page gadget.js
......@@ -343,7 +301,7 @@
})
.push(function (parameters_doc) {
game_parameters_json = JSON.parse(parameters_doc.text_content);
return gadget.jio_get("rescue_swarm_script_module/" + "lp_loiter");
return gadget.jio_get("rescue_swarm_script_module/" + "log_loiter");
})
.push(function (log) {
log_content = log.text_content;
......
......@@ -244,7 +244,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>1003.10118.17019.16759</string> </value>
<value> <string>1003.15820.63775.27392</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -262,7 +262,7 @@
</tuple>
<state>
<tuple>
<float>1663954426.68</float>
<float>1664374475.4</float>
<string>UTC</string>
</tuple>
</state>
......
......@@ -12,9 +12,6 @@
/**************************** ROQUE WW EVENTS ****************************/
/*************************************************************************/
console.log("WITH EVENTS");
self.window = {
addEventListener: function (event, fn, opt) {
bindHandler('window', event, fn, opt);
......@@ -52,11 +49,15 @@ function mainToWorker(evt) {
RSVP = window.RSVP;
return new RSVP.Queue()
.push(function () {
postMessage({'type': 'started'});
return runGame(offscreen_canvas, evt.data.script,
evt.data.game_parameters_json, evt.data.log);
})
.push(function () {
return postMessage({'type': 'started'});
.push(function (result) {
return postMessage({'type': 'finished', 'result': result});
}, function(error) {
console.log("ERROR:", error);
return postMessage({'type': 'error', 'error': error});
});
break;
case 'update':
......@@ -76,98 +77,6 @@ function mainToWorker(evt) {
}
};
/*****************************************************************************/
/************************************************************************/
/***************************** WORKING GAME *****************************/
/************************************************************************/
/*console.log("WORKING GAME WITH NO EVENTS");
var document = {
addEventListener: function () {},
createElement: function () {}
};
(function (worker) {
importScripts('babylon.js', 'babylon.gui.js');
}(this));
var window = {
addEventListener: function (event, fn, opt) {
bindHandler('window', event, fn, opt);
},
PointerEvent: true
};
document = {
addEventListener: function (event, fn, opt) {
bindHandler('document', event, fn, opt);
},
createElement: function () {
return {onwheel: true};
},
defaultView: window
};
function mainToWorker(evt) {
switch (evt.data.type) {
case 'start':
var offscreen_canvas = prepareCanvas(evt.data);
//override createElement as it is needed by babylon to create a canvas
document.createElement = function (type) {
if (type === 'canvas') { return offscreen_canvas; }
return { onwheel: true };
}
//TODO evt.data.logic_url should contain the list of scripts
importScripts(
'rsvp.js',
'GameManager.js',
'DroneManager.js',
'MapManager.js',
'ObstacleManager.js',
'DroneAaileFixeAPI.js',
'DroneLogAPI.js',
'DroneAPI.js',
evt.data.logic_url);
RSVP = window.RSVP;
window = undefined;
return new RSVP.Queue()
.push(function () {
return runGame(offscreen_canvas, evt.data.script,
evt.data.game_parameters_json, evt.data.log);
})
.push(function () {
return postMessage({'type': 'started'});
});
break;
case 'update':
return new RSVP.Queue()
.push(function () {
return updateGame();
})
.push(function () {
return postMessage({'type': 'updated'});
});
break;
case 'event':
handleEvent(evt.data);
break;
//case 'mousewheel':
// eventGame(evt.data.eventClone);
// break;
default:
throw new Error('Unsupported message ' + JSON.stringify(evt.data));
}
};*/
/*****************************************************************************/
// Doesn't work without it
class HTMLElement {}
......@@ -200,7 +109,6 @@ function bindHandler(targetName, eventName, fn, opt) {
function handleEvent(event) {
const handlerId = event.targetName + event.eventName;
console.log("[WEBWORKER] handlerId:", handlerId);
event.eventClone.preventDefault = noop;
event.eventClone.target = self.canvas;
if (!handlers.has(handlerId)) {
......@@ -252,7 +160,7 @@ function prepareCanvas(data) {
return canvas;
}
function noop() { console.log("noop!"); }
function noop() {}
(function (worker) {
worker.onmessage = mainToWorker;
......
......@@ -244,7 +244,7 @@
</item>
<item>
<key> <string>serial</string> </key>
<value> <string>1003.14300.40470.3601</string> </value>
<value> <string>1003.15818.58123.3379</string> </value>
</item>
<item>
<key> <string>state</string> </key>
......@@ -262,7 +262,7 @@
</tuple>
<state>
<tuple>
<float>1664202107.66</float>
<float>1664291386.25</float>
<string>UTC</string>
</tuple>
</state>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment