This repository has been archived by the owner on Nov 6, 2024. It is now read-only.
forked from TecProg-grupo4-2018-2/panel-attack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_file_io.lua
222 lines (200 loc) · 7.57 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
local lfs = require("lfs")
local csvfile = require("simplecsv")
function os_rename(old_name, new_name)
os.rename(old_name, new_name)
end
function isFile(name)
if type(name)~="string" then return false end
if not isDir(name) then
return os.rename(name,name) and true or false
-- note that the short evaluation is to
-- return false instead of a possible nil
end
return false
end
function isFileOrDir(name)
if type(name)~="string" then return false end
return os.rename(name, name) and true or false
end
function isDir(name)
if type(name)~="string" then return false end
local cd = lfs.currentdir()
local is = lfs.chdir(name) and true or false
lfs.chdir(cd)
return is
end
function mkDir(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
function write_players_file() pcall(function()
local f = assert(io.open("players.txt", "w"))
io.output(f)
io.write(json.encode(playerbase.players))
io.close(f)
end) end
function read_players_file() pcall(function()
local f = assert(io.open("players.txt", "r"))
io.input(f)
playerbase.players = json.decode(io.read("*all"))
io.close(f)
end) end
function write_deleted_players_file() pcall(function()
local f = assert(io.open("deleted_players.txt", "w"))
io.output(f)
io.write(json.encode(playerbase.players))
io.close(f)
end) end
function read_deleted_players_file() pcall(function()
local f = assert(io.open("deleted_players.txt", "r"))
io.input(f)
playerbase.deleted_players = json.decode(io.read("*all"))
io.close(f)
end) end
function write_leaderboard_file() pcall(function()
-- local f = assert(io.open("leaderboard.txt", "w"))
-- io.output(f)
-- io.write(json.encode(leaderboard.players))
-- io.close(f)
--now also write a CSV version of the file
--local csv = "user_id,user_name,rating,placement_done,placement_rating,ranked_games_played,ranked_games_won"
local sep = package.config:sub(1, 1)
local leaderboard_table = {}
leaderboard_table[#leaderboard_table+1] = {"user_id","user_name","rating","placement_done","placement_rating","ranked_games_played","ranked_games_won"}
for user_id,v in pairs(leaderboard.players) do
leaderboard_table[#leaderboard_table+1] =
{user_id, v.user_name,v.rating,tostring(v.placement_done or ""),v.placement_rating,v.ranked_games_played,v.ranked_games_won}
end
csvfile.write('.'..sep..'leaderboard.csv', leaderboard_table)
end) end
function read_leaderboard_file() pcall(function()
local csv_table = csvfile.read('./leaderboard.csv')
if csv_table[2] then
print("read leaderboard.csv")
for row=2,#csv_table do
csv_table[row][1] = tostring(csv_table[row][1])
leaderboard.players[csv_table[row][1]] = {}
for col=1, #csv_table[1] do
--Note csv_table[row][1] will be the player's user_id
--csv_table[1][col] will be a property name such as "rating"
if csv_table[row][col] == '' then
csv_table[row][col] = nil
end
--player with this user_id gets this property equal to the csv_table cell's value
if csv_table[1][col] == "user_name" then
leaderboard.players[csv_table[row][1]][csv_table[1][col]] = tostring(csv_table[row][col])
elseif csv_table[1][col] == "rating" then
leaderboard.players[csv_table[row][1]][csv_table[1][col]] = tonumber(csv_table[row][col])
elseif csv_table[1][col] == "placement_done" then
leaderboard.players[csv_table[row][1]][csv_table[1][col]] = csv_table[row][col] and true and string.lower(csv_table[row][col]) ~= "false"
else
leaderboard.players[csv_table[row][1]][csv_table[1][col]] = csv_table[row][col]
end
end
end
else
print("failed to read any data from leaderboard.csv, checking for a leaderboard.txt")
local f = assert(io.open("leaderboard.txt", "r"))
io.input(f)
leaderboard.players = json.decode(io.read("*all"))
io.close(f)
end
end) end
function read_user_placement_match_file(user_id) return pcall(function()
local sep = package.config:sub(1, 1)
local csv_table = csvfile.read('./placement_matches/incomplete/'..user_id..'.csv')
if not csv_table or #csv_table < 2 then
print("csv_table from read_user_placement_match_file was nil or <2 length")
return nil
else
print("csv_table from read_user_placement_match_file :")
print(json.encode(csv_table))
end
local ret = {}
for row=2,#csv_table do
csv_table[row][1] = tostring(csv_table[row][1]) --change the op_user_id to a string
ret[#ret+1] = {}
for col=1, #csv_table[1] do
--Note csv_table[row][1] will be the player's user_id
--csv_table[1][col] will be a property name such as "rating"
if csv_table[row][col] == '' then
csv_table[row][col] = nil
end
--player with this user_id gets this property equal to the csv_table cell's value
if csv_table[1][col] == "op_name" then
ret[#ret][csv_table[1][col]] = tostring(csv_table[row][col])
elseif csv_table[1][col] == "op_rating" then
ret[#ret][csv_table[1][col]] = tonumber(csv_table[row][col])
elseif csv_table[1][col] == "op_user_id" then
ret[#ret][csv_table[1][col]] = tostring(csv_table[row][col])
elseif csv_table[1][col] == "outcome" then
ret[#ret][csv_table[1][col]] = tonumber(csv_table[row][col])
else
ret[#ret][csv_table[1][col]] = csv_table[row][col]
end
end
end
print("read_user_placement_match_file ret: ")
print(tostring(ret))
print(json.encode(ret))
return ret
end) end
function move_user_placement_file_to_complete(user_id) pcall(function()
local sep = package.config:sub(1, 1)
mkDir('./placement_matches/complete')
local moved, err = os.rename('./placement_matches/incomplete/'..user_id..'.csv','./placement_matches/complete/'..user_id..'.csv')
end) end
function write_user_placement_match_file(user_id, placement_matches) pcall(function()
local sep = package.config:sub(1, 1)
local pm_table = {}
pm_table[#pm_table+1] = {"op_user_id","op_name","op_rating","outcome"}
for k,v in ipairs(placement_matches) do
pm_table[#pm_table+1] =
{v.op_user_id, v.op_name,v.op_rating, v.outcome}
end
mkDir('placement_matches'..sep..'incomplete')
csvfile.write('placement_matches'..sep..'incomplete'..sep..user_id..'.csv', pm_table)
end) end
function write_replay_file(replay, path, filename) pcall(function()
local sep = package.config:sub(1, 1)
print("about to open new replay file for writing")
mkDir(path)
local f = assert(io.open(path..sep..filename, "w"))
print("past file open")
io.output(f)
io.write(json.encode(replay))
io.close(f)
print("finished write_replay_file()")
end) end
function read_csprng_seed_file() pcall(function()
local f = io.open("csprng_seed.txt", "r")
if f then
io.input(f)
csprng_seed = io.read("*all")
io.close(f)
else
print("csprng_seed.txt could not be read. Writing a new default (2000) csprng_seed.txt")
local new_file = 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 tempvar = tonumber(csprng_seed)
csprng_seed = tempvar
else
print("ERROR: csprng_seed.txt content is not numeric. Using default (2000) as csprng_seed")
csprng_seed = 2000
end
end) end
--old
-- function write_replay_file(replay_table, file_name) pcall(function()
-- local f = io.open(file_name, "w")
-- io.output(f)
-- io.write(json.encode(replay_table))
-- io.close(f)
-- end) end