-
Notifications
You must be signed in to change notification settings - Fork 11
/
RockPaperScissors_v3.js
210 lines (195 loc) Β· 8.5 KB
/
RockPaperScissors_v3.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
var rps = ["rock", "paper", "scissors"];
var commands = ["!commands", "!points", "!rps"];
var playerList = {};
var playerListArray = [];
var roomObject = {
emojis: ["πͺ¨", "π", "βοΈ"],
game: {
limits: {
lower: 100,
upper: 300
}
},
maxPlayers: 2,
noPlayer: true,
public: true,
roomName: "πͺ¨πβοΈ Rock - Paper - Scissors Game πͺ¨πβοΈ"
};
var colors = {
Chat: {
Commands: [0xFFFFFF, 0xFFDB72],
NotACommand: 0xFF0000,
Player: [0xFFFFFF, 0xFFDB72],
Points: {
Info: 0x00FFFF,
List: 0xFFFFFF
},
RPS: {
Draw: 0xC0C0C0,
Fail: 0xFF0000,
Invalid: 0xFFFF00,
OutOfRange: 0xFFFF00,
Success: 0x00FF00
}
}
};
var fonts = {
Chat: {
Commands: ["normal", "bold"],
NotACommand: "bold",
Player: ["normal", "bold"],
Points: {
Info: "normal",
List: "normal"
},
RPS: {
Draw: "bold",
Fail: "bold",
Invalid: "bold",
OutOfRange: "bold",
Success: "normal"
}
}
};
var sounds = {
Chat: {
Commands: [1, 1],
NotACommand: 2,
Player: [1, 1],
Points: {
Info: 1,
List: 0
},
RPS: {
Draw: 2,
Fail: 2,
Invalid: 2,
OutOfRange: 2,
Success: 1
}
}
};
var messages = {
Chat: {
Commands: [["!commands", "!points", "!rps [Guess] [Point]"], ["!commands", "!points", "!rps [Guess] [Point]"]],
NotACommand: "There's no such a command! Type commands to see the available commands.",
Player: ["PLAYER", "ADMIN"],
Points: {
Info: "Here are the points for the players in room:\n"
},
RPS: {
Draw: "It's a draw, no points.",
Fail: ["You have failed! You have lost", "points"],
Invalid: "Invalid value! Please try again.",
OutOfRange: `Invalid point! The given point must be between ${roomObject.game.limits.lower} and ${roomObject.game.limits.upper} with a value multiple of 100.`,
Success: ["Congratulations! You have won! You have gained", "points"]
}
}
};
var room = HBInit({ roomName: roomObject.roomName, noPlayer: roomObject.noPlayer, public: roomObject.public, maxPlayers: roomObject.maxPlayers });
function getPreviousAccounts(player) {
return playerListArray.filter(p => p.auth == player.auth || p.conn == player.conn);
}
function getRandomInteger(length) {
return Math.floor(Math.random() * length);
}
function getResult(check, response) {
if ((check.toLowerCase() === rps[0] && response.toLowerCase() === rps[2]) || (check.toLowerCase() === rps[1] && response.toLowerCase() === rps[0]) || (check.toLowerCase() === rps[2] && response.toLowerCase() === rps[1])) {
return "Win";
}
else if (rps.indexOf(check.toLowerCase()) === rps.indexOf(response.toLowerCase())) {
return "Draw";
}
else {
return "Lose";
}
}
function updatePlayerPoint(player, point) {
playerList[player.name].points += point;
}
room.onPlayerChat = function (player, message) {
if (message.startsWith("!") == true) {
if (message.toLowerCase().split(" ")[0] == commands[0]) {
room.sendAnnouncement(`${messages.Chat.Commands[Number(player.admin)].toString()}`, player.id, colors.Chat.Commands[Number(player.admin)], fonts.Chat.Commands[Number(player.admin)], sounds.Chat.Commands[Number(player.admin)]);
return false;
}
else if (message.toLowerCase().split(" ")[0] == commands[1]) {
var players = room.getPlayerList().sort(function (p1, p2) {
return p1.points - p2.points;
}).map(p => p.name + ": " + playerList[p.name].points).join("\n");
room.sendAnnouncement(`${messages.Chat.Points.Info}`, player.id, colors.Chat.Points.Info, fonts.Chat.Points.Info, sounds.Chat.Points.Info);
room.sendAnnouncement(`${players}`, player.id, colors.Chat.Points.List, fonts.Chat.Points.List, sounds.Chat.Points.List);
return false;
}
else if (message.toLowerCase().split(" ")[0] == commands[2]) {
var check = message.toLowerCase().split(" ")[1];
var index = rps.indexOf(check);
if (index === -1) {
room.sendAnnouncement(`${messages.Chat.RPS.Invalid}`, player.id, colors.Chat.RPS.Invalid, fonts.Chat.RPS.Invalid, sounds.Chat.RPS.Invalid);
return false;
}
else {
var point = parseInt(message.toLowerCase().split(" ")[2]);
if (isNaN(point)) {
room.sendAnnouncement(`${messages.Chat.RPS.OutOfRange}`, player.id, colors.Chat.RPS.OutOfRange, fonts.Chat.RPS.OutOfRange, sounds.Chat.RPS.OutOfRange);
return false;
}
else {
if (point < roomObject.game.limits.lower || roomObject.game.limits.upper < point || point % 100 != 0) {
room.sendAnnouncement(`${messages.Chat.RPS.OutOfRange}`, player.id, colors.Chat.RPS.OutOfRange, fonts.Chat.RPS.OutOfRange, sounds.Chat.RPS.OutOfRange);
return false;
}
else {
var randomInt = getRandomInteger(rps.length);
var response = rps[randomInt];
if (getResult(check, response).toString() === "Lose") {
room.sendAnnouncement(`${messages.Chat.RPS.Fail[0]} ${point} ${messages.Chat.RPS.Fail[1]} (${roomObject.emojis[index]} π ${roomObject.emojis[randomInt]})`, player.id, colors.Chat.RPS.Fail, fonts.Chat.RPS.Fail, sounds.Chat.RPS.Fail);
updatePlayerPoint(player, -point);
return false;
}
else if (getResult(check, response).toString() === "Draw") {
room.sendAnnouncement(`${messages.Chat.RPS.Draw} ${messages.Chat.RPS.Draw} (${roomObject.emojis[index]} π ${roomObject.emojis[randomInt]})`, player.id, colors.Chat.RPS.Draw, fonts.Chat.RPS.Draw, sounds.Chat.RPS.Draw);
updatePlayerPoint(player, 0);
return false;
}
else if (getResult(check, response).toString() === "Win") {
room.sendAnnouncement(`${messages.Chat.RPS.Success[0]} ${point} ${messages.Chat.RPS.Success[1]} (${roomObject.emojis[index]} π ${roomObject.emojis[randomInt]})`, player.id, colors.Chat.RPS.Success, fonts.Chat.RPS.Success, sounds.Chat.RPS.Success);
updatePlayerPoint(player, point);
return false;
}
}
}
}
}
else {
room.sendAnnouncement(`${messages.Chat.NotACommand}`, player.id, colors.Chat.NotACommand, fonts.Chat.NotACommand, sounds.Chat.NotACommand);
return false;
}
}
else {
room.sendAnnouncement(`(${new Date(Date.now()).toLocaleString()}) [${playerList[player.name].points}] [${messages.Chat.Player[Number(player.admin)]}] ${player.name} #${player.id}: ${message}`, null, colors.Chat.Player[Number(player.admin)], fonts.Chat.Player[Number(player.admin)], sounds.Chat.Player[Number(player.admin)]);
return false;
}
}
room.onPlayerJoin = function (player) {
var accounts = getPreviousAccounts(player);
if (playerList[player.name] == undefined) {
if (accounts.length == 0) {
playerList[player.name] = { name: player.name, auth: player.auth, conn: player.conn, points: 600 };
}
else {
var lastAccount = accounts[accounts.length - 1];
playerList[player.name] = { name: player.name, auth: player.auth, conn: player.conn, points: lastAccount.points };
}
}
else {
if (accounts.length == 0) {
playerList[player.name] = { name: player.name, auth: player.auth, conn: player.conn, points: 600 }; //A rare situation, theorically it's almost impossible to happen.
}
else {
var lastAccount = accounts[accounts.length - 1];
playerList[player.name] = { name: player.name, auth: player.auth, conn: player.conn, points: lastAccount.points };
}
}
playerListArray.push(playerList[player.name]);
}