-
Notifications
You must be signed in to change notification settings - Fork 2
/
ledger.js
338 lines (213 loc) · 10.6 KB
/
ledger.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
$(document).ready(function(e) {
var players = [
{ "name": "Satoshi", "balance": 0, "color" : "red"},
{ "name": "Hal", "balance": 0, "color" : "yellow"},
{ "name": "Hash", "balance": 0, "color" : "blue"},
{ "name": "Merkle", "balance": 0, "color" : "green"}
];
var players_total = players.length;
var space, open_row, close_row;
var rows = '';
var row_id = 1;
var genesis_block_year = 2009;
var block_time = 10;
var genesis_block_reward = 50;
var blocks_per_play = 210000/players_total;
var halving_interval = 210000;
var initial_fees_ratio = 1;
var year;
var block_height = 0;
var halving_era = 0;
var circulation = 0;
var player_reward = 0;
var halving = reward_formated = end_reward = player_reward_formated = circulation_formated = '';
var reward = genesis_block_reward;
var play_limit = 280;
var play = "";
var i;
/*
var miner_select = '<select class="miner-select" disabled>';
miner_select += '<option disabled selected value> - </option>'
for (i = 0; i < players_total; i++) {
miner_select += '<option value="'+i+'">'+players[i].name+'</option>';
}
miner_select += '</select>';
*/
var miner_select = '';
for (i = 0; i < players_total; i++) {
//miner_select += '<input type="radio" name="miner-select" value="'+i+'">'+players[i].name;
miner_select += '<label>';
miner_select += '<input type="radio" name="miner-select" value="'+i+'">';
//miner_select += '<img src="http://placehold.it/40x60/0bf/fff&text=A'+i+' '+players[i].name+'">';
miner_select += '<div class="input-name player-'+(i+1)+'">'+players[i].name+'</div>';
miner_select += '</label>';
}
var player_balances = '';
//still need to customize this loop to show only the change in balance.
player_balances += '<div class="cell player-balances-container change">';
for (i = 0; i < players_total; i++) {
player_balances += '<div class="cell player-balance player-balance-'+i+'" data-player-name="'+players[i].name+' Change"> - </div>';
}
player_balances += '</div>';
player_balances += '<div class="cell player-balances-container totals">';
for (i = 0; i < players_total; i++) {
player_balances += '<div class="cell player-balance player-balance-'+i+'" data-player-name="'+players[i].name+' Total"> - </div>';
}
player_balances += '</div>';
/*
var outcome_select = '<select class="outcome-select" disabled>';
outcome_select += '<option disabled selected value> - </option>'
outcome_select += '<option value="positive">Positive</option>';
outcome_select += '<option value="neutral">Neutral</option>';
outcome_select += '<option value="negative">Negative</option>';
outcome_select += '</select>';
*/
var outcome_select = '<label>';
outcome_select += '<input type="radio" name="outcome-select" value="positive">';
outcome_select += '<div class="input-name positive">↑</div>';
outcome_select += '</label>';
outcome_select += '<label>';
outcome_select += '<input type="radio" name="outcome-select" value="neutral">';
outcome_select += '<div class="input-name neutral">-</div>';
outcome_select += '</label>';
outcome_select += '<label>';
outcome_select += '<input type="radio" name="outcome-select" value="negative">';
outcome_select += '<div class="input-name negative">↓</div>';
outcome_select += '</label>';
for (i = 1; i < play_limit; i++) {
if(i%players_total == 1 && i !== 1){
halving_era = halving_era + 1;
reward = reward/2;
halving = 'Halving #'+halving_era;
}else if( i === 1){
halving = 'Genesis Block';
}else{
halving = '';
}
year = Math.ceil(genesis_block_year+((block_height*block_time)/60/24/365));
//don't let rewards have more than 8 decimal places
if(block_height < 2100000){
reward_formated = reward;
}else{
reward_formated = reward.toFixed(8);
}
if(year >= 2140){
end_reward = "end-reward";
reward_formated = 0;
circulation = 21000000;
}
player_reward = reward_formated*blocks_per_play;
player_reward_formated = formatNumber(player_reward.toFixed(3));
circulation = (circulation+player_reward);
circulation_formated = formatNumber(circulation.toFixed(3));
play += '<div class="play play-'+i+' '+end_reward+'">';
play += '<div class="row play-info">';
play += '<div class="cell play-id turn-number">'+i+'</div>';
play += '<div class="cell reward">'+reward_formated+'</div>';
//play += '<div class="cell block">'+block_height+'</div>';
//play += '<div class="cell year">'+year+'</div>';
play += '<div class="cell circulation">'+circulation_formated+'</div>';
play += '<div class="cell player-reward" data="'+player_reward+'">'+player_reward_formated+'</div>';
play += '<div class="cell halving">'+halving+'</div>';
play += '<div class="cell player-miner">'+miner_select+'</div>';
play += '<div class="cell player-turn" data="'+(i-1)%players_total+'">'+players[(i-1)%players_total].name+'<br><input type="checkbox" name="card-buy" value="1"> Card Buy</div>';
play += '<div class="cell space"><input class="space-input" type="text" name="space" disabled></div>';
play += '<div class="cell outcome">'+outcome_select+'</div>';
play += '</div>';
play += '<div class="row play-balances">';
play += player_balances;
play += '</div>';
play += '</div>';
block_height = block_height + blocks_per_play;
}
$('#ledger').append(play);
var space_rules;
var turn_players = [];
$(".play-1").find(".miner-select").prop('disabled', false);
//$(".miner-select").change(function(){
$('input:radio[name=miner-select]').change(function() {
$(this).parent().parent().children('label').children('.input-name').removeClass('selected-input');
$(this).parent().children('.input-name').addClass('selected-input');
var this_row = $(this).parent().parent().parent().parent();
if(this_row.hasClass('play-1')){
for (i = 0; i < players_total; i++) {
this_row.children(".play-balances").children(".player-balances-container").children(".player-balance-"+i).text(parseInt(0));
}
}else{
var previous_balences_el = this_row.prev();
for (i = 0; i < players_total; i++) {
var previous_player_balance = parseInt(previous_balences_el.children(".play-balances").children(".player-balances-container").children(".player-balance-"+i).text());
this_row.children(".play-balances").children(".player-balances-container").children(".player-balance-"+i).text(parseInt(previous_player_balance));
}
this_row.prev().find(".outcome-select").prop('disabled', true);
}
var miner_player_id = $(this).val();
var play_reward = this_row.children('.play-info').children(".player-reward").attr('data');
var current_balance = this_row.children(".play-balances").children(".player-balances-container").children(".player-balance-"+miner_player_id).text();
this_row.children(".play-balances").children(".player-balances-container").children(".player-balance-"+miner_player_id).text(parseInt(current_balance) + parseInt(play_reward));
for (i = 0; i < players_total; i++) {
players[i].balance = parseInt(this_row.children(".play-balances").children(".player-balances-container").children(".player-balance-"+i).text());
}
this_row.children('.play-info').find(".space-input").prop('disabled', false);
});
$(".space-input").change(function(){
var this_row = $(this).parent().parent().parent();
//TODO force number only (between the length of the spaces array)
this_row.find(".outcome-select").prop('disabled', false);
this_row.find(".miner-select").prop('disabled', true);
space_rules = spaces[$(this).val()];
turn_players = players;
console.log(turn_players);
});
$('input:radio[name=outcome-select]').change(function() {
$(this).parent().parent().children('label').children('.input-name').removeClass('selected-input');
$(this).parent().children('.input-name').addClass('selected-input');
var other_player_balance;
var current_player_balance;
var transfer_sum = transfer_single = 0;
var this_row = $(this).parent().parent().parent().parent();
var current_player_id = this_row.children('.play-info').children(".player-turn").attr('data');
var outcome = $(this).val();
this_row.find(".space-input").prop('disabled', true);
this_row.next().find(".miner-select").prop('disabled', false);
console.log(current_player_id);
players = turn_players;
console.log(turn_players);
for (i = 0; i < players_total; i++) {
//Reset balances
this_row.children('.play-balances').children(".player-balance-"+i).text(parseInt(turn_players[i].balance));
console.log(this_row.children('.play-balances').children(".player-balance-"+i).text());
if(outcome == "positive"){
//Take from the others
if(i != current_player_id){
//console.log(i +' - '+current_player_id+' - '+players[i].balance);
other_player_balance = players[i].balance;
console.log(players);
players[current_player_id].balance = players[current_player_id].balance;
transfer_single = (other_player_balance/100)*space_rules[outcome];
players[i].balance = other_player_balance-transfer_single;
//transfer_sum = transfer_sum + transfer_single;
current_player_balance = players[current_player_id].balance;
players[current_player_id].balance = current_player_balance+transfer_single;
//console.log("take "+transfer_single+" from player "+i+" and give to player "+current_player_id+" - Rule "+space_rules[outcome]);
this_row.children(".player-balance-"+i).text(parseInt(players[i].balance));
this_row.children(".player-balance-"+current_player_id).text(parseInt(players[current_player_id].balance));
//console.log(i +' + '+current_player_id+' + '+players[i].balance);
console.log('POSITIVE');
}
}else if(outcome == "negative"){
//Give to all others
console.log('NEGATIVE');
}else{
//Neutal nothing happens to ballances
this_row.children(".player-balance-"+i).text(turn_players[i].balance);
players = turn_players;
//console.log(turn_players[i].balance);
console.log('NEUTRAL');
}
}
});
});
function formatNumber(num) {
return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1,')
}