-
Notifications
You must be signed in to change notification settings - Fork 702
/
random.ts
198 lines (171 loc) · 4.86 KB
/
random.ts
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
/*
* Copyright 2017 The boardgame.io Authors
*
* Use of this source code is governed by a MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/
import type { AleaState } from './random.alea';
import { alea } from './random.alea';
export interface RandomState {
seed: string | number;
prngstate?: AleaState;
}
export interface RandomAPI {
D4(): number;
D4(diceCount: number): number[];
D6(): number;
D6(diceCount: number): number[];
D10(): number;
D10(diceCount: number): number[];
D12(): number;
D12(diceCount: number): number[];
D20(): number;
D20(diceCount: number): number[];
Die(spotvalue?: number): number;
Die(spotvalue: number, diceCount: number): number[];
Number(): number;
Shuffle<T>(deck: T[]): T[];
}
export interface PrivateRandomAPI {
_obj: {
isUsed(): boolean;
getState(): RandomState;
};
}
/**
* Random
*
* Calls that require a pseudorandom number generator.
* Uses a seed from ctx, and also persists the PRNG
* state in ctx so that moves can stay pure.
*/
export class Random {
state: RandomState;
used: boolean;
/**
* Generates a new seed from the current date / time.
*/
static seed() {
return Date.now().toString(36).slice(-10);
}
/**
* constructor
* @param {object} ctx - The ctx object to initialize from.
*/
constructor(state?: RandomState) {
// If we are on the client, the seed is not present.
// Just use a temporary seed to execute the move without
// crashing it. The move state itself is discarded,
// so the actual value doesn't matter.
this.state = state || { seed: '0' };
this.used = false;
}
isUsed() {
return this.used;
}
getState() {
return this.state;
}
/**
* Generate a random number.
*/
_random() {
this.used = true;
const R = this.state;
const seed = R.prngstate ? '' : R.seed;
const rand = alea(seed, R.prngstate);
const number = rand();
this.state = {
...R,
prngstate: rand.state(),
};
return number;
}
api(): RandomAPI & PrivateRandomAPI {
const random: Random['_random'] = this._random.bind(this);
const SpotValue = {
D4: 4,
D6: 6,
D8: 8,
D10: 10,
D12: 12,
D20: 20,
};
type DieFn = {
(): number;
(diceCount: number): number[];
};
// Generate functions for predefined dice values D4 - D20.
const predefined = {} as Record<keyof typeof SpotValue, DieFn>;
for (const key in SpotValue) {
const spotvalue = SpotValue[key];
predefined[key] = (diceCount?: number) => {
return diceCount === undefined
? Math.floor(random() * spotvalue) + 1
: [...new Array(diceCount).keys()].map(
() => Math.floor(random() * spotvalue) + 1
);
};
}
function Die(spotValue?: number): number;
function Die(spotValue: number, diceCount: number): number[];
function Die(spotvalue = 6, diceCount?: number) {
return diceCount === undefined
? Math.floor(random() * spotvalue) + 1
: [...new Array(diceCount).keys()].map(
() => Math.floor(random() * spotvalue) + 1
);
}
return {
/**
* Similar to Die below, but with fixed spot values.
* Supports passing a diceCount
* if not defined, defaults to 1 and returns the value directly.
* if defined, returns an array containing the random dice values.
*
* D4: (diceCount) => value
* D6: (diceCount) => value
* D8: (diceCount) => value
* D10: (diceCount) => value
* D12: (diceCount) => value
* D20: (diceCount) => value
*/
...predefined,
/**
* Roll a die of specified spot value.
*
* @param {number} spotvalue - The die dimension (default: 6).
* @param {number} diceCount - number of dice to throw.
* if not defined, defaults to 1 and returns the value directly.
* if defined, returns an array containing the random dice values.
*/
Die,
/**
* Generate a random number between 0 and 1.
*/
Number: () => {
return random();
},
/**
* Shuffle an array.
*
* @param {Array} deck - The array to shuffle. Does not mutate
* the input, but returns the shuffled array.
*/
Shuffle: <T extends any>(deck: T[]) => {
const clone = deck.slice(0);
let srcIndex = deck.length;
let dstIndex = 0;
const shuffled = new Array<T>(srcIndex);
while (srcIndex) {
const randIndex = Math.trunc(srcIndex * random());
shuffled[dstIndex++] = clone[randIndex];
clone[randIndex] = clone[--srcIndex];
}
return shuffled;
},
_obj: this,
};
}
}