forked from TecProg-grupo4-2018-2/panel-attack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
analytics.lua
290 lines (253 loc) · 9.23 KB
/
analytics.lua
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
local analytics = {}
local analytics_version = 3
local function create_blank_data()
return {
-- the amount of destroyed panels
destroyed_panels = 0,
-- the amount of sent garbage
sent_garbage_lines = 0,
-- the amount of times the cursor was moved
move_count = 0,
-- the amount of times the panels were swapped
swap_count = 0,
-- sparse dictionary with a count of each chain reached, mystery chains are recorded as whatever chain they were, 1 is obviously meaningless
reached_chains = {},
-- sparse dictionary with a count of each combo reached, 1 to 3 being meaningless
used_combos = {},
shockGarbageCount = 0
}
end
-- The class representing one set of analytics data
AnalyticsInstance =
class(
function(self, save_to_overall)
self.save_to_overall = save_to_overall -- whether this data should count towards the overall
self.data = create_blank_data()
-- temporary
self.lastGPM = 0
self.lastAPM = 0
end
)
local analytics_data = {
-- The lastly used version
version = analytics_version,
last_game = create_blank_data(),
overall = create_blank_data()
}
local function analytic_clear(analytic)
analytic.destroyed_panels = 0
analytic.sent_garbage_lines = 0
analytic.move_count = 0
analytic.swap_count = 0
analytic.reached_chains = {}
analytic.used_combos = {}
analytic.shockGarbageCount = 0
end
local amount_of_garbages_lines_per_combo = {0, 0, 0, 0.5, 1, 1, 1, 1.5, 2, 2, 2, 2, 3, 4, [20] = 6, [27] = 8}
for i = 1, 72 do
amount_of_garbages_lines_per_combo[i] = amount_of_garbages_lines_per_combo[i] or amount_of_garbages_lines_per_combo[i - 1]
end
local function compute_above_chain_card_limit(analytic)
--computing chain ? count
local chain_above_limit = 0
for k, v in pairs(analytic.reached_chains) do
if k > themes[config.theme].chainCardLimit then
chain_above_limit = chain_above_limit + v
end
end
return chain_above_limit
end
local function refresh_sent_garbage_lines(analytic)
local sent_garbage_lines_count = 0
for k, v in pairs(analytic.used_combos) do
if k then
sent_garbage_lines_count = sent_garbage_lines_count + amount_of_garbages_lines_per_combo[k] * v
end
end
for i = 2, 13 do
if analytic.reached_chains[i] then
sent_garbage_lines_count = sent_garbage_lines_count + (i - 1) * analytic.reached_chains[i]
end
end
local chain_above_13 = compute_above_chain_card_limit(analytics.last_game)
sent_garbage_lines_count = sent_garbage_lines_count + 13 * chain_above_13
sent_garbage_lines_count = sent_garbage_lines_count + analytic.shockGarbageCount
analytic.sent_garbage_lines = sent_garbage_lines_count
end
--TODO: cleanup all functions in this file to be object oriented and not global
function maxComboReached(data)
local maxCombo = 0
for index, _ in pairs(data.used_combos) do
maxCombo = math.max(index, maxCombo)
end
return maxCombo
end
--TODO: cleanup all functions in this file to be object oriented and not global
function maxChainReached(data)
local maxChain = 0
for index, _ in pairs(data.reached_chains) do
maxChain = math.max(index, maxChain)
end
return maxChain
end
-- this is a function that exists to address issue https://github.com/panel-attack/panel-attack/issues/609
-- analytics - per standard - increment the values on number indices such as used_combos[4] = used_combos[4] + 1
-- for unknown reasons, at some point in time, some combos started to get saved as string values - and they are loaded every time on analytics.init
-- the json library we use does not support string and integer keys on the same table and only saves the entries with a string key to analytics.json
-- due to that, combo data is lost and in this function any string indices are converted to int
-- honestly no idea how they ever became strings, I assume someone fixed that already in the past but the lingering data continued to screw stuff over
local function correctComboIndices(dataToCorrect)
local correctedCombos = {}
for key, value in pairs(dataToCorrect["overall"]["used_combos"]) do
local numberKey = tonumber(key)
if type(numberKey) == "number" then
if correctedCombos[numberKey] then
correctedCombos[numberKey] = correctedCombos[numberKey] + value
else
correctedCombos[numberKey] = value
end
end
end
dataToCorrect["overall"]["used_combos"] = correctedCombos
return dataToCorrect
end
function analytics.init()
pcall(
function()
local analytics_file, err = love.filesystem.newFile("analytics.json", "r")
if analytics_file then
local teh_json = analytics_file:read(analytics_file:getSize())
analytics_file:close()
analytics_data = json.decode(teh_json)
analytic_clear(analytics_data.last_game)
analytics_data = correctComboIndices(analytics_data)
-- do stuff regarding version compatibility here, before we patch it
if analytics_data.version < 2 then
refresh_sent_garbage_lines(analytics_data.overall)
end
analytics_data.version = analytics_version
end
end
)
end
local function output_pretty_analytics()
if not config.enable_analytics then
return
end
local analytics_filters = {analytics_data.last_game, analytics_data.overall}
local titles = {"Last game\n-------------------------------------\n", "Overall\n-------------------------------------\n"}
local text = ""
for i, analytic in pairs(analytics_filters) do
text = text .. titles[i]
text = text .. "Destroyed " .. analytic.destroyed_panels .. " panels.\n"
text = text .. "Sent " .. analytic.sent_garbage_lines .. " lines of garbage.\n"
text = text .. "Moved " .. analytic.move_count .. " times.\n"
text = text .. "Swapped " .. analytic.swap_count .. " times.\n"
text = text .. "Performed combos:\n"
local maxCombo = maxComboReached(analytic)
for j = 4, maxCombo do
if analytic.used_combos[j] ~= nil then
text = text .. "\t" .. analytic.used_combos[j] .. " combo(s) of size " .. j .. "\n"
end
end
text = text .. "Reached chains:\n"
local maxChain = maxChainReached(analytic)
for j = 2, maxChain do
if analytic.reached_chains[j] ~= nil then
text = text .. "\t" .. analytic.reached_chains[j] .. " chain(s) have ended at length " .. j .. "\n"
end
end
text = text .. "\n\n"
end
pcall(
function()
local file = love.filesystem.newFile("analytics.txt")
file:open("w")
file:write(text)
file:close()
end
)
end
local function write_analytics_files()
pcall(
function()
if not config.enable_analytics then
return
end
local file = love.filesystem.newFile("analytics.json")
file:open("w")
file:write(json.encode(analytics_data))
file:close()
end
)
output_pretty_analytics()
end
function AnalyticsInstance.compute_above_chain_card_limit(self)
return compute_above_chain_card_limit(self.data)
end
function AnalyticsInstance.data_update_list(self)
local data_update_list = {self.data}
if self.save_to_overall then
table.insert(data_update_list, analytics_data.overall)
end
return data_update_list
end
function AnalyticsInstance.register_destroyed_panels(self, amount)
local analytics_filters = self:data_update_list()
for _, analytic in pairs(analytics_filters) do
analytic.destroyed_panels = analytic.destroyed_panels + amount
if amount > 3 then
if not analytic.used_combos[amount] then
analytic.used_combos[amount] = 1
else
analytic.used_combos[amount] = analytic.used_combos[amount] + 1
end
analytic.sent_garbage_lines = analytic.sent_garbage_lines + amount_of_garbages_lines_per_combo[amount]
end
end
end
function AnalyticsInstance.register_chain(self, size)
local analytics_filters = self:data_update_list()
for _, analytic in pairs(analytics_filters) do
if not analytic.reached_chains[size] then
analytic.reached_chains[size] = 1
else
analytic.reached_chains[size] = analytic.reached_chains[size] + 1
end
analytic.sent_garbage_lines = analytic.sent_garbage_lines + (size - 1)
end
end
function AnalyticsInstance.register_swap(self)
local analytics_filters = self:data_update_list()
for _, analytic in pairs(analytics_filters) do
analytic.swap_count = analytic.swap_count + 1
end
end
function AnalyticsInstance.register_move(self)
local analytics_filters = self:data_update_list()
for _, analytic in pairs(analytics_filters) do
analytic.move_count = analytic.move_count + 1
end
end
function AnalyticsInstance:registerShock()
-- we don't track shock garbage sent in all-time analytics - for now
self.data.shockGarbageCount = self.data.shockGarbageCount
local analytics_filters = self:data_update_list()
for _, analytic in pairs(analytics_filters) do
analytic.sent_garbage_lines = analytic.sent_garbage_lines + 1
end
end
function analytics.game_ends(analytic)
if analytic then
analytics_data.last_game = analytic.data
end
if config.enable_analytics then
write_analytics_files()
end
analytic_clear(analytics_data.last_game)
end
function AnalyticsInstance:getRoundedGPM(clock)
local garbagePerMinute = self.data.sent_garbage_lines / (clock / 60 / 60)
return string.format("%0.1f", round(garbagePerMinute, 1))
end
return analytics