-
Notifications
You must be signed in to change notification settings - Fork 3
/
dominion.js
587 lines (501 loc) · 15.8 KB
/
dominion.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
// Map from player name to Player object.
var players = new Object();
var started = false;
var disabled = false;
var turn_number = 0;
var player_count = 0;
var show_action_count = false;
var show_unique_count = false;
var show_victory_count = false;
var show_duchy_count = false;
var had_error = false;
var announced_error = false;
var player_re = null;
var userID;
var gameID;
var myName;
var send_sequence = 0;
var receive_sequence = 0;
var last_status_print = 0;
// The version of the extension currently loaded.
var extension_version = 'Unknown';
// Quotes a string so it matches literally in a regex.
RegExp.quote = function(str) {
return str.replace(/([.?*+^$[\]\\(){}-])/g, "\\$1");
};
function debugString(thing) {
return JSON.stringify(thing);
}
function handleError(text) {
console.log(text);
if (!had_error) {
had_error = true;
alert("Point counter error. Results may no longer be accurate: " + text);
}
}
function maybeAnnounceFailure(text) {
if (!disabled && !announced_error) {
console.log("Logging error: " + text);
writeText(text);
}
announced_error = true;
}
function pointsForCard(card_name) {
if (card_name == undefined) {
handleError("Undefined card for points...");
return 0;
}
if (card_name.indexOf("Colony") == 0) return 10;
if (card_name.indexOf("Province") == 0) return 6;
if (card_name.indexOf("Duchy") == 0) return 3;
if (card_name.indexOf("Estate") == 0) return 1;
if (card_name.indexOf("Curse") == 0) return -1;
if (card_name.indexOf("Island") == 0) return 2;
if (card_name.indexOf("Nobles") == 0) return 2;
if (card_name.indexOf("Harem") == 0) return 2;
if (card_name.indexOf("Great Hall") == 0) return 1;
if (card_name.indexOf("Farmland") == 0) return 2;
if (card_name.indexOf("Tunnel") == 0) return 2;
return 0;
}
function Player(name) {
this.name = name;
this.score = 3;
this.deck_size = 10;
// Map from special counts (such as number of gardens) to count.
this.special_counts = { "Treasure" : 7, "Victory" : 3, "Uniques" : 2 };
this.card_counts = { "Copper" : 7, "Estate" : 3 };
this.getScore = function() {
var score_str = this.score;
var total_score = this.score;
if (this.card_counts["Gardens"] != undefined) {
var gardens = this.card_counts["Gardens"];
var garden_points = Math.floor(this.deck_size / 10);
score_str = score_str + "+" + gardens + "g@" + garden_points;
total_score = total_score + gardens * garden_points;
}
if (this.card_counts["Silk Road"] != undefined) {
var silk_roads = this.card_counts["Silk Road"];
var silk_road_points = 0;
if (this.special_counts["Victory"] != undefined) {
silk_road_points = Math.floor(this.special_counts["Victory"] / 4);
}
score_str = score_str + "+" + silk_roads + "s@" + silk_road_points;
total_score = total_score + silk_roads * silk_road_points;
}
if (this.card_counts["Duke"] != undefined) {
var dukes = this.card_counts["Duke"];
var duke_points = 0;
if (this.card_counts["Duchy"] != undefined) {
duke_points = this.card_counts["Duchy"];
}
score_str = score_str + "+" + dukes + "d@" + duke_points;
total_score = total_score + dukes * duke_points;
}
if (this.card_counts["Vineyard"] != undefined) {
var vineyards = this.card_counts["Vineyard"];
var vineyard_points = 0;
if (this.special_counts["Actions"] != undefined) {
vineyard_points = Math.floor(this.special_counts["Actions"] / 3);
}
score_str = score_str + "+" + vineyards + "v@" + vineyard_points;
total_score = total_score + vineyards * vineyard_points;
}
if (this.card_counts["Fairgrounds"] != undefined) {
var fairgrounds = this.card_counts["Fairgrounds"];
var fairgrounds_points = 0;
if (this.special_counts["Uniques"] != undefined) {
fairgrounds_points = Math.floor(this.special_counts["Uniques"] / 5) * 2;
}
score_str = score_str + "+" + fairgrounds + "f@" + fairgrounds_points;
total_score = total_score + fairgrounds * fairgrounds_points;
}
if (total_score != this.score) {
score_str = score_str + "=" + total_score;
}
return score_str;
}
this.getDeckString = function() {
var str = this.deck_size;
var need_action_string = (show_action_count && this.special_counts["Actions"]);
var need_unique_string = (show_unique_count && this.special_counts["Uniques"]);
var need_victory_string = (show_victory_count && this.special_counts["Victory"]);
var need_duchy_string = (show_duchy_count && this.card_counts["Duchy"]);
if (need_action_string || need_unique_string || need_duchy_string || need_victory_string) {
var special_types = [];
if (need_unique_string) {
special_types.push(this.special_counts["Uniques"] + "u");
}
if (need_action_string) {
special_types.push(this.special_counts["Actions"] + "a");
}
if (need_duchy_string) {
special_types.push(this.card_counts["Duchy"] + "d");
}
if (need_victory_string) {
special_types.push(this.special_counts["Victory"] + "v");
}
str += '(' + special_types.join(", ") + ')';
}
return str;
}
this.changeScore = function(points) {
this.score = this.score + parseInt(points);
}
this.changeSpecialCount = function(name, delta) {
if (this.special_counts[name] == undefined) {
this.special_counts[name] = 0;
}
this.special_counts[name] = this.special_counts[name] + delta;
}
this.recordCards = function(name, count) {
if (this.card_counts[name] == undefined || this.card_counts[name] == 0) {
this.card_counts[name] = count;
this.special_counts["Uniques"] += 1;
} else {
this.card_counts[name] += count;
}
if (this.card_counts[name] <= 0) {
if (this.card_counts[name] < 0) {
handleError("Card count for " + name + " is negative (" + this.card_counts[name] + ")");
}
delete this.card_counts[name];
this.special_counts["Uniques"] -= 1;
}
}
this.recordSpecialCounts = function(singular_card_name, card, count) {
// TODO(drheld): Implement based on rrenaud's list.
}
this.gainCard = function(card, count) {
count = parseInt(count);
this.deck_size = this.deck_size + count;
this.changeScore(pointsForCard(card) * count);
this.recordSpecialCounts(card, count);
this.recordCards(card, count);
}
}
function stateStrings() {
var state = '';
for (var player in players) {
player = players[player];
state += '<b>' + player.name + "</b>: " +
player.getScore() + " points [deck size is " +
player.getDeckString() + "] - " +
JSON.stringify(player.special_counts) + "<br>" +
JSON.stringify(player.card_counts) + "<br>";
}
return state;
}
function getPlayer(name) {
if (players[name] == undefined) return null;
return players[name];
}
function maybeHandleTurnChange(text) {
// TODO
}
function maybeHandleGameStart(text) {
if (text != "------------ Game Setup ------------") {
return false;
}
started = true;
disabled = false;
turn_number = 0;
player_count = 0;
// TODO(drheld): Decide here.
show_action_count = false;
show_unique_count = false;
show_victory_count = false;
show_duchy_count = false;
had_error = false;
announced_error = false;
players = new Object();
introducePlugin();
return true;
}
function maybeHandlePlayerStart(text) {
var arr = text.match(/^(.*) - starting cards:/);
if (!arr || arr.length != 2) return false;
players[arr[1]] = new Player(arr[1]);
players[arr[1]].id = "player" + ++player_count;
var player_names = [];
for (player in players) {
player_names.push(RegExp.quote(players[player].name));
}
player_re = new RegExp('^(' + player_names.join('|') + ') - (.*)$');
return true;
}
// Get the cards in an action (sometimes there are more than one).
function getCards(text) {
return text.split(', ');
}
function handleLogEntry(text) {
if (maybeHandleGameStart(text)) return;
if (!started) return;
if (maybeHandlePlayerStart(text)) return;
if (maybeHandleTurnChange(text)) return;
var split = text.match(player_re);
if (!split) return;
var player = getPlayer(split[1]);
var entry = split[2];
// Handle gain.
var arr = entry.match(/^gains (.*)$/);
if (arr) {
$.each(getCards(arr[1]), function(i, card) {
player.gainCard(card, 1);
});
return;
}
// Handle trash.
var arr = entry.match(/^trashes (.*)$/);
if (arr) {
$.each(getCards(arr[1]), function(i, card) {
player.gainCard(card, -1);
});
return;
}
// Handle return to supply.
var arr = entry.match(/^returns (.*) to the Supply$/);
if (arr) {
player.gainCard(arr[1], -1);
return;
}
// Handle points.
var arr = entry.match(/^receives ([0-9]*) victory point chips$/);
if (arr) {
player.changeScore(arr[1]);
return;
}
// Handle Bishop's initial VP token.
// This should be reported in the log, but isn't.
// See: http://funsockets.ipbhost.com/index.php?/topic/598-bishop-log-reports-wrong-number-of-vp-tokens/
// To work around this, special case Bishop.
var arr = entry.match(/^plays Bishop$/);
if (arr) {
player.changeScore(1);
return;
}
}
function getDecks() {
var decks = "Cards: ";
for (var player in players) {
decks += playerString(players[player], players[player].getDeckString());
}
return decks;
}
function updateDeck() {
if (deck_spot == undefined) {
var spot = $('a[href="/signout"]');
if (spot.length != 1) return;
deck_spot = spot[0];
}
ignore_events = true;
deck_spot.innerHTML = getDecks();
ignore_events = false;
}
function introducePlugin() {
writeText("★ Cards counted by Dominion Point Counter ★");
//writeText("http://goo.gl/iDihS (screenshot: http://goo.gl/G9BTQ)");
writeText("Type !status to see the current score.");
writeText("Type !details to see deck details for each player.");
// TODO(drheld): Options.
//if (getOption("allow_disable")) {
// writeText("Type !disable by turn 5 to disable the point counter.");
//}
}
function maybeShowStatus(request_time) {
if (last_status_print < request_time) {
last_status_print = new Date().getTime();
// Build up a string to show.
var to_show = " >> Decks: ";
for (var player in players) {
to_show += " " + players[player].name + "=" + players[player].getDeckString();
}
to_show += " | Points: "
for (var player in players) {
to_show += " " + players[player].name + "=" + players[player].getScore();
}
var my_name = localStorage["name"];
if (my_name == undefined || my_name == null) my_name = "Me";
writeText(to_show.replace(/You=/g, my_name + "="));
}
}
function maybeShowDetails(request_time) {
if (last_status_print < request_time) {
last_status_print = new Date().getTime();
var my_name = localStorage["name"];
if (my_name == undefined || my_name == null) my_name = "Me";
for (var player in players) {
player = players[player];
var name = player.name == "You" ? my_name : player.name;
writeText('>> *' + name + '* => ' + player.getScore() +
' points [deck size is ' + player.getDeckString() + '] - ' +
JSON.stringify(player.special_counts));
writeText('>> ' + JSON.stringify(player.card_counts));
}
}
}
function hideExtension() {
// TODO(drheld)
}
function delayedRunCommand(command) {
var time = new Date().getTime();
var command = command + "(" + time + ")";
var wait_time = 200 * Math.floor(Math.random() * 10 + 1);
// If we introduced the extension, we get first dibs on answering.
// TODO(drheld): Re-add this.
// if (i_introduced) wait_time = 100;
setTimeout(command, wait_time);
}
function handleChatText(text) {
if (!text) return;
if (disabled) return;
if (text == "!status") delayedRunCommand("maybeShowStatus");
if (text == "!details") delayedRunCommand("maybeShowDetails");
// if (getOption("allow_disable") && text == " !disable" && turn_number <= 5) {
// localStorage.setItem("disabled", "t");
// disabled = true;
// hideExtension();
// writeText(">> Point counter disabled.");
// }
if (text.indexOf(" >> ") == 0) {
last_status_print = new Date().getTime();
}
//if (!introduced && text.indexOf(" ★ ") == 0) {
// introduced = true;
// if (speaker == localStorage["name"]) {
// i_introduced = true;
// }
// }
}
function addSetting(setting, output) {
if (localStorage[setting] != undefined) {
output[setting] = localStorage[setting];
}
}
function settingsString() {
var settings = new Object();
addSetting("debug", settings);
addSetting("always_display", settings);
addSetting("allow_disable", settings);
addSetting("name", settings);
addSetting("show_card_counts", settings);
addSetting("status_announce", settings);
addSetting("status_msg", settings);
return JSON.stringify(settings);
}
function handleGameEnd(doc) {
// TODO
}
function playerString(player, text) {
return "<span class=" + player.id + ">" + text + "</span>";
}
function showState() {
var html = '';
for (player in players) {
var player = players[player];
var player_string = player.name + '<br>';
player_string += 'Cards: ' + player.getDeckString() + '<br>';
player_string += 'Score: ' + player.getScore() + '<br><br>';
var card_array = [];
for (var c in player.card_counts) {
card_array.push(c + ': ' + player.card_counts[c]);
}
card_array.sort();
player_string += card_array.join('<br>') + '<br>';
html += playerString(player, player_string) + '<br><br>'
}
$('#status').html(html);
}
function handle(text) {
try {
handleLogEntry(text);
if (!started) return;
showState();
}
catch (err) {
console.log(err);
console.log(text);
handleError("Javascript exception: " + err.stack);
}
}
function handleGameMessage(node) {
var msg = $.parseJSON(node.text());
console.log(msg.message);
console.log(msg);
if (msg.message == 'GameMessage') {
var innerdata = msg.data;
var msgname = innerdata.messageName;
var gmdata = innerdata.data;
if (msgname == 'gameSetup') {
userID = msg.destination;
gameID = msg.source;
myName = gmdata.playerInfos[gmdata.playerIndex].name;
}
}
if (msg.message == 'Chat') {
handleChatText(msg.data.text);
}
node.remove();
}
function writeText(text) {
// Send to others.
var msg = {
'message': 'GameMessage',
'version': 1,
'tag': '',
'source': userID,
'destination': gameID,
'data': {
'messageName': 'sendChat',
'data': {
'text': text
}
}
};
var data = JSON.stringify(msg);
$('#text_to_send').append(
$('<div id=send_msg_' + send_sequence++ + '>').text(data));
// Send to me.
var msg = {
'message': 'GameMessage',
'version': 1,
'tag': '',
'source': gameID,
'destination': userID,
'data': {
'messageName': 'addChat',
'data': {
'playerName': myName,
'text': text
}
}
};
var data = JSON.stringify(msg);
$('#text_to_receive').append(
$('<div id=receive_msg_' + receive_sequence++ + '>').text(data));
}
//
// Chat status handling.
//
function buildStatusMessage() {
var status_message = "/me Auto▼Count";
if (localStorage["status_msg"] != undefined &&
localStorage["status_msg"] != "") {
status_message = status_message + " - " + localStorage["status_msg"];
}
return status_message;
}
$(document).ready(function() {
$('body').append('<div id="status"></div>');
$('#status').css('color', 'white');
$('#status').css('font-family', 'monospace');
$('#logs').bind('DOMNodeInserted', function(event) {
handle($(event.target).text());
});
$('#socket_messages').bind('DOMNodeInserted', function(event) {
handleGameMessage($(event.target));
});
});
chrome.extension.sendRequest({ type: "version" }, function(response) {
extension_version = response;
});