forked from TecProg-grupo4-2018-2/panel-attack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
save.lua
212 lines (192 loc) · 6.13 KB
/
save.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
-- the save.lua file contains the read/write functions
local sep = package.config:sub(1, 1) --determines os directory separator (i.e. "/" or "\")
local logger = require("logger")
-- writes to the "keys.txt" file
function write_key_file()
pcall(
function()
local file = love.filesystem.newFile("keysV2.txt")
file:open("w")
file:write(json.encode(GAME.input.inputConfigurations))
file:close()
end
)
end
-- reads the "keys.txt" file
function read_key_file()
pcall(
function()
local inputConfigs = GAME.input.inputConfigurations
local file = love.filesystem.newFile("keysV2.txt")
file:open("r")
local teh_json = file:read(file:getSize())
file:close()
local user_conf = json.decode(teh_json)
for k, v in ipairs(user_conf) do
inputConfigs[k] = v
end
GAME.input.inputConfigurations = inputConfigs
end
)
end
-- reads the .txt file of the given path and filename
function read_txt_file(path_and_filename)
local s
pcall(
function()
local file = love.filesystem.newFile(path_and_filename)
file:open("r")
s = file:read(file:getSize())
file:close()
end
)
if not s then
s = "Failed to read file" .. path_and_filename
else
s = s:gsub("\r\n?", "\n")
end
return s or "Failed to read file"
end
-- writes to the "user_id.txt" file of the directory of the connected ip
function write_user_id_file(userID, serverIP)
pcall(
function()
love.filesystem.createDirectory("servers/" .. serverIP)
local file = love.filesystem.newFile("servers/" .. serverIP .. "/user_id.txt")
file:open("w")
file:write(tostring(userID))
file:close()
end
)
end
-- reads the "user_id.txt" file of the directory of the connected ip
function read_user_id_file(serverIP)
local userID
pcall(
function()
local file = love.filesystem.newFile("servers/" .. serverIP .. "/user_id.txt")
file:open("r")
userID = file:read()
file:close()
userID = userID:match("^%s*(.-)%s*$")
end
)
return userID
end
-- writes the stock puzzles
function write_puzzles()
pcall(
function()
local currentPuzzles = FileUtil.getFilteredDirectoryItems("puzzles") or {}
local customPuzzleExists = false
for _, filename in pairs(currentPuzzles) do
if love.filesystem.getInfo("puzzles/" .. filename) and filename ~= "stock (example).json" and filename ~= "README.txt" then
customPuzzleExists = true
break
end
end
if customPuzzleExists == false then
love.filesystem.createDirectory("puzzles")
recursive_copy("default_data/puzzles", "puzzles")
end
end
)
end
-- reads the selected puzzle file
function read_puzzles()
pcall(
function()
-- if type(replay.in_buf) == "table" then
-- replay.in_buf=table.concat(replay.in_buf)
-- end
puzzle_packs = FileUtil.getFilteredDirectoryItems("puzzles") or {}
logger.debug("loading custom puzzles...")
for _, filename in pairs(puzzle_packs) do
logger.trace(filename)
if love.filesystem.getInfo("puzzles/" .. filename) and filename ~= "README.txt" then
logger.debug("loading custom puzzle set: " .. (filename or "nil"))
local current_set = {}
local file = love.filesystem.newFile("puzzles/" .. filename)
file:open("r")
local teh_json = file:read(file:getSize())
file:close()
local current_json = json.decode(teh_json) or {}
if current_json["Version"] == 2 then
for _, puzzleSet in pairs(current_json["Puzzle Sets"]) do
local puzzleSetName = puzzleSet["Set Name"]
local puzzles = {}
for _, puzzle in pairs(puzzleSet["Puzzles"]) do
local puzzle = Puzzle(puzzle["Puzzle Type"], puzzle["Do Countdown"], puzzle["Moves"], puzzle["Stack"], puzzle["Stop"], puzzle["Shake"])
puzzles[#puzzles + 1] = puzzle
end
local puzzleSet = PuzzleSet(puzzleSetName, puzzles)
GAME.puzzleSets[puzzleSetName] = puzzleSet
end
else -- old file format compatibility
for set_name, puzzle_set in pairs(current_json) do
local puzzles = {}
for _, puzzleData in pairs(puzzle_set) do
local puzzle = Puzzle("moves", true, puzzleData[2], puzzleData[1])
puzzles[#puzzles + 1] = puzzle
end
local puzzleSet = PuzzleSet(set_name, puzzles)
GAME.puzzleSets[set_name] = puzzleSet
end
end
logger.debug("loaded above set")
end
end
end
)
end
function readAttackFile(path)
if love.filesystem.getInfo(path, "file") then
local jsonData = love.filesystem.read(path)
local trainingConf, position, errorMsg = json.decode(jsonData)
if trainingConf then
if not trainingConf.name or type(trainingConf.name) ~= "string" then
local filenameOnly = path:match('%' .. sep .. '?(.*)$')
if filenameOnly ~= nil then
trainingConf.name = FileUtil.getFileNameWithoutExtension(filenameOnly)
end
end
return trainingConf
else
error("Error deserializing " .. path .. ": " .. errorMsg .. " at position " .. position)
end
end
end
function readAttackFiles(path)
local results = {}
local lfs = love.filesystem
local raw_dir_list = FileUtil.getFilteredDirectoryItems(path)
for _, v in ipairs(raw_dir_list) do
local current_path = path .. "/" .. v
if lfs.getInfo(current_path) then
if lfs.getInfo(current_path).type == "directory" then
readAttackFiles(current_path)
else
local training_conf = readAttackFile(current_path)
if training_conf ~= nil then
results[#results+1] = training_conf
end
end
end
end
return results
end
function saveJSONToPath(data, state, path)
pcall(
function()
local file = love.filesystem.newFile(path)
file:open("w")
file:write(json.encode(data, state))
file:close()
end
)
end
function print_list(t)
for i, v in ipairs(t) do
print(v)
end
end