forked from TetrisMetrics/BizhawkMetrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameReplay.lua
54 lines (46 loc) · 1.44 KB
/
GameReplay.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
require('Game')
require("helpers")
json = require "json"
GameReplay = class(function(a, filename, startLevel, first, second, frames, tetriminos)
a.filename = filename
a.startLevel = startLevel
a.firstTetrimino = first
a.secondTetrimino = second
a.frames = frames
a.tetriminos = tetriminos
end)
function gameReplayFromGame(game)
return GameReplay(game.filename, game.startLevel, game.firstTetrimino, game.secondTetrimino, game.frames, game.tetriminos)
end
function gameReplayJsonObject(filename, j)
return GameReplay(filename, j["startLevel"], j["firstTetrimino"], j["secondTetrimino"], j["frames"], j["tetriminos"])
end
function gameReplayFromJsonFile (filename)
local f = io.open(filename, "r")
io.input(f)
local j = json.decode(io.read())
io.close(f)
return gameReplayJsonObject(filename, j)
end
function getLatestReplayFile ()
local rs = getSortedValues(scandir("./replays"))
return "./replays/" .. rs[#rs]
end
function getLatestReplay()
return gameReplayFromJsonFile(getLatestReplayFile())
end
function GameReplay:toJson()
local t = {}
t.startLevel = self.startLevel
t.firstTetrimino = self.firstTetrimino
t.secondTetrimino = self.secondTetrimino
t.frames = self.frames
t.tetriminos = self.tetriminos
return json.encode(t)
end
function GameReplay:writeReplay()
local f = io.open("replays/" .. self.filename, "w")
f:write(self:toJson())
f:flush()
f:close()
end