KananGamer

[TFM-LUA] Save/Load Example

Aug 30th, 2018
317
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.45 KB | None | 0 0
  1. --[[
  2.     Author(s): Nettoork#0000
  3. ]]--
  4.  
  5. table.encrypt=function(a,b,c)if not a or not b or not c or type(a)~='table'or b==''or c==''then return end;local d,e,f,g='','','',''for h in b:gmatch('.')do d=d..h:byte()end;for h in c:gmatch('.')do g=g..h:byte()end;math.randomseed(d)otherSeed=math.random(1000000)local i=pcall(function()for h,j in next,a do if type(j)=='string'or type(j)=='number'then if type(j)=='string'then j=[[']]..j..[[']]end;f=f..' '..j..' '..h:upper()else return end end;f=f..' '..g;for h in f:gmatch('.')do local k=h:byte()+68+math.random(5)otherSeed=otherSeed+h:byte()math.randomseed(otherSeed)if k>=65 and k<=122 and not(k>=91 and k<=96)then k=string.char(k)end;e=e..k end end)math.randomseed(os.time())if not i then return else return e end end;table.decrypt=function(l,b,c)if not l or not b or not c or type(l)~='string'or b==''or c==''then return end;local d,e,m,g='','','',''for h in b:gmatch('.')do d=d..h:byte()end;for h in c:gmatch('.')do g=g..h:byte()end;math.randomseed(d)otherSeed=math.random(1000000)local i=pcall(function()for h in l:gmatch('.')do if h:byte()>=65 and h:byte()<=122 then local k=h:byte()-68-math.random(5)otherSeed=otherSeed+k;math.randomseed(otherSeed)e=e..string.char(k)else m=m..h;if m:len()>=3 then local k=tonumber(m)-68-math.random(5)otherSeed=otherSeed+k;math.randomseed(otherSeed)e=e..string.char(k)m=''end end end end)math.randomseed(os.time())if not i then return else local n,o,p,q,r={},0,0;for h,j in string.gmatch(e,'[^%s]+')do p=p+1 end;for h,j in string.gmatch(e,'[^%s]+')do o=o+1;if o==p and h~=g then return elseif q then if q:sub(-1)==[[']]then n[h:lower()]=q:gsub([[']],'')q=nil else q=q..' '..h end elseif r then n[h:lower()]=r;r=nil elseif h:sub(1,1)==[[']]then q=h else r=h end end;return n end end
  6.  
  7. local dbPassword, key = 'henin00b<3', 'henim1t0<3' -- Save's password
  8. local db = {}
  9.  
  10. local updateInfo = function(name)
  11.     ui.addTextArea(3, '<font size="16" color="#010101"><p align="center">Collected Cheeses: '..db[name].save.cheese, name, 5, 252, 120, 42, 0xFFFFFF, 1, 0.7, true)
  12. end
  13.  
  14. eventPopupAnswer = function(id, name, answer)
  15.     if id == 2 then
  16.         local gen = table.decrypt(answer, dbPassword, key)
  17.         if gen and tonumber(gen.id) == tfm.get.room.playerList[name].id then
  18.             db[name].save = gen
  19.             updateInfo(name)
  20.             print(name..' uploaded your save successfully!')
  21.         end
  22.     end
  23. end
  24.  
  25. eventTextAreaCallback = function(id, name, callback)
  26.     if callback == 'save' then
  27.         if db[name].timer < os.time() then
  28.             local gen = (table.encrypt(db[name].save, dbPassword, key))
  29.             ui.addPopup(1, 0, 'Save your code in a safe local:\n '..gen, name, 3, 26, 400, 100)
  30.             db[name].timer = os.time() + 5000
  31.             print(('%s generated a new save! %s'):format(name, gen))
  32.         else
  33.             ui.addPopup(1, 0, 'Wait 5 seconds!', name, 3, 26, 400, 100)
  34.         end
  35.     elseif callback == 'load' then
  36.         ui.addPopup(2, 2, 'Put your save', name, 411, 26, 385, 100)
  37.     end
  38. end
  39.  
  40. eventPlayerWon = function(name)
  41.     db[name].save.cheese = db[name].save.cheese + 1
  42.     updateInfo(name)
  43. end
  44.  
  45. eventNewPlayer = function(name)
  46.     db[name] = {
  47.         timer = os.time(),
  48.         save = {
  49.             id = tfm.get.room.playerList[name].id,
  50.             cheese = 0
  51.         }
  52.     }
  53.     ui.addTextArea(1, '<a href="event:save"><D><font size="28"><p align="center">Save', name, 5, 150, 120, 42, 0xFFFFFF, 1, 0.7, true)
  54.     ui.addTextArea(2, '<a href="event:load"><D><font size="28"><p align="center">Load', name, 5, 201, 120, 42, 0xFFFFFF, 1, 0.7, true)
  55.     updateInfo(name)
  56. end
  57.  
  58. table.foreach(tfm.get.room.playerList, eventNewPlayer)
Advertisement
Add Comment
Please, Sign In to add comment