Big update

This commit is contained in:
Daniel Ledda
2020-08-13 15:11:18 +02:00
parent 6f4ccffb63
commit 652092f741
16 changed files with 110 additions and 29 deletions

View File

@@ -9,7 +9,7 @@
"scripts": { "scripts": {
"build-dev": "webpack --mode development && npm postbuild", "build-dev": "webpack --mode development && npm postbuild",
"build": "webpack --mode production", "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", "start": "webpack-dev-server --mode development",
"test": "echo \"Error: no test specified\" && exit 1" "test": "echo \"Error: no test specified\" && exit 1"
}, },

View File

@@ -1,7 +1,8 @@
import {ScoreCellValue} from "./ScoreCell"; import {ScoreCellValue} from "./ScoreCell";
import ScoreBlock, {createBlockFromDef, BlockState, ScoreBlockJSONRepresentation} from "./ScoreBlock"; import ScoreBlock, {createBlockFromDef, BlockState, ScoreBlockJSONRepresentation} from "./ScoreBlock";
import {BlockDef, Ruleset, CellFlag} from "../../../shared/rulesets";
import { Memento, Originator } from "./Caretaker"; import { Memento, Originator } from "./Caretaker";
import {BlockDef, Ruleset} from "../static/rulesets";
import {CellFlag} from "../static/enums";
export type CellLocation = { blockId: string, cellId: string }; export type CellLocation = { blockId: string, cellId: string };

View File

@@ -4,7 +4,8 @@ import ScoreCell, {
CellState, CellState,
ScoreCellJSONRepresentation ScoreCellJSONRepresentation
} from "./ScoreCell"; } 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 => { export const createBlockFromDef = (blockId: string, blockDef: BlockDef) : ScoreBlock => {
if (blockDef.hasBonus) { if (blockDef.hasBonus) {

View File

@@ -1,12 +1,5 @@
import { import {BoolCellDef, CellDef, FieldType, MultiplierCellDef, NumberCellDef, SuperkadiCellDef} from "../static/rulesets";
BoolCellDef, import {CellFlag} from "../static/enums";
CellDef,
CellFlag,
FieldType,
MultiplierCellDef,
NumberCellDef,
SuperkadiCellDef
} from "../../../shared/rulesets";
export const createCellFromDef = (cellId: string, cellDef: CellDef): ScoreCell => { export const createCellFromDef = (cellId: string, cellDef: CellDef): ScoreCell => {
switch (cellDef.fieldType) { switch (cellDef.fieldType) {
@@ -91,9 +84,11 @@ abstract class ScoreCell {
restoreFromJSON(jsonRep: ScoreCellJSONRepresentation): void { restoreFromJSON(jsonRep: ScoreCellJSONRepresentation): void {
this.update(jsonRep.value); this.update(jsonRep.value);
} }
abstract setRandom(): void;
} }
type IterableSequenceValues = boolean | number | CellFlag.strike; type IterableSequenceValues = boolean | number;
abstract class IterableScoreCell extends ScoreCell { abstract class IterableScoreCell extends ScoreCell {
protected iteratedSequence: IterableSequenceValues[]; protected iteratedSequence: IterableSequenceValues[];
@@ -106,7 +101,10 @@ abstract class IterableScoreCell extends ScoreCell {
} }
update(value: ScoreCellValue | CellFlag): void { update(value: ScoreCellValue | CellFlag): void {
if (value == CellFlag.strike) { if (value === CellFlag.random) {
this.setRandom();
}
else if (value == CellFlag.strike) {
this.strike(); this.strike();
} }
else if (this.struck) { else if (this.struck) {
@@ -153,22 +151,31 @@ abstract class IterableScoreCell extends ScoreCell {
if (this.isStruck()) { if (this.isStruck()) {
this.unstrike(); this.unstrike();
} }
if (valueAtIteratorIndex == CellFlag.strike) {
this.strike();
}
else { else {
this.value = valueAtIteratorIndex; 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 { class NumberScoreCell extends ScoreCell {
protected static readonly fieldType = FieldType.number; protected static readonly fieldType = FieldType.number;
protected readonly max: number;
constructor(cellId: string, cellDef: NumberCellDef) { constructor(cellId: string, cellDef: NumberCellDef) {
super(cellId, cellDef); super(cellId, cellDef);
this.value = 0; this.value = 0;
this.max = cellDef.max;
} }
getScore(): number { getScore(): number {
@@ -176,7 +183,13 @@ class NumberScoreCell extends ScoreCell {
} }
update(value: ScoreCellValue | CellFlag): void { update(value: ScoreCellValue | CellFlag): void {
if (typeof value === "boolean") {
throw new Error("Number cells cannot be updated with boolean values!");
}
switch (value){ switch (value){
case CellFlag.random:
this.setRandom();
break;
case CellFlag.strike: case CellFlag.strike:
this.strike(); this.strike();
break; break;
@@ -185,9 +198,26 @@ class NumberScoreCell extends ScoreCell {
break; break;
default: default:
this.value = value; this.value = value;
if (this.value > this.max) {
this.value = this.max;
}
else if (this.value < 0) {
this.value = 0;
}
break; 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 { class BoolScoreCell extends IterableScoreCell {
@@ -210,6 +240,17 @@ class BoolScoreCell extends IterableScoreCell {
return 0; 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 { class SuperkadiScoreCell extends IterableScoreCell {

View File

@@ -16,6 +16,7 @@ interface GameState {
settingUp: boolean; settingUp: boolean;
loading: boolean; loading: boolean;
availablePlayers: Player[] availablePlayers: Player[]
debug: boolean;
} }
interface GameProps {} interface GameProps {}
@@ -36,6 +37,7 @@ class Game extends React.Component<GameProps, GameState> {
settingUp: true, settingUp: true,
loading: true, loading: true,
availablePlayers: [], availablePlayers: [],
debug: Settings.debug,
}; };
} }
@@ -47,7 +49,7 @@ class Game extends React.Component<GameProps, GameState> {
console.log(this.state.availablePlayers); console.log(this.state.availablePlayers);
} }
addNewPlayers(players: Player[]) { addNewPlayers(players: Player[]): void {
const availablePlayers: Player[] = this.state.availablePlayers; const availablePlayers: Player[] = this.state.availablePlayers;
availablePlayers.push(...players); availablePlayers.push(...players);
this.setState({availablePlayers}); this.setState({availablePlayers});
@@ -81,6 +83,7 @@ class Game extends React.Component<GameProps, GameState> {
) : ) :
( (
<KadiBoard <KadiBoard
debug={this.state.debug}
settings={this.state.currentSettings} settings={this.state.currentSettings}
returnToSetup={this.returnToSetup} returnToSetup={this.returnToSetup}
/> />

View File

@@ -1,7 +1,7 @@
import React, {ReactNode} from "react"; import React, {ReactNode} from "react";
import KadiCell from "./KadiCell"; import KadiCell from "./KadiCell";
import {CellScores} from "./KadiBoard"; import {CellScores} from "./KadiBoard";
import {FieldType} from "../../../shared/rulesets"; import {FieldType} from "../static/rulesets";
interface KadiBlockBonusRowProps { interface KadiBlockBonusRowProps {
blockId: string; blockId: string;

View File

@@ -7,7 +7,7 @@ import KadiBlockTotalRow from "./KadiBlockTotalRow";
import KadiBlockSubtotalRow from "./KadiBlockSubtotalRow"; import KadiBlockSubtotalRow from "./KadiBlockSubtotalRow";
import KadiBlockBonusRow from "./KadiBlockBonusRow"; import KadiBlockBonusRow from "./KadiBlockBonusRow";
import LocaleContext from "../LocaleContext"; import LocaleContext from "../LocaleContext";
import {BlockDef, FieldType} from "../../../shared/rulesets"; import {BlockDef, FieldType} from "../static/rulesets";
interface BlockRendererProps { interface BlockRendererProps {
blockId: string; blockId: string;

View File

@@ -1,7 +1,7 @@
import React, {ReactNode} from "react"; import React, {ReactNode} from "react";
import KadiCell from "./KadiCell"; import KadiCell from "./KadiCell";
import {CellScores} from "./KadiBoard"; import {CellScores} from "./KadiBoard";
import {FieldType} from "../../../shared/rulesets"; import {FieldType} from "../static/rulesets";
interface KadiBlockSubtotalRowProps { interface KadiBlockSubtotalRowProps {
blockId: string; blockId: string;

View File

@@ -1,7 +1,7 @@
import React, {ReactNode} from "react"; import React, {ReactNode} from "react";
import KadiCell from "./KadiCell"; import KadiCell from "./KadiCell";
import {CellScores} from "./KadiBoard"; import {CellScores} from "./KadiBoard";
import {FieldType} from "../../../shared/rulesets"; import {FieldType} from "../static/rulesets";
interface KadiBlockTotalRowProps { interface KadiBlockTotalRowProps {
blockId: string; blockId: string;

View File

@@ -1,6 +1,6 @@
import React, {ReactElement, ReactNode, useContext} from "react"; import React, {ReactElement, ReactNode, useContext} from "react";
import PlayerScoreCard, {CellLocation, PlayerScoreCardJSONRepresentation} from "../Classes/PlayerScoreCard"; import PlayerScoreCard, {CellLocation, PlayerScoreCardJSONRepresentation} from "../Classes/PlayerScoreCard";
import {getGameSchemaById} from "../static/rulesets"; import {getGameSchemaById, Ruleset} from "../static/rulesets";
import LocaleContext from "../LocaleContext"; import LocaleContext from "../LocaleContext";
import {ScoreCellValue} from "../Classes/ScoreCell"; import {ScoreCellValue} from "../Classes/ScoreCell";
import {CaretakerSet} from "../Classes/Caretaker"; import {CaretakerSet} from "../Classes/Caretaker";
@@ -14,7 +14,7 @@ import KadiBlockRenderer from "./KadiBlockRenderer";
import {KadiCellDisplayValue} from "./KadiCell"; import {KadiCellDisplayValue} from "./KadiCell";
import {Player} from "./Game"; import {Player} from "./Game";
import {SERVER_BASE_NAME} from "../index"; import {SERVER_BASE_NAME} from "../index";
import {CellFlag, Ruleset} from "../../../shared/rulesets"; import {CellFlag} from "../static/enums";
export interface CellScores { export interface CellScores {
@@ -37,11 +37,13 @@ export interface CellEventResponse {
export interface KadiBoardProps { export interface KadiBoardProps {
settings: GameSettings; settings: GameSettings;
returnToSetup: () => void; returnToSetup: () => void;
debug: boolean;
} }
interface KadiBoardState { interface KadiBoardState {
scoreSheet: ScoreSheet; scoreSheet: ScoreSheet;
players: Player[]; players: Player[];
debug: boolean;
showResults: boolean; showResults: boolean;
savingGame: boolean; savingGame: boolean;
locked: boolean; locked: boolean;
@@ -65,6 +67,7 @@ class KadiBoard extends React.Component<KadiBoardProps, KadiBoardState> {
this.state = { this.state = {
scoreSheet: this.generateNewScoreSheet(this.props.settings.players), scoreSheet: this.generateNewScoreSheet(this.props.settings.players),
players: this.props.settings.players, players: this.props.settings.players,
debug: this.props.debug,
showResults: true, showResults: true,
savingGame: false, savingGame: false,
locked: false, locked: false,
@@ -147,7 +150,7 @@ class KadiBoard extends React.Component<KadiBoardProps, KadiBoardState> {
ruleset: this.gameSchema.id, ruleset: this.gameSchema.id,
players: this.state.players, players: this.state.players,
results: JSONScoreCards results: JSONScoreCards
}); })
} }
private canSave(): boolean { private canSave(): boolean {
@@ -182,6 +185,20 @@ class KadiBoard extends React.Component<KadiBoardProps, KadiBoardState> {
console.log("Error saving:", error); 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 { render(): ReactNode {
const Locale = this.context.strings; const Locale = this.context.strings;
const rows: ReactElement[] = []; const rows: ReactElement[] = [];
@@ -290,6 +307,17 @@ class KadiBoard extends React.Component<KadiBoardProps, KadiBoardState> {
Locale.buttons.saveGameButton.gameSaved : Locale.buttons.saveGameButton.gameSaved :
Locale.buttons.saveGameButton.saveGame} Locale.buttons.saveGameButton.saveGame}
</Button> </Button>
{this.state.debug && (
<Button
icon={true}
labelPosition={"left"}
secondary={true}
onClick={() => this.fillOutRandomly()}
>
<Icon name={"random"}/>
Random results
</Button>
)}
</div> </div>
</Container> </Container>
</div> </div>

View File

@@ -3,7 +3,8 @@ import {ScoreCellValue} from "../Classes/ScoreCell";
import {CellEventResponse} from "./KadiBoard"; import {CellEventResponse} from "./KadiBoard";
import {CellLocation} from "../Classes/PlayerScoreCard"; import {CellLocation} from "../Classes/PlayerScoreCard";
import {useLongPress} from "./useLongPress"; 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; export type KadiCellDisplayValue = ScoreCellValue | CellFlag.strike;

View File

@@ -2,7 +2,7 @@ import {CellLocation} from "../Classes/PlayerScoreCard";
import React, {ReactElement} from "react"; import React, {ReactElement} from "react";
import KadiCell from "./KadiCell"; import KadiCell from "./KadiCell";
import {CellEventResponse, CellScores} from "./KadiBoard"; import {CellEventResponse, CellScores} from "./KadiBoard";
import {FieldType} from "../../../shared/rulesets"; import {FieldType} from "../static/rulesets";
interface KadiEditableRowCellsProps { interface KadiEditableRowCellsProps {
location: CellLocation; location: CellLocation;

View File

@@ -3,7 +3,7 @@ import LocaleContext from "../LocaleContext";
import KadiCell from "./KadiCell"; import KadiCell from "./KadiCell";
import {Icon} from "semantic-ui-react"; import {Icon} from "semantic-ui-react";
import {CellScores} from "./KadiBoard"; import {CellScores} from "./KadiBoard";
import {FieldType} from "../../../shared/rulesets"; import {FieldType} from "../static/rulesets";
interface KadiGrandTotalRowProps { interface KadiGrandTotalRowProps {
showResults: boolean; showResults: boolean;

View File

@@ -1,6 +1,7 @@
export enum CellFlag { export enum CellFlag {
strike = "cellFlagStrike", strike = "cellFlagStrike",
unstrike = "cellFlagUnstrike", unstrike = "cellFlagUnstrike",
random = "random",
} }
export enum SupportedLang { export enum SupportedLang {

View File

@@ -67,6 +67,7 @@ export interface SuperkadiCellDef extends DefaultCellDef {
export interface NumberCellDef extends DefaultCellDef { export interface NumberCellDef extends DefaultCellDef {
fieldType: FieldType.number; fieldType: FieldType.number;
max: number,
} }
interface DefaultCellDef { interface DefaultCellDef {
@@ -139,11 +140,13 @@ const gameSchemas: Ruleset[] = [
threeKind: { threeKind: {
fieldType: FieldType.number, fieldType: FieldType.number,
label: "Three of a Kind", label: "Three of a Kind",
max: defaultDiceCount * 6,
}, },
fourKind: { fourKind: {
fieldType: FieldType.number, fieldType: FieldType.number,
label: "Four of a Kind", label: "Four of a Kind",
max: defaultDiceCount * 6,
}, },
fullHouse: { fullHouse: {
@@ -174,6 +177,7 @@ const gameSchemas: Ruleset[] = [
chance: { chance: {
fieldType: FieldType.number, fieldType: FieldType.number,
label: "Chance", label: "Chance",
max: defaultDiceCount * 6,
}, },
}, },
}, },

View File

@@ -1,4 +1,5 @@
{ {
"debug": true,
"ruleset": "DEFAULT_RULESET", "ruleset": "DEFAULT_RULESET",
"maxHistoryLength": 256 "maxHistoryLength": 256
} }