Add and Remove Actions from Transition
All checks were successful
E2E Testing / test (push) Successful in 1m30s

This commit is contained in:
Sebastian Böckelmann 2024-02-17 16:04:31 +01:00
parent f38626683e
commit f07cb270f1
5 changed files with 153 additions and 0 deletions

View File

@ -0,0 +1,59 @@
import { test, expect } from '@playwright/test';
import {GamesystemTrainer} from "../GamesystemTrainer";
import {SimpleActionTrainer} from "./SimpleActionTrainer";
import {ScriptAccount} from "../../../../src/app/game-model/scriptAccounts/ScriptAccount";
import {ScriptAccountAction} from "../../../../src/app/game-model/gamesystems/actions/ScriptAccountAction";
test.describe('Test Create SimpleActions', () => {
test('Test creating gamesystem with invalid name', async => {
const gameModel = GamesystemTrainer.givenEmptyGameModel();
let result = gameModel.createGamesystem(undefined, undefined);
expect(result).toBeUndefined();
result = gameModel.createGamesystem(null, undefined);
expect(result).toBeUndefined();
})
test("Adding invalid actions", async () => {
const transition = SimpleActionTrainer.withEmptyActions();
transition.addScriptAccountAction(null);
expect(transition.scriptAccountActions.length).toEqual(0);
transition.addScriptAccountAction(undefined);
expect(transition.scriptAccountActions.length).toEqual(0);
})
test("Adding not existing action", async () => {
const transition = SimpleActionTrainer.withEmptyActions();
const scriptAccount = new ScriptAccount("test", "");
const action = new ScriptAccountAction(scriptAccount, 10);
transition.addScriptAccountAction(action);
expect(transition.scriptAccountActions.length).toEqual(1);
expect(transition.scriptAccountActions[0].scriptAccount).toEqual(action.scriptAccount);
expect(transition.scriptAccountActions[0].changingValue).toEqual(10);
})
test("Adding existing action", async () => {
const transition = SimpleActionTrainer.withSingleAction();
const action = transition.scriptAccountActions[0];
transition.addScriptAccountAction(action);
expect(transition.scriptAccountActions.length).toEqual(1);
expect(transition.scriptAccountActions[0].changingValue).toEqual(20);
expect(transition.scriptAccountActions[0].scriptAccount).toEqual(action.scriptAccount);
})
test("Adding not existing action into non empty actions", async () => {
const transition = SimpleActionTrainer.withSingleAction();
const scriptAccount = new ScriptAccount("Tes", "");
transition.addScriptAccountAction(new ScriptAccountAction(scriptAccount, 10));
expect(transition.scriptAccountActions.length).toEqual(2);
})
});

View File

@ -0,0 +1,33 @@
import { test, expect } from '@playwright/test';
import {GamesystemTrainer} from "../GamesystemTrainer";
import {SimpleActionTrainer} from "./SimpleActionTrainer";
import {ScriptAccount} from "../../../../src/app/game-model/scriptAccounts/ScriptAccount";
import {ScriptAccountAction} from "../../../../src/app/game-model/gamesystems/actions/ScriptAccountAction";
import {SimpleTransition} from "../../../../src/app/game-model/gamesystems/transitions/SimpleTransition";
test.describe('Test Remove SimpleActions', () => {
test("Test Removing invalid Actions", async () => {
const transition = SimpleActionTrainer.withSingleAction();
transition.removeScriptAccountAction(null);
expect(transition.scriptAccountActions.length).toEqual(1)
transition.removeScriptAccountAction(undefined);
expect(transition.scriptAccountActions.length).toEqual(1);
})
test("Test removing unknown scriptAccount Action", async () => {
const transition = SimpleActionTrainer.withSingleAction();
const scriptAccount = new ScriptAccount("Test");
transition.removeScriptAccountAction(scriptAccount);
expect(transition.scriptAccountActions.length).toEqual(1);
})
test("Test removing known ScriptAccount", async () => {
const transition = SimpleActionTrainer.withSingleAction();
transition.removeScriptAccountAction(transition.scriptAccountActions[0].scriptAccount)
expect(transition.scriptAccountActions.length).toEqual(0);
})
});

View File

@ -0,0 +1,25 @@
import {SimpleState} from "../../../../src/app/game-model/gamesystems/states/SimpleState";
import {SimpleTransition} from "../../../../src/app/game-model/gamesystems/transitions/SimpleTransition";
import {Script} from "node:vm";
import {ScriptAccount} from "../../../../src/app/game-model/scriptAccounts/ScriptAccount";
import {ScriptAccountAction} from "../../../../src/app/game-model/gamesystems/actions/ScriptAccountAction";
export class SimpleActionTrainer {
static withEmptyActions() {
const startingState = new SimpleState("Wolkig", "");
const endingState = new SimpleState("Schnee", "");
return new SimpleTransition(startingState, endingState);
}
static withSingleAction() {
const startingState = new SimpleState("Wolkig", "");
const endingState = new SimpleState("Schnee", "");
const scriptAccount = new ScriptAccount("Temperature", "");
const transition = new SimpleTransition(startingState, endingState);
transition.scriptAccountActions.push(new ScriptAccountAction(scriptAccount, 10));
return transition;
}
}

View File

@ -0,0 +1,12 @@
import {ScriptAccount} from "../../scriptAccounts/ScriptAccount";
export class ScriptAccountAction {
scriptAccount: ScriptAccount
changingValue: number = 0;
constructor(scriptAccount: ScriptAccount, changingValue: number) {
this.scriptAccount = scriptAccount;
this.changingValue = changingValue;
}
}

View File

@ -1,9 +1,12 @@
import {State} from "../states/State"; import {State} from "../states/State";
import {ScriptAccountAction} from "../actions/ScriptAccountAction";
import {ScriptAccount} from "../../scriptAccounts/ScriptAccount";
export abstract class Transition<S extends State<any>> { export abstract class Transition<S extends State<any>> {
startingState: S startingState: S
endingState: S endingState: S
scriptAccountActions: ScriptAccountAction[] = [];
constructor(startingState: S, endingState: S) { constructor(startingState: S, endingState: S) {
this.startingState = startingState; this.startingState = startingState;
@ -12,4 +15,25 @@ export abstract class Transition<S extends State<any>> {
this.startingState.addOutgoingTransition(this); this.startingState.addOutgoingTransition(this);
this.endingState.addIncomingTransition(this); this.endingState.addIncomingTransition(this);
} }
addScriptAccountAction(scriptAccountAction: ScriptAccountAction) {
if(scriptAccountAction != undefined) {
const searchedScriptAccount = this.findScriptAccountActionByScriptAccount(scriptAccountAction.scriptAccount);
if(searchedScriptAccount == undefined) {
this.scriptAccountActions.push(scriptAccountAction)
} else {
searchedScriptAccount.changingValue += scriptAccountAction.changingValue;
}
}
}
removeScriptAccountAction(scriptAccount: ScriptAccount) {
if(scriptAccount != undefined) {
this.scriptAccountActions = this.scriptAccountActions.filter(sA => sA.scriptAccount.componentName !== scriptAccount.componentName);
}
}
findScriptAccountActionByScriptAccount(scriptAccount: ScriptAccount) {
return this.scriptAccountActions.find(sA => sA.scriptAccount.componentName === scriptAccount.componentName);
}
} }