Copied the rulesets and parts of the enums file into the repository to remove the coupling. (once the database object for rulesets exists rulesets.ts will no longer be necessary).

This commit is contained in:
Daniel Ledda
2020-06-28 13:19:35 +02:00
parent ffd800751f
commit 40a0d7289a
7 changed files with 308 additions and 17 deletions

View File

@@ -4,7 +4,7 @@ import ScoreCell, {
CellState, CellState,
ScoreCellJSONRepresentation ScoreCellJSONRepresentation
} from "./ScoreCell"; } from "./ScoreCell";
import {CellDef, BlockDef, BonusBlockDef, NoBonusBlockDef, CellFlag } from "../../../shared/rulesets"; import {CellDef, BlockDef, BonusBlockDef, NoBonusBlockDef } from "../rulesets";
export const createBlockFromDef = (blockId: string, blockDef: BlockDef) : ScoreBlock => { export const createBlockFromDef = (blockId: string, blockDef: BlockDef) : ScoreBlock => {
if (blockDef.hasBonus) { if (blockDef.hasBonus) {

View File

@@ -1,4 +1,4 @@
import {BlockDef, Ruleset} from "../../../shared/rulesets"; import {BlockDef, Ruleset} from "../rulesets";
import ScoreBlock, {createBlockFromDef, ScoreBlockJSONRepresentation} from "./ScoreBlock"; import ScoreBlock, {createBlockFromDef, ScoreBlockJSONRepresentation} from "./ScoreBlock";
export type CellLocation = { blockId: string, cellId: string }; export type CellLocation = { blockId: string, cellId: string };

View File

@@ -1,12 +1,11 @@
import { import {
BoolCellDef, BoolCellDef,
CellDef, CellDef,
CellFlag,
FieldType,
MultiplierCellDef, MultiplierCellDef,
NumberCellDef, NumberCellDef,
SuperkadiCellDef SuperkadiCellDef
} from "../../../shared/rulesets"; } from "../rulesets";
import { CellFlag, FieldType } from "../enums";
export const createCellFromDef = (cellId: string, cellDef: CellDef): ScoreCell => { export const createCellFromDef = (cellId: string, cellDef: CellDef): ScoreCell => {
switch (cellDef.fieldType) { switch (cellDef.fieldType) {

View File

@@ -1,5 +1,21 @@
export enum CellFlag {
strike = "cellFlagStrike",
unstrike = "cellFlagUnstrike",
}
export enum SupportedLang { export enum SupportedLang {
gb = "gb", gb = "gb",
de = "de", de = "de",
it = "it", it = "it",
}
export enum FieldType {
number = "numberField",
bool = "boolField",
bonus = "bonusField",
subtotal = "subtotalField",
globalTotal = "globalTotalField",
total = "totalField",
superkadi = "superkadiField",
multiplier = "multiplierField",
} }

View File

@@ -10,7 +10,7 @@ import {
} from "./utils"; } from "./utils";
import {CellValue} from "../controllers/statsController"; import {CellValue} from "../controllers/statsController";
import mongo from "mongodb"; import mongo from "mongodb";
import {Ruleset} from "../../../shared/rulesets"; import {Ruleset} from "../rulesets";
export interface CellDetails { export interface CellDetails {
id: string; id: string;

View File

@@ -1,4 +1,5 @@
import {FieldType, Ruleset} from "../../../shared/rulesets"; import {Ruleset} from "../rulesets";
import {FieldType} from "../enums";
import ScoreCalculator, {ScoreCardJSONRepresentation} from "../classes/ScoreCalculator"; import ScoreCalculator, {ScoreCardJSONRepresentation} from "../classes/ScoreCalculator";
class UpdateError extends Error { class UpdateError extends Error {
@@ -137,16 +138,6 @@ class BaseStatsUpdater {
} }
} }
class MongoStoredRuleset {
private readonly data: Ruleset;
constructor(data: Ruleset) {
this.data = data;
}
getId() {
return this.data.id;
}
}
export class PlayerStatsUpdater { export class PlayerStatsUpdater {
private data?: PlayerStats; private data?: PlayerStats;
private readonly updater: BaseStatsUpdater; private readonly updater: BaseStatsUpdater;

285
src/rulesets.ts Executable file
View File

@@ -0,0 +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.superkadi]: 0,
[FieldType.multiplier]: 0,
};
export interface Ruleset {
id: string;
label: string;
blocks: Record<string, BlockDef>;
}
export type BlockDef = BonusBlockDef | NoBonusBlockDef;
export interface NoBonusBlockDef extends DefaultBlockDef {
hasBonus: false;
}
export interface BonusBlockDef extends DefaultBlockDef {
hasBonus: true;
bonusScore: number;
bonusFor: number;
}
interface DefaultBlockDef {
label: string;
cells: Record<string, CellDef>;
}
export type CellDef =
| BoolCellDef
| MultiplierCellDef
| NumberCellDef
| SuperkadiCellDef;
export interface BoolCellDef extends DefaultCellDef {
fieldType: FieldType.bool;
score: number;
}
export interface MultiplierCellDef extends DefaultCellDef {
fieldType: FieldType.multiplier;
multiplier: number;
maxMultiples: number;
}
export interface SuperkadiCellDef extends DefaultCellDef {
fieldType: FieldType.superkadi;
score: number;
maxSuperkadis: number;
}
export interface NumberCellDef extends DefaultCellDef {
fieldType: FieldType.number;
}
interface DefaultCellDef {
label: string;
}
// ----- Predefined sets
const defaultDiceCount = 5;
const DEFAULT_RULESET = "DEFAULT_RULESET";
const gameSchemas: Ruleset[] = [
{
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: {
threeKind: {
fieldType: FieldType.number,
label: "Three of a Kind",
},
fourKind: {
fieldType: FieldType.number,
label: "Four of a Kind",
},
fullHouse: {
fieldType: FieldType.bool,
label: "Full House",
score: 25,
},
smlStraight: {
fieldType: FieldType.bool,
label: "Small Straight",
score: 30,
},
lgSraight: {
fieldType: FieldType.bool,
label: "Large Straight",
score: 40,
},
kadi: {
fieldType: FieldType.superkadi,
label: "Super Kadis",
score: 50,
maxSuperkadis: 5,
},
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: {
threeKind: {
fieldType: FieldType.number,
label: "Dreierpasch",
},
fourKind: {
fieldType: FieldType.number,
label: "Viererpasch",
},
fullSouse: {
fieldType: FieldType.bool,
label: "Full House",
score: 25,
},
smlStraight: {
fieldType: FieldType.bool,
label: "Kleine Straße",
score: 30,
},
lgStraight: {
fieldType: FieldType.bool,
label: "Große Straße",
score: 40,
},
kadi: {
fieldType: FieldType.superkadi,
label: "Ultrakadi",
score: 50,
maxSuperkadis: 5,
},
change: {
fieldType: FieldType.number,
label: "Chance",
},
},
},
},
},
];
export function getGameSchemaById(schemaId: string): Ruleset {
for (const schema of gameSchemas) {
if (schema.id === schemaId) {
return schema;
}
}
throw new RangeError("No such GameSchema with id '" + schemaId + "'!");
}
export interface SchemaListing {
id: string;
label: string;
}
export function getSchemaListings(): SchemaListing[] {
return gameSchemas.map((s) => ({ id: s.id, label: s.label }));
}