forked from kotarou3/Pokemon-Showdown-Addons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclans.diff
More file actions
560 lines (560 loc) · 18.5 KB
/
clans.diff
File metadata and controls
560 lines (560 loc) · 18.5 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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
diff --git a/chat-plugins/clans.js b/chat-plugins/clans.js
new file mode 100644
index 0000000..5974e6f
--- /dev/null
+++ b/chat-plugins/clans.js
@@ -0,0 +1,519 @@
+var elo = require('elo-rank')();
+
+function ratingToName(rating) {
+ if (rating > 1500)
+ return "Gold";
+ else if (rating > 1200)
+ return "Silver";
+ else
+ return "Bronze";
+}
+
+var getClan, War;
+var ClanRoom = exports.ClanRoom = (function () {
+ function ClanRoom(name, data) {
+ data = data || {};
+ data.isClanRoom = true;
+ if (!data.ratingData) {
+ data.ratingData = {
+ wins: 0,
+ losses: 0,
+ draws: 0,
+ rating: 1000
+ };
+ }
+
+ Rooms.ChatRoom.call(this, toId(name), name, data);
+
+ this.availableMembers = {};
+ this.challengesFrom = {};
+ this.challengeTo = null;
+ }
+ ClanRoom.prototype = Object.create(Rooms.ChatRoom.prototype);
+
+ ClanRoom.prototype.getRating = function () {
+ return {
+ wins: this.ratingData.wins,
+ losses: this.ratingData.losses,
+ draws: this.ratingData.draws,
+ rating: this.ratingData.rating,
+ ratingName: ratingToName(this.ratingData.rating)
+ };
+ };
+
+ ClanRoom.prototype.setMemberAvailable = function (user) {
+ if (!this.auth || !this.auth[toId(user)]) return false;
+ var expiryTime = this.availableMembers[toId(user)] = Date.now() + (5).minutes();
+ return expiryTime;
+ };
+ ClanRoom.prototype.getAvailableMembers = function () {
+ this.pruneAvailableMembers();
+ return Object.keys(this.availableMembers);
+ };
+ ClanRoom.prototype.pruneAvailableMembers = function () {
+ for (var m in this.availableMembers) {
+ var user = Users.getExact(m);
+ if (this.availableMembers[m] < Date.now() || !this.auth[m] || !user || !user.connected) {
+ delete this.availableMembers[m];
+ }
+ }
+ };
+ ClanRoom.prototype.isEnoughAvailableMembers = function () {
+ this.pruneAvailableMembers();
+ if (Object.size(this.availableMembers) < 1) {
+ this.add("You do not have enough available members for a war. At least 4 is required.");
+ return false;
+ }
+ return true;
+ };
+
+ ClanRoom.prototype.updateChallenges = function () {
+ if (this.challengeTo) {
+ var otherClan = getClan(this.challengeTo.to);
+ if (otherClan) {
+ this.add("You are challenging " + otherClan.title);
+ } else {
+ this.challengeTo = null;
+ }
+ }
+
+ var challengesFrom = [];
+ for (var c in this.challengesFrom) {
+ challengesFrom.push(this.challengesFrom[c].from);
+ }
+ if (challengesFrom.length > 0) this.add("You are being challenged by: " + challengesFrom.join(", "));
+
+ this.update();
+ };
+ ClanRoom.prototype.makeChallenge = function (otherClan, format) {
+ if (otherClan === this) return;
+ if (this.currentWar) return;
+ if (!this.isEnoughAvailableMembers()) return;
+ if (this.challengeTo) {
+ this.updateChallenges();
+ return;
+ }
+
+ var challenge = {
+ from: this.id,
+ to: otherClan.id,
+ format: format || ''
+ };
+ this.challengeTo = challenge;
+ otherClan.challengesFrom[this.id] = challenge;
+
+ this.updateChallenges();
+ otherClan.updateChallenges();
+ };
+ ClanRoom.prototype.cancelChallengeTo = function () {
+ if (!this.challengeTo) return;
+
+ var otherClan = getClan(this.challengeTo.to);
+ this.challengeTo = null;
+ delete otherClan.challengesFrom[this.id];
+
+ this.add("You have cancelled your challenge.");
+ otherClan.add("||" + this.title + " has cancelled their challenge.");
+
+ this.updateChallenges();
+ otherClan.updateChallenges();
+ };
+ ClanRoom.prototype.rejectChallengeFrom = function (otherClan) {
+ if (!this.challengesFrom[otherClan.id]) return;
+
+ delete this.challengesFrom[otherClan.id];
+ otherClan.challengeTo = null;
+
+ this.add("You have rejected " + otherClan.title + "'s challenge.");
+ otherClan.add("||" + this.title + " has rejected your challenge.");
+
+ this.updateChallenges();
+ otherClan.updateChallenges();
+ };
+ ClanRoom.prototype.acceptChallengeFrom = function (otherClan) {
+ if (!this.challengesFrom[otherClan.id]) return;
+ if (this.currentWar) return;
+ if (otherClan.currentWar) return;
+
+ if (!this.isEnoughAvailableMembers()) return;
+ if (!otherClan.isEnoughAvailableMembers()) {
+ this.add("The other clan currently do not have enough available members for a war.");
+ return;
+ }
+
+ var challenge = otherClan.challengeTo;
+ delete this.challengesFrom[otherClan.id];
+ otherClan.challengeTo = null;
+
+ this.updateChallenges();
+ otherClan.updateChallenges();
+
+ var allies = this.getAvailableMembers();
+ var opponents = otherClan.getAvailableMembers();
+ var matchupsCount = Math.min(allies.length, opponents.length);
+
+ var war = new War(otherClan, this, opponents.slice(0, matchupsCount), allies.slice(0, matchupsCount), challenge.format, otherClan, otherClan.onWarEnd.bind(otherClan));
+ this.currentWar = war;
+ otherClan.currentWar = war;
+ };
+
+ ClanRoom.prototype.onWarEnd = function (clanA, clanB, score) {
+ var expectedScore = elo.getExpected(clanA.ratingData.rating, clanB.ratingData.rating);
+ var oldRatingA = clanA.ratingData.rating;
+ var oldRatingB = clanB.ratingData.rating;
+ clanA.ratingData.rating = elo.updateRating(expectedScore, score, clanA.ratingData.rating);
+ clanB.ratingData.rating = elo.updateRating(1 - expectedScore, 1 - score, clanB.ratingData.rating);
+ if (clanA.ratingData.rating < 1000) clanA.ratingData.rating = 1000;
+ if (clanB.ratingData.rating < 1000) clanB.ratingData.rating = 1000;
+
+ if (score === 1) {
+ ++clanA.ratingData.wins;
+ ++clanB.ratingData.losses;
+ } else if (score === 0) {
+ ++clanA.ratingData.losses;
+ ++clanB.ratingData.wins;
+ } else {
+ ++clanA.ratingData.draws;
+ ++clanB.ratingData.draws;
+ }
+
+ Rooms.global.writeChatRoomData();
+
+ this.add("||" + clanA.title + " has " + (["lost", "won"][score] || "drawn") + " the clan war against " + clanB.title + ".");
+ this.add("|raw|<strong>" + Tools.escapeHTML(clanA.title) + ":</strong> " + oldRatingA + " → " + clanA.ratingData.rating + " (" + ratingToName(clanA.ratingData.rating) + ")");
+ this.add("|raw|<strong>" + Tools.escapeHTML(clanB.title) + ":</strong> " + oldRatingB + " → " + clanB.ratingData.rating + " (" + ratingToName(clanB.ratingData.rating) + ")");
+ this.update();
+
+ clanA.endCurrentWar();
+ };
+ ClanRoom.prototype.endCurrentWar = function () {
+ if (!this.currentWar) return;
+
+ var otherClan = this.currentWar.clanA === this ? this.currentWar.clanB : this.currentWar.clanA;
+ delete otherClan.currentWar;
+ delete this.currentWar;
+ };
+
+ ClanRoom.prototype.destroy = function () {
+ this.cancelChallengeTo();
+ for (var c in this.challengesFrom) this.rejectChallengeFrom(c);
+ this.endCurrentWar();
+
+ Rooms.ChatRoom.prototype.destroy.call(this);
+ };
+
+ return ClanRoom;
+})();
+
+var War = exports.War = (function () {
+ function War(clanA, clanB, battlersA, battlersB, format, room, onEnd) {
+ this.clanA = clanA;
+ this.clanB = clanB;
+ this.battlersA = battlersA.map(toId).randomize();
+ this.battlersB = battlersB.map(toId).randomize();
+ this.format = format;
+ this.room = room;
+ this.onEnd = onEnd;
+
+ this.matchups = {};
+ for (var b = 0; b < this.battlersA.length; ++b) {
+ var matchup = {from: this.battlersA[b], to: this.battlersB[b]};
+ this.matchups[this.battlersA[b]] = matchup;
+ this.matchups[this.battlersB[b]] = matchup;
+
+ Users.getExact(this.battlersA[b]).joinRoom(this.room);
+ Users.getExact(this.battlersB[b]).joinRoom(this.room);
+ }
+ this.remainingMatches = this.battlersA.length;
+
+ this.score = 0; // Positive: clanA winning; Negative: clanB winning
+
+ this.room.add('|raw|' +
+ "<strong>A clan war between " + Tools.escapeHTML(this.clanA.title) + " and " + Tools.escapeHTML(this.clanB.title) + " has started!</strong><br />" +
+ this.getMatchups().map(function (matchup) {
+ return '<strong>' + Tools.escapeHTML(matchup.from) + "</strong> vs <strong>" + Tools.escapeHTML(matchup.to);
+ }).join('<br />')
+ );
+ this.room.update();
+ }
+
+ War.prototype.getMatchups = function () {
+ var matchups = [];
+ for (var m in this.matchups) {
+ if (this.matchups[m].from === m) {
+ matchups.push(this.matchups[m]);
+ }
+ }
+ return matchups;
+ };
+
+ War.prototype.onBattleWin = function (userA, userB, score, format) {
+ if (format !== this.format) return;
+
+ var userAId = toId(userA);
+ var userBId = toId(userB);
+
+ var matchup = this.matchups[userAId];
+ if (!matchup || (userBId !== matchup.from && userBId !== matchup.to) || matchup.isEnded) return;
+
+ matchup.isEnded = true;
+ --this.remainingMatches;
+
+ if (userAId === matchup.to) {
+ var tmp = userA;
+ userA = userB;
+ userB = tmp;
+ score = 1 - score;
+ }
+ this.score += (score - 0.5) * 2;
+
+ this.room.add("|raw|<strong>(" + Tools.escapeHTML(this.clanA.title) + " vs " + Tools.escapeHTML(this.clanB.title) + ") " + Tools.escapeHTML(userA.name) + " has " + (["lost", "won"][score] || "drawn") + " the clan war battle against " + Tools.escapeHTML(userB.name) + ".</strong>");
+ this.room.update();
+
+ if (this.remainingMatches === 0) {
+ var overallScore = (this.score && this.score / Math.abs(this.score)) / 2 + 0.5;
+ this.onEnd(this.clanA, this.clanB, overallScore);
+ }
+ };
+
+ War.prototype.isEnded = function () {
+ return this.remainingMatches === 0;
+ };
+
+ return War;
+})();
+
+var patchRooms = exports.patchRooms = function () {
+ for (var r = 0; r < Rooms.global.chatRooms.length; ++r) {
+ var room = Rooms.global.chatRooms[r];
+ if (room.isClanRoom && !room.availableMembers) {
+ var newRoom = new ClanRoom(room.title, room.chatRoomData);
+ Rooms.global.chatRooms[r] = newRoom;
+ Rooms.rooms[room.id] = newRoom;
+ }
+ }
+};
+patchRooms();
+
+var getClans = exports.getClans = function () {
+ var results = [];
+ for (var r in Rooms.rooms)
+ if (Rooms.rooms[r] instanceof ClanRoom)
+ results.push(Rooms.rooms[r]);
+ return results;
+};
+var getClan = exports.get = function (name) {
+ var room = Rooms.get(toId(name));
+ return room && room.isClanRoom ? room : null;
+};
+var getClansFromMember = exports.getFromMember = function (user) {
+ var results = [];
+ var userId = toId(user);
+ for (var r in Rooms.rooms)
+ if (Rooms.rooms[r] instanceof ClanRoom && Rooms.rooms[r].auth && Rooms.rooms[r].auth[userId])
+ results.push(Rooms.rooms[r]);
+ return results;
+};
+
+var createClan = exports.createClan = function (name) {
+ if (Rooms.get(toId(name))) return false;
+ if (!Rooms.global.addChatRoom(name)) return false;
+
+ var room = Rooms.get(toId(name));
+ room.isClanRoom = room.chatRoomData.isClanRoom = true;
+ Rooms.global.writeChatRoomData();
+ patchRooms();
+ return room;
+};
+var deleteClan = exports.deleteClan = function (name) {
+ var room = getClan(name);
+ if (!room) return false;
+ return Rooms.global.removeChatRoom(toId(name));
+};
+
+var oldWin = Rooms.BattleRoom.prototype.win;
+Rooms.BattleRoom.prototype.win = function (winner) {
+ var winnerId = toId(winner);
+ var score = 0.5;
+ if (winnerId === toId(this.p1)) {
+ score = 1;
+ } else if (winnerId === toId(this.p2)) {
+ score = 0;
+ }
+
+ var clans = getClansFromMember(this.p1);
+ for (var c = 0; c < clans.length; ++c) {
+ if (clans[c].currentWar) {
+ clans[c].currentWar.onBattleWin(this.p1, this.p2, score, this.format);
+ }
+ }
+
+ return oldWin.call(this, winner);
+};
+
+var commands = {
+ help: function () {
+ if (!this.canBroadcast()) return;
+ this.sendReplyBox(
+ "/clans [name] - Gets information about all clans, or about the specified clan<br />" +
+ "/clan waravailable - Sets yourself as available for clan wars for 5 minutes<br />" +
+ "/clan create <name> - Creates a clan<br />" +
+ "/clan delete <name> - Deletes a clan<br />" +
+ "/clan challenge <clan> - Challenge another clan<br />" +
+ "/clan cancelchallenge - Cancel your challenge<br />" +
+ "/clan accept <clan> - Accept <clan>'s challenge<br />" +
+ "/clan reject <clan> - Reject <clan>'s challenge<br />" +
+ "/clan endwar- Ends the current war forcibly<br />" +
+ "/clan matchups - Shows the war battles that haven't yet been started<br />"
+ );
+ },
+
+ default: function (target, room, user, connection, cmd) {
+ if (!this.canBroadcast()) return;
+ target = target || cmd;
+
+ var clans = [getClan(target)];
+ if (!clans[0]) clans = getClansFromMember(target);
+ if (!clans[0] && target.length > 0) {
+ clans = [];
+ var allClans = getClans();
+ var targetId = toId(target);
+ for (var c = 0; c < allClans.length; ++c) {
+ if (allClans[c].id.slice(0, targetId.length) === targetId) {
+ clans.push(allClans[c]);
+ }
+ }
+ }
+ if (!clans[0] && target.length > 0) return this.sendReply("No clan or clan member found under '" + target + "'.");
+
+ if (!clans[0]) {
+ this.sendReplyBox(
+ "<strong>Clans:</strong><br />" +
+ getClans().map(function (clan) {
+ var result = clan.getRating();
+ result.name = clan.title;
+ result.id = clan.id;
+ return result;
+ }).sort(function (a, b) {
+ return b.rating - a.rating;
+ }).map(function (clan) {
+ return '<a class="ilink" href="/' + clan.id + '"><strong>' + Tools.escapeHTML(clan.name) + ':</strong></a> ' + clan.rating + " (" + clan.ratingName + ") " + clan.wins + "/" + clan.losses + "/" + clan.draws;
+ }).join('<br />')
+ );
+ return;
+ }
+
+ clans = clans.sort(function (a, b) {
+ return a.id.localeCompare(b.id);
+ });
+ for (var c = 0; c < clans.length; ++c) {
+ var clan = clans[c];
+ var rating = clan.getRating();
+ this.sendReplyBox(
+ '<h1>' + Tools.escapeHTML(clan.title) + '</h1>' +
+ (clan.introMessage || '') +
+ '<hr />' +
+ "<strong>Rating:</strong> " + rating.rating + " (" + rating.ratingName + ")<br />" +
+ "<strong>Wins/Losses/Draws:</strong> " + rating.wins + "/" + rating.losses + "/" + rating.draws + '<br />' +
+ "<strong>Members:</strong> " + Tools.escapeHTML(Object.keys(clan.auth || {}).sort().join(", ")) + '<br />' +
+ "<button name=\"joinRoom\" value=\"" + clan.id + "\">Join</button>"
+ );
+ }
+ },
+
+ create: function (target) {
+ if (!this.can('makeroom')) return;
+ if (target.length < 2) {
+ this.sendReply("The clan's name is too short.");
+ } else if (!createClan(target)) {
+ this.sendReply("Could not create the clan. Does a room by it's name already exist?");
+ } else {
+ this.sendReply("Clan: " + target + " successfully created.");
+ }
+ },
+
+ delete: function (target) {
+ if (!this.can('makeroom')) return;
+ if (!deleteClan(target)) {
+ this.sendReply("Could not delete the clan. Did you spell it correctly?");
+ } else {
+ this.sendReply("Clan: " + target + " successfully deleted.");
+ }
+ },
+
+ waravailable: function (target, room, user) {
+ if (!room.isClanRoom) return this.sendReply("This is not a clan room.");
+ var expiryTime = room.setMemberAvailable(user);
+ if (!expiryTime) return this.sendReply("You are not a member of this clan.");
+ this.sendReply("You have been marked available for this clan's wars for " + (expiryTime - Date.now()).duration() + ".");
+ },
+
+ challenge: function (target, room) {
+ if (!room.isClanRoom) return this.sendReply("This is not a clan room.");
+ if (!this.can('clans', room)) return;
+ if (room.currentWar) return this.sendReply("You are already at war.");
+
+ var otherClan = getClan(target);
+ if (!otherClan) return this.sendReply("The clan does not exist.");
+ if (otherClan === room) return this.sendReply("You cannot challenge your own clan.");
+
+ room.makeChallenge(otherClan, 'ou');
+ },
+
+ cancelchallenge: function (target, room) {
+ if (!room.isClanRoom) return this.sendReply("This is not a clan room.");
+ if (!this.can('clans', room)) return;
+ if (!room.challengeTo) return this.sendReply("This clan isn't currently challenging anyone.");
+
+ room.cancelChallengeTo();
+ },
+
+ accept: function (target, room) {
+ if (!room.isClanRoom) return this.sendReply("This is not a clan room.");
+ if (!this.can('clans', room)) return;
+ if (room.currentWar) return this.sendReply("You are already at war.");
+
+ var otherClan = getClan(target);
+ if (!otherClan) return this.sendReply("The clan does not exist");
+ if (!room.challengesFrom[otherClan.id]) return this.sendReply("||" + otherClan.title + " is not challenging you right now.");
+
+ room.acceptChallengeFrom(otherClan);
+ },
+
+ reject: function (target, room) {
+ if (!room.isClanRoom) return this.sendReply("This is not a clan room.");
+ if (!this.can('clans', room)) return;
+
+ var otherClan = getClan(target);
+ if (!otherClan) return this.sendReply("The clan does not exist");
+
+ room.rejectChallengeFrom(otherClan);
+ },
+
+ endwar: function (target, room) {
+ if (!room.isClanRoom) return this.sendReply("This is not a clan room.");
+ if (!this.can('clans', room)) return;
+ if (!room.currentWar) return this.sendReply("This clan currently isn't at war.");
+ if (room.currentWar.room !== room) return this.sendReply("This room is not hosting a war.");
+
+ room.endCurrentWar();
+ room.add("The clan war was forcibly ended.");
+ },
+
+ matchups: function (target, room) {
+ if (!this.canBroadcast()) return;
+ if (!room.isClanRoom) return this.sendReply("This is not a clan room.");
+ if (!room.currentWar) return this.sendReply("This clan currently isn't at war.");
+
+ this.sendReplyBox(
+ "<strong>Clan war matchups between " + Tools.escapeHTML(room.currentWar.clanA.title) + " and " + Tools.escapeHTML(room.currentWar.clanB.title) + ':</strong><br />' +
+ room.currentWar.getMatchups().map(function (matchup) {
+ return matchup.isEnded ? "" : '<strong>' + Tools.escapeHTML(matchup.from) + "</strong> vs <strong>" + Tools.escapeHTML(matchup.to);
+ }).join('<br />')
+ );
+ }
+};
+exports.commands = {
+ clans: 'clan',
+ clan: commands
+};
diff --git a/package.json b/package.json
index d5e5d95..c2f25c4 100644
--- a/package.json
+++ b/package.json
@@ -4,11 +4,12 @@
"dependencies": {
"sugar": "1.4.1",
"node-static": "0.7.4",
"cloud-env": "0.0.4",
"sockjs": "0.3.9",
- "es6-shim": "0.16.0"
+ "es6-shim": "0.16.0",
+ "elo-rank": "0.2.2"
},
"optionalDependencies": {
"nodemailer": "0.4.1",
"http-proxy": "0.10.0"
},
diff --git a/users.js b/users.js
index 88bc5b1..87e51e5 100644
--- a/users.js
+++ b/users.js
@@ -565,11 +565,11 @@ User = (function () {
var room = Rooms.rooms[roomid];
if (room && room.auth) {
if (room.auth[this.userid]) {
return room.auth[this.userid] + this.name;
}
- if (room.isPrivate === true) return Config.groups.default[room.type + 'Room'] + this.name;
+ if (room.isPrivate === true || room.isClanRoom) return Config.groups.default[room.type + 'Room'] + this.name;
}
}
return this.group + this.name;
};
User.prototype.can = function (permission, target, room) {