Skip to content

Commit

Permalink
Battle view's view-model ('bt.game.battle.model.battleField') loading…
Browse files Browse the repository at this point in the history
… / unloading is now tied in with loading / unloading of the battle view UI.
  • Loading branch information
juraj committed Jun 13, 2013
1 parent 5609c2d commit d65a4be
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 42 deletions.
20 changes: 5 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
```


Expand Down
28 changes: 14 additions & 14 deletions res/js/bt_authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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');
}
70 changes: 61 additions & 9 deletions res/js/bt_game_battle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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();
}
},

Expand Down Expand Up @@ -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

}
24 changes: 20 additions & 4 deletions res/js/model/bt_model_battle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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!');
Expand Down Expand Up @@ -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!');
Expand All @@ -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);

}
}
}
Expand Down

0 comments on commit d65a4be

Please sign in to comment.