-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.js
More file actions
196 lines (196 loc) · 10.9 KB
/
init.js
File metadata and controls
196 lines (196 loc) · 10.9 KB
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
const CONSTS = require("./strings.js"), Map = require("./maps.js"), Admin = require("./actionsHost.js");
const IDtoNicknameMappings = {};
function GetSpeedName(i) {
switch(i) {
case 0.75: return "Fast";
case 1: return "Normal";
case 2: return "Slow";
case 4: return "Very Slow";
}
}
function GetMapName(mapIdx) {
if(mapIdx < 0) { return "Random"; }
return Map.GetMapName(mapIdx);
}
function GetNickname(bot, serverID, userID) {
if(IDtoNicknameMappings[userID] !== undefined) { return IDtoNicknameMappings[userID]; }
let nick = bot.servers[serverID].members[userID].nick;
if(nick === null) {
nick = bot.users[userID].username;
}
IDtoNicknameMappings[userID] = nick;
return nick;
}
const self = module.exports = {
Init: function(gameData, channelID, userID, message) {
if(message !== "INIT") { return; }
gameData.bot.sendMessage({ to: channelID, message: CONSTS.TITLE }, function(err) {
if(err === null) {
self.Init2(gameData, channelID, userID);
} else if(err.statusMessage !== "FORBIDDEN") { // don't initialize in blocked channels!
self.Init2(gameData, channelID, userID, true);
} else {
console.log(`[${(new Date()).toLocaleString("en-US", { timeZone: "America/Los_Angeles" })}] Match in channel ${channelID} was not started due to insufficient permissions.`);
}
});
},
Init2: function(gameData, channelID, userID, tryTitleAgain) {
gameData.channelID = channelID;
gameData.serverID = gameData.bot.channels[channelID].guild_id;
gameData.hostUserID = userID;
gameData.hostUserName = GetNickname(gameData.bot, gameData.serverID, userID);
gameData.initialized = true;
if(tryTitleAgain) { gameData.discordHelper.Say(CONSTS.TITLE); }
const playerStr = gameData.players.length === 0 ? "" : `\n+ Current Players: ${gameData.players.map(e => GetNickname(gameData.bot, gameData.serverID, e)).join(", ")}.`;
gameData.discordHelper.SayP(`Current Settings -- Maximum Players: ${gameData.numPlayers} -- Game Speed: ${GetSpeedName(gameData.gameSpeed)} -- Level: ${GetMapName(gameData.selectedMapIdx)}
+ Anyone can type "join" to join the next match or "leave" to leave it.
+ Anyone can type "!HELP" to see how to play, or "!HELP [action]" to learn more about a specific command.
- The host can type "!players #" to set the player count (valid values are 2-100).
- The host can type "!speed #" to set the speed (valid values are Fast, Normal, Slow, Very Slow).
- The host can type "!changelevel LEVELNUMBER", "!changelevel Random", "!viewlevels" or "!viewlevel LEVELNUMBER"!
- The host can type "start" to begin the game or "cancel" to end the game.
- The host can type "!kick @user" to kick a user before or during the game, and can type "!endgame" during the game to end it early.
- ${gameData.hostUserName} is the host!${playerStr}`);
},
HandlePostInitCommand: function(gameData, userID, message) {
if(userID == gameData.hostUserID) {
if(self.JustHostyThings(gameData, message)) { return; }
}
if(message === "who") {
const users = [];
for(let i = 0; i < gameData.players.length; i++) {
users.push(GetNickname(gameData.bot, gameData.serverID, gameData.players[i]));
}
gameData.discordHelper.SayP(`Current Players are: ${users.join(", ")}.`);
return;
}
const user = GetNickname(gameData.bot, gameData.serverID, userID);
if(message === "join") {
if(gameData.players.indexOf(userID) >= 0) { return; }
if(gameData.kickedUserIDs.indexOf(userID) >= 0) { return; }
if(gameData.players.length >= gameData.numPlayers) {
gameData.discordHelper.SayM(`This round already has ${gameData.numPlayers} players in it. ${gameData.hostUserName}, type *Start* to begin the game.`);
} else {
gameData.players.push(userID);
gameData.discordHelper.SayP(`${user} has joined the round. ${gameData.numPlayers - gameData.players.length} player slot${(gameData.numPlayers - gameData.players.length !== 1) ? "s are" : " is"} still available.`);
}
} else if(message === "leave") {
const playerIdx = gameData.players.indexOf(userID);
if(playerIdx < 0) { return false; }
gameData.players.splice(playerIdx, 1);
gameData.discordHelper.SayP(`${user} has left the round. ${gameData.numPlayers - gameData.players.length} player slot${(gameData.numPlayers - gameData.players.length !== 1) ? "s are" : " is"} are still available.`);
}
},
JustHostyThings: function(gameData, message) {
if(message.indexOf("!kick") === 0) {
const target = message.replace("!kick ", "");
Admin.KickUser(gameData, { nick: gameData.hostUserName }, gameData.hostUserID, target);
return true;
} else if(message.indexOf("!changelevel") === 0) {
const potentialNum = message.replace("!changelevel ", "");
if(potentialNum === "random") {
gameData.selectedMapIdx = -1;
gameData.discordHelper.SayP(`The current level will now be randomly selected when the game starts!`);
} else {
const tryNum = parseInt(potentialNum);
if(isNaN(tryNum) || tryNum <= 0 || !Map.IsValidMap(tryNum - 1)) { gameData.discordHelper.SayM(`Invalid level number. Please specify a valid level number or "RANDOM."`); return true; }
gameData.selectedMapIdx = tryNum - 1;
gameData.discordHelper.SayP(`The current level is now "${Map.GetMapName(gameData.selectedMapIdx)}"`);
}
return true;
} else if(message.indexOf("!viewlevels") === 0) {
gameData.discordHelper.SayM(`Here are the available maps. Type "!changelevel LEVELNUMBER" to specify one, or "!changelevel Random" to pick a random level.\n${Map.GetMaps(gameData.gameSpeed)}`);
return true;
} else if(message.indexOf("!viewlevel") === 0) {
const potentialNum = message.replace("!viewlevel ", "");
const tryNum = parseInt(potentialNum);
if(isNaN(tryNum) || tryNum <= 0 || !Map.IsValidMap(tryNum - 1)) { gameData.discordHelper.SayM(`Invalid level number. Please specify a valid level number.`); return true; }
gameData.discordHelper.SayP(`Viewing Level ${Map.GetMapStr(tryNum - 1, gameData.gameSpeed)}\n${Map.GetMapImg(tryNum - 1)}`);
return true;
} else if(message.indexOf("!players") === 0) {
const potentialNum = message.replace("!players ", "");
const tryNum = parseInt(potentialNum);
if(isNaN(tryNum) || tryNum < 2 || tryNum > 99) {
gameData.discordHelper.SayM(`Invalid player count. Please enter a number from 2 to 99.`);
} else {
const clearNumPlayers = (tryNum < gameData.players.length);
gameData.numPlayers = tryNum;
gameData.discordHelper.SayP(`The next match's player count is now ${tryNum}!`);
if(clearNumPlayers) {
gameData.discordHelper.SayM(`Since there were already ${gameData.players.length} players signed up, they have all been cleared from the queue. Please type "join" again to join the next match!`);
gameData.players = [];
}
}
return true;
} else if(message.indexOf("!speed") === 0) {
const potentialSpeed = message.replace("!speed ", "");
switch(potentialSpeed) {
case "fast": gameData.gameSpeed = 0.75; break;
case "normal": gameData.gameSpeed = 1; break;
case "slow": gameData.gameSpeed = 2; break;
case "very slow": gameData.gameSpeed = 4; break;
default:
gameData.discordHelper.SayM(`Invalid speed. Please enter either Fast, Normal, Slow, or Very Slow.`);
return true;
}
gameData.discordHelper.SayP(`The next match's speed is now ${potentialSpeed}!`);
return true;
} else if(message === "start") {
if(gameData.players.length === 0) {
gameData.discordHelper.SayM(`Come on, bro, at least wait until the match has someone in it before starting it!`);
return true;
}
gameData.map = Map.GetMap(gameData);
if(gameData.map === null) { return true; }
if(gameData.map.isTutorial) {
gameData.isTutorial = true;
gameData.tutorialState = 0;
gameData.tutWaiting = false;
}
gameData.started = true;
gameData.playerDetails = {};
gameData.endingTime = gameData.map.time;
const numRooms = gameData.map.rooms.length;
const roomsArray = [];
for(let i = 0; i < gameData.players.length; i++) {
const playerId = gameData.players[i];
const playerRoom = i % numRooms;
const playerNick = GetNickname(gameData.bot, gameData.serverID, playerId);
gameData.playerDetails[playerId] = {
color: (i % 4),
nick: playerNick,
room: playerRoom,
shoes: 2, hat: true,
pants: true, shirt: true,
holding: null,
stuckTimer: 0,
activeActions: [],
};
if(roomsArray[playerRoom] === undefined) { roomsArray[playerRoom] = []; }
roomsArray[playerRoom].push(playerNick);
}
let informationStr = `The game has begun! You are on the map "${gameData.map.name}!" You have ${Map.FormatTime(gameData.map.time, 1)} remaining!
${gameData.map.img}\n`;
for(let i = 0; i < roomsArray.length; i++) {
const peopleInRoom = roomsArray[i];
let peopleName = "";
if(peopleInRoom.length === 1) { peopleName = peopleInRoom[0]; }
else if(peopleInRoom.length === 2) { peopleName = `${peopleInRoom[0]} and ${peopleInRoom[1]}`; }
else {
const lastElement = peopleInRoom.splice(-1, 1)[0];
peopleName = `${peopleInRoom.join(", ")}, and ${lastElement}`;
}
informationStr += `+ ${peopleName} ${peopleInRoom.length === 1 ? "is" : "are"} in Room ${i + 1}.\n`;
}
informationStr += `+ Bone Apple Tea!`;
gameData.discordHelper.SayP(informationStr);
return true;
} else if(message === "cancel") {
gameData.initialized = false;
gameData.cancelled = true;
gameData.players = [];
return false;
}
return false;
}
};