1
0
Fork 0

Fixed AI spawning a new web worker at each turn (thus downloading the app again and again)

This commit is contained in:
Michaël Lemaire 2018-03-06 18:41:38 +01:00
parent a2e40d2b91
commit b4fff901cd
4 changed files with 30 additions and 11 deletions

View file

@ -32,6 +32,7 @@ Character sheet
Battle Battle
------ ------
* Fix stats only filling for one fleet
* Add a voluntary retreat option * Add a voluntary retreat option
* Toggle bar/text display in power section of action bar * Toggle bar/text display in power section of action bar
* Show a cooldown indicator on move action icon, if the simulation would cause the engine to overheat * Show a cooldown indicator on move action icon, if the simulation would cause the engine to overheat
@ -67,6 +68,7 @@ Ships models and actions
Artificial Intelligence Artificial Intelligence
----------------------- -----------------------
* If web worker is not responsive, or produces only errors, it should be disabled for the session
* Produce interesting "angle" areas * Produce interesting "angle" areas
* Evaluate active effects * Evaluate active effects
* Account for luck * Account for luck

View file

@ -19,5 +19,5 @@ onmessage = function (e) {
debug("[AI Worker] Send", maneuver); debug("[AI Worker] Send", maneuver);
postMessage(serializer.serialize(maneuver)); postMessage(serializer.serialize(maneuver));
return maneuver.apply(battle); return maneuver.apply(battle);
}).catch(postMessage).then(close); }).catch(postMessage);
} }

View file

@ -97,7 +97,7 @@ module TK.SpaceTac {
} else { } else {
this.update(-1); this.update(-1);
} }
this.scheduled = Timer.global.schedule(100, () => this.next()); this.scheduled = Timer.global.schedule(50, () => this.next());
} }
/** /**

View file

@ -1,4 +1,20 @@
module TK.SpaceTac { module TK.SpaceTac {
/**
* Initialize the background worker, if possible
*/
function initializeWorker(): Worker | null {
if (typeof window != "undefined" && (<any>window).Worker) {
try {
return new Worker('aiworker.js'); // TODO not hard-coded
} catch {
console.error("Could not initialize AI web worker");
return null;
}
} else {
return null;
}
}
/** /**
* AI processing, either in the current process or in a web worker * AI processing, either in the current process or in a web worker
*/ */
@ -6,6 +22,7 @@ module TK.SpaceTac {
private battle: Battle; private battle: Battle;
private ship: Ship; private ship: Ship;
private debug: boolean; private debug: boolean;
private static worker = initializeWorker();
constructor(battle: Battle, debug = false) { constructor(battle: Battle, debug = false) {
this.battle = battle; this.battle = battle;
@ -27,8 +44,13 @@ module TK.SpaceTac {
* Process AI in a webworker if possible, else do the work in the render thread * Process AI in a webworker if possible, else do the work in the render thread
*/ */
async processAuto(feedback: AIFeedback): Promise<void> { async processAuto(feedback: AIFeedback): Promise<void> {
if (!this.debug && (<any>window).Worker) { if (!this.debug && AIWorker.worker) {
await this.processInWorker(feedback); try {
await this.processInWorker(AIWorker.worker, feedback);
} catch (err) {
console.error("Web worker error, falling back to main thread", err);
await this.processHere(feedback);
}
} else { } else {
await this.processHere(feedback); await this.processHere(feedback);
} }
@ -37,14 +59,10 @@ module TK.SpaceTac {
/** /**
* Process AI in a webworker * Process AI in a webworker
*/ */
async processInWorker(feedback: AIFeedback): Promise<void> { async processInWorker(worker: Worker, feedback: AIFeedback): Promise<void> {
let worker = new Worker('aiworker.js'); // TODO not hard-coded
let serializer = new Serializer(TK.SpaceTac); let serializer = new Serializer(TK.SpaceTac);
let promise = new Promise((resolve, reject) => { let promise = new Promise((resolve, reject) => {
worker.onerror = (error) => { worker.onerror = reject;
worker.terminate();
reject(error);
};
worker.onmessage = (message) => { worker.onmessage = (message) => {
let maneuver = serializer.unserialize(message.data); let maneuver = serializer.unserialize(message.data);
if (maneuver instanceof Maneuver) { if (maneuver instanceof Maneuver) {
@ -56,7 +74,6 @@ module TK.SpaceTac {
resolve(); resolve();
} }
} else { } else {
worker.terminate();
reject("Received something that is not a Maneuver"); reject("Received something that is not a Maneuver");
} }
}; };