-
Notifications
You must be signed in to change notification settings - Fork 2
/
Player.ts
227 lines (205 loc) · 4.8 KB
/
Player.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
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
import User from "../user/User";
const full = {
successful: false,
// not needed for init
gameoverreason: null,
replay: {},
source: {},
stats: {
lines: 0,
level_lines: 0,
level_lines_needed: 1,
inputs: 0,
holds: 0,
time: {
start: 0,
zero: true,
locked: false,
prev: 0,
frameoffset: 0,
},
score: 0,
zenlevel: 1,
zenprogress: 0,
level: 1,
combo: 0,
currentcombopower: 0,
topcombo: 0,
btb: 0,
topbtb: 0,
currentbtbchainpower: 0,
tspins: 0,
piecesplaced: 0,
clears: {
singles: 0,
doubles: 0,
triples: 0,
quads: 0,
pentas: 0,
realtspins: 0,
minitspins: 0,
minitspinsingles: 0,
tspinsingles: 0,
minitspindoubles: 0,
tspindoubles: 0,
tspintriples: 0,
tspinquads: 0,
tspinpentas: 0,
allclear: 0,
},
garbage: {
sent: 0,
received: 0,
attack: 0,
cleared: 0,
},
kills: 0,
finesse: {
combo: 0,
faults: 0,
perfectpieces: 0,
},
},
diyusi: 0,
enemies: [],
targets: [],
// not needed for init
fire: 0,
game: {
hold: {
piece: null,
locked: false,
},
controlling: {
ldas: 0,
ldasiter: 0,
lshift: false,
rdas: 0,
rdasiter: 0,
rshift: false,
lastshift: 0,
softdrop: false,
},
playing: true,
},
killer: {
gameid: null,
name: null,
type: "sizzle",
},
aggregatestats: {
apm: 0,
pps: 0,
vsscore: 0,
},
};
interface Tetrominos {
[piece: string]: { width: number; orientation: (null | string)[][][] };
}
export default class Player {
constructor(player: any, user: User) {
this.id = player.gameid;
this.user = user;
this.options = player.options;
this.board = new Array(player.options.boardheight + player.options.boardbuffer).fill(
new Array(player.options.boardwidth).fill(null)
);
this.resetBag();
this.replayFrames.push({
frame: 0,
type: "full",
data: {
...full,
options: this.options,
stats: { ...full.stats, seed: this.options.seed },
game: {
...full.game,
bag: this.nextPieces,
board: this.board,
g: this.options.g,
handling: this.options.handling,
},
},
});
this.resetBag();
}
public id: string;
public user: User;
public options: any;
public board: any;
public replayFrames: any[] = [];
private bag = ["z", "l", "o", "s", "i", "j", "t"];
private currentBag?: string[] = [];
private lastGenerated?: number;
private t = 0;
private bagQueue: string[][] = [];
private next(): number {
return (this.t = (16807 * this.t) % 2147483647);
}
private nextFloat(): number {
return (this.next() - 1) / 2147483646;
}
private shuffleArray(array: string[]): string[] {
if (array.length == 0) {
return array;
}
for (let i = array.length - 1; i != 0; i--) {
const r = Math.floor(this.nextFloat() * (i + 1));
[array[i], array[r]] = [array[r], array[i]];
}
return array;
}
public resetBag() {
this.bag = ["z", "l", "o", "s", "i", "j", "t"];
this.lastGenerated = undefined;
this.t = this.options.seed % 2147483647;
if (this.t <= 0) this.t += 2147483646;
this.bagQueue = [];
}
private nextPiece(): void {
this.currentBag?.shift();
if (!this.currentBag || this.currentBag.length <= 0) {
if (this.bagQueue.length <= 0) {
this.nextPieces;
}
this.currentBag = this.bagQueue.shift();
}
}
public get currentPiece(): string | undefined {
if (!this.currentBag || this.currentBag.length <= 0) this.nextPiece();
if (this.currentBag) return this.currentBag[0];
}
public get nextPieces(): string[] {
let bag: string[];
switch (this.options.bagtype) {
case "7-bag":
bag = this.shuffleArray(this.bag);
break;
case "14-bag":
bag = this.shuffleArray(this.bag.concat(this.bag));
break;
case "classic":
let index = Math.floor(this.nextFloat() * (this.bag.length + 1));
if (index === this.lastGenerated || index >= this.bag.length) {
index = Math.floor(this.nextFloat() * this.bag.length);
}
this.lastGenerated = index;
bag = [this.bag[index]];
break;
case "pairs":
let s = this.shuffleArray(["z", "l", "o", "s", "i", "j", "t"]);
let pairs = [s[0], s[0], s[0], s[1], s[1], s[1]];
this.shuffleArray(pairs);
bag = pairs;
break;
case "total mayhem":
bag = [this.bag[Math.floor(this.nextFloat() * this.bag.length)]];
break;
default:
bag = this.bag;
break;
}
this.bagQueue.push(bag.slice());
return bag;
}
}