mirror of
https://github.com/smogon/pokemon-showdown.git
synced 2026-03-21 17:25:10 -05:00
Utils#Multiset: Redefine get to remove undefined return (#10329)
This commit is contained in:
parent
bf76033680
commit
89128dee97
|
|
@ -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',
|
||||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -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;
|
||||
|
|
@ -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;
|
||||
},
|
||||
},
|
||||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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 = (
|
||||
|
|
|
|||
|
|
@ -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 = (
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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";
|
||||
|
||||
|
|
@ -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',
|
||||
|
|
@ -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);
|
||||
}
|
||||
},
|
||||
},
|
||||
|
|
@ -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);
|
||||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
@ -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>`;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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."
|
||||
|
|
@ -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!`
|
||||
|
|
|
|||
|
|
@ -123,6 +123,7 @@ export class ModdedDex {
|
|||
|
||||
deepClone = Utils.deepClone;
|
||||
deepFreeze = Utils.deepFreeze;
|
||||
Multiset = Utils.Multiset;
|
||||
|
||||
readonly formats: DexFormats;
|
||||
readonly abilities: DexAbilities;
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user