Skip to content

Commit 9c32c6d

Browse files
committed
local storage support
1 parent 8d66c32 commit 9c32c6d

File tree

11 files changed

+98
-47
lines changed

11 files changed

+98
-47
lines changed

index.html

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
height: 60px;
2525
width: 60px;
2626
}
27+
28+
.center-table
29+
{
30+
margin: 0 auto !important;
31+
float: none !important;
32+
}
2733
</style>
2834

2935
<script id="transaction-page-template" type="text/html">
@@ -59,33 +65,24 @@ <h2>Transactions list: </h2>
5965
<script id="main-page-template" type="text/html">
6066
<div id="mainpage">
6167
<div class="row-fluid">
62-
<p class="span1"></p>
68+
<p class="span1"></p>
6369
<p class="span2">Online users: </p>
6470
<p class="span3" id="online-users-count"></p>
65-
<p class="span2">Cash: </p>
71+
<p class="span2">Cash: </p>
6672
<p class="span3" id="user-account-amount"></p>
67-
<p class="span1"></p>
73+
<p class="span1"></p>
6874
</div>
6975

70-
<!--<div class="row-fluid">
71-
<p class="span2">Cash: </p>
72-
<p class="span10" id="user-account-amount"></p>
73-
</div>-->
74-
7576
<div class="row-fluid">
76-
<p class="span1"></p>
77+
<p class="span1"></p>
7778
<p class="span2">Your bet: </p>
7879
<div class="span3" id="multipiers-box"></div>
79-
<p class="span2">Number of mines: </p>
80+
<p class="span2">Number of mines: </p>
8081
<div class="span3" id="mines-box"></div>
8182
<p class="span1"></p>
8283
</div>
8384

84-
<!--<div class="row-fluid">
85-
86-
</div>-->
87-
88-
<div id="game-field" class="row-fluid ">
85+
<div id="game-field" class="row-fluid">
8986
<div class="span12">
9087
<table class="table table-bordered min_width max_width" align="center">
9188
<tbody>
@@ -132,10 +129,10 @@ <h2>Transactions list: </h2>
132129
</div>
133130

134131
<div class="row-fluid">
135-
<p class="span1"></p>
132+
<p class="span1"></p>
136133
<p class="span8">Your bet is multiplied by: </p>
137134
<p class="span2" id="mines-multiplier-value"></p>
138-
<p class="span1"></p>
135+
<p class="span1"></p>
139136
</div>
140137

141138
<div class="row-fluid" align="center">

js/app/controller/mainPageController.js

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
define(['lodash', 'serverGateway', 'userCountView', 'multipierView', 'user', 'userAccountAmountView', 'minefieldView', 'minefield', 'minesView', 'authenticationService', 'game'],
2-
function(_, ServerGateway, UserCountView, MultipierView, User, UserAccountAmountView, MinefieldView, MinefieldModel, MinesView, AuthenticationService, Game) {
1+
define(['lodash', 'serverGateway', 'userCountView', 'multipierView', 'user', 'userAccountAmountView', 'minefieldView', 'minefield', 'minesView', 'authenticationService', 'game', 'bootbox'],
2+
function(_, ServerGateway, UserCountView, MultipierView, User, UserAccountAmountView, MinefieldView, MinefieldModel, MinesView, AuthenticationService, Game, bootbox) {
33
function MainPageController() {
44
this.userCountView = new UserCountView();
55
this.multipierView = new MultipierView();
@@ -32,7 +32,7 @@ define(['lodash', 'serverGateway', 'userCountView', 'multipierView', 'user', 'us
3232
this.minesView.disableMinesSelect();
3333
this.multipierView.disable();
3434
} else {
35-
alert('Your bet is greater than your cash amount. Please deposit or change your bet.');
35+
this.minefieldView.showMessage('Your bet is greater than your cash amount. Please deposit or change your bet.');
3636
}
3737
}
3838

@@ -44,6 +44,11 @@ define(['lodash', 'serverGateway', 'userCountView', 'multipierView', 'user', 'us
4444
this.minefieldView.cleanState();
4545
this.minefieldModel.cleanState();
4646
this.updateMinesMultiplier();
47+
if (Game.isFinishedByUser()) {
48+
this.minefieldView.showMessage("You won!");
49+
} else {
50+
this.minefieldView.showMessage("Sorry, you lost!");
51+
}
4752
Game.finishByApp();
4853
}
4954

@@ -77,15 +82,23 @@ define(['lodash', 'serverGateway', 'userCountView', 'multipierView', 'user', 'us
7782
}
7883
}
7984

85+
MainPageController.prototype.initViews = function() {
86+
this.userCountView.update(Game.getUserCount());
87+
this.userAccountAmountView.update(this.user.getAccountAmount());
88+
this.multipierView.update(Game.getAvailableBets(), this.user.getAccountAmount());
89+
}
90+
8091
MainPageController.prototype.handleRequest = function() {
8192
var that = this;
93+
8294
this.serverGateway = ServerGateway;
8395
this.authenticationService.authenticate();
8496

85-
this.serverGateway.registerCallback("objectsync", function(objectsyncJson) {
86-
var objectSync = JSON.parse(objectsyncJson);
97+
this.initViews();
8798

8899

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

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

136149
} else if (_.has(object, 'done')) {
137-
if (Game.isFinishedByUser()) {
138-
that.minefieldView.showMessage("You won!");
139-
} else {
140-
that.minefieldView.showMessage("Sorry, you lost!");
141-
}
142150
that.finishGameAction();
143151
}
144152
}
@@ -149,8 +157,9 @@ define(['lodash', 'serverGateway', 'userCountView', 'multipierView', 'user', 'us
149157
console.log(value);
150158

151159
if (_.has(value, 'cash')) {
152-
that.user.setAccountAmount(value['cash']);
153-
that.userAccountAmountView.update(value['cash']);
160+
var transformedMoney = value['cash'] / 100000000;
161+
that.user.setAccountAmount(transformedMoney);
162+
that.userAccountAmountView.update(transformedMoney);
154163
}
155164
if (_.has(value, 'address_deposit')) {
156165
that.user.setDepositAddresses(value['address_deposit']);
@@ -165,10 +174,12 @@ define(['lodash', 'serverGateway', 'userCountView', 'multipierView', 'user', 'us
165174

166175
if (_.has(value, 'users')) {
167176
that.userCountView.update(value['users']);
177+
Game.setUserCount(value['users']);
168178
}
169179

170180
if (_.has(value, 'availiablebets')) {
171181
that.multipierView.update(value['availiablebets'], that.user.getAccountAmount());
182+
Game.setAvailableBets(value['availiablebets'])
172183
}
173184
}
174185
}

js/app/controller/transactionPageController.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ define(['serverGateway', 'lodash', 'user', 'transactionPageView', 'userAccountAm
1111
console.log("Got answer: ");
1212
console.log(answer);
1313

14-
if (_.isObject(answer) && _.has(answer, 'args') && _.has(answer.args, 'answerid')) {
15-
if(answer.args.answerid.indexOf('listTransactions') > -1) {
16-
that.transactionPageView.renderTransactionsList(answer.args.data);
14+
if (_.isObject(answer) && _.has(answer, 'answerid')) {
15+
if(answer.answerid.indexOf('listTransactions') > -1) {
16+
that.transactionPageView.renderTransactionsList(JSON.parse(answer.data));
1717
}
1818
}
1919
});
@@ -64,8 +64,9 @@ define(['serverGateway', 'lodash', 'user', 'transactionPageView', 'userAccountAm
6464
}
6565

6666
if (_.has(object, 'cash')) {
67-
that.user.setAccountAmount(object['cash']);
68-
that.userAccountAmountView.update(object['cash']);
67+
var transformedMoney = object['cash'] / 100000000
68+
User.setAccountAmount(transformedMoney);
69+
that.userAccountAmountView.update(transformedMoney);
6970
}
7071
}
7172

js/app/model/game.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,24 @@ define(['lodash'], function(_) {
88

99
function game() {
1010
this.state = STATE.NOT_STARTED;
11+
this.userCount = 0;
12+
this.availableBets = [0];
13+
}
14+
15+
game.prototype.setAvailableBets = function(availableBets) {
16+
this.availableBets = availableBets;
17+
}
18+
19+
game.prototype.getAvailableBets = function() {
20+
return this.availableBets;
21+
}
22+
23+
game.prototype.setUserCount = function(userCount) {
24+
this.userCount = userCount;
25+
}
26+
27+
game.prototype.getUserCount = function() {
28+
return this.userCount;
1129
}
1230

1331
game.prototype.startGame = function() {

js/app/model/user.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
define(['lodash'], function(_) {
22
function user() {
33
this.depositAddresses = [];
4+
this.accountAmount = 0;
45
}
56

67
user.prototype.setDepositAddresses = function(depositAddresses) {

js/app/serverGateway.js

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
define(['socket', 'lodash'], function(socket, _) {
22

33
function ServerGateway() {
4+
console.log("Create connection with minefield server.")
45
this.connection = socket.connect("minefield.bitcoinlab.org:45284");
5-
this.callbacks = {}
6+
this.callbacks = {};
7+
this.isConnected = false;
68
}
79

810
ServerGateway.prototype.connect = function(uid, secret) {
9-
this.connection.emit("hello", {
10-
uid: uid,
11-
secret: secret
12-
});
11+
if (! this.isConnected) {
12+
this.connection.emit("hello", {
13+
uid: uid,
14+
secret: secret
15+
});
16+
this.isConnected = true;
17+
}
1318
};
1419

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

2429
ServerGateway.prototype.cleanState = function() {
30+
console.log('Remove all callbacks.');
31+
this.callbacks = {};
2532
this.connection.removeAllListeners();
2633
}
2734

@@ -33,6 +40,7 @@ define(['socket', 'lodash'], function(socket, _) {
3340
}
3441
console.log('register callback: ' + eventName);
3542
this.connection.on(eventName, callback);
43+
console.log(this.connection);
3644
}
3745

3846
var instance = new ServerGateway();

js/app/service/authenticationService.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ define(['serverGateway', 'lodash'], function(serverGateway, _) {
22
function authenticationService() {}
33

44
authenticationService.prototype.authenticate = function() {
5-
console.log(sessionStorage);
6-
if (_.isNull(sessionStorage.getItem('auth_data_uid')) || _.isNull(sessionStorage.getItem('auth_data_secret'))) {
5+
if (_.isNull(localStorage.getItem('auth_data_uid')) || _.isNull(localStorage.getItem('auth_data_secret'))) {
76
serverGateway.newUser(function(object) {
8-
sessionStorage.setItem('auth_data_uid', object.id);
9-
sessionStorage.setItem('auth_data_secret', object.secret);
7+
localStorage.setItem('auth_data_uid', object.id);
8+
localStorage.setItem('auth_data_secret', object.secret);
109
serverGateway.connect(object.id, object.secret);
1110
});
1211
} else {
13-
serverGateway.connect(sessionStorage.getItem('auth_data_uid'), sessionStorage.getItem('auth_data_secret'));
12+
serverGateway.connect(localStorage.getItem('auth_data_uid'), localStorage.getItem('auth_data_secret'));
1413
}
1514
}
1615

js/app/view/transactionPageView.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,23 @@ define(['lodash', 'jquery'], function(_, $) {
2020
}
2121

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

26-
function generateListAsHtml(list, element) {
29+
function generateListAsHtml(list, element, render) {
2730
if (_.isArray(list)) {
2831
$(element).html('');
2932
_.each(list, function(value) {
3033
var li = $('<li>');
31-
li.html(value);
34+
if (_.isFunction(render)) {
35+
li.html(render(value));
36+
} else {
37+
li.html(value);
38+
}
39+
3240
$(element).append(li);
3341
});
3442
}

js/lib/bootbox.min.js

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/main.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
require.config({
22
paths: {
3+
'bootstrap': 'lib/bootstrap.min',
4+
'bootbox': 'lib/bootbox.min',
35
'jquery': 'lib/jquery',
46
'socket': 'lib/socket.io',
57
'lodash': 'lib/lodash.min',

0 commit comments

Comments
 (0)