Skip to content

Commit 86a0c40

Browse files
committed
Attempt revert for conditional, choice and input actions
1 parent 9d3dd94 commit 86a0c40

7 files changed

Lines changed: 68 additions & 23 deletions

File tree

core/lib/actions/Choice.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export class Choice extends Action {
164164
// Check if the choice had an onChosen function with it's matching
165165
// onRevert functionality, or if no onChosen function was provided
166166
// which are the only cases where it can be reverted.
167-
const functionReversible = (typeof this.statement[choice]['onRevert'] === 'function' && typeof this.statement[choice]['onChosen'] === 'function') || typeof this.statement[choice]['onChosen'] !== 'function';
167+
const functionReversible = (typeof this.statement[choice].onRevert === 'function' && typeof this.statement[choice].onChosen === 'function') || typeof this.statement[choice].onChosen !== 'function';
168168

169169
if (functionReversible) {
170170
return Promise.resolve ();
@@ -177,10 +177,13 @@ export class Choice extends Action {
177177
revert () {
178178
const choice = Monogatari.history ('choice')[Monogatari.history ('choice').length - 1];
179179
return Monogatari.revert (this.statement[choice].Do, false).then (() => {
180+
if (typeof this.statement[choice].onRevert === 'function') {
181+
return Util.callAsync (this.statement[choice].onRevert, Monogatari).then (() => {
182+
return Monogatari.run (this.statement, false);
183+
});
184+
}
180185
return Monogatari.run (this.statement);
181-
}).catch (() => {
182186
});
183-
return Promise.resolve ();
184187
}
185188

186189
didRevert () {

core/lib/actions/Conditional.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,18 @@ export class Conditional extends Action {
3333
// it as a key so we run the branch that has that key
3434
if (returnValue === true) {
3535
Monogatari.run (this.statement.True, false);
36+
Monogatari.history ('conditional').push ('True');
3637
} else if (typeof returnValue === 'string') {
3738
Monogatari.run (this.statement[returnValue], false);
39+
Monogatari.history ('conditional').push (returnValue);
3840
} else {
3941
Monogatari.run (this.statement.False, false);
42+
Monogatari.history ('conditional').push ('False');
4043
}
4144
}).catch (() => {
4245
Monogatari.global ('block', false);
4346
Monogatari.run (this.statement.False, false);
47+
Monogatari.history ('conditional').push ('False');
4448
}).finally (() => {
4549
resolve ();
4650
});
@@ -51,8 +55,26 @@ export class Conditional extends Action {
5155
// tell what they actually did in all cases. And there's also no history on
5256
// what function was applied.
5357
willRevert () {
58+
if (Monogatari.history ('conditional').length > 0) {
59+
const conditional = Monogatari.history ('conditional')[Monogatari.history ('conditional').length - 1];
60+
if (this.statement[conditional] !== 'undefined') {
61+
return Promise.resolve ();
62+
}
63+
}
5464
return Promise.reject ();
5565
}
66+
67+
revert () {
68+
const conditional = Monogatari.history ('conditional')[Monogatari.history ('conditional').length - 1];
69+
return Monogatari.revert (this.statement[conditional], false).then (() => {
70+
//return Monogatari.run (this.statement);
71+
});
72+
}
73+
74+
didRevert () {
75+
Monogatari.history ('conditional').pop ();
76+
return Promise.resolve ({ advance: false, step: false });
77+
}
5678
}
5779

5880
Conditional.id = 'Conditional';

core/lib/actions/InputModal.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Action } from '../Action';
22
import { Monogatari } from '../monogatari';
3-
import { $_ } from '@aegis-framework/artemis';
3+
import { $_, Util } from '@aegis-framework/artemis';
44

55
export class InputModal extends Action {
66

@@ -92,8 +92,21 @@ export class InputModal extends Action {
9292
}
9393

9494
willRevert () {
95+
if (typeof this.statement.Revert === 'function') {
96+
return Promise.resolve ();
97+
}
9598
return Promise.reject ();
9699
}
100+
101+
revert () {
102+
return Util.callAsync (this.statement.Revert, Monogatari).then (() => {
103+
return this.apply ();
104+
});
105+
}
106+
107+
didRevert () {
108+
return Promise.resolve ({ advance: false, step: true });
109+
}
97110
}
98111

99112
InputModal.id = 'Input';

core/lib/monogatari.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { FancyError } from './FancyError';
1212
* Components: The list of screens and other HTML elements available in the game.
1313
*
1414
* State: The current state of the game, this simple object contains the current
15-
* label and step as well as the thigs being shown or played by every action.
15+
* label and step as well as the things being shown or played by every action.
1616
*
1717
* History: Every action and even components may keep a history on what statements
1818
* have been applied. The history is
@@ -24,14 +24,14 @@ import { FancyError } from './FancyError';
2424
*
2525
* Characters: The list of characters that participate in the script of the game.
2626
*
27-
* Monogatari follows a 3-step lifecycle:
27+
* Monogatari follows a 3-step life cycle:
2828
*
2929
* 1. Setup - All needed elements are added to the DOM and all variables get
3030
* initialized. This first step is all about preparing all the needed
3131
* elements.
3232
*
3333
* 2. Bind - Once the game has been setup, its time to bind all the necessary
34-
* event listeners or perfom more operations on the DOM.
34+
* event listeners or perform more operations on the DOM.
3535
*
3636
* 3. Init - Finally, once the game was setup and it performed all the needed
3737
* bindings, it may declare or modify variables that needed the HTML to
@@ -45,7 +45,7 @@ class Monogatari {
4545

4646
/**
4747
* @static onStart - This is the main onStart function, it acts as an event
48-
* listerner when the game is started. This function will call its action
48+
* listener when the game is started. This function will call its action
4949
* counterparts.
5050
*
5151
* @return {Promise} - The promise is resolved if all action's onStart function
@@ -66,7 +66,7 @@ class Monogatari {
6666

6767
/**
6868
* @static onLoad - This is the main onStart function, it acts as an event
69-
* listerner when a game is loaded. This function will call its action
69+
* listener when a game is loaded. This function will call its action
7070
* counterparts so that each action is able to run any operations needed
7171
* when a game is loaded such as restoring their state.
7272
*
@@ -108,7 +108,7 @@ class Monogatari {
108108

109109
/**
110110
* @static string - Gets the translation of a string. This is of course limited
111-
* to the translations defined for eeach language and string using the translation
111+
* to the translations defined for each language and string using the translation
112112
* function.
113113
*
114114
* @param {string} key - The key of the string whose translation is needed
@@ -181,7 +181,7 @@ class Monogatari {
181181

182182
/**
183183
* @static history - Simple function to access, create and modify history
184-
* objects. Each history is a simple arrray.
184+
* objects. Each history is a simple array.
185185
*
186186
* @param {Object|string} [object = null] - Object with which current
187187
* history object will be updated with (i.e. Object.assign) or a string to access
@@ -321,7 +321,7 @@ class Monogatari {
321321
* all declared assets such as audio, videos and images should be registered
322322
* in these objects.
323323
*
324-
* @param {string} [type = null] - The type of asset you are refering to
324+
* @param {string} [type = null] - The type of asset you are referring to
325325
* @param {Object} [object = null] - The key/value object to assign to that asset type
326326
* @return {Object} - If this function is called with no arguments, the whole
327327
* assets object will be returned.
@@ -823,7 +823,7 @@ class Monogatari {
823823
$_('[data-screen="load"] [data-ui="autoSaveSlots"] [data-ui="slots"]').append (Monogatari.component ('SLOT').render (slot, name, image, data));
824824
}
825825

826-
// Get's the highest number currently available as a slot id (Save_{?})
826+
// Gets the highest number currently available as a slot id (Save_{?})
827827
static getMaxSlotId (prefix = 'SaveLabel') {
828828
return Monogatari.Storage.keys ().then ((keys) => {
829829
let max = 1;
@@ -1199,7 +1199,7 @@ class Monogatari {
11991199

12001200
}
12011201

1202-
// Start game automatically withouth going trough the main menu
1202+
// Start game automatically without going trough the main menu
12031203
static showMainScreen () {
12041204
if (!Monogatari.setting ('ShowMainScreen')) {
12051205
Monogatari.stopAmbient ();
@@ -1406,7 +1406,7 @@ class Monogatari {
14061406
return act.willRevert ().then (() => {
14071407
// If it can be reverted, then run the revert method
14081408
return act.revert ().then (() => {
1409-
// If the reversion was succesfull, run the didRevert
1409+
// If the reversion was successful, run the didRevert
14101410
// function. The action will return a boolean (shouldContinue)
14111411
// specifying if the game should go ahead and revert
14121412
// the previous statement as well or if it should
@@ -1563,7 +1563,7 @@ class Monogatari {
15631563
if (Monogatari.setting ('ServiceWorkers')) {
15641564
if (!Platform.electron () && !Platform.cordova () && Platform.serviceWorkers ()) {
15651565
// TODO: There's a place in hell for this quick fix, the splitting
1566-
// of the sw file is just preventing parcel from tryng to bundle it
1566+
// of the sw file is just preventing parcel from trying to bundle it
15671567
// when building the core libraries.
15681568
navigator.serviceWorker.register ('./../service-worker' + '.js').then ((registration) => {
15691569

@@ -1718,7 +1718,7 @@ class Monogatari {
17181718
}, false);
17191719
}
17201720

1721-
// Add event listener for back buttons. If the player is plaing, the back
1721+
// Add event listener for back buttons. If the player is playing, the back
17221722
// button will return to the game, if its not playing, then it'll return
17231723
// to the main menu.
17241724
$_(`${selector} [data-screen]`).on ('click', '[data-action="back"]:not([data-screen="game"]), [data-action="back"]:not([data-screen="game"]) *', () => {

0 commit comments

Comments
 (0)