@urso/core 0.1.92 → 0.2.1-dev

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,8 +5,12 @@ class ModulesStatesManagerController {
5
5
  this._configStates;
6
6
  this._currentState;
7
7
  this._started = false;
8
- this._guardsCounter = 0;
9
- this._guards = {};
8
+
9
+ this.statesGuards = this.getInstance('FunctionsStorage');
10
+ this.actionsGuards = this.getInstance('FunctionsStorage');
11
+ this.actionsRuns = this.getInstance('FunctionsStorage');
12
+ this.actionsTerminations = this.getInstance('FunctionsStorage');
13
+
10
14
 
11
15
  this._iterator = this._iteratorConstructor();
12
16
  this._nextState = this._nextState.bind(this);
@@ -21,41 +25,6 @@ class ModulesStatesManagerController {
21
25
  this._nextState();
22
26
  }
23
27
 
24
- setGuard(key, guard) {
25
- if (!this._guards[key])
26
- this._guards[key] = {};
27
-
28
- const guid = this._getGuardsUid();
29
- guard._guid = guid;
30
- this._guards[key][guid] = guard;
31
- }
32
-
33
- checkGuard(key) {
34
- if (this._guards[key]) {
35
- let guardsArray = Object.values(this._guards[key]);
36
-
37
- for (let guard of guardsArray) { // any
38
- if (!guard())
39
- return false;
40
- }
41
- }
42
-
43
- return true;
44
- }
45
-
46
- removeGuard(key, guard) {
47
- const guid = guard._guid;
48
- delete this._guards[key][guid];
49
-
50
- if (Urso.helper.getObjectSize(this._guards[key]) === 0)
51
- delete this._guards[key];
52
- }
53
-
54
- _getGuardsUid() {
55
- this._guardsCounter++;
56
- return 'guard_' + this._guardsCounter;
57
- }
58
-
59
28
  _iteratorConstructor() {
60
29
  let nextIndex = 0;
61
30
 
@@ -63,6 +32,28 @@ class ModulesStatesManagerController {
63
32
  next: (() => {
64
33
  let statesArray = Object.keys(this._configStates);
65
34
 
35
+ //nextState
36
+ if (this._currentState) {
37
+ const currentState = this._configStates[this._currentState];
38
+
39
+ if (currentState.nextState) { //nextState: ["PICK_GAME2", "PICK_GAME1", "IDLE"]
40
+
41
+ for (const stateKey of currentState.nextState) {
42
+ if (this.checkStateGuard(stateKey)) {
43
+ nextIndex = statesArray.indexOf(stateKey);
44
+
45
+ if (nextIndex === -1) {
46
+ Urso.logger.error('ModulesStatesManagerController: nextState name error', stateKey);
47
+ continue;
48
+ }
49
+
50
+ return stateKey;
51
+ }
52
+ }
53
+ }
54
+ }
55
+
56
+ //regular round logic
66
57
  if (nextIndex === statesArray.length)
67
58
  nextIndex = 0;
68
59
 
@@ -81,11 +72,61 @@ class ModulesStatesManagerController {
81
72
  let config = this._configStates[this._currentState];
82
73
  let classInstance = this.getInstance('Helper').getActionByConfig(config);
83
74
 
75
+ //state guard
76
+ if (!this.checkStateGuard(this._currentState))
77
+ return this._nextState();
78
+
79
+ //actions instances guard
84
80
  if (!classInstance.guard())
85
81
  return this._nextState();
86
82
 
87
83
  classInstance.run(this._nextState);
88
84
  }
85
+
86
+ //actions guards
87
+ addActionGuard(key, guard) {
88
+ this.actionsGuards.add(key, guard);
89
+ }
90
+
91
+ checkActionGuard(key) {
92
+ return this.actionsGuards.checkGuard(key);
93
+ }
94
+
95
+ removeActionGuard(key, guard) {
96
+ this.actionsGuards.remove(key, guard);
97
+ }
98
+
99
+ //actions runs
100
+ addActionRun(key, runFunction) {
101
+ this.actionsRuns.add(key, runFunction);
102
+ }
103
+
104
+ runAction(key, onFinishCallback) {
105
+ this.actionsRuns.runAndCallbackOnFinish(key, onFinishCallback);
106
+ }
107
+
108
+ //actions terminations
109
+ addActionTerminate(key, terminateFunction) {
110
+ this.actionsTerminations.add(key, terminateFunction);
111
+ }
112
+
113
+ terminateAction(key) {
114
+ this.actionsTerminations.run(key);
115
+ }
116
+
117
+ //states guards
118
+ setStateGuard(key, guard) {
119
+ this.statesGuards.add(key, guard);
120
+ }
121
+
122
+ checkStateGuard(key) {
123
+ return this.statesGuards.checkGuard(key);
124
+ }
125
+
126
+ removeStateGuard(key, guard) {
127
+ this.statesGuards.remove(key, guard);
128
+ }
129
+
89
130
  }
90
131
 
91
132
  module.exports = ModulesStatesManagerController;
@@ -0,0 +1,83 @@
1
+
2
+ class ModulesStatesManagerFunctionsStorage {
3
+ constructor() {
4
+ this._functionsCounter = 0;
5
+ this._functions = {};
6
+ }
7
+
8
+ //guards
9
+ add(key, guard) {
10
+ this._addToStorage(key, guard, true);
11
+ }
12
+
13
+ run(key) {
14
+ if (this._functions[key]) {
15
+ const functionsArray = Object.values(this._functions[key]);
16
+
17
+ for (const func of functionsArray) {
18
+ func();
19
+ }
20
+ }
21
+ }
22
+
23
+ runAndCallbackOnFinish(key, onFinishCallback) {
24
+ if (this._functions[key]) {
25
+ const functionsArray = Object.values(this._functions[key]);
26
+
27
+ const promises = [];
28
+
29
+ for (const func of functionsArray) {
30
+ promises.push(new Promise((resolve, reject) => { func(resolve); }));
31
+ }
32
+
33
+ Promise.all(promises).then(onFinishCallback);
34
+ } else {
35
+ onFinishCallback();
36
+ }
37
+ }
38
+
39
+ checkGuard(key) {
40
+ if (this._functions[key]) {
41
+ const guardsArray = Object.values(this._functions[key]);
42
+
43
+ for (const guard of guardsArray) {
44
+ if (!guard())
45
+ return false;
46
+ }
47
+ }
48
+
49
+ return true;
50
+ }
51
+
52
+ remove(key, guard) {
53
+ this._removeFromStorage(key, guard);
54
+ }
55
+
56
+ _addToStorage(key, func, onlyOneFlag) {
57
+ if (!this._functions[key])
58
+ this._functions[key] = {};
59
+ else if (onlyOneFlag) {
60
+ Urso.logger.error('ModulesStatesManagerFunctionsStorage: action or state can have only one guard', key, func);
61
+ return;
62
+ }
63
+
64
+ const guid = this._getGuardsUid();
65
+ func._guid = guid;
66
+ this._functions[key][guid] = func;
67
+ }
68
+
69
+ _removeFromStorage(key, func) {
70
+ const guid = func._guid;
71
+ delete this._functions[key][guid];
72
+
73
+ if (Urso.helper.getObjectSize(this._functions[key]) === 0)
74
+ delete this._functions[key];
75
+ }
76
+
77
+ _getGuardsUid() {
78
+ this._functionsCounter++;
79
+ return 'guard_' + this._functionsCounter;
80
+ }
81
+ }
82
+
83
+ module.exports = ModulesStatesManagerFunctionsStorage;
@@ -6,22 +6,12 @@ class ModulesStatesManagerHelper {
6
6
  getActionByConfig(config) {
7
7
  let actionType = Object.keys(config)[0];
8
8
  let actionParams = config[actionType];
9
- let configActions = this.getInstance('ConfigActions').get();
10
9
 
11
10
  if (actionType === "action") {
12
11
  //actionParams is action name
13
12
  let actionName = actionParams;
14
13
 
15
- if (configActions[actionName]) {
16
- let actionParamsConfig = Urso.helper.objectClone(configActions[actionName]);
17
-
18
- if (!actionParamsConfig.name)
19
- actionParamsConfig.name = actionName;
20
-
21
- actionParams = this.getInstance('ActionModel', actionParamsConfig); //replace params to action model
22
- }
23
-
24
- let customActionInstance = this.getInstance('Actions.' + Urso.helper.capitaliseFirstLetter(actionName), actionParams);
14
+ let customActionInstance = this.getInstance('Actions.' + Urso.helper.capitaliseFirstLetter(actionName));
25
15
 
26
16
  if (customActionInstance) {
27
17
  return customActionInstance;
@@ -24,11 +24,6 @@ class ModulesStatesManagerRace extends Action {
24
24
  return true;
25
25
  }
26
26
 
27
- //can action be terminated ?
28
- isTerminable() {
29
- return true;
30
- }
31
-
32
27
  run(onFinishCallback) {
33
28
  log(`%c action run ---> ${this.name}`, 'color: orange', this.params);
34
29
 
@@ -1,48 +0,0 @@
1
- class ModulesStatesManagerActionModel {
2
- constructor(params) {
3
- this._eventsEndings = {
4
- events: {
5
- onStart: '.onStart',
6
- toComplete: '.completed',
7
- },
8
- terminateEvents: {
9
- onStart: '.onTerminate',
10
- toComplete: '.terminated',
11
- }
12
- }
13
-
14
- this.name = Urso.helper.recursiveGet('name', params, 'unnamedAction');;
15
- this._eventBlank = Urso.helper.recursiveGet('eventBlank', params, null);
16
-
17
- this.events = {
18
- onStart: Urso.helper.recursiveGet('events.onStart', params, null),
19
- toComplete: Urso.helper.recursiveGet('events.toComplete', params, null) //return boolean param
20
- };
21
-
22
- this.isTerminable = Urso.helper.recursiveGet('isTerminable', params, true);
23
-
24
- this.terminateEvents = {
25
- onStart: Urso.helper.recursiveGet('terminateEvents.onStart', params, null),
26
- toComplete: Urso.helper.recursiveGet('terminateEvents.toComplete', params, null) //return boolean param
27
- };
28
-
29
- //setup defaults from _eventBlank
30
- this._setupDefaults();
31
- }
32
-
33
- _setupDefaults() {
34
- if (!this._eventBlank)
35
- return false;
36
-
37
- for (let eventsType in this._eventsEndings) {
38
- for (let event in this._eventsEndings[eventsType]) {
39
- if (!this[eventsType][event]) {
40
- this[eventsType][event] = this._eventBlank + this._eventsEndings[eventsType][event];
41
- }
42
- }
43
- }
44
- }
45
-
46
- }
47
-
48
- module.exports = ModulesStatesManagerActionModel;
@@ -1,54 +0,0 @@
1
- class ModulesStatesManagerConfigActions {
2
- constructor() {
3
- this.singleton = true;
4
-
5
- this.contents = {
6
- startSpin: {
7
- name: 'startSpin',
8
- /*events: {
9
- onStart: 'action.showWinPopup.start',
10
- toComplete: 'winPopup.display.finished'
11
- },*/
12
- isTerminable: true,
13
- /*terminateEvents: {
14
- onStart: 'action.showWinPopup.terminate',
15
- toComplete: 'winPopup.display.terminated'
16
- }*/
17
- },
18
- showWinPopup: {
19
- name: 'showWinPopup',
20
- /*events: {
21
- onStart: 'action.showWinPopup.start',
22
- toComplete: 'winPopup.display.finished'
23
- },
24
- isTerminable: true,
25
- terminateEvents: {
26
- onStart: 'action.showWinPopup.terminate',
27
- toComplete: 'winPopup.display.terminated'
28
- }*/
29
- },
30
- showWinlinesAnimationAll: {//full view ---> action model
31
- events: {
32
- onStart: 'components.winlines.animateAll.start',
33
- toComplete: 'components.winlines.animateAll.finished'
34
- },
35
- isTerminable: true,
36
- terminateEvents: {
37
- onStart: 'components.winlines.animateAll.terminate',
38
- toComplete: 'components.winlines.animateAll.terminated'
39
- }
40
- },
41
- showWinlinesAnimationAll: { //simple view ---> action model
42
- eventBlank: 'components.winlines.animateAll',
43
- isTerminable: false
44
- }
45
- };
46
- }
47
-
48
- get() {
49
- return this.contents;
50
- };
51
-
52
- }
53
-
54
- module.exports = ModulesStatesManagerConfigActions;