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
local save_path = "sys/lua/money/"
local save_ext = ".txt"
local file_delim = " "
local cache = {}
local function saveCache(index,data) cache[index] = data end
local function clearCache() cache = {} end
local function removeCache(file) if cache[file] then cache[file] = nil end end
local function getCache(index) return cache[index] end
local function split(str, delim)
local res = {}
local start = 1
local df, dt = string.find(str, delim, start)
while df do
res[#res + 1] = string.sub(str, start , df-1)
start = dt + 1
df, dt = string.find(str, delim, start)
end
res[#res + 1] = string.sub(str, start)
return res
end
local function fileExists(name)
local f=io.open(name,"r")
if f~=nil then io.close(f) return true else return false end
end
local function getFileContent(file, index)
if cache[file] then
if index then
return cache[file][index]
end
return cache[file]
end
if fileExists(file) then
local f = io.open(file, "r")
local content = f:read("*a")
f:close()
content = split(content, file_delim)
saveCache(file, content)
if index then
return content[index]
end
return content
end
return nil
end
local function saveFile(file, data)
local file = io.open(file, "w")
file:write(data)
file:close()
end
local function getPlayerAsFile(usgn)
return save_path..usgn..save_ext
end
addhook("join", "hook_Join")
addhook("spawn", "hook_Spawn")
addhook("leave", "hook_Leave")
function hook_Join(id)
local usgn = player(id, "usgn")
if usgn > 0 then
getFileContent(getPlayerAsFile(usgn))
end
end
function hook_Spawn(id)
local data = getCache(getPlayerAsFile(player(id,"usgn")))
if data then
parse("setmoney "..id.." "..data[1])
end
end
function hook_Leave(id)
local usgn = player(id, "usgn")
if usgn > 0 then
saveFile(getPlayerAsFile(usgn), player(id, "money"))
removeCache(getPlayerAsFile(usgn))
end
end