Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local game_name = "Test"
- local game_id = 127
- local game_data_version = 1
- local data_header = "PATO0002"
- local admin = {Hinakagiyama=true}
- local stats_klist = {rounds=true,cheese=true,first=true}
- local stats_name = {"rounds","cheese","first"}
- local stats_size = {3,3,3}
- local stats_n = 3
- local playerData = {}
- local playerFile = {}
- local mice = {}
- function main()
- for n in pairs(tfm.get.room.playerList) do
- eventNewPlayer(n)
- end
- end
- function eventNewPlayer(name)
- playerData[name] = defaultData()
- system.loadPlayerData(name)
- end
- function defaultData()
- local d = {}
- for i=1,stats_n do
- d[stats_name[i]] = 0
- end
- return d
- end
- function dataToFile(d)
- local f,l,n = "",4
- for i=1,stats_n do
- n = stats_size[i]
- f=f..numberToBytes(d[stats_name[i]], n)
- l = l + n
- end
- f = numberToBytes(l, 2)..numberToBytes(game_id, 1)..numberToBytes(game_data_version)..f
- return f,l
- end
- function fileToData(f)
- local d,s,l,n = {},5,bytesToNumber(f:sub(1,2))
- for i=1,stats_n do
- n = stats_size[i]
- d[stats_name[i]] = bytesToNumber(f:sub(s,s+n-1))
- s = s + n
- end
- return d
- end
- function debug_fileToString(f)
- local s,t = 5,bytesToNumber(f:sub(1,2)).." "..bytesToNumber(f:sub(3,3)).." "..bytesToNumber(f:sub(4,4))
- for i=1,stats_n do
- n = stats_size[i]
- t=t.." "..bytesToNumber(f:sub(s,s+n-1))
- s = s + n
- end
- return t
- end
- function eventPlayerDataLoaded(name, file)
- local header,s,l,z,game_found,f,id,pfile = file:sub(1,8),9,0,file:len()
- if header == data_header then
- while s <= z do
- l = bytesToNumber(file:sub(s,s+1))
- id = bytesToNumber(file:sub(s+2,s+2))
- if id == game_id then
- game_found = true
- break
- end
- s = s+l
- end
- else
- file = data_header
- end
- if game_found then
- f = file:sub(s,s+l-1)
- local v = bytesToNumber(f:sub(4,4))
- if v == game_data_version then
- playerData[name] = fileToData(f)
- debug_msg(debug_fileToString(f).." "..name)
- else
- --version migration
- debug_msg(name.." - No version migration, replacing with default stats")
- local d = defaultData()
- f = dataToFile(d)
- --
- file = string_replace(file, s, s+l-1, f)
- system.savePlayerData(name, file)
- end
- else
- debug_msg(s)
- f,l = dataToFile(playerData[name])
- file = file..f
- system.savePlayerData(name, file)
- end
- pfile = {file=file, start=s, lenght=l}
- playerFile[name] = pfile
- end
- function saveData(name)
- local f,pfile = dataToFile(playerData[name]),playerFile[name]
- pfile.file = string_replace(pfile.file, pfile.start, pfile.start + pfile.lenght - 1, f)
- system.savePlayerData(name, pfile.file)
- end
- function string_replace(s, i, j, t)
- return s:sub(1,i-1)..t..s:sub(j+1)
- end
- function bytesToNumber(s)
- local n = 0
- local l = string.len(s)
- for i = 1, l do
- n = n + (string.byte(s, i) % 128) * 128 ^ (l - i)
- end
- return n
- end
- function numberToBytes(n, l)
- n = math.floor(math.abs(n))
- l = l or 1
- local s = ""
- for i = 1, l do
- local e = 128 ^ (l - i)
- local p = math.floor(n / e)
- s = s .. string.char(p)
- n = n - p * e
- end
- return s
- end
- function eventChatCommand(name, com)
- if true or admin[name] then
- if stats_klist[com] then
- local d = playerData[name]
- d[com] = d[com] + 1
- debug_msg(d[com])
- saveData(name)
- end
- end
- end
- function debug_msg(m)
- tfm.exec.chatMessage("<V>"..m, nil)
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement