Skip to content

Commit

Permalink
Utils#Multiset: Redefine get to remove undefined return (smogon#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
KrisXV authored Jun 6, 2024
1 parent bf76033 commit 89128de
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 36 deletions.
20 changes: 10 additions & 10 deletions config/formats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ export const Formats: FormatList = [
},
onValidateTeam(team, f, teamHas) {
if (this.ruleTable.has('abilityclause')) {
const abilityTable = new Map<string, number>();
const abilityTable = new this.dex.Multiset<string>();
const base: {[k: string]: string} = {
airlock: 'cloudnine',
armortail: 'queenlymajesty',
Expand All @@ -860,13 +860,13 @@ export const Formats: FormatList = [
let ability = this.toID(set.ability.split('0')[0]);
if (!ability) continue;
if (ability in base) ability = base[ability] as ID;
if ((abilityTable.get(ability) || 0) >= num) {
if (abilityTable.get(ability) >= num) {
return [
`You are limited to ${num} of each ability by ${num} Ability Clause.`,
`(You have more than ${num} ${this.dex.abilities.get(ability).name} variants)`,
];
}
abilityTable.set(ability, (abilityTable.get(ability) || 0) + 1);
abilityTable.add(ability);
}
}

Expand Down Expand Up @@ -2158,9 +2158,9 @@ export const Formats: FormatList = [
],
onValidateTeam(team, format, teamHas) {
const problems = [];
for (const trademark in teamHas.trademarks) {
if (teamHas.trademarks[trademark] > 1) {
problems.push(`You are limited to 1 of each Trademark.`, `(You have ${teamHas.trademarks[trademark]} Pok\u00e9mon with ${trademark} as a Trademark.)`);
for (const trademark of teamHas.trademarks.keys()) {
if (teamHas.trademarks.get(trademark) > 1) {
problems.push(`You are limited to 1 of each Trademark.`, `(You have ${teamHas.trademarks.get(trademark)} Pok\u00e9mon with ${trademark} as a Trademark.)`);
}
}
return problems;
Expand Down Expand Up @@ -2199,8 +2199,8 @@ export const Formats: FormatList = [
set.ability = 'No Ability';
problems = problems.concat(validator.validateSet(set, teamHas) || []);
set.ability = ability.id;
if (!teamHas.trademarks) teamHas.trademarks = {};
teamHas.trademarks[ability.name] = (teamHas.trademarks[ability.name] || 0) + 1;
if (!teamHas.trademarks) teamHas.trademarks = new this.dex.Multiset<string>();
teamHas.trademarks.add(ability.name);
return problems.length ? problems : null;
},
},
Expand Down Expand Up @@ -2424,11 +2424,11 @@ export const Formats: FormatList = [
restricted: ['Arceus'],
onValidateTeam(team, format) {
// baseSpecies:count
const restrictedPokemonCount = new Map<string, number>();
const restrictedPokemonCount = new this.dex.Multiset<string>();
for (const set of team) {
const species = this.dex.species.get(set.species);
if (!this.ruleTable.isRestrictedSpecies(species)) continue;
restrictedPokemonCount.set(species.baseSpecies, (restrictedPokemonCount.get(species.baseSpecies) || 0) + 1);
restrictedPokemonCount.add(species.baseSpecies);
}
for (const [baseSpecies, count] of restrictedPokemonCount) {
if (count > 1) {
Expand Down
4 changes: 0 additions & 4 deletions data/random-battles/gen8/teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ export class MoveCounter extends Utils.Multiset<string> {
this.damagingMoves = new Set();
this.setupType = '';
}

get(key: string): number {
return super.get(key) || 0;
}
}

type MoveEnforcementChecker = (
Expand Down
4 changes: 0 additions & 4 deletions data/random-battles/gen9/teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ export class MoveCounter extends Utils.Multiset<string> {
this.damagingMoves = new Set();
this.ironFist = 0;
}

get(key: string): number {
return super.get(key) || 0;
}
}

type MoveEnforcementChecker = (
Expand Down
10 changes: 5 additions & 5 deletions data/random-battles/gen9baby/teams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -770,7 +770,7 @@ export class RandomBabyTeams extends RandomTeams {

// Limit two of any type
for (const typeName of types) {
if ((typeCount.get(typeName) || 0) >= 2 * limitFactor) {
if (typeCount.get(typeName) >= 2 * limitFactor) {
skip = true;
break;
}
Expand All @@ -781,13 +781,13 @@ export class RandomBabyTeams extends RandomTeams {
for (const typeName of this.dex.types.names()) {
// it's weak to the type
if (this.dex.getEffectiveness(typeName, species) > 0) {
if ((typeWeaknesses.get(typeName) || 0) >= 3 * limitFactor) {
if (typeWeaknesses.get(typeName) >= 3 * limitFactor) {
skip = true;
break;
}
}
if (this.dex.getEffectiveness(typeName, species) > 1) {
if ((typeDoubleWeaknesses.get(typeName) || 0) >= 1 * limitFactor) {
if (typeDoubleWeaknesses.get(typeName) >= 1 * limitFactor) {
skip = true;
break;
}
Expand All @@ -797,12 +797,12 @@ export class RandomBabyTeams extends RandomTeams {

// Limit four weak to Freeze-Dry
if (weakToFreezeDry) {
if ((typeWeaknesses.get('Freeze-Dry') || 0) >= 4 * limitFactor) continue;
if (typeWeaknesses.get('Freeze-Dry') >= 4 * limitFactor) continue;
}
}

// Limit three of any type combination in Monotype
if (!this.forceMonotype && isMonotype && ((typeComboCount.get(typeCombo) || 0) >= 3 * limitFactor)) continue;
if (!this.forceMonotype && isMonotype && typeComboCount.get(typeCombo) >= 3 * limitFactor) continue;

const set: RandomTeamsTypes.RandomSet = this.randomSet(species, teamDetails, false, false);
pokemon.push(set);
Expand Down
11 changes: 5 additions & 6 deletions data/rulesets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Note: These are the rules that formats use

import {Utils} from "../lib";
import type {Learnset} from "../sim/dex-species";
import {Pokemon} from "../sim/pokemon";

Expand Down Expand Up @@ -812,7 +811,7 @@ export const Rulesets: {[k: string]: FormatData} = {
},
onValidateTeam(team) {
if (this.format.id === 'gen8multibility') return;
const abilityTable = new Map<string, number>();
const abilityTable = new this.dex.Multiset<string>();
const base: {[k: string]: string} = {
airlock: 'cloudnine',
armortail: 'queenlymajesty',
Expand All @@ -838,13 +837,13 @@ export const Rulesets: {[k: string]: FormatData} = {
let ability = this.toID(set.ability);
if (!ability) continue;
if (ability in base) ability = base[ability] as ID;
if ((abilityTable.get(ability) || 0) >= num) {
if (abilityTable.get(ability) >= num) {
return [
`You are limited to ${num} of each ability by Ability Clause.`,
`(You have more than ${num} ${this.dex.abilities.get(ability).name} variant${num === 1 ? '' : 's'})`,
];
}
abilityTable.set(ability, (abilityTable.get(ability) || 0) + 1);
abilityTable.add(ability);
}
},
},
Expand Down Expand Up @@ -1590,7 +1589,7 @@ export const Rulesets: {[k: string]: FormatData} = {
return null;
},
onValidateTeam(team) {
const sketches = new Utils.Multiset<string>();
const sketches = new this.dex.Multiset<string>();
for (const set of team) {
if ((set as any).sketchMove) {
sketches.add((set as any).sketchMove);
Expand Down Expand Up @@ -2697,7 +2696,7 @@ export const Rulesets: {[k: string]: FormatData} = {
}
},
onValidateTeam(team, format) {
const donors = new Utils.Multiset<string>();
const donors = new this.dex.Multiset<string>();
for (const set of team) {
const species = this.dex.species.get(set.species);
const fusion = this.dex.species.get(set.name);
Expand Down
7 changes: 5 additions & 2 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,12 +414,15 @@ export function formatSQLArray(arr: unknown[], args?: unknown[]) {
}

export class Multiset<T> extends Map<T, number> {
get(key: T) {
return super.get(key) ?? 0;
}
add(key: T) {
this.set(key, (this.get(key) ?? 0) + 1);
this.set(key, this.get(key) + 1);
return this;
}
remove(key: T) {
const newValue = (this.get(key) ?? 0) - 1;
const newValue = this.get(key) - 1;
if (newValue <= 0) return this.delete(key);
this.set(key, newValue);
return true;
Expand Down
2 changes: 1 addition & 1 deletion server/chat-plugins/chat-monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ export const namefilter: Chat.NameFilter = (name, user) => {
if (Punishments.namefilterwhitelist.has(id)) return name;
if (Monitor.forceRenames.has(id)) {
if (typeof Monitor.forceRenames.get(id) === 'number') {
// we check this for hotpatching reasons, since on the initial chat patch this will still be a Utils.MultiSet
// we check this for hotpatching reasons, since on the initial chat patch this will still be a Utils.Multiset
// we're gonna assume no one has seen it since that covers people who _haven't_ actually, and those who have
// likely will not be attempting to log into it
Monitor.forceRenames.set(id, false);
Expand Down
4 changes: 2 additions & 2 deletions server/chat-plugins/modlog-viewer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ export const pages: Chat.PageTable = {
if (entry.ip) {
let ipTable = punishmentsByIp.get(entry.ip);
if (!ipTable) {
ipTable = new Utils.Multiset();
ipTable = new Utils.Multiset<string>();
punishmentsByIp.set(entry.ip, ipTable);
}
ipTable.add(entry.action);
Expand Down Expand Up @@ -448,7 +448,7 @@ export const pages: Chat.PageTable = {
for (const [ip, table] of punishmentsByIp) {
buf += `<tr><td><a href="https://whatismyipaddress.com/ip/${ip}">${ip}</a></td>`;
for (const key of keys) {
buf += `<td>${table.get(key) || 0}</td>`;
buf += `<td>${table.get(key)}</td>`;
}
buf += `</tr>`;
}
Expand Down
4 changes: 2 additions & 2 deletions server/chat-plugins/wifi.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ export class QuestionGiveaway extends Giveaway {
if (Giveaway.checkBanned(this.room, user)) return user.sendTo(this.room, "You are banned from entering giveaways.");
if (this.checkExcluded(user)) return user.sendTo(this.room, "You are disallowed from entering the giveaway.");

if ((this.answered.get(user.id) ?? 0) >= 3) {
if (this.answered.get(user.id) >= 3) {
return user.sendTo(
this.room,
"You have already guessed three times. You cannot guess anymore in this.giveaway."
Expand All @@ -468,7 +468,7 @@ export class QuestionGiveaway extends Giveaway {

this.joined.set(user.latestIp, user.id);
this.answered.add(user.id);
if ((this.answered.get(user.id) ?? 0) >= 3) {
if (this.answered.get(user.id) >= 3) {
user.sendTo(
this.room,
`Your guess '${guess}' is wrong. You have used up all of your guesses. Better luck next time!`
Expand Down
1 change: 1 addition & 0 deletions sim/dex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ export class ModdedDex {

deepClone = Utils.deepClone;
deepFreeze = Utils.deepFreeze;
Multiset = Utils.Multiset;

readonly formats: DexFormats;
readonly abilities: DexAbilities;
Expand Down

0 comments on commit 89128de

Please sign in to comment.