Skip to content

Commit

Permalink
Implemented routing: views are now represented by different URLs and …
Browse files Browse the repository at this point in the history
…work with browser's Forward & Back functions (Code needs cleanup). Implemented service timers (BattleService.timeLeft) with client-side approximation to reduce server calls. (TO DO: to avoid errors, setInterval() call needs to be run inside Angular context)
  • Loading branch information
juraj committed Jun 12, 2013
1 parent c2127d2 commit f558e8f
Show file tree
Hide file tree
Showing 13 changed files with 477 additions and 279 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,43 @@
### Battle service (not hooked to view model, will not have UI manifestation)

```javascript
bt.services.execute('battleService', function(service) {
bt.services.execute('BattleService', function(service) {
service.getUsername(
function(data) { console.log("Success:"); console.log(data); },
function(data) { console.log("Fail:"); console.log(data); }
);
});
bt.services.execute('battleService', function(service) {
bt.services.execute('BattleService', function(service) {
service.timeLeft(
function(data) { console.log("Success:"); console.log(data); },
function(data) { console.log("Fail:"); console.log(data); }
);
});
bt.services.execute('battleService', function(service) {
bt.services.execute('BattleService', function(service) {
service.initialState(
function(data) { console.log("Success:"); console.log(data); },
function(data) { console.log("Fail:"); console.log(data); }
);
});
bt.services.execute('battleService', function(service) {
bt.services.execute('BattleService', function(service) {
service.lastResult(
function(data) { console.log("Success:"); console.log(data); },
function(data) { console.log("Fail:"); console.log(data); }
);
});
bt.services.execute('battleService', function(service) {
bt.services.execute('BattleService', function(service) {
service.getStates(
function(data) { console.log("Success:"); console.log(data); },
function(data) { console.log("Fail:"); console.log(data); }
);
});
bt.services.execute('battleService', function(service) {
bt.services.execute('BattleService', function(service) {
service.getLastState(
function(data) { console.log("Success:"); console.log(data); },
function(data) { console.log("Fail:"); console.log(data); }
);
});
bt.services.execute('battleService', function(service) {
bt.services.execute('BattleService', function(service) {
service.processAction(
'action type',
function(data) { console.log("Success:"); console.log(data); },
Expand Down
22 changes: 13 additions & 9 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,21 @@

<body ng-controller="bt.ng.controller">

<!-- Includes current view -->
<!-- Header -->
<div class="section">
Current user: {{ bt.game.common.user.username }}
<hr/><br/>
Select view:
<select ng-model="bt.navigation.nextView" ng-change=" bt.navigation.selectView() " ng-options="v.name for v in bt.config.views.viewsArray"></select>
<a ng-show=" bt.game.authentication.isAuthenticated ">Current user: {{ bt.game.authentication.username }}</a>
<hr/>
<div class="menu">
<div class="link" ng-repeat=" (key, view) in bt.config.views.viewsByName | orderBy: view._index ">
<a ng-if=" view.isPublic || bt.game.authentication.isAuthenticated " href="#{{ key }}"> {{ view.name }} </a>
</div>
</div>
<div style="clear: both"></div>
</div>

<div ng-include src=" bt.navigation.selectedView.url " ng-animate=" bt.effects.viewChange "></div>
<!-- Includes current view -->
<div ng-view ng-animate=" bt.effects.viewChange "></div>

<div id="test" ng-controller=" bt.game.common.ng.controller "></div>

<!-- 3rd party: JS inclusions -->
<script type="text/javascript" src="res/js/3rd_party/angular.115.js"></script>
<!-- Not yet needed: <script type="text/javascript" src="res/js/3rd_party/angular_resource.115.js"></script> -->
Expand All @@ -28,7 +31,8 @@
<!-- BT: JS inclusions -->
<script type="text/javascript" src="res/js/bt_init.js"></script>
<script type="text/javascript" src="res/js/bt_config.js"></script>
<script type="text/javascript" src="res/js/bt_game.js"></script>
<script type="text/javascript" src="res/js/bt_authentication.js"></script>
<script type="text/javascript" src="res/js/bt_game_frontpage.js"></script>
<script type="text/javascript" src="res/js/bt_game_equanimity.js"></script>
<script type="text/javascript" src="res/js/bt_game_battle.js"></script>
<script type="text/javascript" src="res/js/bt_game_equipment.js"></script>
Expand Down
36 changes: 36 additions & 0 deletions res/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,39 @@ div.section {
font-size: 11px;
color: c6ff00;
}

/* Page markup (TEMP)
* ---------------------------------------------------------------------------------------------------------------------- */

div.menu {
clear: both;
float: left;
margin: 10px 0px 0px 0px;
padding: 4px;
}
div.menu div.link {
float: left;
margin: 0px 4px 0px 0px;
}
div.menu div.link a {
padding: 5px;
cursor: pointer;
border: 1px solid #444444;
}
div.menu div.link:hover a {
padding: 4px;
border: 2px solid #444444;
}

/* Battle view markup (TEMP)
* ---------------------------------------------------------------------------------------------------------------------- */

div.timers {
float: right;
margin: 4px 10px 4px 10px;
padding: 4px;
border: 1px solid #444444;
}
div.timer {
color: #990000;
}
122 changes: 122 additions & 0 deletions res/js/bt_authentication.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
"use strict";

/* =====================================================================================================================
* Binary tactics: Authentication
* ================================================================================================================== */


// Services
// ---------------------------------------------------------------------------------------------------------------------

// Authentication service
/*
Console call syntax example:
bt.services.execute('authService', function(service) {
service.authenticate( 'atkr', 'atkr2', function() { alert("Success"); }, function() { alert("Fail!"); } );
});
*/
bt.services.authenticationService = app.factory('AuthenticationService', function ($http) {
return {

// Authenticate method, takes onSuccess and onFail callback functions
// Calls authentication service and trys authenticating user
authenticate : function(username, password, onSuccess, onFail, onError) {
// Check if authentication in progress
if (bt.game.authentication.login._isAuthenticating) return false;
// Update username and password
if (username != null) bt.game.authentication.login.username = username;
if (password != null) bt.game.authentication.login.password = password;
// Prompt authenticating
// Set authenticating username and status
bt.game.authentication.login._isAuthenticating = true;
bt.game.authentication.login._authUsername = bt.game.authentication.login.username
$http({

// Service call configuration
method: 'POST',
url: '/auth/login',
params : { u : bt.game.authentication.login.username, p : bt.game.authentication.login.password}

}).success(function(data, status, headers, config) {

// Handle response: Success / Check response
if (data.login == "successful") {
// Authenticate
bt.game.authentication.isAuthenticated = true;
bt.game.authentication.username = bt.game.authentication.login._authUsername;
// Execute callback
if (onSuccess) onSuccess(data, status, headers(), config);
// Fire event
bt.services.authenticationService.authenticationSuccessfull.dispatch( 'Authentication successfull.' );
} else {
// De-Authenticate
bt.game.authentication.isAuthenticated = false;
bt.game.authentication.username = '';
// Execute callback
if (onFail) onFail(data, status, headers(), config);
// Fire event
bt.services.authenticationService.authenticationFailed.dispatch( 'Authentication failed.' );
}
// Set authenticating status
bt.game.authentication.login._isAuthenticating = false;

}).error(function(data, status, headers, config) {

// De-Authenticate
bt.game.authentication.isAuthenticated = false;
bt.game.authentication.username = '';
// Execute callback
if (onError) onError(data, status, headers(), config);
if (onFail) onFail(data, status, headers(), config);
// Fire event
bt.services.authenticationService.authenticationError.dispatch( 'Authentication error!' );
// Set authenticating status
bt.game.authentication.login._isAuthenticating = false;

});
// Return 'started authentication'
return true;
}

}
});


// Initialize common game functionality
// ---------------------------------------------------------------------------------------------------------------------
bt.game.authentication = {

// Holds authentication status
isAuthenticated : false,
// Holds user's username
username : '',

// Login functionality namespace
login : {

_isAuthenticating : false,
// Currently authenticating username
_authUsername : '',

// Holds user's username
username : 'atkr',
// Holds user's password
password : 'atkr'

},

}


// 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');
15 changes: 3 additions & 12 deletions res/js/bt_config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,8 @@ bt.config.urls = {
servicesUrl : '/battle/'
}

// Add public views
bt.config.views.addView('frontpage', {
id : 'bt.game.common',
name : 'frontpage',
url : 'res/partials/views/frontpage.html',
depth : 0,
verify : function() { return true; },
onLoad : function() { console.log('> Loading frontpage view!'); },
onUnload : function() { console.log('> Unloading frontpage view!'); }
});
bt.navigation.selectView( bt.config.views.viewsByName.frontpage );
// Set poling
bt.config.poling.minimalIntervalBetweenPolls = 2000;

// Set debugging options
bt.debugging.events.pushToConsole = true;
bt.debugging.events.publishToConsole = true;
Loading

0 comments on commit f558e8f

Please sign in to comment.