-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcontestmanager.js
191 lines (155 loc) · 6.2 KB
/
contestmanager.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
import GLib from 'gi://GLib';
import Gio from 'gi://Gio';
import GObject from 'gi://GObject';
import { getClist, getCodeforces } from './scraper.js';
import { Contest } from './contest.js';
export var ContestManager = GObject.registerClass(
{
Signals: {
'update-contests': {},
'update-next-contest': {},
}
},
class Contests extends GObject.Object {
_init(settings) {
super._init();
let cacheFilePath = GLib.build_filenamev([GLib.get_user_cache_dir(), 'ContestCountdown', 'contests.json']);
this.cacheFile = Gio.File.new_for_path(cacheFilePath);
this.settings = settings;
this.allContests = [];
// index of next contest (whose countdown is shown)
// -1 loading
// -2 No upcoming contest
// -3 failed to load
this.nextContest = -1;
// this.emit('update-contests');
console.debug("reading cache");
try {
let [, contents] = this.cacheFile.load_contents(null);
let contentsString = "[]";
if (contents)
contentsString = (new TextDecoder('utf-8')).decode(contents);
this.updateContests(JSON.parse(contentsString).map(
c => new Contest(c.url, c.name, c.website, new Date(c.date), c.duration)
));
}
catch (e) {
console.warn("No cache File / Cant open cache", e);
}
this.regularRefresh();
this.regularTimerId = GLib.timeout_add_seconds(
GLib.PRIORITY_DEFAULT,
3600, // refresh 1 hour later
this.regularRefresh.bind(this)
);
this.settings.connect('changed::refresh-signal', this.refresh.bind(this));
}
setNextContest(val) {
console.debug("update next contest");
this.nextContest = val;
this.emit('update-next-contest');
}
// updates the this.nextContest field and saves allContests to cache file
// is called after clicking on checkbox and after refreshing contests.
updateCache() {
console.debug("updateCache");
if (!GLib.mkdir_with_parents(this.cacheFile.get_parent().get_path(), parseInt("0755", 8)) === 0) {
console.error("Failed to create cache folder");
resolve();
}
this.cacheFile.replace_contents_bytes_async(
new GLib.Bytes(JSON.stringify(this.allContests)),
null,
false,
Gio.FileCreateFlags.REPLACE_DESTINATION,
null,
(_, result) => {
try {
this.cacheFile.replace_contents_finish(result);
} catch (e) {
console.error("Failed to write to cache file", e);
}
}
);
}
refresh() {
let result = this.settings.get_boolean("enable-clist") ? getClist(
this.settings.get_string("clist-username"),
this.settings.get_string("clist-token"),
this.settings.get_boolean("use-whitelisted-websites-only"),
this.settings.get_string("clist-websites-whitelist"),
this.settings.get_string("clist-websites-blacklist"),
) : getCodeforces();
this.updateContests(result);
}
failiureRefresh() {
console.debug("retrying refreshing...");
try {
this.refresh();
// returning false will remove this from event loop
this.failiureRefreshTimerId = 0;
return false;
}
catch (e) {
console.error("refresh failed", e);
}
return true;
}
regularRefresh() {
console.debug("refreshing...");
try {
this.refresh();
}
catch (e) {
console.error("refresh failed", e);
if (!this.failiureRefreshTimerId) {
this.failiureRefreshTimerId = GLib.timeout_add_seconds(
GLib.PRIORITY_DEFAULT,
30,
this.failiureRefresh.bind(this)
);
}
}
return true;
}
destroy() {
if (this.regularTimerId)
GLib.source_remove(this.regularTimerId);
if (this.failiureRefreshTimerId)
GLib.source_remove(this.failiureRefreshTimerId);
// super.destroy();
}
updateContests(newContests) {
console.debug("updateContests");
let oldContests = {};
for (let contest of this.allContests)
oldContests[contest.url] = contest;
for (let contest of newContests) {
contest.onChange = this.onChange.bind(this);
// if the contest does not have participating defined
// then try to get the value of participating from oldContests
if (contest.participating == null) {
let old = oldContests[contest.url];
contest.participating = (old ? old.contest.participating : true);
}
}
this.allContests = newContests;
this.onChange();
}
onChange() {
let curDate = new Date();
this.allContests = this.allContests.filter(c => c.date > curDate);
this.allContests.sort((a, b) => a.date > b.date);
let newNext = this.allContests.findIndex((c) => c.participating);
if (newNext >= 0)
this.setNextContest(newNext);
else if (this.retriesLeft)
this.setNextContest(-1); // loading
else if (this.allContests.length)
this.setNextContest(-2); // no upcoming
else
this.setNextContest(-3); // failed to load
this.updateCache();
this.emit('update-contests');
}
});