From 166ec7525aefa9cc71a387f8404c48fcc3d9b9f2 Mon Sep 17 00:00:00 2001 From: Daniel Ledda Date: Tue, 26 May 2020 11:11:01 +0200 Subject: [PATCH] Changed the definition of the rulesets so there are no arrays. --- src/Classes/PlayerScoreCard.ts | 6 +- src/Classes/ScoreBlock.ts | 24 +- src/Classes/ScoreCell.ts | 34 +- src/Components/KadiBlockRenderer.tsx | 31 +- src/Components/KadiBoard.tsx | 26 +- src/static/rulesets.ts | 479 +++++++++++++-------------- src/static/settings.json | 2 +- 7 files changed, 298 insertions(+), 304 deletions(-) diff --git a/src/Classes/PlayerScoreCard.ts b/src/Classes/PlayerScoreCard.ts index ed1b618..363fd90 100755 --- a/src/Classes/PlayerScoreCard.ts +++ b/src/Classes/PlayerScoreCard.ts @@ -15,10 +15,10 @@ class PlayerScoreCard implements Originator { this.blocks = PlayerScoreCard.generateBlocks(gameSchema.blocks); } - private static generateBlocks(blockDefs: BlockDef[]): ScoreBlock[] { + private static generateBlocks(blockDefs: Record): ScoreBlock[] { const blocks = []; - for (const blockDef of blockDefs) { - blocks.push(createBlockFromDef(blockDef)); + for (const blockId in blockDefs) { + blocks.push(createBlockFromDef(blockId, blockDefs[blockId])); } return blocks; } diff --git a/src/Classes/ScoreBlock.ts b/src/Classes/ScoreBlock.ts index 2ca582e..54bca80 100755 --- a/src/Classes/ScoreBlock.ts +++ b/src/Classes/ScoreBlock.ts @@ -7,12 +7,12 @@ import ScoreCell, { import {CellDef, BlockDef, BonusBlockDef, NoBonusBlockDef } from "../static/rulesets"; import {CellFlag} from "../static/enums"; -export const createBlockFromDef = (blockDef: BlockDef) : ScoreBlock => { +export const createBlockFromDef = (blockId: string, blockDef: BlockDef) : ScoreBlock => { if (blockDef.hasBonus) { - return new ScoreBlockWithBonus(blockDef); + return new ScoreBlockWithBonus(blockId, blockDef); } else { - return new ScoreBlockNoBonus(blockDef); + return new ScoreBlockNoBonus(blockId, blockDef); } }; @@ -30,15 +30,15 @@ abstract class ScoreBlock { protected cells: ScoreCell[]; protected id: string; - protected constructor(blockDef: BlockDef) { + protected constructor(blockId: string, blockDef: BlockDef) { this.cells = ScoreBlock.generateCells(blockDef.cells); - this.id = blockDef.id; + this.id = blockId; } - private static generateCells(cellDefs: CellDef[]): ScoreCell[] { + private static generateCells(cellDefs: Record): ScoreCell[] { const cells = []; - for (const cellDef of cellDefs) { - cells.push(createCellFromDef(cellDef)); + for (const cellId in cellDefs) { + cells.push(createCellFromDef(cellId, cellDefs[cellId])); } return cells; } @@ -120,8 +120,8 @@ class ScoreBlockWithBonus extends ScoreBlock { protected readonly bonus: number; protected readonly bonusFor: number; - constructor(blockDef: BonusBlockDef) { - super(blockDef); + constructor(blockId: string, blockDef: BonusBlockDef) { + super(blockId, blockDef); this.bonus = blockDef.bonusScore; this.bonusFor = blockDef.bonusFor; } @@ -137,8 +137,8 @@ class ScoreBlockWithBonus extends ScoreBlock { } class ScoreBlockNoBonus extends ScoreBlock { - constructor(blockDef: NoBonusBlockDef) { - super(blockDef); + constructor(blockId: string, blockDef: NoBonusBlockDef) { + super(blockId, blockDef); } getTotal(): number { diff --git a/src/Classes/ScoreCell.ts b/src/Classes/ScoreCell.ts index 9128185..c2ec3cd 100755 --- a/src/Classes/ScoreCell.ts +++ b/src/Classes/ScoreCell.ts @@ -1,16 +1,16 @@ import {CellFlag, FieldType} from "../static/enums"; import {BoolCellDef, CellDef, MultiplierCellDef, NumberCellDef, YahtzeeCellDef} from "../static/rulesets" -export const createCellFromDef = (cellDef: CellDef) : ScoreCell => { +export const createCellFromDef = (cellId: string, cellDef: CellDef) : ScoreCell => { switch (cellDef.fieldType) { case FieldType.number: - return new NumberScoreCell(cellDef); + return new NumberScoreCell(cellId, cellDef); case FieldType.bool: - return new BoolScoreCell(cellDef); + return new BoolScoreCell(cellId, cellDef); case FieldType.multiplier: - return new MultiplierScoreCell(cellDef); + return new MultiplierScoreCell(cellId, cellDef); case FieldType.yahtzee: - return new YahtzeeScoreCell(cellDef); + return new YahtzeeScoreCell(cellId, cellDef); } }; @@ -34,8 +34,8 @@ abstract class ScoreCell { protected struck: boolean; protected value: number | boolean; - protected constructor(cellDef: CellDef) { - this.id = cellDef.id; + protected constructor(cellId: string, cellDef: CellDef) { + this.id = cellId; this.struck = false; this.value = 0; } @@ -89,8 +89,8 @@ abstract class IterableScoreCell extends ScoreCell { protected iteratedSequence: IterableSequenceValues[]; protected currentIteratorIndex: number; - protected constructor(cellDef: CellDef) { - super(cellDef); + protected constructor(cellId: string, cellDef: CellDef) { + super(cellId, cellDef); this.iteratedSequence = []; this.currentIteratorIndex = 0; } @@ -156,8 +156,8 @@ abstract class IterableScoreCell extends ScoreCell { class NumberScoreCell extends ScoreCell { protected static readonly fieldType = FieldType.number; - constructor(cellDef: NumberCellDef) { - super(cellDef); + constructor(cellId: string, cellDef: NumberCellDef) { + super(cellId, cellDef); this.value = 0; } @@ -185,8 +185,8 @@ class BoolScoreCell extends IterableScoreCell { private readonly score: number; protected value: boolean; - constructor(cellDef: BoolCellDef) { - super(cellDef); + constructor(cellId: string, cellDef: BoolCellDef) { + super(cellId, cellDef); this.score = cellDef.score; this.value = false; this.iteratedSequence = [false, true]; @@ -207,8 +207,8 @@ class YahtzeeScoreCell extends IterableScoreCell { private readonly score: number; protected value: number; - constructor(cellDef: YahtzeeCellDef) { - super(cellDef); + constructor(cellId: string, cellDef: YahtzeeCellDef) { + super(cellId, cellDef); this.score = cellDef.score; this.value = 0; @@ -232,8 +232,8 @@ class MultiplierScoreCell extends IterableScoreCell { protected readonly multiplier: number; protected value: number; - constructor(cellDef: MultiplierCellDef) { - super(cellDef); + constructor(cellId: string, cellDef: MultiplierCellDef) { + super(cellId, cellDef); this.multiplier = cellDef.multiplier; this.value = 0; diff --git a/src/Components/KadiBlockRenderer.tsx b/src/Components/KadiBlockRenderer.tsx index 348bdf4..0a3b479 100755 --- a/src/Components/KadiBlockRenderer.tsx +++ b/src/Components/KadiBlockRenderer.tsx @@ -12,27 +12,30 @@ import KadiBlockBonusRow from "./KadiBlockBonusRow"; import LocaleContext from "../LocaleContext"; interface BlockRendererProps { + blockId: string; blockSchema: BlockDef; showResults: boolean; onCellEdit(res: CellEventResponse): void; scores: BlockScores; } -const KadiBlockRenderer: React.FunctionComponent = ({ blockSchema , showResults, scores, onCellEdit}) => { +const KadiBlockRenderer: React.FunctionComponent = (props) => { + const { blockSchema, showResults, scores, onCellEdit, blockId} = props; const rowsInBlock: ReactElement[] = []; const Locale = React.useContext(LocaleContext).strings; - for (const cell of blockSchema.cells) { + for (const cell in blockSchema.cells) { + const cellSchema = blockSchema.cells[cell]; rowsInBlock.push(( @@ -41,24 +44,24 @@ const KadiBlockRenderer: React.FunctionComponent = ({ blockS if (blockSchema.hasBonus) { rowsInBlock.push( ); rowsInBlock.push( @@ -67,12 +70,12 @@ const KadiBlockRenderer: React.FunctionComponent = ({ blockS } rowsInBlock.push( diff --git a/src/Components/KadiBoard.tsx b/src/Components/KadiBoard.tsx index 4f0481e..74fbc59 100755 --- a/src/Components/KadiBoard.tsx +++ b/src/Components/KadiBoard.tsx @@ -144,7 +144,7 @@ class KadiBoard extends React.Component { JSONScoreCards.push(this.state.scoreSheet[playerId].getJSONRepresentation()); } return JSON.stringify({ - //rulesetUsed: this.gameSchema.id, + ruleset: this.gameSchema.id, players: this.state.players, results: JSONScoreCards }); @@ -186,24 +186,26 @@ class KadiBoard extends React.Component { const Locale = this.context.strings; const rows: ReactElement[] = []; - for (const block of this.gameSchema.blocks) { + for (const block in this.gameSchema.blocks) { + const blockSchema = this.gameSchema.blocks[block]; const scores: BlockScores = {subtotals: {}, bonuses: {}, totals: {}}; - for (const cell of block.cells) { - scores[cell.id] = {}; + for (const cell in blockSchema.cells) { + scores[cell] = {}; } this.state.players.forEach(player => { - scores.totals[player.id] = this.getBlockTotalByPlayerId(block.id, player.id); - scores.bonuses[player.id] = this.playerHasBonusForBlock(player.id, block.id); - scores.subtotals[player.id] = this.getBlockSubtotalByPlayerId(block.id, player.id); - for (const cell of block.cells) { - scores[cell.id][player.id] = this.getCellDisplayValueByPlayerIdAndLocation( - player.id, { blockId: block.id, cellId: cell.id }); + scores.totals[player.id] = this.getBlockTotalByPlayerId(block, player.id); + scores.bonuses[player.id] = this.playerHasBonusForBlock(player.id, block); + scores.subtotals[player.id] = this.getBlockSubtotalByPlayerId(block, player.id); + for (const cell in blockSchema.cells) { + scores[cell][player.id] = this.getCellDisplayValueByPlayerIdAndLocation( + player.id, { blockId: block, cellId: cell}); } }); rows.push( ; } export type BlockDef = BonusBlockDef | NoBonusBlockDef; export interface NoBonusBlockDef extends DefaultBlockDef { - hasBonus: false; + hasBonus: false; } export interface BonusBlockDef extends DefaultBlockDef { - hasBonus: true; - bonusScore: number; - bonusFor: number; + hasBonus: true; + bonusScore: number; + bonusFor: number; } interface DefaultBlockDef { - id: string; - label: string; - cells: CellDef[]; + label: string; + cells: Record; } -export type CellDef = BoolCellDef | MultiplierCellDef | NumberCellDef | YahtzeeCellDef; +export type CellDef = + | BoolCellDef + | MultiplierCellDef + | NumberCellDef + | YahtzeeCellDef; export interface BoolCellDef extends DefaultCellDef { - fieldType: FieldType.bool; - score: number; + fieldType: FieldType.bool; + score: number; } export interface MultiplierCellDef extends DefaultCellDef { - fieldType: FieldType.multiplier; - multiplier: number; - maxMultiples: number; + fieldType: FieldType.multiplier; + multiplier: number; + maxMultiples: number; } export interface YahtzeeCellDef extends DefaultCellDef { - fieldType: FieldType.yahtzee; - score: number; - maxYahtzees: number; + fieldType: FieldType.yahtzee; + score: number; + maxYahtzees: number; } export interface NumberCellDef extends DefaultCellDef { - fieldType: FieldType.number; + fieldType: FieldType.number; } interface DefaultCellDef { - id: string; - label: string; + label: string; } // ----- Predefined sets const defaultDiceCount = 5; +const DEFAULT_RULESET = "DEFAULT_RULESET"; const gameSchemas: GameSchema[] = [ - { - id: "default_en", - label: "Standard Kadi Rules (en)", - blocks: [ - { - id: "top", - label: "Upper", - hasBonus: true, - bonusScore: 35, - bonusFor: 63, - cells: [ - { - id: "aces", - fieldType: FieldType.multiplier, - label: "Aces", - multiplier: 1, - maxMultiples: defaultDiceCount, - }, - { - id: "twos", - fieldType: FieldType.multiplier, - label: "Twos", - multiplier: 2, - maxMultiples: defaultDiceCount, - }, - { - id: "threes", - fieldType: FieldType.multiplier, - label: "Threes", - multiplier: 3, - maxMultiples: defaultDiceCount, - }, - { - id: "fours", - fieldType: FieldType.multiplier, - label: "Fours", - multiplier: 4, - maxMultiples: defaultDiceCount, - }, - { - id: "fives", - fieldType: FieldType.multiplier, - label: "Fives", - multiplier: 5, - maxMultiples: defaultDiceCount, - }, - { - id: "sixes", - fieldType: FieldType.multiplier, - label: "Sixes", - multiplier: 6, - maxMultiples: defaultDiceCount, - } - ] - }, - { - id: "bottom", - label: "Lower", - hasBonus: false, - cells: [ - { - id: "three_kind", - fieldType: FieldType.number, - label: "Three of a Kind", - }, - { - id: "four_kind", - fieldType: FieldType.number, - label: "Four of a Kind", - }, - { - id: "full_house", - fieldType: FieldType.bool, - label: "Full House", - score: 25, - }, - { - id: "sml_straight", - fieldType: FieldType.bool, - label: "Small Straight", - score: 30, - }, - { - id: "lg_straight", - fieldType: FieldType.bool, - label: "Large Straight", - score: 40, - }, - { - id: "yahtzee", - fieldType: FieldType.yahtzee, - label: "Kadi", - score: 50, - maxYahtzees: 5, - }, - { - id: "chance", - fieldType: FieldType.number, - label: "Chance", - } - ] - } - ] + { + id: DEFAULT_RULESET, + label: "Standard Kadi Rules (en)", + blocks: { + top: { + label: "Upper", + hasBonus: true, + bonusScore: 35, + bonusFor: 63, + cells: { + aces: { + fieldType: FieldType.multiplier, + label: "Aces", + multiplier: 1, + maxMultiples: defaultDiceCount, + }, + + twos: { + fieldType: FieldType.multiplier, + label: "Twos", + multiplier: 2, + maxMultiples: defaultDiceCount, + }, + + threes: { + fieldType: FieldType.multiplier, + label: "Threes", + multiplier: 3, + maxMultiples: defaultDiceCount, + }, + + fours: { + fieldType: FieldType.multiplier, + label: "Fours", + multiplier: 4, + maxMultiples: defaultDiceCount, + }, + + fives: { + fieldType: FieldType.multiplier, + label: "Fives", + multiplier: 5, + maxMultiples: defaultDiceCount, + }, + + sixes: { + fieldType: FieldType.multiplier, + label: "Sixes", + multiplier: 6, + maxMultiples: defaultDiceCount, + }, + }, + }, + bottom: { + label: "Lower", + hasBonus: false, + cells: { + three_kind: { + fieldType: FieldType.number, + label: "Three of a Kind", + }, + + four_kind: { + fieldType: FieldType.number, + label: "Four of a Kind", + }, + + full_house: { + fieldType: FieldType.bool, + label: "Full House", + score: 25, + }, + + sml_straight: { + fieldType: FieldType.bool, + label: "Small Straight", + score: 30, + }, + + lg_straight: { + fieldType: FieldType.bool, + label: "Large Straight", + score: 40, + }, + + yahtzee: { + fieldType: FieldType.yahtzee, + label: "Kadi", + score: 50, + maxYahtzees: 5, + }, + + chance: { + fieldType: FieldType.number, + label: "Chance", + }, + }, + }, }, - { - id: "default_de", - label: "Standard-Kadi-Regelwerk (de)", - blocks: [ - { - id: "top", - label: "Oben", - hasBonus: true, - bonusScore: 35, - bonusFor: 63, - cells: [ - { - id: "aces", - fieldType: FieldType.multiplier, - label: "Einser", - multiplier: 1, - maxMultiples: defaultDiceCount, - }, - { - id: "twos", - fieldType: FieldType.multiplier, - label: "Zweier", - multiplier: 2, - maxMultiples: defaultDiceCount, - }, - { - id: "threes", - fieldType: FieldType.multiplier, - label: "Dreier", - multiplier: 3, - maxMultiples: defaultDiceCount, - }, - { - id: "fours", - fieldType: FieldType.multiplier, - label: "Vierer", - multiplier: 4, - maxMultiples: defaultDiceCount, - }, - { - id: "fives", - fieldType: FieldType.multiplier, - label: "Fünfer", - multiplier: 5, - maxMultiples: defaultDiceCount, - }, - { - id: "sixes", - fieldType: FieldType.multiplier, - label: "Sechser", - multiplier: 6, - maxMultiples: defaultDiceCount, - } - ] - }, - { - id: "bottom", - label: "Unten", - hasBonus: false, - cells: [ - { - id: "three_kind", - fieldType: FieldType.number, - label: "Dreierpasch", - }, - { - id: "four_kind", - fieldType: FieldType.number, - label: "Viererpasch", - }, - { - id: "full_house", - fieldType: FieldType.bool, - label: "Full House", - score: 25, - }, - { - id: "sml_straight", - fieldType: FieldType.bool, - label: "Kleine Straße", - score: 30, - }, - { - id: "lg_straight", - fieldType: FieldType.bool, - label: "Große Straße", - score: 40, - }, - { - id: "yahtzee", - fieldType: FieldType.yahtzee, - label: "Kadi", - score: 50, - maxYahtzees: 5, - }, - { - id: "chance", - fieldType: FieldType.number, - label: "Chance", - } - ] - } - ] - } + }, + { + id: DEFAULT_RULESET, + label: "Standard-Kadi-Regelwerk (de)", + blocks: { + top: { + label: "Oben", + hasBonus: true, + bonusScore: 35, + bonusFor: 63, + cells: { + aces: { + fieldType: FieldType.multiplier, + label: "Einser", + multiplier: 1, + maxMultiples: defaultDiceCount, + }, + + twos: { + fieldType: FieldType.multiplier, + label: "Zweier", + multiplier: 2, + maxMultiples: defaultDiceCount, + }, + + threes: { + fieldType: FieldType.multiplier, + label: "Dreier", + multiplier: 3, + maxMultiples: defaultDiceCount, + }, + + fours: { + fieldType: FieldType.multiplier, + label: "Vierer", + multiplier: 4, + maxMultiples: defaultDiceCount, + }, + + fives: { + fieldType: FieldType.multiplier, + label: "Fünfer", + multiplier: 5, + maxMultiples: defaultDiceCount, + }, + + sixes: { + fieldType: FieldType.multiplier, + label: "Sechser", + multiplier: 6, + maxMultiples: defaultDiceCount, + }, + }, + }, + bottom: { + label: "Unten", + hasBonus: false, + cells: { + three_kind: { + fieldType: FieldType.number, + label: "Dreierpasch", + }, + four_kind: { + fieldType: FieldType.number, + label: "Viererpasch", + }, + full_house: { + fieldType: FieldType.bool, + label: "Full House", + score: 25, + }, + sml_straight: { + fieldType: FieldType.bool, + label: "Kleine Straße", + score: 30, + }, + lg_straight: { + fieldType: FieldType.bool, + label: "Große Straße", + score: 40, + }, + yahtzee: { + fieldType: FieldType.yahtzee, + label: "Kadi", + score: 50, + maxYahtzees: 5, + }, + change: { + fieldType: FieldType.number, + label: "Chance", + }, + }, + }, + }, + }, ]; export function getGameSchemaById(schemaId: string): GameSchema { - for (const schema of gameSchemas) { - if (schema.id === schemaId) { - return schema; - } + for (const schema of gameSchemas) { + if (schema.id === schemaId) { + return schema; } - throw new RangeError("No such GameSchema with id '" + schemaId + "'!"); + } + throw new RangeError("No such GameSchema with id '" + schemaId + "'!"); } export interface SchemaListing { - id: string; - label: string; + id: string; + label: string; } export function getSchemaListings(): SchemaListing[] { - return gameSchemas.map(s => ({ id: s.id, label: s.label })); -} \ No newline at end of file + return gameSchemas.map((s) => ({ id: s.id, label: s.label })); +} diff --git a/src/static/settings.json b/src/static/settings.json index fe33823..11edc9d 100755 --- a/src/static/settings.json +++ b/src/static/settings.json @@ -1,4 +1,4 @@ { - "ruleset": "default_en", + "ruleset": "DEFAULT_RULESET", "maxHistoryLength": 256 } \ No newline at end of file