Skip to content

Commit

Permalink
Fix detection of round numbers: Was broken if the first dealer left t…
Browse files Browse the repository at this point in the history
…he table.
  • Loading branch information
uncaught committed Apr 19, 2020
1 parent d731a49 commit 0147614
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 13 deletions.
5 changes: 1 addition & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ cd /var/www/doko
wget https://github.com/uncaught/doko/archive/v1.3.0.tar.gz
tar xf v1.3.0.tar.gz
cd doko-1.3.0
./yarn.sh common install
./yarn.sh client install
./yarn.sh server install
./yarn.sh client build
./yarn.sh common install && ./yarn.sh client install && ./yarn.sh server install && ./yarn.sh client build
cd ..
./backup.sh
docker-compose down
Expand Down
2 changes: 1 addition & 1 deletion client/src/store/Games.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export function useAddGame() {
const players = roundParticipatingPlayers.filter((p) => p.leftAfterGameNumber === null);
const nextDealerId = lastGame ? getNextDealer(players, lastGame) : players[0].groupMemberId;
const data = getDefaultGameData(settings);
data.runNumber = detectRunNumber(currentGames, nextDealerId);
data.runNumber = detectRunNumber(currentGames, nextDealerId, roundParticipatingPlayers);
detectPlayers(data, nextDealerId, players);
detectBockGame(round.data, data, currentGames, nextDealerId);
detectLastGameAndForcedSolo(round.data, data, currentGames, nextDealerId, players, playersWithStats);
Expand Down
18 changes: 10 additions & 8 deletions client/src/store/Games/DetectRunNumber.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import {Game} from '@doko/common';
import {Game, Player} from '@doko/common';

export function detectRunNumber(
sortedGames: Game[],
newGameDealerId: string,
roundParticipatingPlayers: Player[],
): number {
const roundParticipatingPlayerIds = roundParticipatingPlayers.map(({groupMemberId}) => groupMemberId);
const newGameDealerRoundIndex = roundParticipatingPlayerIds.indexOf(newGameDealerId);
let runNumber = 1;
if (sortedGames.length) {
const firstDealer = sortedGames[0].dealerGroupMemberId;
let lastDealer = firstDealer;

sortedGames.forEach(({gameNumber, data, dealerGroupMemberId}, idx) => {
if (dealerGroupMemberId === firstDealer && lastDealer !== firstDealer) {
let lastDealerRoundIndex = 0;
sortedGames.forEach(({dealerGroupMemberId}) => {
const dealerRoundIndex = roundParticipatingPlayerIds.indexOf(dealerGroupMemberId);
if (dealerRoundIndex < lastDealerRoundIndex) {
runNumber++;
}
lastDealer = dealerGroupMemberId;
lastDealerRoundIndex = dealerRoundIndex;
});
if (newGameDealerId === firstDealer && lastDealer !== firstDealer) {
if (newGameDealerRoundIndex < lastDealerRoundIndex) {
runNumber++;
}
}
Expand Down
30 changes: 30 additions & 0 deletions server/src/database/migrations/Version004.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import {query} from '../../Connection';

async function fixBrokenRoundNumbers(update: typeof query) {
await update(`update rounds set data = JSON_REPLACE(data, '$.roundDuration', 7)
where id = '0fe83345-4b63-42fd-b960-5a9ee008ea7e'`);

await update(`update games set data = JSON_REPLACE(data, '$.runNumber', 6)
where id IN (
'c9978373-d63e-472e-a1a9-95f0334d4f24',
'd8e1f301-ad5e-461a-8c98-8f1d21766e13',
'd492906f-e795-4da3-874d-9d8d7700b85e',
'1a354f45-08a4-4dd9-9149-9b9d35f7c77e'
)`);

await update(`update games set data = JSON_REPLACE(data, '$.runNumber', 7)
where id IN (
'8f0d82ce-14d3-41b9-9358-3f8170da2a80',
'8f6be680-674f-420a-9c66-91f141fbcda4',
'5c1ed61b-f0c9-43b7-86e0-2ad205735685',
'ff13004f-3185-4171-a028-7e6cc7eb3758'
)`);

await update(`update games set data = JSON_REPLACE(data, '$.isLastGame', TRUE)
where id ='ff13004f-3185-4171-a028-7e6cc7eb3758'`);
}

export async function run(update: typeof query) {
console.log(`Running 004 ...`);
await fixBrokenRoundNumbers(update);
}

0 comments on commit 0147614

Please sign in to comment.