From d5f593a8248b856ff53da6b21aeede86a6cd9aac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20B=C3=B6ckelmann?= Date: Mon, 5 Feb 2024 20:44:39 +0100 Subject: [PATCH] Datastructure for Gamesystems and Create and Remove SimpleStates and Transitions --- .../gamesystems/SimpleGamesystem.spec.ts | 104 ++++++++++++++++++ src/app/game-model/GameModel.ts | 10 +- src/app/game-model/gamesystems/Gamesystem.ts | 23 +++- .../gamesystems/ProductGamesystem.ts | 7 ++ .../game-model/gamesystems/ProductState.ts | 7 ++ .../gamesystems/ProductTransition.ts | 6 + .../gamesystems/SimpleGamesystem.ts | 45 ++++++++ src/app/game-model/gamesystems/SimpleState.ts | 6 + .../gamesystems/SimpleTransition.ts | 6 + src/app/game-model/gamesystems/State.ts | 30 +++++ src/app/game-model/gamesystems/Transition.ts | 15 +++ 11 files changed, 252 insertions(+), 7 deletions(-) create mode 100644 e2e/game-model/gamesystems/SimpleGamesystem.spec.ts create mode 100644 src/app/game-model/gamesystems/ProductGamesystem.ts create mode 100644 src/app/game-model/gamesystems/ProductState.ts create mode 100644 src/app/game-model/gamesystems/ProductTransition.ts create mode 100644 src/app/game-model/gamesystems/SimpleGamesystem.ts create mode 100644 src/app/game-model/gamesystems/SimpleState.ts create mode 100644 src/app/game-model/gamesystems/SimpleTransition.ts create mode 100644 src/app/game-model/gamesystems/State.ts create mode 100644 src/app/game-model/gamesystems/Transition.ts diff --git a/e2e/game-model/gamesystems/SimpleGamesystem.spec.ts b/e2e/game-model/gamesystems/SimpleGamesystem.spec.ts new file mode 100644 index 0000000..504bc30 --- /dev/null +++ b/e2e/game-model/gamesystems/SimpleGamesystem.spec.ts @@ -0,0 +1,104 @@ +import { BrowserContext, ElectronApplication, Page, _electron as electron } from 'playwright'; +import { test, expect } from '@playwright/test'; +import * as PATH from 'path'; +import {GameModel} from "../../../src/app/game-model/GameModel"; +import {Gamesystem} from "../../src/app/game-model/gamesystems/Gamesystem"; +import {ScriptAccount} from "../../../src/app/game-model/scriptAccounts/ScriptAccount"; +import {ModelComponentType} from "../../../src/app/game-model/ModelComponentType"; +import {SimpleGamesystem} from "../../../src/app/game-model/gamesystems/SimpleGamesystem"; +import exp = require("node:constants"); +import {end} from "electron-debug"; + +test.describe('Test SimpleGamesystem', () => { + + test("Create SimpleState", async () => { + const gamesystem = new SimpleGamesystem(); + let state = gamesystem.createState("Test", "Test2"); + expect(state.stateLabel).toEqual("Test"); + expect(state.stateDescription).toEqual("Test2"); + expect(state.incomingTransitions.length).toEqual(0); + expect(state.outgoingTransitions.length).toEqual(0); + expect(gamesystem.states.includes(state)).toBeTruthy(); + + state = gamesystem.createState(null, null); + expect(state).toBeUndefined(); + expect(gamesystem.states.includes(state)).toBeFalsy(); + + state = gamesystem.createState(null, "test2"); + expect(state).toBeUndefined() + expect(gamesystem.states.includes(state)).toBeFalsy(); + + state = gamesystem.createState("test2", null); + expect(state).toBeDefined(); + expect(state.stateLabel).toEqual("test2"); + expect(state.stateDescription).toEqual(""); + expect(gamesystem.states.includes(state)).toBeTruthy(); + + state = gamesystem.createState(undefined, "State"); + expect(state).toBeUndefined(); + expect(gamesystem.states.includes(state)).toBeFalsy(); + + state = gamesystem.createState("Test3", undefined); + expect(state).toBeDefined(); + expect(state.stateLabel).toEqual("Test3"); + expect(state.stateDescription).toEqual(""); + expect(gamesystem.states.includes(state)).toBeTruthy(); + + state = gamesystem.createState("", ""); + expect(state).toBeDefined(); + expect(state.stateLabel).toEqual(""); + expect(state.stateDescription).toEqual(""); + expect(gamesystem.states.includes(state)).toBeTruthy(); + + state = gamesystem.createState("Test3", ""); + expect(state).toBeUndefined(); + expect(gamesystem.states.includes(state)).toBeFalsy(); + }) + + test("Create SimpleTransition", async () => { + const gamesystem = new SimpleGamesystem(); + const startingState = gamesystem.createState("StartingState", "")!; + const endingState = gamesystem.createState("EndingState", "")! + + let transition = gamesystem.createTransition(startingState, endingState); + expect(transition).toBeDefined(); + expect(transition.startingState).toEqual(startingState); + expect(transition.endingState).toEqual(endingState); + expect(startingState.outgoingTransitions.includes(transition)).toBeTruthy(); + expect(endingState.incomingTransitions.includes(transition)).toBeTruthy(); + expect(gamesystem.transitions.includes(transition)).toBeTruthy(); + + transition = gamesystem.createTransition(null, null); + expect(transition).toBeUndefined(); + + transition = gamesystem.createTransition(null, endingState); + expect(transition).toBeUndefined(); + + transition = gamesystem.createTransition(undefined, undefined); + expect(transition).toBeUndefined(); + + transition = gamesystem.createTransition(undefined, endingState); + expect(transition).toBeUndefined(); + + transition = gamesystem.createTransition(startingState, null); + expect(transition).toBeUndefined(); + + transition = gamesystem.createTransition(startingState, undefined); + expect(transition).toBeUndefined(); + + transition = gamesystem.createTransition(startingState, startingState); + expect(transition).toBeUndefined(); + + transition = gamesystem.createTransition(startingState, endingState); + expect(transition).toBeUndefined(); + + }) + + test("Remove SimpleState", async () => { + + }) + + test("Remove SimpleTransition", async () => { + + }) +}); diff --git a/src/app/game-model/GameModel.ts b/src/app/game-model/GameModel.ts index a061db3..b8cec69 100644 --- a/src/app/game-model/GameModel.ts +++ b/src/app/game-model/GameModel.ts @@ -4,7 +4,7 @@ import {ScriptAccount} from "./scriptAccounts/ScriptAccount"; export class GameModel { private readonly _gameModelName: string - private _gamesystems: Gamesystem[] = []; + private _gamesystems: Gamesystem[] = []; private _scriptAccounts: ScriptAccount[] = []; constructor(gameModelName: string) { @@ -13,22 +13,20 @@ export class GameModel { get gameModelName(): string { return this._gameModelName; } - - - get gamesystems(): Gamesystem[] { + get gamesystems(): Gamesystem[] { return this._gamesystems; } get scriptAccounts(): ScriptAccount[] { return this._scriptAccounts; } - addGamesystem(gamesystem: Gamesystem) { + addGamesystem(gamesystem: Gamesystem) { if(!this.gamesystems.includes(gamesystem)) { this._gamesystems.push(gamesystem); } } - removeGamesystem(gamesystem : Gamesystem) { + removeGamesystem(gamesystem : Gamesystem) { this._gamesystems = this._gamesystems.filter(g => g !== gamesystem); } diff --git a/src/app/game-model/gamesystems/Gamesystem.ts b/src/app/game-model/gamesystems/Gamesystem.ts index 989e9c0..1af0da7 100644 --- a/src/app/game-model/gamesystems/Gamesystem.ts +++ b/src/app/game-model/gamesystems/Gamesystem.ts @@ -1,7 +1,28 @@ -export class Gamesystem { +export abstract class Gamesystem { gamesystemName: string + gamesystemDescription: string + + states: S[] = []; + transitions: T[] = []; constructor(gamesystemName: string) { this.gamesystemName = gamesystemName; + this.gamesystemDescription = ""; + } + + abstract createState(label: string, description: string): S|undefined; + + abstract createTransition(startingState: S, endingState: S): T|undefined; + + abstract removeState(state: S): boolean; + + removeTransition(transition: T): boolean { + const updatedTransitions = this.transitions.filter(t => t !== transition); + if(updatedTransitions.length == this.transitions.length) { + return false; + } + + this.transitions = updatedTransitions; + return true; } diff --git a/src/app/game-model/gamesystems/ProductGamesystem.ts b/src/app/game-model/gamesystems/ProductGamesystem.ts new file mode 100644 index 0000000..7ae5ff5 --- /dev/null +++ b/src/app/game-model/gamesystems/ProductGamesystem.ts @@ -0,0 +1,7 @@ +import {Gamesystem} from "./Gamesystem"; +import {ProductState} from "./ProductState"; +import {ProductTransition} from "./ProductTransition"; + +export class ProductGamesystem extends Gamesystem { + +} diff --git a/src/app/game-model/gamesystems/ProductState.ts b/src/app/game-model/gamesystems/ProductState.ts new file mode 100644 index 0000000..8a5cbaf --- /dev/null +++ b/src/app/game-model/gamesystems/ProductState.ts @@ -0,0 +1,7 @@ +import {ProductTransition} from "./ProductTransition"; +import {State} from "./State"; +import {SimpleState} from "./SimpleState"; + +export class ProductState extends State { + innerStates: SimpleState[] = []; +} diff --git a/src/app/game-model/gamesystems/ProductTransition.ts b/src/app/game-model/gamesystems/ProductTransition.ts new file mode 100644 index 0000000..3e862ea --- /dev/null +++ b/src/app/game-model/gamesystems/ProductTransition.ts @@ -0,0 +1,6 @@ +import {Transition} from "./Transition"; +import {ProductState} from "./ProductState"; + +export class ProductTransition extends Transition { + +} diff --git a/src/app/game-model/gamesystems/SimpleGamesystem.ts b/src/app/game-model/gamesystems/SimpleGamesystem.ts new file mode 100644 index 0000000..6654332 --- /dev/null +++ b/src/app/game-model/gamesystems/SimpleGamesystem.ts @@ -0,0 +1,45 @@ +import {Gamesystem} from "./Gamesystem"; +import {SimpleState} from "./SimpleState"; +import {SimpleTransition} from "./SimpleTransition"; +export class SimpleGamesystem extends Gamesystem { + createState(label: string, description: string): SimpleState | undefined { + if(label == null) { + return undefined; + } + + if(description == null) { + description = ""; + } + + const state = new SimpleState(label, description); + if(this.states.find(s => s.stateLabel == label) == undefined) { + this.states.push(state); + return state; + } else { + return undefined + } + } + + createTransition(startingState: SimpleState, endingState: SimpleState): SimpleTransition | undefined{ + if((startingState == null || endingState == null) || startingState === endingState) { + return undefined; + } + const transition = new SimpleTransition(startingState, endingState); + if(this.transitions.find(t => t.startingState === startingState && t.endingState === endingState) == undefined) { + this.transitions.push(transition) + return transition; + } else { + startingState.removeOutgoingTransition(transition); + endingState.removeIncomingTransition(transition); + return undefined + } + + } + + + removeState(state: SimpleState): boolean { + return false; + } + + +} diff --git a/src/app/game-model/gamesystems/SimpleState.ts b/src/app/game-model/gamesystems/SimpleState.ts new file mode 100644 index 0000000..d39ffba --- /dev/null +++ b/src/app/game-model/gamesystems/SimpleState.ts @@ -0,0 +1,6 @@ +import {State} from "./State"; +import {SimpleTransition} from "./SimpleTransition"; + +export class SimpleState extends State { + +} diff --git a/src/app/game-model/gamesystems/SimpleTransition.ts b/src/app/game-model/gamesystems/SimpleTransition.ts new file mode 100644 index 0000000..e7356ed --- /dev/null +++ b/src/app/game-model/gamesystems/SimpleTransition.ts @@ -0,0 +1,6 @@ +import {SimpleState} from "./SimpleState"; +import {Transition} from "./Transition"; + +export class SimpleTransition extends Transition { + +} diff --git a/src/app/game-model/gamesystems/State.ts b/src/app/game-model/gamesystems/State.ts new file mode 100644 index 0000000..96fe983 --- /dev/null +++ b/src/app/game-model/gamesystems/State.ts @@ -0,0 +1,30 @@ +import {Transition} from "./Transition"; + +export abstract class State> { + stateLabel: string = ""; + stateDescription: string = ""; + incomingTransitions: T[] =[]; + outgoingTransitions: T[] =[]; + + + constructor(stateLabel: string, stateDescription: string) { + this.stateLabel = stateLabel; + this.stateDescription = stateDescription; + } + + addIncomingTransition(transition: T) { + this.incomingTransitions.push(transition); + } + + addOutgoingTransition(transition: T) { + this.outgoingTransitions.push(transition); + } + + removeIncomingTransition(transition: T) { + + } + + removeOutgoingTransition(transition: T) { + + } +} diff --git a/src/app/game-model/gamesystems/Transition.ts b/src/app/game-model/gamesystems/Transition.ts new file mode 100644 index 0000000..5f75cbd --- /dev/null +++ b/src/app/game-model/gamesystems/Transition.ts @@ -0,0 +1,15 @@ +import {State} from "./State"; + +export abstract class Transition> { + startingState: S + endingState: S + + + constructor(startingState: S, endingState: S) { + this.startingState = startingState; + this.endingState = endingState; + + this.startingState.addOutgoingTransition(this); + this.endingState.addIncomingTransition(this); + } +}