alt-templatesystem-impl #29
@ -1,42 +1,54 @@
|
||||
import { SimpleGamesystem } from "./SimpleGamesystem";
|
||||
import { ScriptAccountAction } from "./actions/ScriptAccountAction";
|
||||
import { ScriptAccountCondition } from "./conditions/ScriptAccountCondition";
|
||||
import { SimpleState } from "./states/SimpleState";
|
||||
import { SimpleTransition } from "./transitions/SimpleTransition";
|
||||
import {SimpleGamesystem} from "./SimpleGamesystem";
|
||||
import {SimpleTemplateState} from "./states/SimpleTemplateState";
|
||||
import {Gamesystem} from "./Gamesystem";
|
||||
import {SimpleTransition} from "./transitions/SimpleTransition";
|
||||
import {SimpleState} from "./states/SimpleState";
|
||||
import {SimpleTemplateTransition} from "./transitions/SimpleTemplateTransition";
|
||||
|
||||
export class SimpleTemplateGamesystem extends SimpleGamesystem {
|
||||
referenceSystem: SimpleGamesystem
|
||||
export class SimpleTemplateGamesystem<ReferenceType> extends Gamesystem<SimpleTemplateState<ReferenceType>, SimpleTemplateTransition<ReferenceType>> {
|
||||
|
||||
constructor(referenceSystem: SimpleGamesystem) {
|
||||
super(referenceSystem.componentName, referenceSystem.componentDescription)
|
||||
this.referenceSystem = referenceSystem;
|
||||
createState(label: string, description: string): SimpleState | undefined {
|
||||
if(label == null) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
this.referenceSystem.states.forEach(state => {
|
||||
const templateState = new SimpleState(state.stateLabel, state.stateDescription)
|
||||
templateState.initial = state.initial;
|
||||
templateState.conditions = state.conditions.map(condition => ScriptAccountCondition.constructScriptAccountCondition(condition.scriptAccount, condition.minValue, condition.maxValue)!)
|
||||
if(description == null) {
|
||||
description = "";
|
||||
}
|
||||
|
||||
this.states.push(templateState);
|
||||
});
|
||||
|
||||
this.referenceSystem.transitions.forEach(transition => {
|
||||
const startingState = this.findStateByStateLabel(transition.startingState.stateLabel);
|
||||
const endingState = this.findStateByStateLabel(transition.endingState.stateLabel);
|
||||
|
||||
const templateTransition = new SimpleTransition(startingState!, endingState!)
|
||||
|
||||
templateTransition.scriptAccountConditions = transition.scriptAccountConditions.map(condition =>
|
||||
ScriptAccountCondition.constructScriptAccountCondition(condition.scriptAccount, condition.minValue, condition.maxValue)!)
|
||||
|
||||
templateTransition.scriptAccountActions = transition.scriptAccountActions.map(action => {
|
||||
return new ScriptAccountAction(action.scriptAccount, action.changingValue);
|
||||
});
|
||||
|
||||
this.transitions.push(templateTransition);
|
||||
})
|
||||
|
||||
this.referenceSystem.addTemplateSystem(this)
|
||||
const state = new SimpleTemplateState<ReferenceType>(label, description);
|
||||
if(this.states.find(s => s.stateLabel == label) == undefined) {
|
||||
this.states.push(state);
|
||||
return state;
|
||||
} else {
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
createTransition(startingState: SimpleTemplateState<ReferenceType>, endingState: SimpleTemplateState<ReferenceType>): SimpleTemplateTransition<ReferenceType> | undefined {
|
||||
if((startingState == null || endingState == null) || startingState === endingState) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
const transition = new SimpleTemplateTransition<ReferenceType>(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: SimpleTemplateState<ReferenceType>): boolean {
|
||||
const updatedStates = this.states.filter(s => s !== state);
|
||||
const updated = updatedStates.length != this.states.length;
|
||||
this.states = updatedStates;
|
||||
|
||||
this.transitions = this.transitions.filter(t => t.startingState !== state && t.endingState !== state);
|
||||
|
||||
return updated;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,8 @@
|
||||
import {SimpleState} from "./SimpleState";
|
||||
import {ScriptAccountCondition} from "../conditions/ScriptAccountCondition";
|
||||
|
||||
export class SimpleTemplateState<ReferenceType> extends SimpleState {
|
||||
|
||||
conditionMap: Map<ReferenceType, ScriptAccountCondition> = new Map<ReferenceType, ScriptAccountCondition>()
|
||||
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
import {SimpleTransition} from "./SimpleTransition";
|
||||
import {ScriptAccountCondition} from "../conditions/ScriptAccountCondition";
|
||||
import {ScriptAccountAction} from "../actions/ScriptAccountAction";
|
||||
|
||||
export class SimpleTemplateTransition<ReferenceType> extends SimpleTransition {
|
||||
|
||||
conditions: Map<ReferenceType, ScriptAccountCondition[]> = new Map();
|
||||
actions: Map<ReferenceType, ScriptAccountAction[]> = new Map();
|
||||
}
|
Loading…
Reference in New Issue
Block a user