Changed the definition of the rulesets so there are no arrays.

This commit is contained in:
Daniel Ledda
2020-05-26 11:11:01 +02:00
parent 5342b7a48e
commit 166ec7525a
7 changed files with 298 additions and 304 deletions

View File

@@ -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<string, BlockDef>): 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;
}

View File

@@ -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<string, CellDef>): 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 {

View File

@@ -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;

View File

@@ -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<BlockRendererProps> = ({ blockSchema , showResults, scores, onCellEdit}) => {
const KadiBlockRenderer: React.FunctionComponent<BlockRendererProps> = (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((
<GenericKadiRowContainer
key={"rowCont" + cell.id + blockSchema.id}
label={cell.label}
cellCssClassName={cell.fieldType}
key={"rowCont" + cell + blockId}
label={cellSchema.label}
cellCssClassName={cellSchema.fieldType}
>
<KadiEditableRowCells
location={{blockId: blockSchema.id, cellId: cell.id}}
fieldType={cell.fieldType}
scores={scores[cell.id]}
location={{blockId, cellId: cell}}
fieldType={cellSchema.fieldType}
scores={scores[cell]}
onCellEdit={onCellEdit}
/>
</GenericKadiRowContainer>
@@ -41,24 +44,24 @@ const KadiBlockRenderer: React.FunctionComponent<BlockRendererProps> = ({ blockS
if (blockSchema.hasBonus) {
rowsInBlock.push(
<GenericKadiRowContainer
key={"rowContSubtotal" + blockSchema.id}
key={"rowContSubtotal" + blockId}
label={Locale.rowLabels.subtotal}
cellCssClassName={FieldType.subtotal + (showResults ? "" : " hideResults")}
>
<KadiBlockSubtotalRow
blockId={blockSchema.id}
blockId={blockId}
scores={scores.subtotals}
/>
</GenericKadiRowContainer>
);
rowsInBlock.push(
<GenericKadiRowContainer
key={"rowContBonus" + blockSchema.id}
key={"rowContBonus" + blockId}
label={Locale.rowLabels.bonus}
cellCssClassName={FieldType.bonus + (showResults ? "" : " hideResults")}
>
<KadiBlockBonusRow
blockId={blockSchema.id}
blockId={blockId}
bonusScore={blockSchema.bonusScore}
scores={scores.bonuses}
/>
@@ -67,12 +70,12 @@ const KadiBlockRenderer: React.FunctionComponent<BlockRendererProps> = ({ blockS
}
rowsInBlock.push(
<GenericKadiRowContainer
key={"rowContTotal" + blockSchema.id}
key={"rowContTotal" + blockId}
label={formatUnicorn(Locale.rowLabels.blockTotal, blockSchema.label)}
cellCssClassName={FieldType.total + (showResults ? "" : " hideResults")}
>
<KadiBlockTotalRow
blockId={blockSchema.id}
blockId={blockId}
scores={scores.totals}
/>
</GenericKadiRowContainer>

View File

@@ -144,7 +144,7 @@ class KadiBoard extends React.Component<KadiBoardProps, KadiBoardState> {
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<KadiBoardProps, KadiBoardState> {
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(
<KadiBlockRenderer
key={"block" + block.id}
blockSchema={block}
key={"block" + block}
blockId={block}
blockSchema={blockSchema}
showResults={this.state.showResults}
onCellEdit={this.onCellEdit}
scores={scores}

View File

@@ -1,296 +1,285 @@
import { FieldType } from "./enums";
export const defaultCellValues = {
[FieldType.number]: 0,
[FieldType.bool]: false,
[FieldType.subtotal]: 0,
[FieldType.total]: 0,
[FieldType.bonus]: 0,
[FieldType.yahtzee]: 0,
[FieldType.multiplier]: 0,
[FieldType.number]: 0,
[FieldType.bool]: false,
[FieldType.subtotal]: 0,
[FieldType.total]: 0,
[FieldType.bonus]: 0,
[FieldType.yahtzee]: 0,
[FieldType.multiplier]: 0,
};
// ----- gameSchema interface definitions
export interface GameSchema {
id: string;
label: string;
blocks: BlockDef[];
id: string;
label: string;
blocks: Record<string, BlockDef>;
}
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<string, CellDef>;
}
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 }));
return gameSchemas.map((s) => ({ id: s.id, label: s.label }));
}

View File

@@ -1,4 +1,4 @@
{
"ruleset": "default_en",
"ruleset": "DEFAULT_RULESET",
"maxHistoryLength": 256
}