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
--Helper variables
local save_path = "sys/lua/money/"
local save_ext = ".txt"
local file_delim = " "
--Store file contents, so you don't have to read it repeatedly
local cache = {}
--Store a given data into an index
local function saveCache(index,data) cache[index] = data end
--Empty the cache
local function clearCache() cache = {} end
--Remove a specific
local function removeCache(file) if cache[file] then cache[file] = nil end end
--Gets a cache
local function getCache(index) return cache[index] end
--Helper functions
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
--Check if a specific file exists
local function fileExists(name)
local f=io.open(name,"r")
if f~=nil then io.close(f) return true else return false end
end
--Return the content of a file (as a table) if the specified file exists
--Also takes an index and returns a specific element in your content
--depending on the given delimiter to the function 'split'
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
--Saves a file with a given data
local function saveFile(file, data)
local file = io.open(file, "w")
file:write(data)
file:close()
end
--Get a players file in the 'cache'
--We'll use their usgn to verify
local function getPlayerAsFile(usgn)
return save_path..usgn..save_ext
end
--Hooks
addhook("join", "hook_Join")
addhook("spawn", "hook_Spawn")
addhook("leave", "hook_Leave")
--Hook Functions
function hook_Join(id)
local usgn = player(id, "usgn")
--Check if the player is logged into USGN
if usgn > 0 then
--Get the money from the file from index 1 (We assume that the money is stored
--as the first element in the file)
--It will automatically store it in the cache so we can use it later on
getFileContent(getPlayerAsFile(usgn))
end
end
function hook_Spawn(id)
--Load the player data from the cache
local data = getCache(getPlayerAsFile(player(id,"usgn")))
--Check if the data exists
if data then
--Do whatever you want with the data...
--It's stored as a table with the same order as your file
parse("setmoney "..id.." "..data[1])
end
end
function hook_Leave(id)
local usgn = player(id, "usgn")
if usgn > 0 then
--Save a file with the players money if they are logged in
saveFile(getPlayerAsFile(usgn), player(id, "money"))
removeCache(getPlayerAsFile(usgn))
end
end