-
Notifications
You must be signed in to change notification settings - Fork 0
/
fileparser.lua
134 lines (123 loc) · 3.91 KB
/
fileparser.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
local fileparser = {}
local function gsort(a,b)
return a[2] < b[2]
end
local diffis = {"easy","medium","hard","custom"}
local function checkForDiffs(str)
local isintable = false
for k,v in pairs(diffis) do
if str == v then isintable = true end
end
return isintable
end
local function makeEmptySettings()
local wfile = love.filesystem.newFile("settings.txt","w")
wfile:write(_Username.."\n")
wfile:write("easy")
wfile:close()
_SetMode = "easy"
end
function fileparser.loadSettings()
local sfile = love.filesystem.getInfo("settings.txt")
if sfile then
local rfile = love.filesystem.newFile("settings.txt","r")
local setts = {}
for line in rfile:lines() do
table.insert(setts,line)
end
rfile:close()
_Username = setts[1]
_SetMode = setts[2]
if _SetMode == "custom" then
_SetGridW = tonumber(setts[3])
_SetGridH = tonumber(setts[4])
_SetMines = tonumber(setts[5])
end
if type(_SetMode) ~= "string" then _SetMode = "easy"
else
if not checkForDiffs(_SetMode) then _SetMode = "easy" end
end
if type(_SetGridW) ~= "number" then _SetGridW = 10 end
if type(_SetGridH) ~= "number" then _SetGridH = 10 end
if type(_SetMines) ~= "number" then _SetMines = 10 end
else
makeEmptySettings()
end
end
function fileparser.saveSettings()
local wfile = love.filesystem.newFile("settings.txt","w")
wfile:write(_Username.."\n")
wfile:write(_Mode.."\n")
if _Mode == "custom" then
wfile:write(tostring(_GridW).."\n")
wfile:write(tostring(_GridH).."\n")
wfile:write(tostring(_Mines))
end
wfile:close()
end
function fileparser.loadScores(diffi)
diffi = diffi or "easy"
local sfile = love.filesystem.getInfo("scores_"..diffi..".txt")
if sfile then
local rfile = love.filesystem.newFile("scores_"..diffi..".txt","r")
local linecount = 1
local position = 1
for line in rfile:lines() do
if position <= 10 then -- limit to only 10 scores
if linecount % 2 == 0 then
if type(tonumber(line)) ~= "number" then
_Scores[diffi][position][2] = 999.99
break
end
_Scores[diffi][position][2] = tonumber(line)
position = position + 1
else
if line == "" then break end
_Scores[diffi][position] = {}
_Scores[diffi][position][1] = line
end
linecount = linecount + 1
end
end
else
local wfile = love.filesystem.newFile("scores_"..diffi..".txt","w")
wfile:close()
end
table.sort(_Scores[diffi],gsort)
end
function fileparser.saveScores(diffi)
if diffi == "custom" then return end
local wfile = love.filesystem.newFile("scores_"..diffi..".txt","w")
for k,v in ipairs(_Scores[diffi]) do
if v[1] == "" then break end
wfile:write(v[1].."\n"..tostring(v[2]).."\n")
end
wfile:close()
end
function fileparser.loadScale()
local sfile = love.filesystem.getInfo("scale.txt")
if sfile then
local rfile = love.filesystem.newFile("scale.txt","r")
local outs = rfile:read()
rfile:close()
local out = tonumber(outs)
if type(out) == "number" then
if out % 0.5 ~= 0 then
_Scale = 1
return
end
_Scale = out
return
end
_Scale = 1
end
local wfile = love.filesystem.newFile("scale.txt","w")
wfile:write(tostring(_Scale))
wfile:close()
end
function fileparser.saveScale()
local wfile = love.filesystem.newFile("scale.txt","w")
wfile:write(tostring(_Scale))
wfile:close()
end
return fileparser