/
storelog.js
221 lines (198 loc) · 5.85 KB
/
storelog.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
/**
* Function that stores game information loaclly and returns the gameID
*
* returns: gameID
*/
async function storeLogLocally(gameLog, uuid, gameStatus=undefined, VPs = undefined, kingdom = undefined, dateString = "none", ignoreBotGames = null) {
try{
// get game ID from the log
let gameID = gameLog.match(/#\d+/).toString();
console.log(gameID)
if(gameID.length > 0){
// extract player name and order. This is tested for english and should work for german, french and might work for russian.
// It will fail before both players had their first turn.
gameLog = gameLog.replace(/Rattington/g, "Rat");
let regexp = />(Turn|Zug|Tour|Ход) 1 - (.+?)</g;
players = [];
console.log("trying to save")
let player = regexp.exec(gameLog);
let bots = ["Lord Rat","Revenge Witch","Lord Voldebot"];
if(ignoreBotGames == null){
ignoreBotGames = await getSetting("ignoreBotGames", defaultValue = false);
}
console.log("ignoreBotGames", ignoreBotGames);
while(player!=null){
players.push(player[2])
if(ignoreBotGames && bots.indexOf(player[2])!=-1){
console.log("Ignoring Bot Game")
return;
}
player = regexp.exec(gameLog);
}
if(players.length <2){
// not enough players
console.log("Start of game, no player names available yet");
return;
}
// get a String representing the current date
if(dateString=="none"){
dateString = getDateDDMMYYYY();
}
//generate object with game info (can be expanded further later)
let game = {
uuid: uuid,
gameID: gameID,
players: players,
date: dateString,
kingdom: kingdom,
VPs: VPs,
log: gameLog,
gameStatus: gameStatus
};
// save the game to local storage
let saveGame = {};
saveGame[uuid] = game;
if(/#\d+/.test(gameID)){
chrome.storage.local.set(saveGame, function() {
console.log("Log saved locally: ",VPs, kingdom, gameID, players, dateString, gameStatus);
});
}else{
console.log("No correct gameID: " + gameID)
}
return gameID;
}
}catch(e){
console.log("Error while saving the log");
console.log(e);
}
return "none";
}
/**
* Function that loads and returns a game from local storage by its game id. The
* function returns a promise and needs an await in an async function.
*/
function loadStoredGameByGameID(gameID) {
return new Promise((resolve) => {
chrome.storage.local.get(null, function(games) {
let allGames = []
for(uuid in games){
if(games[uuid].gameID == gameID){
allGames.push(games[uuid])
}
}
if(allGames.length>1){
let bots = ["Lord Rattington","Revenge Witch","Lord Voldebot"];
for(game of allGames){
//Check if the game was played without bots, if so, this is the original game
let combined = bots.concat(game.players);
if((combined.length == new Set(combined).size)){
resolve(game);
return;
}
}
}
resolve(allGames[0]);
return;
});
});
}
/**
* Function that loads and returns a game from local storage by its game id. The
* function returns a promise and needs an await in an async function.
*/
function loadStoredGameByUUID(uuid) {
return new Promise((resolve) => {
chrome.storage.local.get([uuid], function(games) {
resolve(games[uuid]);
});
});
}
/**
* Functions to handle user-defined settings values.
*/
function getSetting(property, defaultValue = undefined){
return new Promise((resolve) => {
chrome.storage.local.get("settings",function(value){
settings = value["settings"];
try{
if(defaultValue == undefined || settings[property] != undefined){
resolve(settings[property]);
return;
}
}catch(e){}
resolve(defaultValue);
});
});
}
function setSetting(property, value){
chrome.storage.local.get(["settings"], function(settings){
if(settings == undefined){
settings = {}
}
settings[property] = value;
let settingsObj = {}
settingsObj["settings"] = settings;
chrome.storage.local.set(settingsObj, function(){
console.log("updated settings",settings)
});
});
}
/**
* A function that returns an array with DD, MM, YYYY info of the current day. This function is used below to generate two different date strings for files and logs.
*/
function getDateArray(){
let today = new Date();
let dd = today.getDate().toString();
while (dd.length < 2) {
dd = '0' + dd;
}
let mm = (today.getMonth()+1).toString();
while (mm.length < 2) {
mm = '0' + mm;
}
let yyyy = (today.getYear()+1900).toString();
return [dd,mm,yyyy];
}
/**
* Function that returns today as DDMMYYYY with a selected separator such as
* 28/05/2020
*/
function getDateDDMMYYYY(separator="/"){
return getDateArray().join(separator);
}
/**
* Function that returns today as DDMMYYYY with a selected separator such as
* 20/05/28
*/
function getDateYYMMDD(separator="/"){
let date = getDateArray();
let newDate = [];
newDate.push(date[2].substring(2));
newDate.push(date[1]);
newDate.push(date[0]);
return newDate.join(separator);
}
/** function that generates a uuid
*
*/
function getDateUUID(date=null) {
if(date == null){
date = getDateYYMMDD("");
}
return date + '-xxxx-xxxx-xxxx-xxxx'.replace(/[x]/g, function(c) {
let r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
/**
* check whter a key is a uuid
*/
function checkUUID(uuid){
try{
return uuid.match(/\d{6}(-[0-9abcdef]{4}){4}/)[0] == uuid;
}catch(e){
return false;
}
}
// variable that can be put ito the LogPanel div, if there is nothing to show.
var emptyMessage = "no Log available "