/
store.js
145 lines (142 loc) · 3.77 KB
/
store.js
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
import { Cell } from "./js/objects/Cell";
import { Grid } from "./js/objects/Grid";
import { Piece } from "./js/objects/Piece";
const state = {
score: 0,
bankPieces: [
new Piece(Piece.PIECE_NONE.type),
new Piece(Piece.PIECE_NONE.type),
new Piece(Piece.PIECE_NONE.type)
],
backupPiece: new Piece(Piece.PIECE_NONE.type),
grid: new Grid(),
changes: {
grid: {
previous: 0,
value: 1,
},
bank: {
previous: 0,
value: 1,
}
},
history: [
]
};
export const store = {
state,
mutate: {
undo () {
const {
score = state.score,
bankPieces,
backupPiece,
grid
} = state.history.pop() || {};
if(bankPieces && backupPiece && grid) {
store.mutate.restart();
state.score = score;
state.bankPieces.forEach((piece, index) => piece.setState(bankPieces[index]));
state.backupPiece.setState(backupPiece);
state.grid.setState(grid);
state.changes.grid.value += 1;
state.changes.bank.value += 1;
}
},
saveHistory () {
const {
score,
bankPieces,
backupPiece,
grid
} = state;
state.history.push({
score,
bankPieces: bankPieces.map(piece => piece.getState()),
backupPiece: backupPiece.getState(),
grid: grid.previousCells.map(({x, y, value}) => ({
x, y, value: value === Cell.SET ? Cell.SET : Cell.UNSET
})),
});
},
resetBank () {
state.bankPieces.forEach(piece => piece.shuffle());
state.changes.bank.value += 1;
},
restart () {
state.grid.reset();
state.score = 0;
store.mutate.resetBank();
state.backupPiece.disable();
state.changes.grid.value += 1;
},
hasRendered (target) {
state.changes[target].previous = state.changes[target].value;
},
bumpScoreBy (points) {
state.score += points;
},
setBackupPiece (piece) {
state.changes.bank.value += 1;
state.backupPiece.copyPiece(piece);
},
removeFromBank (piece) {
state.changes.bank.value += 1;
piece.disable();
if (state.bankPieces.filter(({type}) => type === Piece.PIECE_NONE.type).length === 3) {
store.mutate.resetBank();
}
},
checkWins () {
// find full lines (two passes for 2d overlaps)
const rows = []
const cols = []
for (let y = 0; y < 10; y += 1) {
let row = 0;
let col = 0;
for (let x = 0; x < 10; x += 1) {
if (state.grid.getCellValueAtPoint(x, y) === Cell.SET) {
row += 1;
}
if (state.grid.getCellValueAtPoint(y, x) === Cell.SET) {
col += 1;
}
}
rows.push(row);
cols.push(col);
}
// unset complete lines
for (let y = 0; y < 10; y += 1) {
if (rows[y] == 10) {
// full row
for (let x = 0; x < 10; x += 1) {
state.grid.setCellValueAtPoint(x, y, Cell.UNSET);
}
}
if (cols[y] == 10) {
// full col
for (let x = 0; x < 10; x += 1) {
state.grid.setCellValueAtPoint(y, x, Cell.UNSET);
}
}
}
},
},
getters: {
getPieceById: pieceId => {
switch (pieceId) {
case 'bank-piece-1':
return state.bankPieces[0];
case 'bank-piece-2':
return state.bankPieces[1];
case 'bank-piece-3':
return state.bankPieces[2];
case 'backup-piece':
return state.backupPiece;
}
},
canAcceptBackup: () => state.backupPiece.type === Piece.PIECE_NONE.type,
gridChanged: () => state.changes.grid.previous !== state.changes.grid.value,
bankChanged: () => state.changes.bank.previous !== state.changes.bank.value,
}
}