302 lines
13 KiB
TypeScript
302 lines
13 KiB
TypeScript
import {Gamesystem} from "./Gamesystem";
|
|
import {ProductState} from "./states/ProductState";
|
|
import {ProductTransition} from "./transitions/ProductTransition";
|
|
import {State} from "./states/State";
|
|
import {Transition} from "./transitions/Transition";
|
|
import {SimpleState} from "./states/SimpleState";
|
|
import {SimpleGamesystem} from "./SimpleGamesystem";
|
|
import {GameModel} from "../GameModel";
|
|
import {ProductStateTrainer} from "../../../../e2e/game-model/gamesystems/productGamesystems/ProductStateTrainer";
|
|
import {ScriptAccountCondition} from "./conditions/ScriptAccountCondition";
|
|
import {ScriptAccountAction} from "./actions/ScriptAccountAction";
|
|
|
|
export class ProductGamesystem extends Gamesystem<ProductState, ProductTransition> {
|
|
|
|
innerGamesystems: Gamesystem<State<any>, Transition<any>>[] = [];
|
|
|
|
static constructFromSimpleGamesystem(simpleGamesystem: SimpleGamesystem, gameModel: GameModel) {
|
|
const productGamesystem = new ProductGamesystem(simpleGamesystem.componentName, simpleGamesystem.componentDescription);
|
|
const parentGamesystem = simpleGamesystem.parentGamesystem;
|
|
|
|
if(simpleGamesystem.states.length > 0) {
|
|
simpleGamesystem.componentName += "(Child)";
|
|
productGamesystem.addChildGamesystem(simpleGamesystem);
|
|
}
|
|
|
|
|
|
if(parentGamesystem != undefined) {
|
|
parentGamesystem.removeChildGamesystem(simpleGamesystem);
|
|
parentGamesystem.addChildGamesystem(productGamesystem);
|
|
} else {
|
|
gameModel.removeGamesystem(simpleGamesystem);
|
|
gameModel.addGamesystem(productGamesystem);
|
|
}
|
|
|
|
return productGamesystem;
|
|
}
|
|
|
|
createState(innerStates: State<any>[]): ProductState | undefined {
|
|
if(innerStates.length == this.innerGamesystems.length && this.findProductStateByInnerStates(innerStates)== undefined) {
|
|
const productState = new ProductState(innerStates);
|
|
this.states.push(productState);
|
|
return productState;
|
|
} else {
|
|
return undefined;
|
|
}
|
|
}
|
|
|
|
createTransition(startingState: ProductState, endingState: ProductState): ProductTransition | undefined {
|
|
if(startingState == undefined || endingState == undefined || this.existsTransition(startingState, endingState)
|
|
|| !this.existsState(startingState) || !this.existsState(endingState) || startingState.equals(endingState)) {
|
|
return undefined;
|
|
}
|
|
|
|
const productTransition = new ProductTransition(startingState, endingState);
|
|
this.transitions.push(productTransition);
|
|
return productTransition;
|
|
}
|
|
|
|
removeState(state: ProductState): boolean {
|
|
return false;
|
|
}
|
|
|
|
removeInnerState(state: SimpleState) {
|
|
|
|
}
|
|
|
|
generateFromChildsystems() {
|
|
//Assume that childsystems that are productsystems are already generated
|
|
if(this.innerGamesystems.length < 2) {
|
|
//currently only safe-guard
|
|
return;
|
|
}
|
|
|
|
const integratedSystems: Gamesystem<any, any>[] = [this.innerGamesystems[0], this.innerGamesystems[1]];
|
|
if(this.innerGamesystems[0] instanceof ProductGamesystem) {
|
|
this.innerGamesystems[0].generateFromChildsystems();
|
|
}
|
|
|
|
if(this.innerGamesystems[1] instanceof ProductGamesystem) {
|
|
this.innerGamesystems[1].generateFromChildsystems();
|
|
}
|
|
let gamesystem: ProductGamesystem = ProductGamesystem.generateFromChildsystems(this.innerGamesystems[0], this.innerGamesystems[1], false, integratedSystems);
|
|
|
|
for(let i=2; i<this.innerGamesystems.length; i++) {
|
|
if(this.innerGamesystems[i] instanceof ProductGamesystem) {
|
|
(this.innerGamesystems[i] as ProductGamesystem).generateFromChildsystems();
|
|
}
|
|
integratedSystems.push(this.innerGamesystems[i]);
|
|
gamesystem = ProductGamesystem.generateFromChildsystems(gamesystem, this.innerGamesystems[i], true, integratedSystems);
|
|
}
|
|
this.states = gamesystem.states;
|
|
this.transitions = gamesystem.transitions;
|
|
}
|
|
|
|
static generateFromChildsystems(leftSystem: Gamesystem<any, any>, rightSystem: Gamesystem<any, any>, left_temp: boolean, integratedSystems: Gamesystem<any, any>[]) {
|
|
const productGamesystem = new ProductGamesystem("Temporary Gamesystem", "");
|
|
integratedSystems.forEach(integratedSystem => productGamesystem.addChildGamesystem(integratedSystem));
|
|
|
|
leftSystem.states.forEach(leftState => {
|
|
rightSystem.states.forEach(rightState => {
|
|
for(let i=0; i<leftState.outgoingTransitions.length; i++) {
|
|
for(let j=0; j<rightState.outgoingTransitions.length; j++) {
|
|
const startingState = productGamesystem.generateBinaryProductState(leftState, rightState, left_temp);
|
|
|
|
if(startingState != undefined) {
|
|
const endingState_right = productGamesystem.generateBinaryProductState(leftState, rightState.outgoingTransitions[j].endingState, left_temp);
|
|
if(endingState_right != undefined) {
|
|
const transition_right = productGamesystem.createTransition(startingState, endingState_right);
|
|
if(transition_right != undefined) {
|
|
transition_right.scriptAccountActions = [...rightState.outgoingTransitions[j].scriptAccountActions]
|
|
transition_right.scriptAccountConditions = [... rightState.outgoingTransitions[j].scriptAccountConditions]
|
|
}
|
|
}
|
|
|
|
const endingState_left = productGamesystem.generateBinaryProductState(leftState.outgoingTransitions[i].endingState, rightState, left_temp);
|
|
if(endingState_left != undefined) {
|
|
const transition_left = productGamesystem.createTransition(startingState, endingState_left);
|
|
if(transition_left != undefined) {
|
|
transition_left.scriptAccountActions = [... leftState.outgoingTransitions[i].scriptAccountActions]
|
|
transition_left.scriptAccountConditions = [...leftState.outgoingTransitions[i].scriptAccountConditions]
|
|
}
|
|
}
|
|
|
|
|
|
const endingState_left_right = productGamesystem.generateBinaryProductState(leftState.outgoingTransitions[i].endingState, rightState.outgoingTransitions[j].endingState, left_temp);
|
|
if(endingState_left_right != undefined) {
|
|
const leftConditions = leftState.outgoingTransitions[i].scriptAccountConditions;
|
|
const rightConditions = rightState.outgoingTransitions[j].scriptAccountConditions;
|
|
if(!this.contradictCombinedConditions(leftConditions, rightConditions)) {
|
|
const transition_left_right = productGamesystem.createTransition(startingState, endingState_left_right)!;
|
|
|
|
if(transition_left_right != undefined) {
|
|
transition_left_right.scriptAccountActions = this.generateCombinedActions(leftState.outgoingTransitions[i].scriptAccountActions, rightState.outgoingTransitions[j].scriptAccountActions)
|
|
transition_left_right.scriptAccountConditions = this.generateCombinedConditions(leftState.outgoingTransitions[i].scriptAccountConditions, rightState.outgoingTransitions[j].scriptAccountConditions)
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
if(rightState.outgoingTransitions.length == 0) {
|
|
const startingState = productGamesystem.generateBinaryProductState(leftState, rightState, left_temp);
|
|
const endingState = productGamesystem.generateBinaryProductState(leftState.outgoingTransitions[i].endingState, rightState, left_temp);
|
|
|
|
if(startingState != undefined && endingState != undefined) {
|
|
const transition = productGamesystem.createTransition(startingState, endingState);
|
|
if(transition != undefined) {
|
|
transition.scriptAccountActions = [... leftState.outgoingTransitions[i].scriptAccountActions]
|
|
transition.scriptAccountConditions = [...leftState.outgoingTransitions[i].scriptAccountConditions]
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if(leftState.outgoingTransitions.length == 0) {
|
|
for(let j=0; j<rightState.outgoingTransitions.length; j++) {
|
|
const startingState = productGamesystem.generateBinaryProductState(leftState, rightState, left_temp);
|
|
const endingState = productGamesystem.generateBinaryProductState(leftState, rightState.outgoingTransitions[j].endingState, left_temp);
|
|
|
|
if(startingState != undefined && endingState != undefined) {
|
|
const transition = productGamesystem.createTransition(startingState, endingState);
|
|
if(transition != undefined) {
|
|
transition.scriptAccountActions = [... rightState.outgoingTransitions[j].scriptAccountActions]
|
|
transition.scriptAccountConditions = [...rightState.outgoingTransitions[j].scriptAccountConditions]
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
})
|
|
})
|
|
|
|
|
|
return productGamesystem;
|
|
}
|
|
|
|
private static generateCombinedActions(leftActions: ScriptAccountAction[], rightActions: ScriptAccountAction[]) {
|
|
const combinedActions: ScriptAccountAction[] = []
|
|
for(let i=0; i<leftActions.length; i++) {
|
|
for(let j=0; j<rightActions.length; j++) {
|
|
const combinedAction = leftActions[i].combineActions(rightActions[j])
|
|
if(combinedAction == undefined) {
|
|
if(!combinedActions.includes(leftActions[i])) {
|
|
combinedActions.push(leftActions[i])
|
|
}
|
|
|
|
if(!combinedActions.includes(rightActions[j])) {
|
|
combinedActions.push(rightActions[j])
|
|
}
|
|
} else {
|
|
|
|
combinedActions.push(combinedAction)
|
|
}
|
|
}
|
|
}
|
|
return combinedActions;
|
|
}
|
|
|
|
private static generateCombinedConditions(leftConditions: ScriptAccountCondition[], rightConditions: ScriptAccountCondition[]) {
|
|
const combinedConditions: ScriptAccountCondition[] = [];
|
|
for(let i=0; i<leftConditions.length; i++) {
|
|
for(let j=0; j<rightConditions.length; j++) {
|
|
const combinedCondition = leftConditions[i].combineCondition(rightConditions[j]);
|
|
if(combinedCondition == undefined) {
|
|
if(!combinedConditions.includes(leftConditions[i])) {
|
|
combinedConditions.push(leftConditions[i])
|
|
}
|
|
|
|
if(!combinedConditions.includes(rightConditions[j])) {
|
|
combinedConditions.push(rightConditions[j])
|
|
}
|
|
} else {
|
|
combinedConditions.push(combinedCondition)
|
|
}
|
|
}
|
|
}
|
|
return combinedConditions;
|
|
}
|
|
|
|
private static contradictCombinedConditions(leftConditions: ScriptAccountCondition[], rightConditions: ScriptAccountCondition[]): boolean {
|
|
for(let i=0; i<leftConditions.length; i++) {
|
|
for(let j=0; j<rightConditions.length; j++) {
|
|
if(leftConditions[i].isContradicting(rightConditions[j])) {
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
generateBinaryProductState(leftInnerState: State<any>, rightInnerState: State<any>, left_temp: boolean) {
|
|
const combinedStateConditions: ScriptAccountCondition[] = leftInnerState.conditions.concat(rightInnerState.conditions)
|
|
for(let i=0; i<combinedStateConditions.length; i++) {
|
|
for(let j=0; j<combinedStateConditions.length; j++) {
|
|
if(combinedStateConditions[i].isContradicting(combinedStateConditions[j])) {
|
|
return undefined
|
|
}
|
|
}
|
|
}
|
|
|
|
let innerStates: State<any>[] = [];
|
|
if(!left_temp) {
|
|
innerStates = [leftInnerState, rightInnerState];
|
|
} else {
|
|
const left_inner_product_state = leftInnerState as ProductState;
|
|
left_inner_product_state.innerStates.forEach(state => innerStates.push(state));
|
|
innerStates.push(rightInnerState);
|
|
}
|
|
|
|
|
|
let binary_productState = this.findProductStateByInnerStates(innerStates);
|
|
if(binary_productState == undefined) {
|
|
binary_productState = this.createState(innerStates)!;
|
|
}
|
|
|
|
let productInitial = true;
|
|
binary_productState.innerStates.forEach(innerState => productInitial = productInitial && innerState.initial)
|
|
binary_productState!.initial = productInitial
|
|
binary_productState.conditions = combinedStateConditions
|
|
|
|
return binary_productState!;
|
|
}
|
|
|
|
addChildGamesystem(gamesystem: Gamesystem<State<any>, Transition<any>>) {
|
|
this.innerGamesystems.push(gamesystem);
|
|
}
|
|
|
|
removeChildGamesystem(gamesystem: Gamesystem<State<any>, Transition<any>>) {
|
|
this.innerGamesystems = this.innerGamesystems.filter(childSystem => childSystem.componentName !== gamesystem.componentName);
|
|
}
|
|
|
|
findProductStateByInnerStates(innerStates: State<any>[]) {
|
|
return this.states.find(productState => productState.equalInnerStates(innerStates));
|
|
}
|
|
|
|
findChildSystemByName(gamesystemName: string) {
|
|
const gamesystemQueue: Gamesystem<State<any>, Transition<any>>[] = [];
|
|
this.innerGamesystems.forEach(gamesystem => gamesystemQueue.push(gamesystem));
|
|
|
|
while(gamesystemName.length > 0 ){
|
|
const currentGamesystem = gamesystemQueue.shift();
|
|
if(currentGamesystem!.componentName === gamesystemName) {
|
|
return currentGamesystem;
|
|
}
|
|
}
|
|
return undefined;
|
|
}
|
|
|
|
private existsTransition(startingState: ProductState, endingState: ProductState) {
|
|
return this.transitions.find(transition =>
|
|
transition.startingState.equals(startingState) && transition.endingState.equals(endingState)) != undefined;
|
|
}
|
|
|
|
private existsState(state: ProductState) {
|
|
return this.states.find(s => s.equals(state)) != undefined;
|
|
}
|
|
|
|
|
|
}
|