]> git.r.bdr.sh - rbdr/sumo/blame_incremental - lib/systems/detect_winner.js
Merge branch 'feature/rbdr-gitlab-pipeline' into 'develop'
[rbdr/sumo] / lib / systems / detect_winner.js
... / ...
CommitLineData
1import { System } from '@serpentity/serpentity';
2
3import WinnerNode from '../nodes/winner';
4import PointsNode from '../nodes/points';
5
6import Config from '../config';
7
8/**
9 * Handles finding the winner
10 *
11 * @extends {external:Serpentity.System}
12 * @class DetectWinnerSystem
13 * @param {object} config a configuration object to extend.
14 */
15export default class DetectWinnerSystem extends System {
16
17 constructor(config = {}) {
18
19 super();
20
21 /**
22 * The collection of points keepers
23 *
24 * @property {external:Serpentity.NodeCollection} pointsKeepers
25 * @instance
26 * @memberof DetectWinnerSystem
27 */
28 this.pointsKeepers = null;
29
30 /**
31 * The collection of winner keepers
32 *
33 * @property {external:Serpentity.NodeCollection} winnerKeepers
34 * @instance
35 * @memberof DetectWinnerSystem
36 */
37 this.winnerKeepers = null;
38 }
39
40 /**
41 * Initializes system when added. Requests points and winner keepers
42 *
43 * @function added
44 * @memberof DetectWinnerSystem
45 * @instance
46 * @param {external:Serpentity.Engine} engine the serpentity engine to
47 * which we are getting added
48 */
49 added(engine) {
50
51 this.pointsKeepers = engine.getNodes(PointsNode);
52 this.winnerKeepers = engine.getNodes(WinnerNode);
53 }
54
55 /**
56 * Clears system resources when removed.
57 *
58 * @function removed
59 * @instance
60 * @memberof DetectWinnerSystem
61 */
62 removed() {
63
64 this.pointsKeepers = null;
65 this.winnerKeepers = null;
66 }
67
68 /**
69 * Runs on every update of the loop. Checks if someone won
70 *
71 * @function update
72 * @instance
73 * @param {Number} currentFrameDuration the duration of the current
74 * frame
75 * @memberof DetectWinnerSystem
76 */
77 update(currentFrameDuration) {
78
79 let winner = null;
80
81 for (const pointsKeeper of this.pointsKeepers) {
82 for (const pointsCandidate of Object.keys(pointsKeeper.points)) {
83 if (pointsKeeper.points[pointsCandidate] >= Config.maxPoints) {
84 winner = pointsCandidate;
85 break;
86 }
87 }
88 }
89
90 if (!winner) {
91 return;
92 }
93
94 for (const winnerKeeper of this.winnerKeepers) {
95
96 // Only one winner, another system should reset
97 if (!winnerKeeper.winner.winner) {
98 winnerKeeper.winner.winner = winner;
99 }
100
101 }
102 }
103};
104