diff --git a/package.json b/package.json index 587e238..cfc40d8 100755 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "scripts": { "build-dev": "webpack --mode development && npm postbuild", "build": "webpack --mode production", - "postbuild": "rsync -avu --delete dist/ ../kadi_backend/static/game", + "postbuild": "rsync -avu --delete dist/ ../backend/static/game", "start": "webpack-dev-server --mode development", "test": "echo \"Error: no test specified\" && exit 1" }, diff --git a/src/Classes/PlayerScoreCard.ts b/src/Classes/PlayerScoreCard.ts index d2b31f3..d0d1482 100755 --- a/src/Classes/PlayerScoreCard.ts +++ b/src/Classes/PlayerScoreCard.ts @@ -1,7 +1,8 @@ import {ScoreCellValue} from "./ScoreCell"; import ScoreBlock, {createBlockFromDef, BlockState, ScoreBlockJSONRepresentation} from "./ScoreBlock"; -import {BlockDef, Ruleset, CellFlag} from "../../../shared/rulesets"; import { Memento, Originator } from "./Caretaker"; +import {BlockDef, Ruleset} from "../static/rulesets"; +import {CellFlag} from "../static/enums"; export type CellLocation = { blockId: string, cellId: string }; diff --git a/src/Classes/ScoreBlock.ts b/src/Classes/ScoreBlock.ts index 2b608ef..092b03a 100755 --- a/src/Classes/ScoreBlock.ts +++ b/src/Classes/ScoreBlock.ts @@ -4,7 +4,8 @@ import ScoreCell, { CellState, ScoreCellJSONRepresentation } from "./ScoreCell"; -import {CellDef, BlockDef, BonusBlockDef, NoBonusBlockDef, CellFlag } from "../../../shared/rulesets"; +import {BlockDef, BonusBlockDef, CellDef, NoBonusBlockDef} from "../static/rulesets"; +import {CellFlag} from "../static/enums"; export const createBlockFromDef = (blockId: string, blockDef: BlockDef) : ScoreBlock => { if (blockDef.hasBonus) { diff --git a/src/Classes/ScoreCell.ts b/src/Classes/ScoreCell.ts index 92e766c..a98c739 100755 --- a/src/Classes/ScoreCell.ts +++ b/src/Classes/ScoreCell.ts @@ -1,12 +1,5 @@ -import { - BoolCellDef, - CellDef, - CellFlag, - FieldType, - MultiplierCellDef, - NumberCellDef, - SuperkadiCellDef -} from "../../../shared/rulesets"; +import {BoolCellDef, CellDef, FieldType, MultiplierCellDef, NumberCellDef, SuperkadiCellDef} from "../static/rulesets"; +import {CellFlag} from "../static/enums"; export const createCellFromDef = (cellId: string, cellDef: CellDef): ScoreCell => { switch (cellDef.fieldType) { @@ -91,9 +84,11 @@ abstract class ScoreCell { restoreFromJSON(jsonRep: ScoreCellJSONRepresentation): void { this.update(jsonRep.value); } + + abstract setRandom(): void; } -type IterableSequenceValues = boolean | number | CellFlag.strike; +type IterableSequenceValues = boolean | number; abstract class IterableScoreCell extends ScoreCell { protected iteratedSequence: IterableSequenceValues[]; @@ -106,7 +101,10 @@ abstract class IterableScoreCell extends ScoreCell { } update(value: ScoreCellValue | CellFlag): void { - if (value == CellFlag.strike) { + if (value === CellFlag.random) { + this.setRandom(); + } + else if (value == CellFlag.strike) { this.strike(); } else if (this.struck) { @@ -153,22 +151,31 @@ abstract class IterableScoreCell extends ScoreCell { if (this.isStruck()) { this.unstrike(); } - if (valueAtIteratorIndex == CellFlag.strike) { - this.strike(); - } else { this.value = valueAtIteratorIndex; } } -} + setRandom(): void { + const randomInt = Math.round(Math.random() * (this.iteratedSequence.length)) + 1; + if (randomInt >= this.iteratedSequence.length) { + this.strike(); + } + else { + this.unstrike(); + this.value = this.iteratedSequence[randomInt]; + } + } +} class NumberScoreCell extends ScoreCell { protected static readonly fieldType = FieldType.number; + protected readonly max: number; constructor(cellId: string, cellDef: NumberCellDef) { super(cellId, cellDef); this.value = 0; + this.max = cellDef.max; } getScore(): number { @@ -176,7 +183,13 @@ class NumberScoreCell extends ScoreCell { } update(value: ScoreCellValue | CellFlag): void { + if (typeof value === "boolean") { + throw new Error("Number cells cannot be updated with boolean values!"); + } switch (value){ + case CellFlag.random: + this.setRandom(); + break; case CellFlag.strike: this.strike(); break; @@ -185,9 +198,26 @@ class NumberScoreCell extends ScoreCell { break; default: this.value = value; + if (this.value > this.max) { + this.value = this.max; + } + else if (this.value < 0) { + this.value = 0; + } break; } } + + setRandom(): void { + const randomInt = Math.round(Math.random() * (this.max + 10)) + 1; + if (randomInt > this.max) { + this.strike(); + } + else { + this.unstrike(); + this.value = randomInt; + } + } } class BoolScoreCell extends IterableScoreCell { @@ -210,6 +240,17 @@ class BoolScoreCell extends IterableScoreCell { return 0; } } + + setRandom(): void { + const randomInt = Math.round(Math.random()); + if (randomInt > 0) { + this.strike(); + } + else { + this.unstrike(); + this.value = true; + } + } } class SuperkadiScoreCell extends IterableScoreCell { diff --git a/src/Components/Game.tsx b/src/Components/Game.tsx index 4e97003..7494ae6 100755 --- a/src/Components/Game.tsx +++ b/src/Components/Game.tsx @@ -16,6 +16,7 @@ interface GameState { settingUp: boolean; loading: boolean; availablePlayers: Player[] + debug: boolean; } interface GameProps {} @@ -36,6 +37,7 @@ class Game extends React.Component { settingUp: true, loading: true, availablePlayers: [], + debug: Settings.debug, }; } @@ -47,7 +49,7 @@ class Game extends React.Component { console.log(this.state.availablePlayers); } - addNewPlayers(players: Player[]) { + addNewPlayers(players: Player[]): void { const availablePlayers: Player[] = this.state.availablePlayers; availablePlayers.push(...players); this.setState({availablePlayers}); @@ -81,6 +83,7 @@ class Game extends React.Component { ) : ( diff --git a/src/Components/KadiBlockBonusRow.tsx b/src/Components/KadiBlockBonusRow.tsx index cdb7f45..3ba5b60 100755 --- a/src/Components/KadiBlockBonusRow.tsx +++ b/src/Components/KadiBlockBonusRow.tsx @@ -1,7 +1,7 @@ import React, {ReactNode} from "react"; import KadiCell from "./KadiCell"; import {CellScores} from "./KadiBoard"; -import {FieldType} from "../../../shared/rulesets"; +import {FieldType} from "../static/rulesets"; interface KadiBlockBonusRowProps { blockId: string; diff --git a/src/Components/KadiBlockRenderer.tsx b/src/Components/KadiBlockRenderer.tsx index 6a9fe59..c81178c 100755 --- a/src/Components/KadiBlockRenderer.tsx +++ b/src/Components/KadiBlockRenderer.tsx @@ -7,7 +7,7 @@ import KadiBlockTotalRow from "./KadiBlockTotalRow"; import KadiBlockSubtotalRow from "./KadiBlockSubtotalRow"; import KadiBlockBonusRow from "./KadiBlockBonusRow"; import LocaleContext from "../LocaleContext"; -import {BlockDef, FieldType} from "../../../shared/rulesets"; +import {BlockDef, FieldType} from "../static/rulesets"; interface BlockRendererProps { blockId: string; diff --git a/src/Components/KadiBlockSubtotalRow.tsx b/src/Components/KadiBlockSubtotalRow.tsx index 5fa79b9..048e8d4 100755 --- a/src/Components/KadiBlockSubtotalRow.tsx +++ b/src/Components/KadiBlockSubtotalRow.tsx @@ -1,7 +1,7 @@ import React, {ReactNode} from "react"; import KadiCell from "./KadiCell"; import {CellScores} from "./KadiBoard"; -import {FieldType} from "../../../shared/rulesets"; +import {FieldType} from "../static/rulesets"; interface KadiBlockSubtotalRowProps { blockId: string; diff --git a/src/Components/KadiBlockTotalRow.tsx b/src/Components/KadiBlockTotalRow.tsx index eb5f398..82b4458 100755 --- a/src/Components/KadiBlockTotalRow.tsx +++ b/src/Components/KadiBlockTotalRow.tsx @@ -1,7 +1,7 @@ import React, {ReactNode} from "react"; import KadiCell from "./KadiCell"; import {CellScores} from "./KadiBoard"; -import {FieldType} from "../../../shared/rulesets"; +import {FieldType} from "../static/rulesets"; interface KadiBlockTotalRowProps { blockId: string; diff --git a/src/Components/KadiBoard.tsx b/src/Components/KadiBoard.tsx index b666eef..38dc18a 100755 --- a/src/Components/KadiBoard.tsx +++ b/src/Components/KadiBoard.tsx @@ -1,6 +1,6 @@ import React, {ReactElement, ReactNode, useContext} from "react"; import PlayerScoreCard, {CellLocation, PlayerScoreCardJSONRepresentation} from "../Classes/PlayerScoreCard"; -import {getGameSchemaById} from "../static/rulesets"; +import {getGameSchemaById, Ruleset} from "../static/rulesets"; import LocaleContext from "../LocaleContext"; import {ScoreCellValue} from "../Classes/ScoreCell"; import {CaretakerSet} from "../Classes/Caretaker"; @@ -14,7 +14,7 @@ import KadiBlockRenderer from "./KadiBlockRenderer"; import {KadiCellDisplayValue} from "./KadiCell"; import {Player} from "./Game"; import {SERVER_BASE_NAME} from "../index"; -import {CellFlag, Ruleset} from "../../../shared/rulesets"; +import {CellFlag} from "../static/enums"; export interface CellScores { @@ -37,11 +37,13 @@ export interface CellEventResponse { export interface KadiBoardProps { settings: GameSettings; returnToSetup: () => void; + debug: boolean; } interface KadiBoardState { scoreSheet: ScoreSheet; players: Player[]; + debug: boolean; showResults: boolean; savingGame: boolean; locked: boolean; @@ -65,6 +67,7 @@ class KadiBoard extends React.Component { this.state = { scoreSheet: this.generateNewScoreSheet(this.props.settings.players), players: this.props.settings.players, + debug: this.props.debug, showResults: true, savingGame: false, locked: false, @@ -147,7 +150,7 @@ class KadiBoard extends React.Component { ruleset: this.gameSchema.id, players: this.state.players, results: JSONScoreCards - }); + }) } private canSave(): boolean { @@ -182,6 +185,20 @@ class KadiBoard extends React.Component { console.log("Error saving:", error); }; + private fillOutRandomly(): void { + for (const player of this.state.players) { + for (const blockId in this.gameSchema.blocks) { + for (const cellId in this.gameSchema.blocks[blockId].cells) { + this.onCellEdit({ + value: CellFlag.random, + location: {cellId: cellId, blockId: blockId}, + playerId: player.id + }); + } + } + } + } + render(): ReactNode { const Locale = this.context.strings; const rows: ReactElement[] = []; @@ -290,6 +307,17 @@ class KadiBoard extends React.Component { Locale.buttons.saveGameButton.gameSaved : Locale.buttons.saveGameButton.saveGame} + {this.state.debug && ( + + )} diff --git a/src/Components/KadiCell.tsx b/src/Components/KadiCell.tsx index ce9c739..bc057f6 100755 --- a/src/Components/KadiCell.tsx +++ b/src/Components/KadiCell.tsx @@ -3,7 +3,8 @@ import {ScoreCellValue} from "../Classes/ScoreCell"; import {CellEventResponse} from "./KadiBoard"; import {CellLocation} from "../Classes/PlayerScoreCard"; import {useLongPress} from "./useLongPress"; -import {CellFlag, FieldType} from "../../../shared/rulesets"; +import {FieldType} from "../static/rulesets"; +import {CellFlag} from "../static/enums"; export type KadiCellDisplayValue = ScoreCellValue | CellFlag.strike; diff --git a/src/Components/KadiEditableRowCells.tsx b/src/Components/KadiEditableRowCells.tsx index d185dc7..2db98f4 100755 --- a/src/Components/KadiEditableRowCells.tsx +++ b/src/Components/KadiEditableRowCells.tsx @@ -2,7 +2,7 @@ import {CellLocation} from "../Classes/PlayerScoreCard"; import React, {ReactElement} from "react"; import KadiCell from "./KadiCell"; import {CellEventResponse, CellScores} from "./KadiBoard"; -import {FieldType} from "../../../shared/rulesets"; +import {FieldType} from "../static/rulesets"; interface KadiEditableRowCellsProps { location: CellLocation; diff --git a/src/Components/KadiGrandTotalRow.tsx b/src/Components/KadiGrandTotalRow.tsx index fec6ddf..f00e24b 100755 --- a/src/Components/KadiGrandTotalRow.tsx +++ b/src/Components/KadiGrandTotalRow.tsx @@ -3,7 +3,7 @@ import LocaleContext from "../LocaleContext"; import KadiCell from "./KadiCell"; import {Icon} from "semantic-ui-react"; import {CellScores} from "./KadiBoard"; -import {FieldType} from "../../../shared/rulesets"; +import {FieldType} from "../static/rulesets"; interface KadiGrandTotalRowProps { showResults: boolean; diff --git a/src/static/enums.ts b/src/static/enums.ts index 7e8aadf..d3f64cc 100755 --- a/src/static/enums.ts +++ b/src/static/enums.ts @@ -1,6 +1,7 @@ export enum CellFlag { strike = "cellFlagStrike", unstrike = "cellFlagUnstrike", + random = "random", } export enum SupportedLang { diff --git a/src/static/rulesets.ts b/src/static/rulesets.ts index 87f1cd6..1547ddc 100755 --- a/src/static/rulesets.ts +++ b/src/static/rulesets.ts @@ -67,6 +67,7 @@ export interface SuperkadiCellDef extends DefaultCellDef { export interface NumberCellDef extends DefaultCellDef { fieldType: FieldType.number; + max: number, } interface DefaultCellDef { @@ -139,11 +140,13 @@ const gameSchemas: Ruleset[] = [ threeKind: { fieldType: FieldType.number, label: "Three of a Kind", + max: defaultDiceCount * 6, }, fourKind: { fieldType: FieldType.number, label: "Four of a Kind", + max: defaultDiceCount * 6, }, fullHouse: { @@ -174,6 +177,7 @@ const gameSchemas: Ruleset[] = [ chance: { fieldType: FieldType.number, label: "Chance", + max: defaultDiceCount * 6, }, }, }, diff --git a/src/static/settings.json b/src/static/settings.json index 11edc9d..0da6ac6 100755 --- a/src/static/settings.json +++ b/src/static/settings.json @@ -1,4 +1,5 @@ { + "debug": true, "ruleset": "DEFAULT_RULESET", "maxHistoryLength": 256 } \ No newline at end of file