This repository was archived by the owner on Nov 6, 2024. It is now read-only.
forked from TecProg-grupo4-2018-2/panel-attack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalytics.lua
More file actions
285 lines (243 loc) · 8.74 KB
/
analytics.lua
File metadata and controls
285 lines (243 loc) · 8.74 KB
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
local analytics = {}
local analytics_version = 3
local analytic_data_cap = 999999 -- prevents overflow
local analytics_data = {
-- The lastly used version
version = analytics_version,
last_game =
{
-- 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,
-- 1 to 12, then 13+, 1 is obviously meaningless
reached_chains = { },
-- 1 to 40, 1 to 3 being meaningless
used_combos = { }
},
overall =
{
-- 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,
-- 1 to 12, then 13+, 1 is obviously meaningless
reached_chains = { },
-- 1 to 40, 1 to 3 being meaningless
used_combos = { }
}
}
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 = {}
end
local amount_of_garbages_lines_per_combo = {0, 0, 0, 0.5, 1, 1, 1, 2, 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_13(analytic)
--computing chain ? count
local chain_above_13 = 0
for k,v in pairs(analytic.reached_chains) do
if k > 13 then
chain_above_13 = chain_above_13 + v
end
end
return chain_above_13
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_13(analytics.last_game)
sent_garbage_lines_count = sent_garbage_lines_count + 13*chain_above_13
analytic.sent_garbage_lines = sent_garbage_lines_count
end
function analytics.init() pcall(function()
if not config.enable_analytics then
return
end
local read_data = {}
local analytics_file, err = love.filesystem.newFile("analytics.json", "r")
if analytics_file then
local teh_json = analytics_file:read(analytics_file:getSize())
for k,v in pairs(json.decode(teh_json)) do
read_data[k] = v
end
end
if read_data.version and type(read_data.version) == "number" then
analytics_data.version = read_data.version
end
local analytics_filters = { "last_game", "overall" }
local number_params = { "destroyed_panels", "sent_garbage_lines", "move_count", "swap_count" }
local table_params = { "reached_chains", "used_combos" }
for _,analytic in pairs(analytics_filters) do
if read_data[analytic] and type(read_data[analytic]) == "table" then
for n,param in pairs(number_params) do
if read_data[analytic][param] and type(read_data[analytic][param]) == "number" then
analytics_data[analytic][param] = math.min(read_data[analytic][param],analytic_data_cap)
end
end
for m,param in pairs(table_params) do
if read_data[analytic][param] and type(read_data[analytic][param]) == "table" then
analytics_data[analytic][param] = read_data[analytic][param]
end
end
end
end
analytic_clear(analytics_data.last_game)
-- 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
file:close()
end) end
local function output_pretty_analytics() pcall(function()
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"
for k,v in pairs(analytic.used_combos) do
if k then
text = text.."\t"..v.." combo(s) of size "..k.."\n"
end
end
text = text.."Reached chains:\n"
for k,v in pairs(analytic.reached_chains) do
if k then
text = text.."\t"..v.." chain(s) have ended at length "..k.."\n"
end
end
text = text.."\n\n"
end
local file = love.filesystem.newFile("analytics.txt")
file:open("w")
file:write(text)
file:close()
end) end
function analytics.draw(x,y)
if not config.enable_analytics then
return
end
gprint("Panels destroyed: "..analytics_data.last_game.destroyed_panels, x, y)
y = y+15
gprint("Sent garbage lines: "..analytics_data.last_game.sent_garbage_lines, x, y)
y = y+15
gprint("Moved "..analytics_data.last_game.move_count.." times", x, y)
y = y+15
gprint("Swapped "..analytics_data.last_game.swap_count.." times", x, y)
y = y+15
local ycombo = y
for i=2,13 do
local chain_amount = analytics_data.last_game.reached_chains[i] or 0
gprint("x"..i..": "..chain_amount, x, y)
y = y+15
end
local chain_above_13 = compute_above_13(analytics_data.last_game)
gprint("x?: "..chain_above_13, x, y)
local xcombo = x + 50
for i=4,15 do
local combo_amount = analytics_data.last_game.used_combos[i] or 0
gprint("c"..i..": "..combo_amount, xcombo, ycombo)
ycombo = ycombo+15
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()
output_pretty_analytics()
end) end
function analytics.register_destroyed_panels(amount)
if not config.enable_analytics then
return
end
local analytics_filters = { analytics_data.last_game, analytics_data.overall }
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] = math.min(analytic.used_combos[amount]+1,analytic_data_cap)
end
analytic.sent_garbage_lines = analytic.sent_garbage_lines + amount_of_garbages_lines_per_combo[amount]
end
end
end
function analytics.register_chain(size)
if not config.enable_analytics then
return
end
local max_size = math.min(size, 13)
local analytics_filters = { analytics_data.last_game, analytics_data.overall }
for _,analytic in pairs(analytics_filters) do
if not analytic.reached_chains[size] then
analytic.reached_chains[size] = 1
else
analytic.reached_chains[size] = math.min(analytic.reached_chains[size]+1,analytic_data_cap)
end
size = math.min(size, 13)
analytic.sent_garbage_lines = analytic.sent_garbage_lines + (max_size-1)
end
end
function analytics.register_swap()
if not config.enable_analytics then
return
end
local analytics_filters = { analytics_data.last_game, analytics_data.overall }
for _,analytic in pairs(analytics_filters) do
analytic.swap_count = math.min(analytic.swap_count + 1,analytic_data_cap)
end
end
function analytics.register_move()
if not config.enable_analytics then
return
end
local analytics_filters = { analytics_data.last_game, analytics_data.overall }
for _,analytic in pairs(analytics_filters) do
analytic.move_count = math.min(analytic.move_count + 1,analytic_data_cap)
end
end
function analytics.game_ends()
if not config.enable_analytics then
return
end
write_analytics_files()
analytic_clear(analytics_data.last_game)
end
return analytics