Add Conditions
All checks were successful
E2E Testing / test (push) Successful in 1m31s

This commit is contained in:
Sebastian Böckelmann 2024-02-17 19:06:38 +01:00
parent d667a66b79
commit f6b22583c8
4 changed files with 80 additions and 4 deletions

View File

@ -0,0 +1,41 @@
import { test, expect } from '@playwright/test';
import {ScriptAccountCondition} from "../../../../src/app/game-model/gamesystems/conditions/ScriptAccountCondition";
import {ScriptAccount} from "../../../../src/app/game-model/scriptAccounts/ScriptAccount";
import exp = require("node:constants");
import {ConditionTrainer} from "./ConditionTrainer";
import {Conditional} from "@angular/compiler";
import {TransitionConditionTrainer} from "./TransitionConditionTrainer";
import {transition} from "@angular/animations";
test.describe('Test Adding Conditions To Transitions', () => {
test("Test adding not contradicting Conditions", async () => {
const transition = TransitionConditionTrainer.withTransitionWithCondition();
const condition = ScriptAccountCondition.constructScriptAccountCondition(new ScriptAccount("Test", ""), -200, -100);
transition.addScriptAccountCondition(condition);
expect(transition.scriptAccountConditions.length).toEqual(2);
expect(transition.scriptAccountConditions.includes(condition)).toBeTruthy();
})
test("Test adding contradicting Conditions", async () => {
const transition = TransitionConditionTrainer.withTransitionWithCondition();
const condition = ConditionTrainer.withContradictingCondition(transition.scriptAccountConditions[0]);
transition.addScriptAccountCondition(condition)
expect(transition.scriptAccountConditions.length).toEqual(1);
expect(transition.scriptAccountConditions.includes(condition)).toBeFalsy();
})
test("Test expanding Conditions", async () => {
const transition = TransitionConditionTrainer.withTransitionWithCondition();
const condition = ConditionTrainer.withExpendingCondition(transition.scriptAccountConditions[0]);
transition.addScriptAccountCondition(condition);
expect(transition.scriptAccountConditions.length).toEqual(1);
expect(transition.scriptAccountConditions.includes(condition)).toBeFalsy();
expect(transition.scriptAccountConditions[0].minValue).toEqual(-10)
expect(transition.scriptAccountConditions[0].maxValue).toEqual(20)
})
});

View File

@ -10,4 +10,8 @@ export class ConditionTrainer {
static withContradictingCondition(condition: ScriptAccountCondition): ScriptAccountCondition { static withContradictingCondition(condition: ScriptAccountCondition): ScriptAccountCondition {
return ScriptAccountCondition.constructScriptAccountCondition(condition.scriptAccount, condition.minValue-20, condition.minValue-10); return ScriptAccountCondition.constructScriptAccountCondition(condition.scriptAccount, condition.minValue-20, condition.minValue-10);
} }
static withExpendingCondition(condition: ScriptAccountCondition): ScriptAccount {
return ScriptAccountCondition.constructScriptAccountCondition(condition.scriptAccount, condition.minValue-10, condition.maxValue+10);
}
} }

View File

@ -0,0 +1,24 @@
import {SimpleState} from "../../../../src/app/game-model/gamesystems/states/SimpleState";
import {SimpleTransition} from "../../../../src/app/game-model/gamesystems/transitions/SimpleTransition";
import {ScriptAccount} from "../../../../src/app/game-model/scriptAccounts/ScriptAccount";
import {ScriptAccountCondition} from "../../../../src/app/game-model/gamesystems/conditions/ScriptAccountCondition";
export class TransitionConditionTrainer {
static withTransitionWithoutConditions() {
const startingState = new SimpleState("StartingState", "");
const endingState = new SimpleState("EndingState", "");
return new SimpleTransition(startingState, endingState);
}
static withTransitionWithCondition() {
const startingState = new SimpleState("StartingState", "");
const endingState = new SimpleState("EndingState", "");
const transition = new SimpleTransition(startingState, endingState);
const scriptAccount = new ScriptAccount("ScriptAccount", "");
transition.scriptAccountConditions.push(ScriptAccountCondition.constructScriptAccountCondition(scriptAccount, 0, 10)!)
return transition;
}
}

View File

@ -40,14 +40,21 @@ export abstract class Transition<S extends State<any>> {
} }
addScriptAccountCondition(scriptAccountCondition: ScriptAccountCondition) { addScriptAccountCondition(scriptAccountCondition: ScriptAccountCondition) {
const existingScriptAccountCondition = this.findScriptAccountConditionByScriptAccount(scriptAccountCondition.scriptAccount);
if(existingScriptAccountCondition != undefined) {
if(!existingScriptAccountCondition.isContradicting(scriptAccountCondition)) {
existingScriptAccountCondition.extendCondition(scriptAccountCondition)
}
} else {
this.scriptAccountConditions.push(scriptAccountCondition);
}
} }
removeScriptAccountCondition(scriptAccount: ScriptAccount) { removeScriptAccountCondition(scriptAccount: ScriptAccount) {
this.scriptAccountConditions = this.scriptAccountConditions.filter(condition => condition.scriptAccount !== scriptAccount);
} }
findScriptAccountConditionByScriptAccount(scriptAccount: ScriptAccount) { findScriptAccountConditionByScriptAccount(scriptAccount: ScriptAccount): ScriptAccountCondition | undefined {
return this.scriptAccountConditions.find(condition => condition.scriptAccount === scriptAccount);
} }
} }