-
Notifications
You must be signed in to change notification settings - Fork 14
/
card_counts.js
117 lines (97 loc) · 3.49 KB
/
card_counts.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
// Identifier for a card count for a given player.
function cardId(player_id, card_name) {
return player_id + "_" + card_name.replace(/[^a-zA-Z]/gi, "").toLowerCase();
}
// Sets up the per player card count layout.
// Returns true if this call set it up, or false if it was already setup.
function setupPerPlayerCardCounts() {
if (!getOption("show_card_counts")) return true;
if (turn_number < 2) return true;
// Make sure things aren't already setup.
if ($('#player1_copper').length != 0) return false;
if ($('#chat ~ a[href^="/mode/"]').text() == "images") {
setupPerPlayerTextCardCounts();
// Setup the counts again whenever the table is recreated.
$("#supply").bind("DOMNodeInserted", function(e) {
if (e.target.constructor == HTMLTableElement) {
if ($('#player1_copper').length == 0) {
setupPerPlayerTextCardCounts();
}
}
});
} else {
setupPerPlayerImageCardCounts('kingdom');
setupPerPlayerImageCardCounts('basic');
// Setup the counts again whenever the table is recreated.
$("#supply").bind("DOMNodeInserted", function(e) {
if (e.target.constructor == HTMLTableElement) {
if ($('#player1_copper').length == 0) {
setupPerPlayerImageCardCounts('kingdom');
setupPerPlayerImageCardCounts('basic');
}
}
});
}
return true;
}
// Creates an appropriately IDd table cell for a card/player.
function createPlayerCardCountCell(player, card) {
var id = cardId(player.id, card);
var count = player.card_counts[card];
if (count == undefined) count = '-';
var cell = $('<td id="' + id + '">' + count + '</td>');
cell.addClass("playerCardCountCol").addClass(player.id);
return cell;
}
//
// TEXT MODE
//
// Any row that spans a number of columns should span the added columns.
// Use the attribute "grown" to avoid adjusting the same thing multiple times.
function growHeaderColumns() {
var toAdd = player_count + 1; // 1 extra for spacing
$("#supply > table > tbody > tr > td[colspan]:not([grown])")
.each(function() {
var $this = $(this);
var origSpanStr = $this.attr('colspan');
var origSpan = parseInt(origSpanStr);
$this.attr('colspan', (origSpan + toAdd));
$this.attr('grown', toAdd);
});
}
// Set up the card count cells for all players in text mode.
function setupPerPlayerTextCardCounts() {
if (disabled) return;
// For each row in the supply table, add a column count cell for each player.
$(".txcardname").each(function() {
var $this = $(this);
var cardName = $this.children("[cardname]").first().attr('cardname');
// Insert new cells after this one.
var insertAfter = $this.next();
for (var p in players) {
var cell = createPlayerCardCountCell(players[p], cardName);
insertAfter.after(cell);
insertAfter = cell;
}
insertAfter.after($('<td class="availPadding"></td>'));
});
growHeaderColumns();
}
//
// GRAPHIC MODE
//
// Set up the per-player card counts in image mode for a given column.
function setupPerPlayerImageCardCounts(region) {
if (disabled) return;
var selector = '.' + region + '-column';
// make "hr" rows span all columns
var numPlayers = 1 + player_count;
$(selector + ' .hr:empty').append('<td colspan="' + numPlayers + '"></td>');
$(selector + ' .supplycard').each(function() {
var $this = $(this);
var cardName = $this.attr('cardname');
for (var p in players) {
$this.append(createPlayerCardCountCell(players[p], cardName));
}
});
}