@pkmn/randoms 0.6.4 → 0.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/global.d.ts DELETED
@@ -1,6 +0,0 @@
1
- /* eslint-disable no-var */
2
- export {};
3
-
4
- declare global {
5
- var Config: {potd?: string; allowrequestingties?: boolean};
6
- }
package/src/index.ts DELETED
@@ -1,46 +0,0 @@
1
- import {
2
- PlayerOptions,
3
- PokemonSet,
4
- PRNG,
5
- PRNGSeed,
6
- Dex,
7
- Format,
8
- ModdedDex,
9
- } from '@pkmn/sim';
10
-
11
- import {RandomGen1Teams} from './gen1';
12
- import {RandomGen2Teams} from './gen2';
13
- import {RandomGen3Teams} from './gen3';
14
- import {RandomGen4Teams} from './gen4';
15
- import {RandomGen5Teams} from './gen5';
16
- import {RandomGen6Teams} from './gen6';
17
- import {RandomGen7Teams} from './gen7';
18
- import {RandomTeams} from './gen8';
19
-
20
- const GENERATORS: {[mod: string]: new(dex: ModdedDex, format: Format, seed: PRNG | PRNGSeed | null) => TeamGenerator} = {
21
- gen1: RandomGen1Teams,
22
- gen2: RandomGen2Teams,
23
- gen3: RandomGen3Teams,
24
- gen4: RandomGen4Teams,
25
- gen5: RandomGen5Teams,
26
- gen6: RandomGen6Teams,
27
- gen7: RandomGen7Teams,
28
- gen8: RandomTeams,
29
- };
30
-
31
- interface TeamGenerator {
32
- prng: PRNG;
33
- getTeam(options?: PlayerOptions): PokemonSet[];
34
- setSeed(prng?: PRNG | PRNGSeed): void;
35
- }
36
-
37
- export const TeamGenerators = new class {
38
- getTeamGenerator(format: Format | string, seed: PRNG | PRNGSeed | null = null) {
39
- format = Dex.formats.get(format);
40
- if (!format.exists) throw new Error(`Unknown format '${format.name}' - does not exist`);
41
- if (format.team !== 'random') throw new Error(`Unsupported format '${format.name}`);
42
- const Generator = format.mod && GENERATORS[format.mod];
43
- if (!Generator) throw new Error(`Unknown format '${format.name}' - cannot find generator`);
44
- return new Generator(Dex.mod(format.mod), format, seed);
45
- }
46
- };
package/src/utils.ts DELETED
@@ -1,78 +0,0 @@
1
- type Comparable = number | string | boolean | Comparable[] | {reverse: Comparable};
2
-
3
- /** Forces num to be an integer (between min and max). */
4
- function clampIntRange(num: any, min?: number, max?: number): number {
5
- if (typeof num !== 'number') num = 0;
6
- num = Math.floor(num);
7
- if (min !== undefined && num < min) num = min;
8
- if (max !== undefined && num > max) num = max;
9
- return num;
10
- }
11
- /**
12
- * Compares two variables; intended to be used as a smarter comparator.
13
- * The two variables must be the same type (TypeScript will not check this).
14
- *
15
- * - Numbers are sorted low-to-high, use `-val` to reverse
16
- * - Strings are sorted A to Z case-semi-insensitively, use `{reverse: val}` to reverse
17
- * - Booleans are sorted true-first (REVERSE of casting to numbers), use `!val` to reverse
18
- * - Arrays are sorted lexically in the order of their elements
19
- *
20
- * In other words: `[num, str]` will be sorted A to Z, `[num, {reverse: str}]` will be sorted Z to A.
21
- */
22
- function compare(a: Comparable, b: Comparable): number {
23
- if (typeof a === 'number') {
24
- return a - (b as number);
25
- }
26
- if (typeof a === 'string') {
27
- return a.localeCompare(b as string);
28
- }
29
- if (typeof a === 'boolean') {
30
- return (a ? 1 : 2) - (b ? 1 : 2);
31
- }
32
- if (Array.isArray(a)) {
33
- for (let i = 0; i < a.length; i++) {
34
- const comparison = compare(a[i], (b as Comparable[])[i]);
35
- if (comparison) return comparison;
36
- }
37
- return 0;
38
- }
39
- if ('reverse' in a) {
40
- return compare((b as {reverse: string}).reverse, a.reverse);
41
- }
42
- throw new Error(`Passed value ${a} is not comparable`);
43
- }
44
-
45
- /**
46
- * Sorts an array according to the callback's output on its elements.
47
- *
48
- * The callback's output is compared according to `PSUtils.compare`
49
- * (numbers low to high, strings A-Z, booleans true-first, arrays in order).
50
- */
51
- function sortBy<T>(array: T[], callback: (a: T) => Comparable): T[];
52
- /**
53
- * Sorts an array according to `PSUtils.compare`
54
- * (numbers low to high, strings A-Z, booleans true-first, arrays in order).
55
- *
56
- * Note that array.sort() only works on strings, not numbers, so you'll need
57
- * this to sort numbers.
58
- */
59
- function sortBy<T extends Comparable>(array: T[]): T[];
60
- function sortBy<T>(array: T[], callback?: (a: T) => Comparable) {
61
- if (!callback) return (array as any[]).sort(compare);
62
- return array.sort((a, b) => compare(callback(a), callback(b)));
63
- }
64
-
65
- class Multiset<T> extends Map<T, number> {
66
- add(key: T) {
67
- this.set(key, (this.get(key) ?? 0) + 1);
68
- return this;
69
- }
70
- remove(key: T) {
71
- const newValue = (this.get(key) ?? 0) - 1;
72
- if (newValue <= 0) return this.delete(key);
73
- this.set(key, newValue);
74
- return true;
75
- }
76
- }
77
-
78
- export const Utils = {clampIntRange, compare, sortBy, Multiset};