Skip to content

Commit 2797419

Browse files
Dean SoferDean Sofer
authored andcommitted
Fixed some moves
1 parent d4005aa commit 2797419

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-11
lines changed

src/Utils.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -209,35 +209,37 @@ export function calculate(state: GameType, from: number | Color | undefined | nu
209209
return { state };
210210
}
211211
} else {
212-
const fromInt = parseInt(from)
212+
if (from === undefined || from === null) return { state };
213+
if (typeof from === 'string')
214+
from = parseInt(from)
213215
// TODO: indexToPoint needs a color, but we don't have it for local games
214-
const offense = nextGame.board[fromInt];
216+
const offense = nextGame.board[from];
215217
const defense = nextGame.board[to];
218+
const player = Math.sign(defense) === 1 ? Color.White : Color.Black;
216219
if (defense === undefined) {
217220
// bear off
218221
moveLabel = `${from}/off`;
219222
if (offense > 0) {
220223
// White
221-
usedDie = HOME_INDEXES.white[1] - fromInt + 1;
224+
usedDie = HOME_INDEXES.white[1] - from + 1;
222225
nextGame.home.white++;
223226
} else {
224227
// Black
225-
usedDie = HOME_INDEXES.black[1] - fromInt + 1;
228+
usedDie = HOME_INDEXES.black[1] - from + 1;
226229
nextGame.home.black++;
227230
}
228231
} else if (!defense || Math.sign(defense) === Math.sign(offense)) {
229232
// move
230-
moveLabel = `${fromInt}/${to}`;
231-
const player = state.color;
233+
234+
moveLabel = `${from}/${to}`;
232235
const dice = [...state.dice];
233-
usedDie = dice.find(die => destination(player, fromInt, die) === to);
236+
usedDie = dice.find(die => destination(player, from, die) === to);
234237
nextGame.board[to] += Math.sign(offense);
235238
} else if (Math.abs(defense) === 1) {
236239
// hit
237-
moveLabel = `${fromInt}/${to}*`;
238-
const player = state.color;
240+
moveLabel = `${from}/${to}*`;
239241
const dice = [...state.dice];
240-
usedDie = dice.find(die => destination(player, fromInt, die) === to);
242+
usedDie = dice.find(die => destination(player, from, die) === to);
241243
nextGame.board[to] = -Math.sign(defense);
242244
if (offense > 0) nextGame.prison.black++;
243245
else nextGame.prison.white++;
@@ -246,7 +248,7 @@ export function calculate(state: GameType, from: number | Color | undefined | nu
246248
return { state };
247249
}
248250
// remove from previous position
249-
nextGame.board[fromInt] -= Math.sign(nextGame.board[fromInt]);
251+
nextGame.board[from] -= Math.sign(nextGame.board[from]);
250252
if (nextGame.home.white === 15 || nextGame.home.black === 15)
251253
nextGame.status = Status.GameOver;
252254
}

src/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ export function App() {
114114
console.log('Creating user', data);
115115
userRef.set(data);
116116
saveFcmToken();
117+
} else if (!snapshot.val().fcmToken) {
118+
saveFcmToken();
117119
}
118120
userRef.on('value', user => {
119121
setUser(user);

0 commit comments

Comments
 (0)