-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.js
81 lines (73 loc) · 2.07 KB
/
queue.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
74
75
76
77
78
79
80
81
const { logger } = require("./log");
const guesserQueue = [];
const enemyQueue = [];
const flexQueue = [];
const getState = function (snowflake) {
if (guesserQueue.includes(snowflake)) {
return { side: "guesser" };
}
if (enemyQueue.includes(snowflake)) {
return { side: "enemy" };
}
if (flexQueue.includes(snowflake)) {
return { side: "flex" };
}
return false;
};
const enterQueue = function (snowflake, side) {
if (guesserQueue.includes(snowflake) || enemyQueue.includes(snowflake)) {
logger.error(`[enter-queue] ${snowflake} is already queueing!`);
return;
}
switch (side) {
case "guesser":
guesserQueue.push(snowflake);
break;
case "enemy":
enemyQueue.push(snowflake);
break;
default:
flexQueue.push(snowflake);
}
logger.info(`[enter-queue] ${snowflake} entered the ${side} queue.`);
};
const leaveQueue = function (snowflake) {
let i = guesserQueue.findIndex((ticket) => ticket === snowflake);
if (i > -1) {
guesserQueue.splice(i, 1);
logger.info(`[leaveQueue] ${snowflake} left the guesser queue.`);
}
i = enemyQueue.findIndex((ticket) => ticket === snowflake);
if (i > -1) {
enemyQueue.splice(i, 1);
logger.info(`[leaveQueue] ${snowflake} left the enemy queue.`);
}
i = flexQueue.findIndex((ticket) => ticket === snowflake);
if (i > -1) {
flexQueue.splice(i, 1);
logger.info(`[leaveQueue] ${snowflake} left the flex queue.`);
}
};
const matchMake = function () {
logger.info("[matchMake] Serving the queue!");
if (flexQueue.length >= 2) {
return { guesser: flexQueue.shift(), enemy: flexQueue.shift() };
}
if (flexQueue.length == 1) {
if (enemyQueue.length) {
return { guesser: flexQueue.shift(), enemy: enemyQueue.shift() };
}
if (guesserQueue.length) {
return { guesser: guesserQueue.shift(), enemy: flexQueue.shift() };
}
}
if (guesserQueue.length && enemyQueue.length) {
return { guesser: guesserQueue.shift(), enemy: enemyQueue.shift() };
}
};
module.exports = {
getState,
enterQueue,
leaveQueue,
matchMake,
};