-
Notifications
You must be signed in to change notification settings - Fork 0
/
highscores.lua
98 lines (81 loc) · 3.31 KB
/
highscores.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
require "constants"
local http = require("socket.http")
local json = require "json"
Highscores = Object:extend()
function Highscores:new ()
self.highscores = nil
self:load_scores()
end
function Highscores:get_min_highscore()
return self.highscores.scores[self.highscores.nb_scores].score
end
function Highscores:get_highscore()
return self.highscores.scores[1].score
end
function Highscores:is_online()
return self.highscores.session_id ~= nil
end
function Highscores:get_pages_count()
return math.ceil (#self.highscores.scores/10)
end
function Highscores:get_score (index)
return self.highscores.scores[index]
end
function Highscores:set_name (index, name)
self.highscores.scores[index].name = name
end
function Highscores:insert_new_score(score_to_enter, level) -- Retourne la page et l'index dans la page du score inséré
table.remove (self.highscores.scores, self.highscores.nb_scores)
local idx = self.highscores.nb_scores - 1
while idx > 0 and self.highscores.scores[idx].score < score_to_enter do
idx = idx - 1
end
idx = idx + 1
table.insert (self.highscores.scores, idx, {score = score_to_enter, name = "", round = level or 1})
return math.ceil (idx/10), idx
end
function Highscores:load_scores()
if self.highscores and self.highscores.session_id then
body, code, header = http.request(HIGHSCORES_BASE_URL .. "/get_scores?session_id=" .. self.highscores.session_id)
else
body, code, header = http.request(HIGHSCORES_BASE_URL .. "/get_scores")
end
if code == 200 then
self.highscores = json.decode (body)
else
self.highscores = ("highscores.json")
end
end
function Highscores:send_intermediate_score(curr_level, level_score_str, total_score)
if self.highscores.session_id then
co = coroutine.create( function() Highscores.send_json ({session_id = self.highscores.session_id
, score = total_score
, level = curr_level
, level_score_str = level_score_str
}) end )
coroutine.resume(co)
end
end
function Highscores:send_final_score(name, curr_level, level_score_str, total_score)
Highscores.send_json ({session_id = self.highscores.session_id
, score = total_score
, name = name
, level = curr_level
, level_score_str = level_score_str
})
end
function Highscores:close_session()
if self.highscores.session_id then
http.request(HIGHSCORES_BASE_URL .. "/close?session_id=" .. self.highscores.session_id)
end
end
function Highscores.send_json (json_obj)
req = json.encode (json_obj)
resp={}
body, code, header = http.request{url = HIGHSCORES_BASE_URL .. "/enter_score"
, method = "POST"
, headers = {["content-type"] = "application/json", ["content-length"] = tostring(#req)}
, source = ltn12.source.string (req)
, sink=ltn12.sink.table(resp)
}
end