Advertisement
Patosho

Hina playerData

Dec 3rd, 2015
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.35 KB | None | 0 0
  1. local game_name = "Test"
  2. local game_id = 127
  3. local game_data_version = 1
  4. local data_header = "PATO0002"
  5.  
  6. local admin = {Hinakagiyama=true}
  7.  
  8. local stats_klist = {rounds=true,cheese=true,first=true}
  9. local stats_name = {"rounds","cheese","first"}
  10. local stats_size = {3,3,3}
  11. local stats_n = 3
  12.  
  13. local playerData = {}
  14. local playerFile = {}
  15.  
  16. local mice = {}
  17.  
  18. function main()
  19.     for n in pairs(tfm.get.room.playerList) do
  20.         eventNewPlayer(n)
  21.     end
  22. end
  23.  
  24. function eventNewPlayer(name)
  25.     playerData[name] = defaultData()
  26.     system.loadPlayerData(name)
  27. end
  28.  
  29. function defaultData()
  30.     local d = {}
  31.     for i=1,stats_n do
  32.         d[stats_name[i]] = 0
  33.     end
  34.     return d
  35. end
  36.  
  37. function dataToFile(d)
  38.     local f,l,n = "",4
  39.     for i=1,stats_n do
  40.         n = stats_size[i]
  41.         f=f..numberToBytes(d[stats_name[i]], n)
  42.         l = l + n
  43.     end
  44.     f = numberToBytes(l, 2)..numberToBytes(game_id, 1)..numberToBytes(game_data_version)..f
  45.     return f,l
  46. end
  47.  
  48. function fileToData(f)
  49.     local d,s,l,n = {},5,bytesToNumber(f:sub(1,2))
  50.     for i=1,stats_n do
  51.         n = stats_size[i]
  52.         d[stats_name[i]] = bytesToNumber(f:sub(s,s+n-1))
  53.         s = s + n
  54.     end
  55.     return d
  56. end
  57.  
  58. function debug_fileToString(f)
  59.     local s,t = 5,bytesToNumber(f:sub(1,2)).." "..bytesToNumber(f:sub(3,3)).." "..bytesToNumber(f:sub(4,4))
  60.     for i=1,stats_n do
  61.         n = stats_size[i]
  62.         t=t.." "..bytesToNumber(f:sub(s,s+n-1))
  63.         s = s + n
  64.     end
  65.     return t
  66. end
  67.  
  68. function eventPlayerDataLoaded(name, file)
  69.     local header,s,l,z,game_found,f,id,pfile = file:sub(1,8),9,0,file:len()
  70.    
  71.     if header == data_header then
  72.         while s <= z do
  73.             l = bytesToNumber(file:sub(s,s+1))
  74.             id = bytesToNumber(file:sub(s+2,s+2))
  75.             if id == game_id then
  76.                 game_found = true
  77.                 break
  78.             end
  79.             s = s+l
  80.         end
  81.     else
  82.         file = data_header
  83.     end
  84.    
  85.     if game_found then
  86.         f = file:sub(s,s+l-1)
  87.         local v = bytesToNumber(f:sub(4,4))
  88.         if v == game_data_version then
  89.             playerData[name] = fileToData(f)
  90.             debug_msg(debug_fileToString(f).." "..name)
  91.         else
  92.             --version migration
  93.             debug_msg(name.." - No version migration, replacing with default stats")
  94.             local d = defaultData()
  95.             f = dataToFile(d)
  96.             --
  97.             file = string_replace(file, s, s+l-1, f)
  98.             system.savePlayerData(name, file)
  99.         end
  100.     else
  101.         debug_msg(s)
  102.         f,l = dataToFile(playerData[name])
  103.         file = file..f
  104.         system.savePlayerData(name, file)
  105.     end
  106.    
  107.     pfile = {file=file, start=s, lenght=l}
  108.     playerFile[name] = pfile
  109. end
  110.  
  111. function saveData(name)
  112.     local f,pfile = dataToFile(playerData[name]),playerFile[name]
  113.     pfile.file = string_replace(pfile.file, pfile.start, pfile.start + pfile.lenght - 1, f)
  114.     system.savePlayerData(name, pfile.file)
  115. end
  116.  
  117. function string_replace(s, i, j, t)
  118.     return s:sub(1,i-1)..t..s:sub(j+1)
  119. end
  120.  
  121. function bytesToNumber(s)
  122.     local n = 0
  123.     local l = string.len(s)
  124.     for i = 1, l do
  125.         n = n + (string.byte(s, i) % 128) * 128 ^ (l - i)
  126.     end
  127.     return n
  128. end
  129.  
  130. function numberToBytes(n, l)
  131.     n = math.floor(math.abs(n))
  132.     l = l or 1
  133.     local s = ""
  134.     for i = 1, l do
  135.         local e = 128 ^ (l - i)
  136.         local p = math.floor(n / e)
  137.         s = s .. string.char(p)
  138.         n = n - p * e
  139.     end
  140.     return s
  141. end
  142.  
  143. function eventChatCommand(name, com)
  144.     if true or admin[name] then
  145.         if stats_klist[com] then
  146.             local d = playerData[name]
  147.             d[com] = d[com] + 1
  148.             debug_msg(d[com])
  149.             saveData(name)
  150.         end
  151.     end
  152. end
  153.  
  154. function debug_msg(m)
  155.     tfm.exec.chatMessage("<V>"..m, nil)
  156. end
  157.  
  158. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement