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 () { ...@@ -61,14 +61,12 @@ var GameManager = /** @class */ (function () {
GameManager.prototype.run = function () { GameManager.prototype.run = function () {
var gadget = this; var gadget = this;
return gadget._init(); return gadget._init()
}; .push(function () {
//TODO return result
GameManager.prototype.event = function (event) { gadget._final_score = "fake-result 000";
var _this = this; return gadget._final_score;
//TODO });
console.log("[GM] Event. this._camera:", this._camera);
console.log("[GM] Event. event:", event);
}; };
GameManager.prototype.update = function () { GameManager.prototype.update = function () {
...@@ -90,7 +88,11 @@ var GameManager = /** @class */ (function () { ...@@ -90,7 +88,11 @@ var GameManager = /** @class */ (function () {
_this.ongoing_update_promise = null; _this.ongoing_update_promise = null;
triggerUpdateIfPossible(); 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(); triggerUpdateIfPossible();
...@@ -202,12 +204,23 @@ var GameManager = /** @class */ (function () { ...@@ -202,12 +204,23 @@ var GameManager = /** @class */ (function () {
}); });
return queue return queue
//TODO finish .push(function () {
/*.push(function () { if (_this._timeOut()) {
if (_this._allDroneAreOut()) { console.log("TIMEOUT!");
return _this._finish(); 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 () { GameManager.prototype._dispose = function () {
...@@ -392,7 +405,6 @@ var GameManager = /** @class */ (function () { ...@@ -392,7 +405,6 @@ var GameManager = /** @class */ (function () {
// Timing // Timing
this._game_duration = 0; this._game_duration = 0;
this._totalTime = GAMEPARAMETERS.gameTime; this._totalTime = GAMEPARAMETERS.gameTime;
this._canUpdate = true;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
...@@ -408,34 +420,7 @@ var GameManager = /** @class */ (function () { ...@@ -408,34 +420,7 @@ var GameManager = /** @class */ (function () {
return RSVP.all(promise_list); return RSVP.all(promise_list);
}) })
.push(function () { .push(function () {
//The loop is handle from the outside (webworker) _this._canUpdate = true;
//_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();
return _this.finish_deferred.promise; return _this.finish_deferred.promise;
}); });
}; };
......
...@@ -10,11 +10,8 @@ ...@@ -10,11 +10,8 @@
<key> <string>_Access_contents_information_Permission</string> </key> <key> <string>_Access_contents_information_Permission</string> </key>
<value> <value>
<tuple> <tuple>
<string>Anonymous</string>
<string>Assignee</string> <string>Assignee</string>
<string>Assignor</string> <string>Assignor</string>
<string>Associate</string>
<string>Auditor</string>
<string>Manager</string> <string>Manager</string>
</tuple> </tuple>
</value> </value>
...@@ -52,11 +49,8 @@ ...@@ -52,11 +49,8 @@
<key> <string>_View_Permission</string> </key> <key> <string>_View_Permission</string> </key>
<value> <value>
<tuple> <tuple>
<string>Anonymous</string>
<string>Assignee</string> <string>Assignee</string>
<string>Assignor</string> <string>Assignor</string>
<string>Associate</string>
<string>Auditor</string>
<string>Manager</string> <string>Manager</string>
</tuple> </tuple>
</value> </value>
...@@ -169,7 +163,7 @@ ...@@ -169,7 +163,7 @@
<dictionary> <dictionary>
<item> <item>
<key> <string>action</string> </key> <key> <string>action</string> </key>
<value> <string>publish_alive</string> </value> <value> <string>hide</string> </value>
</item> </item>
<item> <item>
<key> <string>actor</string> </key> <key> <string>actor</string> </key>
...@@ -195,7 +189,7 @@ ...@@ -195,7 +189,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1662555757.05</float> <float>1664290777.68</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
...@@ -204,7 +198,7 @@ ...@@ -204,7 +198,7 @@
</item> </item>
<item> <item>
<key> <string>validation_state</string> </key> <key> <string>validation_state</string> </key>
<value> <string>published_alive</string> </value> <value> <string>hidden</string> </value>
</item> </item>
</dictionary> </dictionary>
</list> </list>
...@@ -244,7 +238,7 @@ ...@@ -244,7 +238,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>1003.14332.42567.56439</string> </value> <value> <string>1003.14747.6204.5393</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -262,7 +256,7 @@ ...@@ -262,7 +256,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1664202073.56</float> <float>1664290605.07</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -212,8 +212,6 @@ var runGame, updateGame, eventGame, game_manager_instance; ...@@ -212,8 +212,6 @@ var runGame, updateGame, eventGame, game_manager_instance;
game_parameters_json.randomSpawn.rightTeam.position.y = destination[1]; game_parameters_json.randomSpawn.rightTeam.position.y = destination[1];
return game_parameters_json; return game_parameters_json;
} }
console.log('runGame', canvas);
game_parameters_json = processLog(game_parameters_json, log); game_parameters_json = processLog(game_parameters_json, log);
if (!game_manager_instance) { if (!game_manager_instance) {
game_manager_instance = new GameManager(canvas, script, game_manager_instance = new GameManager(canvas, script,
...@@ -223,11 +221,9 @@ var runGame, updateGame, eventGame, game_manager_instance; ...@@ -223,11 +221,9 @@ var runGame, updateGame, eventGame, game_manager_instance;
}; };
updateGame = function () { updateGame = function () {
if (game_manager_instance) {
return game_manager_instance.update(); return game_manager_instance.update();
}; }
eventGame = function (event) {
return game_manager_instance.event(event);
}; };
/*// Resize canvas on window resize /*// Resize canvas on window resize
......
...@@ -244,7 +244,7 @@ ...@@ -244,7 +244,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>1003.8464.37017.2594</string> </value> <value> <string>1003.14747.21196.46916</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -262,7 +262,7 @@ ...@@ -262,7 +262,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1663849971.49</float> <float>1664291458.36</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -7,51 +7,15 @@ ...@@ -7,51 +7,15 @@
// game.js // game.js
(function (RSVP, requestAnimationFrame, cancelAnimationFrame) { (function (RSVP, requestAnimationFrame, cancelAnimationFrame) {
"use strict"; "use strict";
console.log('game');
// Events props to send to worker // Events props to send to worker
const mouseEventFields = new Set([ const mouseEventFields = new Set(['altKey', 'bubbles', 'button', 'buttons',
'altKey', 'cancelBubble', 'cancelable', 'clientX', 'clientY', 'composed', 'ctrlKey',
'bubbles', 'defaultPrevented', 'detail', 'eventPhase', 'fromElement', 'isTrusted',
'button', 'layerX', 'layerY', 'metaKey', 'movementX', 'movementY', 'offsetX', 'pageX',
'buttons', 'offsetY', 'pageY', 'relatedTarget', 'returnValue', 'screenX', 'screenY',
'cancelBubble', 'shiftKey', 'timeStamp', 'type', 'which', 'x', 'wheelDelta', 'wheelDeltaX',
'cancelable', 'wheelDeltaY', 'y', 'deltaX', 'deltaY', 'deltaZ', 'deltaMode',
'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',
]); ]);
////////////////////////////////////////// //////////////////////////////////////////
...@@ -172,18 +136,8 @@ ...@@ -172,18 +136,8 @@
worker.onmessage = workerToMain; worker.onmessage = workerToMain;
// Always quit the game when the worker callback usage is over // Always quit the game when the worker callback usage is over
// to prevent trying to call pause // to prevent trying to call pause
//context.quit();
return message_error_handler_defer.promise; 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) { function workerToMain(evt) {
switch (evt.data.type) { switch (evt.data.type) {
case 'loaded': case 'loaded':
...@@ -206,6 +160,10 @@ ...@@ -206,6 +160,10 @@
case 'updated': case 'updated':
return update_defer.resolve('updated'); return update_defer.resolve('updated');
break; break;
case 'finished':
console.log('GAME: finished');
return context.quit();
break;
case 'event': case 'event':
bindEvent(evt.data); bindEvent(evt.data);
break; break;
...@@ -215,6 +173,9 @@ ...@@ -215,6 +173,9 @@
case 'canvasStyle': case 'canvasStyle':
options.canvas_original.style[evt.data.name] = evt.data.value; options.canvas_original.style[evt.data.name] = evt.data.value;
break; break;
case 'error':
message_error_handler_defer.reject(evt.data.error);
break;
default: default:
message_error_handler_defer.reject( message_error_handler_defer.reject(
new Error('Unsupported message ' + JSON.stringify(evt.data)) new Error('Unsupported message ' + JSON.stringify(evt.data))
...@@ -260,10 +221,8 @@ ...@@ -260,10 +221,8 @@
// We can`t pass original event to the worker // We can`t pass original event to the worker
const eventClone = cloneEvent(e); const eventClone = cloneEvent(e);
if (eventClone.type === "pointerout") { if (eventClone.type === "pointerout") {
console.log("ignoring pointerout event");
return; return;
} }
console.log("[MAIN][LISTENER] event(cloned)-target:", data.eventName, data.targetName);
worker.postMessage({ worker.postMessage({
type: 'event', type: 'event',
targetName: data.targetName, targetName: data.targetName,
...@@ -302,7 +261,6 @@ ...@@ -302,7 +261,6 @@
// droneaailefixe.js // droneaailefixe.js
(function () { (function () {
"use strict"; "use strict";
//console.log('droneaailefixe');
}()); }());
// page gadget.js // page gadget.js
...@@ -343,7 +301,7 @@ ...@@ -343,7 +301,7 @@
}) })
.push(function (parameters_doc) { .push(function (parameters_doc) {
game_parameters_json = JSON.parse(parameters_doc.text_content); 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) { .push(function (log) {
log_content = log.text_content; log_content = log.text_content;
......
...@@ -244,7 +244,7 @@ ...@@ -244,7 +244,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>1003.10118.17019.16759</string> </value> <value> <string>1003.15820.63775.27392</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -262,7 +262,7 @@ ...@@ -262,7 +262,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1663954426.68</float> <float>1664374475.4</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </state>
......
...@@ -12,9 +12,6 @@ ...@@ -12,9 +12,6 @@
/**************************** ROQUE WW EVENTS ****************************/ /**************************** ROQUE WW EVENTS ****************************/
/*************************************************************************/ /*************************************************************************/
console.log("WITH EVENTS");
self.window = { self.window = {
addEventListener: function (event, fn, opt) { addEventListener: function (event, fn, opt) {
bindHandler('window', event, fn, opt); bindHandler('window', event, fn, opt);
...@@ -52,11 +49,15 @@ function mainToWorker(evt) { ...@@ -52,11 +49,15 @@ function mainToWorker(evt) {
RSVP = window.RSVP; RSVP = window.RSVP;
return new RSVP.Queue() return new RSVP.Queue()
.push(function () { .push(function () {
postMessage({'type': 'started'});
return runGame(offscreen_canvas, evt.data.script, return runGame(offscreen_canvas, evt.data.script,
evt.data.game_parameters_json, evt.data.log); evt.data.game_parameters_json, evt.data.log);
}) })
.push(function () { .push(function (result) {
return postMessage({'type': 'started'}); return postMessage({'type': 'finished', 'result': result});
}, function(error) {
console.log("ERROR:", error);
return postMessage({'type': 'error', 'error': error});
}); });
break; break;
case 'update': case 'update':
...@@ -76,98 +77,6 @@ function mainToWorker(evt) { ...@@ -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 // Doesn't work without it
class HTMLElement {} class HTMLElement {}
...@@ -200,7 +109,6 @@ function bindHandler(targetName, eventName, fn, opt) { ...@@ -200,7 +109,6 @@ function bindHandler(targetName, eventName, fn, opt) {
function handleEvent(event) { function handleEvent(event) {
const handlerId = event.targetName + event.eventName; const handlerId = event.targetName + event.eventName;
console.log("[WEBWORKER] handlerId:", handlerId);
event.eventClone.preventDefault = noop; event.eventClone.preventDefault = noop;
event.eventClone.target = self.canvas; event.eventClone.target = self.canvas;
if (!handlers.has(handlerId)) { if (!handlers.has(handlerId)) {
...@@ -252,7 +160,7 @@ function prepareCanvas(data) { ...@@ -252,7 +160,7 @@ function prepareCanvas(data) {
return canvas; return canvas;
} }
function noop() { console.log("noop!"); } function noop() {}
(function (worker) { (function (worker) {
worker.onmessage = mainToWorker; worker.onmessage = mainToWorker;
......
...@@ -244,7 +244,7 @@ ...@@ -244,7 +244,7 @@
</item> </item>
<item> <item>
<key> <string>serial</string> </key> <key> <string>serial</string> </key>
<value> <string>1003.14300.40470.3601</string> </value> <value> <string>1003.15818.58123.3379</string> </value>
</item> </item>
<item> <item>
<key> <string>state</string> </key> <key> <string>state</string> </key>
...@@ -262,7 +262,7 @@ ...@@ -262,7 +262,7 @@
</tuple> </tuple>
<state> <state>
<tuple> <tuple>
<float>1664202107.66</float> <float>1664291386.25</float>
<string>UTC</string> <string>UTC</string>
</tuple> </tuple>
</state> </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