Spaces:
Running
Running
File size: 8,406 Bytes
5c2ed06 |
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 |
"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var ladders_challenges_exports = {};
__export(ladders_challenges_exports, {
AbstractChallenge: () => AbstractChallenge,
BattleChallenge: () => BattleChallenge,
BattleInvite: () => BattleInvite,
BattleReady: () => BattleReady,
Challenges: () => Challenges,
GameChallenge: () => GameChallenge,
challenges: () => challenges
});
module.exports = __toCommonJS(ladders_challenges_exports);
class BattleReady {
constructor(userid, formatid, settings, rating = 0, challengeType = "challenge") {
this.userid = userid;
this.formatid = formatid;
this.settings = settings;
this.rating = rating;
this.challengeType = challengeType;
this.time = Date.now();
}
}
class AbstractChallenge {
constructor(from, to, ready, options = {}) {
this.from = from;
this.to = to;
this.ready = typeof ready === "string" ? null : ready;
this.format = typeof ready === "string" ? ready : ready.formatid;
this.acceptCommand = options.acceptCommand || null;
this.message = options.message || "";
this.roomid = options.roomid || "";
this.acceptButton = options.acceptButton || "";
this.rejectButton = options.rejectButton || "";
}
destroy(accepted) {
}
}
class BattleChallenge extends AbstractChallenge {
}
class GameChallenge extends AbstractChallenge {
}
class BattleInvite extends AbstractChallenge {
destroy(accepted) {
if (accepted)
return;
const room = Rooms.get(this.roomid);
if (!room)
return;
const battle = room.battle;
let invitesFull = true;
for (const player of battle.players) {
if (!player.invite && !player.id)
invitesFull = false;
if (player.invite === this.to)
player.invite = "";
}
if (invitesFull)
battle.sendInviteForm(true);
}
}
class Challenges extends Map {
getOrCreate(userid) {
let challenges2 = this.get(userid);
if (challenges2)
return challenges2;
challenges2 = [];
this.set(userid, challenges2);
return challenges2;
}
/** Throws Chat.ErrorMessage if a challenge between these users is already in the table */
add(challenge) {
const oldChallenge = this.search(challenge.to, challenge.from);
if (oldChallenge) {
throw new Chat.ErrorMessage(`There is already a challenge (${challenge.format}) between ${challenge.to} and ${challenge.from}!`);
}
const to = this.getOrCreate(challenge.to);
const from = this.getOrCreate(challenge.from);
to.push(challenge);
from.push(challenge);
this.update(challenge.to, challenge.from);
return true;
}
/** Returns false if the challenge isn't in the table */
remove(challenge, accepted) {
const to = this.getOrCreate(challenge.to);
const from = this.getOrCreate(challenge.from);
const toIndex = to.indexOf(challenge);
let success = false;
if (toIndex >= 0) {
to.splice(toIndex, 1);
if (!to.length)
this.delete(challenge.to);
success = true;
}
const fromIndex = from.indexOf(challenge);
if (fromIndex >= 0) {
from.splice(fromIndex, 1);
if (!from.length)
this.delete(challenge.from);
}
if (success) {
this.update(challenge.to, challenge.from);
challenge.destroy(accepted);
}
return success;
}
search(userid1, userid2) {
const challenges2 = this.get(userid1);
if (!challenges2)
return null;
for (const challenge of challenges2) {
if (challenge.to === userid1 && challenge.from === userid2 || challenge.to === userid2 && challenge.from === userid1) {
return challenge;
}
}
return null;
}
searchByRoom(userid, roomid) {
const challenges2 = this.get(userid);
if (!challenges2)
return null;
for (const challenge of challenges2) {
if (challenge.roomid === roomid)
return challenge;
}
return null;
}
/**
* Try to accept a custom challenge, throwing `Chat.ErrorMessage` on failure,
* and returning the user the challenge was from on a success.
*/
resolveAcceptCommand(context) {
const targetid = context.target;
const chall = this.search(context.user.id, targetid);
if (!chall || chall.to !== context.user.id || chall.acceptCommand !== context.message) {
throw new Chat.ErrorMessage(`Challenge not found. You are using the wrong command. Challenges should be accepted with /accept`);
}
return chall;
}
accept(context) {
const chall = this.resolveAcceptCommand(context);
this.remove(chall, true);
const fromUser = Users.get(chall.from);
if (!fromUser)
throw new Chat.ErrorMessage(`User "${chall.from}" is not available right now.`);
return fromUser;
}
clearFor(userid, reason) {
const user = Users.get(userid);
const userIdentity = user ? user.getIdentity() : ` ${userid}`;
const challenges2 = this.get(userid);
if (!challenges2)
return 0;
for (const challenge of challenges2) {
const otherid = challenge.to === userid ? challenge.from : challenge.to;
const otherUser = Users.get(otherid);
const otherIdentity = otherUser ? otherUser.getIdentity() : ` ${otherid}`;
const otherChallenges = this.get(otherid);
const otherIndex = otherChallenges.indexOf(challenge);
if (otherIndex >= 0)
otherChallenges.splice(otherIndex, 1);
if (otherChallenges.length === 0)
this.delete(otherid);
if (!user && !otherUser)
continue;
const header = `|pm|${userIdentity}|${otherIdentity}|`;
let message = `${header}/challenge`;
if (reason)
message = `${header}/text Challenge cancelled because ${reason}.
${message}`;
user?.send(message);
otherUser?.send(message);
}
this.delete(userid);
return challenges2.length;
}
getUpdate(challenge) {
if (!challenge)
return `/challenge`;
const teambuilderFormat = challenge.ready ? challenge.ready.formatid : "";
return `/challenge ${challenge.format}|${teambuilderFormat}|${challenge.message}|${challenge.acceptButton}|${challenge.rejectButton}`;
}
update(userid1, userid2) {
const challenge = this.search(userid1, userid2);
userid1 = challenge ? challenge.from : userid1;
userid2 = challenge ? challenge.to : userid2;
this.send(userid1, userid2, this.getUpdate(challenge));
}
send(userid1, userid2, message) {
const user1 = Users.get(userid1);
const user2 = Users.get(userid2);
const user1Identity = user1 ? user1.getIdentity() : ` ${userid1}`;
const user2Identity = user2 ? user2.getIdentity() : ` ${userid2}`;
const fullMessage = `|pm|${user1Identity}|${user2Identity}|${message}`;
user1?.send(fullMessage);
user2?.send(fullMessage);
}
updateFor(connection) {
const user = connection.user;
const challenges2 = this.get(user.id);
if (!challenges2)
return;
const userIdentity = user.getIdentity();
let messages = "";
for (const challenge of challenges2) {
let fromIdentity, toIdentity;
if (challenge.from === user.id) {
fromIdentity = userIdentity;
const toUser = Users.get(challenge.to);
toIdentity = toUser ? toUser.getIdentity() : ` ${challenge.to}`;
} else {
const fromUser = Users.get(challenge.from);
fromIdentity = fromUser ? fromUser.getIdentity() : ` ${challenge.from}`;
toIdentity = userIdentity;
}
messages += `|pm|${fromIdentity}|${toIdentity}|${this.getUpdate(challenge)}
`;
}
connection.send(messages);
}
}
const challenges = new Challenges();
//# sourceMappingURL=ladders-challenges.js.map
|