-
Notifications
You must be signed in to change notification settings - Fork 1
/
content_script.js
executable file
·140 lines (120 loc) · 3.83 KB
/
content_script.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
var data = {};
var last = {};
var players = [];
chrome.storage.sync.get({
numTurns: 10,
showAllChanges: false,
useCountDown: false
}, function(items) {
NUM_TURNS = items.numTurns;
SHOW_ALL_CHANGES = items.showAllChanges;
USE_COUNTDOWN = items.useCountDown;
});
addedCityLabel = false
addedTurnCounter = false
// mode: https://stackoverflow.com/a/3783970
function mode(array) {
var freq = {};
var max = 0;
var result = null;
for(var v in array) {
freq[array[v]] = (freq[array[v]] || 0) + 1;
if(freq[array[v]] > max) {
max = freq[array[v]];
result = array[v];
}
}
return result;
}
function inGame(){
return document.getElementById("game-leaderboard");
}
function getNextIncrease(){
var turnCounter = document.getElementById('turn-counter');
var currentTurn = parseInt(turnCounter.textContent.split(' ')[1]);
if(USE_COUNTDOWN){
var turnsLeft = 25 - (currentTurn % 25);
} else {
var turnsLeft = 25 - (currentTurn % 25) + parseInt(currentTurn);
}
;
return " Next Increase: " + turnsLeft;
}
function getArmy(player){
var leaderboard = document.getElementById('game-leaderboard');
var player_cell = leaderboard.querySelectorAll('.leaderboard-name.' + player);
return parseInt(player_cell[0].nextSibling.innerHTML);
}
function updateCities(player, cities){
var leaderboard = document.getElementById('game-leaderboard');
var cities_cell = leaderboard.querySelectorAll('.cities-' + player);
var player_row = cities_cell[0].parentElement;
// Add flash if cities increase to > 0 or SHOW_ALL_CHANGES set
if ((SHOW_ALL_CHANGES && parseInt(cities_cell[0].innerHTML) !== cities)
|| (cities_cell[0].innerHTML == "0" && cities > 0)) {
setTimeout(function(row, cl) { row.className = cl; },
1000, player_row, player_row.className);
player_row.className += " flash " + player;
}
cities_cell[0].innerHTML = cities;
}
function turn(){
var leaderboard = document.getElementById('game-leaderboard');
var turnCounter = document.getElementById('turn-counter');
// Initialise columns
if(!addedCityLabel){
leaderboard.rows[0].insertCell(leaderboard.rows[0].cells.length).innerHTML = "Cities";
var table_length = leaderboard.rows[0].cells.length;
for (var i = 0, row; row = leaderboard.rows[i + 1]; i ++){
var player_color = row.children[table_length - 4].classList[1];
var x = row.insertCell(table_length - 1);
x.innerHTML = "0";
x.className = "cities-" + player_color;
// Store player data
players.push(player_color);
data[players[i]] = new Array();
last[players[i]] = new Array();
}
addedCityLabel = true;
}
// add Turn Counter
if(!addedTurnCounter) {
var nextIncrease = document.createElement('div');
nextIncrease.id = "nextIncreaseCounter";
turnCounter.appendChild(nextIncrease);
addedTurnCounter = true;
}
var nextIncreaseCounter = document.getElementById('nextIncreaseCounter');
nextIncreaseCounter.textContent = getNextIncrease();
// Update city counts
for(var i = 0; i < players.length; i++) {
var player = players[i];
var current_army = getArmy(player);
// Save army count
last[player].push(current_army);
// Check change in army
if(last[player].length > 1) {
var army_change = current_army - last[player][last[player].length - 2];
// Save army change
if (army_change > 0) {
data[player].push(army_change);
// Calculate number of cities
if(data[player].length >= NUM_TURNS) {
var guess_cities = mode(data[player].slice(data[player].length - NUM_TURNS));
updateCities(player, guess_cities - 1);
}
}
}
}
}
turnInterval = setInterval(function() {
if (inGame()){
turn();
} else {
addedCityLabel = false
addedTurnCounter = false
data = {};
last = {};
players = [];
}
}, 500);