This repository has been archived by the owner on Oct 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstanceManager.js
73 lines (65 loc) · 2.4 KB
/
InstanceManager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
const path = require('path');
const Instance = require(path.resolve('Instance'));
const UI = require(path.resolve('UI'));
const EventEmitter = require('events');
class InstanceManager extends EventEmitter {
constructor(api, ui, autoChallengeAI, validTimeControls, enginePath) {
super();
this.api = api;
this.ui = ui;
this.autoChallengeAI = autoChallengeAI;
this.validTimeControls = validTimeControls;
this.enginePath = enginePath;
this.instances = [];
this.on('instanceStateUpdate', () => {
this.ui.display(this.instances);
});
if (autoChallengeAI) {
if (this.instances.length == 0)
api.challengeAI();
} else
ui.display(this.instances);
}
handleEvent(event) {
switch (event.type) {
case 'challenge':
this.validateChallenge(event.challenge)
break;
case 'gameStart':
//console.log(`Starting Instance: ${event.game.id}`);
const instance = new Instance(this.api, event.game.id, this, this.enginePath);
instance.start();
this.instances.push(instance);
this.ui.display(this.instances);
break;
case 'gameFinish':
this.instances.pop(this.instances.filter(item => item.instanceID == event.game.id));
this.ui.display(this.instances);
if (this.autoChallengeAI)
if (this.instances.length == 0)
this.api.challengeAI();
break;
}
}
// check that it meets the requirements
validateChallenge(challenge) {
if (challenge.variant.key != 'standard') {
console.log(`Declining Challenge: ${challenge.id}`);
this.api.declineChallenge(challenge.id, 'standard');
return;
}
if (this.validTimeControls.indexOf(challenge.speed) !== -1) {
console.log(`Accepting Challenge: ${challenge.id}`);
this.api.acceptChallenge(challenge.id);
return;
} else {
console.log(`Declining Challenge: ${challenge.id}`);
this.api.declineChallenge(challenge.id, 'timeControl');
return;
}
}
updateInstance() {
this.ui.display(this.instances);
}
}
module.exports = InstanceManager;