-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaction.js
292 lines (277 loc) · 8.59 KB
/
faction.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//third party lib dependencies
import "dotenv/config"
import AsciiTable from "ascii-table"
//globals to name the Teams, and set Discord Channes
const colors = ["blue", "green", "red", "yellow"]
const teamNames = ["Team Blue", "Team Green", " Team Red", "Team Yellow"]
const teamHeaders = [
`:large_blue_diamond: ${teamNames[0]}: `,
`:green_square: ${teamNames[1]}:`,
`:heart: ${teamNames[2]}: `,
`:yellow_circle: ${teamNames[3]}`,
]
const discordChannels = [
"https://discord.com/channels/965039544183431188/965077230952804362",
"https://discord.com/channels/965039544183431188/965078950449647686",
"https://discord.com/channels/965039544183431188/965078506759389194",
"https://discord.com/channels/965039544183431188/967949179328544819",
]
export const roleIds = [
"965071590293393438", // blue
"965071669007908934", // green
"965071787316609045", // red
"967943791644397689", // yellow
]
//creates initial factions objects in an array
export function createFactions(numberOfFactions) {
let factions = []
let max = Math.ceil((60 - numberOfFactions) / numberOfFactions)
for (let i = 0; i < numberOfFactions; i++) {
let faction = {
color: colors[i],
discordChannel: discordChannels[i],
//users are by id, ex: ["109422963136208896", "279052991976308738"],
users: [],
maxUsers: max,
roleId: roleIds[i],
}
factions.push(faction)
}
return factions
}
export function checkSize(faction) {
if (users.length < faction.maxUsers) {
return true
} else {
return false
}
}
//prints teams for discord responses. RETURNS PRINT STATEMENT.
export function printTeams(factions) {
let printStatement = ``
//console.log(`the length of factions are ${factions.length}`);
let usersList = []
factions.forEach((faction) => {
let factionTeamList = { name: faction.color, users: [] }
faction.users.forEach((user) => {
factionTeamList.users.push(`<@${user}>`)
})
usersList.push(factionTeamList)
})
//console.log(usersList);
switch (factions.length) {
case 2:
printStatement = `${teamHeaders[0]} ${usersList[0].users} \n${teamHeaders[1]} ${usersList[1].users}`
break
case 3:
printStatement = `${teamHeaders[0]} ${usersList[0].users}\n${teamHeaders[1]} ${usersList[1].users}\n${teamHeaders[2]} ${usersList[2].users}`
break
case 4:
printStatement = `${teamHeaders[0]} ${usersList[0].users}\n${teamHeaders[1]} ${usersList[1].users}\n${teamHeaders[2]} ${usersList[2].users}\n${teamHeaders[3]} ${usersList[3].users}`
break
default:
console.log(`cannot print factions`)
}
return printStatement
}
//assign one user to one team. RETURNS FACTIONS ARRAY.
export function assignUser(userId, team, factions) {
let matchedTeam = 0
let reachedMaximum = false
let lowerCaseTeam = team.toLowerCase()
let newfactions = factions.map((faction) => {
//check if iterated faction is the correct team
if (faction.color === lowerCaseTeam) {
//check if faction is at max users
if (faction.maxUsers > faction.users.length) {
let updatedFaction = { ...faction }
updatedFaction.users.push(userId)
matchedTeam++
return updatedFaction
} else {
reachedMaximum = true
return { ...faction }
}
} else {
return { ...faction }
}
})
//log for invalid team
if (matchedTeam === 0) {
console.log("invalid team was input")
return false
}
//log for max users
else if (reachedMaximum === true) {
console.log("maximum number of users in this faction")
} else {
console.log(`upated factions: ${printTeams(newfactions)}`)
return newfactions
}
}
//unassigns one user from any factions they may be assigned to. RETURNS FACTION ARRAY.
export function unassign(userId, factions) {
let newFactions = factions.map((faction) => {
return { ...faction }
})
factions.forEach((faction, factionIndex) => {
faction.users.forEach((user, userIndex) => {
if (user === userId) {
newFactions[factionIndex].users.splice(userIndex, 1)
console.log(
`unassigned ${userId} from faction ${factions[factionIndex].color}`
)
}
})
})
return newFactions
}
//unassigns all users from factions. RETURNS FACTION ARRAY.
export function unassignAll(factions) {
let newFactions = factions.map((faction) => {
return { ...faction }
})
newFactions.forEach((faction) => {
faction.users = []
})
return newFactions
}
//assigns queued users evenly across factions randomly. RETURNS FACTION ARRAY.
export function assignAllUsers(userIds, factions) {
//TODO: IF USER LIST IS LARGER THAN 57, RANDOMLY SELECT 57 OF THEM.
let assignedFactions = factions.map((faction) => {
return { ...faction }
})
switch (assignedFactions.length) {
case 2:
userIds.forEach((userId, index) => {
if (index % 2 === 0) {
assignedFactions[1].users.push(userId)
} else {
assignedFactions[0].users.push(userId)
}
})
break
case 3:
userIds.forEach((userId, index) => {
if (index % 3 === 0) {
assignedFactions[2].users.push(userId)
} else if (index % 3 === 1) {
console.log(`assigned ${userId} to ${assignedFactions[1].color}`)
assignedFactions[1].users.push(userId)
} else {
assignedFactions[0].users.push(userId)
}
})
break
case 4:
userIds.forEach((userId, index) => {
if (index % 4 === 0) {
assignedFactions[3].users.push(userId)
} else if (index % 4 === 1) {
assignedFactions[2].users.push(userId)
} else if (index % 4 === 2) {
assignedFactions[1].users.push(userId)
} else {
assignedFactions[0].users.push(userId)
}
})
break
}
return assignedFactions
}
//marks all assigned users as not queued in queue. RETURNS QUEUE ARRAY.
export function flipQueue(queue, factions) {
let assignedUsers = []
factions.forEach((faction) => {
faction.users.forEach((userId) => {
assignedUsers.push(userId)
})
})
let newQueue = queue.map((user) => {
return { ...user }
})
//console.log(newQueue)
newQueue.forEach((user, index) => {
newQueue[index].queued = false
if (assignedUsers.indexOf(user.user_id) != -1) {
newQueue[index].games_played++
}
})
return newQueue.sort(function (a, b) {
return a.games_played - b.games_played
})
}
//generates whispers for all assigned users. RETURNS WHISPERS ARRAY.
export function getWhispers(factions) {
let whisperQueue = []
factions.forEach((faction) => {
faction.users.forEach((user) => {
let whisperObj = {
userId: user,
msg: `You have been assigned to Team ${faction.color}, please join your team in voice: ${faction.discordChannel}`,
}
whisperQueue.push(whisperObj)
})
})
return whisperQueue
}
// selects 57 or less joined users from the queue and priortizes us
export function selectUsersFromQueue(queue) {
let joinedUsers = queue.filter((user) => user.queued === true)
//TODO: REPLACE 5 WITH MAX GAMES PLAYED FOR USERS IN QUEUE
let selectedQueue = []
for (let i = 0; i < 5; i++) {
let filteredList = joinedUsers.filter((user) => user.games_played === i)
filteredList.forEach((user) => {
if (selectedQueue.length < 57) {
selectedQueue.push(user.user_id)
}
})
}
return selectedQueue
}
// add a user to the queue, RETURNS A NEW QUEUE
export function join(queue, member) {
let viewer = {
user_id: member.user.id,
user_name: member.user.username,
queued: true,
games_played: 0,
}
let newQueue = []
if (queue != undefined) {
newQueue = queue.map((user) => {
return { ...user }
})
let isDupe = false
queue.forEach((user, index) => {
if (user.user_id === viewer.user_id) {
//
isDupe = true
newQueue[index].queued = true
}
})
if (!isDupe) {
newQueue.push(viewer)
}
} else {
newQueue.push(viewer)
}
return newQueue
}
//prints the current queue, RETURN CONTENT FOR DISCORD RESPONSE
export function printQueue(queue) {
let rowArray = queue.map((user, index) => {
return [index, user.user_name, user.queued, user.games_played]
})
let jsonQueue = {
title: "Faction Queue",
heading: ["#", "user name", "queued", "games played"],
rows: rowArray,
}
let table = new AsciiTable().fromJSON(jsonQueue)
let responseString = `\`\`\` ${table} \`\`\``
return responseString
}
//mentions looks like <@user_id> like <@86890631690977280> more: https://discordjs.guide/miscellaneous/parsing-mention-arguments.html#how-discord-mentions-work