Skip to content

Commit

Permalink
local storage support
Browse files Browse the repository at this point in the history
  • Loading branch information
darekw committed Oct 7, 2013
1 parent 8d66c32 commit 9c32c6d
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 47 deletions.
31 changes: 14 additions & 17 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
height: 60px;
width: 60px;
}

.center-table
{
margin: 0 auto !important;
float: none !important;
}
</style>

<script id="transaction-page-template" type="text/html">
Expand Down Expand Up @@ -59,33 +65,24 @@ <h2>Transactions list: </h2>
<script id="main-page-template" type="text/html">
<div id="mainpage">
<div class="row-fluid">
<p class="span1"></p>
<p class="span1"></p>
<p class="span2">Online users: </p>
<p class="span3" id="online-users-count"></p>
<p class="span2">Cash: </p>
<p class="span2">Cash: </p>
<p class="span3" id="user-account-amount"></p>
<p class="span1"></p>
<p class="span1"></p>
</div>

<!--<div class="row-fluid">
<p class="span2">Cash: </p>
<p class="span10" id="user-account-amount"></p>
</div>-->

<div class="row-fluid">
<p class="span1"></p>
<p class="span1"></p>
<p class="span2">Your bet: </p>
<div class="span3" id="multipiers-box"></div>
<p class="span2">Number of mines: </p>
<p class="span2">Number of mines: </p>
<div class="span3" id="mines-box"></div>
<p class="span1"></p>
</div>

<!--<div class="row-fluid">

</div>-->

<div id="game-field" class="row-fluid ">
<div id="game-field" class="row-fluid">
<div class="span12">
<table class="table table-bordered min_width max_width" align="center">
<tbody>
Expand Down Expand Up @@ -132,10 +129,10 @@ <h2>Transactions list: </h2>
</div>

<div class="row-fluid">
<p class="span1"></p>
<p class="span1"></p>
<p class="span8">Your bet is multiplied by: </p>
<p class="span2" id="mines-multiplier-value"></p>
<p class="span1"></p>
<p class="span1"></p>
</div>

<div class="row-fluid" align="center">
Expand Down
35 changes: 23 additions & 12 deletions js/app/controller/mainPageController.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
define(['lodash', 'serverGateway', 'userCountView', 'multipierView', 'user', 'userAccountAmountView', 'minefieldView', 'minefield', 'minesView', 'authenticationService', 'game'],
function(_, ServerGateway, UserCountView, MultipierView, User, UserAccountAmountView, MinefieldView, MinefieldModel, MinesView, AuthenticationService, Game) {
define(['lodash', 'serverGateway', 'userCountView', 'multipierView', 'user', 'userAccountAmountView', 'minefieldView', 'minefield', 'minesView', 'authenticationService', 'game', 'bootbox'],
function(_, ServerGateway, UserCountView, MultipierView, User, UserAccountAmountView, MinefieldView, MinefieldModel, MinesView, AuthenticationService, Game, bootbox) {
function MainPageController() {
this.userCountView = new UserCountView();
this.multipierView = new MultipierView();
Expand Down Expand Up @@ -32,7 +32,7 @@ define(['lodash', 'serverGateway', 'userCountView', 'multipierView', 'user', 'us
this.minesView.disableMinesSelect();
this.multipierView.disable();
} else {
alert('Your bet is greater than your cash amount. Please deposit or change your bet.');
this.minefieldView.showMessage('Your bet is greater than your cash amount. Please deposit or change your bet.');
}
}

Expand All @@ -44,6 +44,11 @@ define(['lodash', 'serverGateway', 'userCountView', 'multipierView', 'user', 'us
this.minefieldView.cleanState();
this.minefieldModel.cleanState();
this.updateMinesMultiplier();
if (Game.isFinishedByUser()) {
this.minefieldView.showMessage("You won!");
} else {
this.minefieldView.showMessage("Sorry, you lost!");
}
Game.finishByApp();
}

Expand Down Expand Up @@ -77,15 +82,23 @@ define(['lodash', 'serverGateway', 'userCountView', 'multipierView', 'user', 'us
}
}

MainPageController.prototype.initViews = function() {
this.userCountView.update(Game.getUserCount());
this.userAccountAmountView.update(this.user.getAccountAmount());
this.multipierView.update(Game.getAvailableBets(), this.user.getAccountAmount());
}

MainPageController.prototype.handleRequest = function() {
var that = this;

this.serverGateway = ServerGateway;
this.authenticationService.authenticate();

this.serverGateway.registerCallback("objectsync", function(objectsyncJson) {
var objectSync = JSON.parse(objectsyncJson);
this.initViews();


this.serverGateway.registerCallback("objectsync", function(objectsyncJson) {
var objectSync = JSON.parse(objectsyncJson);

if (_.isObject(objectSync)) {
_.forEach(objectSync, function(value, key) {
Expand Down Expand Up @@ -134,11 +147,6 @@ define(['lodash', 'serverGateway', 'userCountView', 'multipierView', 'user', 'us
that.updateMinesMultiplier(object.multi);

} else if (_.has(object, 'done')) {
if (Game.isFinishedByUser()) {
that.minefieldView.showMessage("You won!");
} else {
that.minefieldView.showMessage("Sorry, you lost!");
}
that.finishGameAction();
}
}
Expand All @@ -149,8 +157,9 @@ define(['lodash', 'serverGateway', 'userCountView', 'multipierView', 'user', 'us
console.log(value);

if (_.has(value, 'cash')) {
that.user.setAccountAmount(value['cash']);
that.userAccountAmountView.update(value['cash']);
var transformedMoney = value['cash'] / 100000000;
that.user.setAccountAmount(transformedMoney);
that.userAccountAmountView.update(transformedMoney);
}
if (_.has(value, 'address_deposit')) {
that.user.setDepositAddresses(value['address_deposit']);
Expand All @@ -165,10 +174,12 @@ define(['lodash', 'serverGateway', 'userCountView', 'multipierView', 'user', 'us

if (_.has(value, 'users')) {
that.userCountView.update(value['users']);
Game.setUserCount(value['users']);
}

if (_.has(value, 'availiablebets')) {
that.multipierView.update(value['availiablebets'], that.user.getAccountAmount());
Game.setAvailableBets(value['availiablebets'])
}
}
}
Expand Down
11 changes: 6 additions & 5 deletions js/app/controller/transactionPageController.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ define(['serverGateway', 'lodash', 'user', 'transactionPageView', 'userAccountAm
console.log("Got answer: ");
console.log(answer);

if (_.isObject(answer) && _.has(answer, 'args') && _.has(answer.args, 'answerid')) {
if(answer.args.answerid.indexOf('listTransactions') > -1) {
that.transactionPageView.renderTransactionsList(answer.args.data);
if (_.isObject(answer) && _.has(answer, 'answerid')) {
if(answer.answerid.indexOf('listTransactions') > -1) {
that.transactionPageView.renderTransactionsList(JSON.parse(answer.data));
}
}
});
Expand Down Expand Up @@ -64,8 +64,9 @@ define(['serverGateway', 'lodash', 'user', 'transactionPageView', 'userAccountAm
}

if (_.has(object, 'cash')) {
that.user.setAccountAmount(object['cash']);
that.userAccountAmountView.update(object['cash']);
var transformedMoney = object['cash'] / 100000000
User.setAccountAmount(transformedMoney);
that.userAccountAmountView.update(transformedMoney);
}
}

Expand Down
18 changes: 18 additions & 0 deletions js/app/model/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,24 @@ define(['lodash'], function(_) {

function game() {
this.state = STATE.NOT_STARTED;
this.userCount = 0;
this.availableBets = [0];
}

game.prototype.setAvailableBets = function(availableBets) {
this.availableBets = availableBets;
}

game.prototype.getAvailableBets = function() {
return this.availableBets;
}

game.prototype.setUserCount = function(userCount) {
this.userCount = userCount;
}

game.prototype.getUserCount = function() {
return this.userCount;
}

game.prototype.startGame = function() {
Expand Down
1 change: 1 addition & 0 deletions js/app/model/user.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
define(['lodash'], function(_) {
function user() {
this.depositAddresses = [];
this.accountAmount = 0;
}

user.prototype.setDepositAddresses = function(depositAddresses) {
Expand Down
18 changes: 13 additions & 5 deletions js/app/serverGateway.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
define(['socket', 'lodash'], function(socket, _) {

function ServerGateway() {
console.log("Create connection with minefield server.")
this.connection = socket.connect("minefield.bitcoinlab.org:45284");
this.callbacks = {}
this.callbacks = {};
this.isConnected = false;
}

ServerGateway.prototype.connect = function(uid, secret) {
this.connection.emit("hello", {
uid: uid,
secret: secret
});
if (! this.isConnected) {
this.connection.emit("hello", {
uid: uid,
secret: secret
});
this.isConnected = true;
}
};

ServerGateway.prototype.send = function(params) {
Expand All @@ -22,6 +27,8 @@ define(['socket', 'lodash'], function(socket, _) {
}

ServerGateway.prototype.cleanState = function() {
console.log('Remove all callbacks.');
this.callbacks = {};
this.connection.removeAllListeners();
}

Expand All @@ -33,6 +40,7 @@ define(['socket', 'lodash'], function(socket, _) {
}
console.log('register callback: ' + eventName);
this.connection.on(eventName, callback);
console.log(this.connection);
}

var instance = new ServerGateway();
Expand Down
9 changes: 4 additions & 5 deletions js/app/service/authenticationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@ define(['serverGateway', 'lodash'], function(serverGateway, _) {
function authenticationService() {}

authenticationService.prototype.authenticate = function() {
console.log(sessionStorage);
if (_.isNull(sessionStorage.getItem('auth_data_uid')) || _.isNull(sessionStorage.getItem('auth_data_secret'))) {
if (_.isNull(localStorage.getItem('auth_data_uid')) || _.isNull(localStorage.getItem('auth_data_secret'))) {
serverGateway.newUser(function(object) {
sessionStorage.setItem('auth_data_uid', object.id);
sessionStorage.setItem('auth_data_secret', object.secret);
localStorage.setItem('auth_data_uid', object.id);
localStorage.setItem('auth_data_secret', object.secret);
serverGateway.connect(object.id, object.secret);
});
} else {
serverGateway.connect(sessionStorage.getItem('auth_data_uid'), sessionStorage.getItem('auth_data_secret'));
serverGateway.connect(localStorage.getItem('auth_data_uid'), localStorage.getItem('auth_data_secret'));
}
}

Expand Down
14 changes: 11 additions & 3 deletions js/app/view/transactionPageView.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,23 @@ define(['lodash', 'jquery'], function(_, $) {
}

transactionPageView.prototype.renderTransactionsList = function(transactionList) {
generateListAsHtml(transactionList, '#transaction-list-box');
generateListAsHtml(transactionList, '#transaction-list-box', function(object) {
return object.address + ", " + object.category + ", " + (object.amount / 100000000);
});

}

function generateListAsHtml(list, element) {
function generateListAsHtml(list, element, render) {
if (_.isArray(list)) {
$(element).html('');
_.each(list, function(value) {
var li = $('<li>');
li.html(value);
if (_.isFunction(render)) {
li.html(render(value));
} else {
li.html(value);
}

$(element).append(li);
});
}
Expand Down
6 changes: 6 additions & 0 deletions js/lib/bootbox.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions js/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require.config({
paths: {
'bootstrap': 'lib/bootstrap.min',
'bootbox': 'lib/bootbox.min',
'jquery': 'lib/jquery',
'socket': 'lib/socket.io',
'lodash': 'lib/lodash.min',
Expand Down
Binary file modified minefield-icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 9c32c6d

Please sign in to comment.