diff --git a/README.md b/README.md index 3dea67f..6fdb91a 100644 --- a/README.md +++ b/README.md @@ -5,26 +5,16 @@ ## Implemented tests: -### Testing BattleService.init_state() call and conversion into the view-model +### Testing BattleService.initial_state() call and conversion into the view-model -First call from console: (Calls 'BattleService.init_state()' and returns result to console and to 'window.tempData' to be accessible from console) +First get initial state into the view-model either with selecting the Battle view or from console with: ```javascript - bt.services.execute('BattleService', function(service) { - service.initialState( - function(data) { console.log("Success:"); console.log(data); window.tempData = data }, - function(data) { console.log("Fail:"); console.log(data); } - ); - }); -``` - -... when service call returns value call from console: (Constructs 'BattleField' object from result) -```javascript - var bf = new bt.model.definitions.battle.battleField(window.tempData); + bt.game.battle.model.initialization.initialize(); ``` -... and then simply inspect the constructed object with: +And then you can inspect the initialized view-model's battleField object with: ```javascript - console.log(bf) + console.log( bt.game.battle.model.battleField ); ``` diff --git a/res/js/bt_authentication.js b/res/js/bt_authentication.js index 0448eff..054daae 100644 --- a/res/js/bt_authentication.js +++ b/res/js/bt_authentication.js @@ -83,6 +83,19 @@ bt.services.authenticationService = app.factory('AuthenticationService', functio }); +// Custom events definitions +// --------------------------------------------------------------------------------------------------------------------- + +// @ bt.services.authService + +// "Authentication successfull" event +bt.events.define(bt.services.authenticationService, 'authenticationSuccessfull'); +// "Authentication failed" event (Fired when server invalidates authentication) +bt.events.define(bt.services.authenticationService, 'authenticationFailed'); +// "Authentication error" event (Fired on authentication request error) +bt.events.define(bt.services.authenticationService, 'authenticationError'); + + // Initialize common game functionality // --------------------------------------------------------------------------------------------------------------------- bt.game.authentication = { @@ -106,17 +119,4 @@ bt.game.authentication = { } -} - - -// Custom events definitions -// --------------------------------------------------------------------------------------------------------------------- - -// @ bt.services.authService - -// "Authentication successfull" event -bt.events.define(bt.services.authenticationService, 'authenticationSuccessfull'); -// "Authentication failed" event (Fired when server invalidates authentication) -bt.events.define(bt.services.authenticationService, 'authenticationFailed'); -// "Authentication error" event (Fired on authentication request error) -bt.events.define(bt.services.authenticationService, 'authenticationError'); +} \ No newline at end of file diff --git a/res/js/bt_game_battle.js b/res/js/bt_game_battle.js index fbd5e5c..a89e509 100644 --- a/res/js/bt_game_battle.js +++ b/res/js/bt_game_battle.js @@ -55,6 +55,19 @@ bt.services.battleService = app.factory('BattleService', function (jsonRpc) { }); +// Custom events definitions +// --------------------------------------------------------------------------------------------------------------------- + +// @ bt.services.battleService + +// "Service call successfull" event +bt.events.define(bt.services.battleService, 'Called'); +// "Service call error" event +bt.events.define(bt.services.battleService, 'Error'); +// "Service call successfull" event +bt.events.define(bt.services.battleService, 'Updated'); + + // Initialize 'battle view' game functionality // --------------------------------------------------------------------------------------------------------------------- bt.game.battle = { @@ -86,12 +99,16 @@ bt.game.battle = { // Start timer interval if (bt.game.battle.timers._interval) clearInterval(bt.game.battle.timers._interval); bt.game.battle.timers._interval = setInterval(bt.game.battle.timers.update, 1000); + // Initialize model-view + bt.game.battle.model.initialization.initialize(); }, // On view unload // TODO: Set interval inside Angular context onUnload : function() { // Stop timer interval if (bt.game.battle.timers._interval) clearInterval(bt.game.battle.timers._interval); + // Destroy model-view + bt.game.battle.model.initialization.destroy(); } }, @@ -187,15 +204,50 @@ bt.game.battle = { } - -// Custom events definitions +// Initialize 'battle view' model-view // --------------------------------------------------------------------------------------------------------------------- +bt.game.battle.model = { -// @ bt.services.battleService + // Battle view's view-model's initialization namespace + initialization : { -// "Service call successfull" event -bt.events.define(bt.services.battleService, 'Called'); -// "Service call error" event -bt.events.define(bt.services.battleService, 'Error'); -// "Service call successfull" event -bt.events.define(bt.services.battleService, 'Updated'); + // Initializes view-model + initialize : function() { + + // Get initial state + bt.services.execute('BattleService', function(service) { + service.initialState( + // On successfull load callback + function(data) { + // Fire event + bt.services.battleService.Error.dispatch({ + message: 'Response from "BattleService.init_state().', + data : data + }); + // Initialize view-model's battleField from response + bt.game.battle.model.battleField = new bt.model.definitions.battle.battleField(data); + }, + // On error callback + function(data) { + // Fire event + bt.services.battleService.Error.dispatch({ + message: 'Error calling "BattleService.init_state()"!', + data : data + }); + } + ); + }); + + }, + + // Destroys view-model + destroy : function() { + delete bt.game.battle.model.battleField; + } + + }, + + // Holds reference to battle view's battleField model + battleField : null + +} \ No newline at end of file diff --git a/res/js/model/bt_model_battle.js b/res/js/model/bt_model_battle.js index 35fd93b..e1f1e9c 100644 --- a/res/js/model/bt_model_battle.js +++ b/res/js/model/bt_model_battle.js @@ -169,6 +169,19 @@ bt.model.definitions.battle = { if (obj) bt.model.extend(this, [new bt.model.definitions.battle.localized(obj), new bt.model.definitions.battle.cStone(obj)]); // Initialize children this.contents = [ ]; + + // Adds content to tile + this.addContent = function(content) { + this.contents.push(content); + }; + // Removes content from tile + this.removedContent = function(content) { + for (var i in this.contents) if (this.contents[i] == content) this.contents[i] = null; + }; + // Clears all title's content + this.clearContent = function(content) { + this.contents = [ ]; + }; }, // Grid definition @@ -210,13 +223,13 @@ bt.model.definitions.battle = { // Initializes battle field from BattleService.init_state() response initialize : function(obj) { // Initialize grid - this.initializeGrid(obj); + this._initializeGrid(obj); // Initialize units - this.initializeUnits(obj); + this._initializeUnits(obj); }, // Initializes battle field's grid from BattleService.init_state() response - initializeGrid : function(obj) { + _initializeGrid : function(obj) { // Verify grid if (bt.debugging.model.verifyModelConstructors) { if ((!angular.isDefined(obj.initial_state)) || (!angular.isDefined(obj.initial_state.grid)) || (!angular.isDefined(obj.initial_state.grid.grid)) || (!angular.isDefined(obj.initial_state.grid.grid.tiles))) console.error(obj, 'Grid object definition incomplete: Missing grid!'); @@ -248,7 +261,7 @@ bt.model.definitions.battle = { }, // Initializes battle view's units from BattleService.init_state() response - initializeUnits : function(obj) { + _initializeUnits : function(obj) { // Verify units if (bt.debugging.model.verifyModelConstructors) { if ((!angular.isDefined(obj.initial_state)) || (!angular.isDefined(obj.initial_state.units))) console.error(obj, 'Grid object definition incomplete: Missing grid!'); @@ -267,6 +280,9 @@ bt.model.definitions.battle = { var unit = new bt.model.definitions.battle[unitType](unitDefinition); base.units.addUnit(unit); + // Add to grid + if (base.grid) base.grid.tilesByX[unit.location.x][unit.location.y].addContent(unit); + } } }