-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathserver_file_io.lua
223 lines (186 loc) · 5.52 KB
/
server_file_io.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
213
214
215
216
217
218
219
220
221
222
223
------
--- Module with IO functions for save game state
--- Write data in files and handle exception in IO operations
-- @module server_file_io
------
--- Check if is really a file
-- @function check_is_file
-- @param name string with name of the file
-- return boolean, true if is a file, else if not.
function check_is_file(name)
if not check_is_dir(name) then
return assert(os.rename(name,name))
-- note that the short evaluation is to
-- return false instead of a possible nil
else
return false
end
end
--- Check if is a directory
-- @function check_is_file_dir
-- @param name name of the file
-- @return boolean, true if is a directory, false if is not.
function check_is_file_dir(name)
if type(name)~='string' then
return false
else
return assert((os.rename(name, name)))
end
end
--- Check if is a directory (most accurately than check_is_file)
-- @function check_is_dir
-- @param name string with name of the file
-- @return boolean, true if is a directory, false if not.
function check_is_dir(name)
if type(name)~='string' then
return false
else
local lfs = require('lfs')
local current_dir = lfs.currentdir()
local is_dir = lfs.chdir(name)
lfs.chdir(current_dir)
return assert(is_dir)
end
end
--- Create a directory using path
-- @function make_dir
-- @param path string with path of the new directory
-- @return nil
function make_dir(path)
assert(path)
local sep, pStr = package.config:sub(1, 1), ''
for dir in path:gmatch('[^' .. sep .. ']+') do
pStr = pStr .. dir .. sep
lfs.mkdir(pStr)
end
end
--- Write text in player files
-- @function write_playters_file
-- @param nil
-- @return nil
function write_players_file()
-- handle exceptions with IO operations
local function write_file()
local file = assert(io.open('players.txt', 'w'))
io.output(file)
io.write(json.encode(playerbase.players))
io.close(file)
end
pcall(write_file)
end
--- Read data of the players file
-- @function read_players_file
-- @param nil
-- return nil
function read_players_file()
local function read_file()
local file = assert(io.open('players.txt', 'r'))
assert(io.input(file), "input is invalid")
playerbase.players = json.decode(io.read('*all'))
io.close(file)
end
pcall(read_file)
end
--- Write data in file deleted_players.txt
-- @function write_deleted_players_file
-- @param nil
-- @return nil
function write_deleted_players_file()
-- Handle IO operation
local function write_file()
local file = assert(io.open('deleted_players.txt', 'w'))
io.output(file)
io.write(json.encode(playerbase.players))
io.close(file)
end
pcall(write_file)
end
--- Read data of deleted players
-- @function read_deletd_players_file
-- @param nil
-- @return nil
function read_deleted_players_file()
local function read_file()
local file = assert(io.open('deleted_players.txt', 'r'))
assert(io.input(file), "input is invalid")
playerbase.deleted_players = json.decode(io.read('*all'))
io.close(file)
end
pcall(read_file)
end
--- Save data in leaderboard
-- @function write_leaderboard_file
-- @param nil
-- @param nil
function write_leaderboard_file()
local function write_file()
local file = assert(io.open('leaderboard.txt', 'w'))
io.output(file)
io.write(json.encode(leaderboard.players))
io.close(file)
end
pcall(write_file())
end
--- Read leaderboard
-- @function read_leaderboard_file
-- @param nil
-- @param nil
function read_leaderboard_file()
local function read_file()
local file = assert(io.open('leaderboard.txt', 'r'))
assert(io.input(file), "input is invalid")
leaderboard.players = json.decode(io.read('*all'))
io.close(file)
end
pcall(read_file)
end
--- Save replay
-- @function write_replay_file
-- @param replay Stack of the moves
-- @param path of file
-- @param filename string with name of the file
-- @return nil
function write_replay_file(replay, path, filename)
assert(replay)
assert(path)
assert(filename)
local function write_file()
make_dir(path)
local file = assert(io.open(path..'/'..filename, 'w'))
io.output(file)
io.write(json.encode(replay))
io.close(file)
end
pcall(write_file)
end
--- User csprng to generate random issues
-- @function read_csprng_seed_file
-- @param nil
-- @return nil
function read_csprng_seed_file()
local function read_file()
local file = assert(io.open('csprng_seed.txt', 'r') )
if file then
assert(io.input(file), "input is invalid")
csprng_seed = io.read('*all')
io.close(file)
else
print('csprng_seed.txt could not be read. Writing a new ' ..
'default (2000) csprng_seed.txt')
local new_file = assert(io.open('csprng_seed.txt', 'w'))
io.output(new_file)
io.write('2000')
io.close(new_file)
csprng_seed = '2000'
end
if tonumber(csprng_seed) then
local temporary = assert(tonumber(csprng_seed))
csprng_seed = temporary
else
print('ERROR: csprng_seed.txt content is not numeric. ' ..
'Using default (2000) as csprng_seed')
csprng_seed = 2000
end
end
pcall(read_file)
end