Guest User

Simples Lua Accountsystem für Teeworlds

a guest
Aug 7th, 2012
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.29 KB | None | 0 0
  1. db = sql.create("accounts.db")
  2. sql.query(db, "CREATE TABLE IF NOT EXISTS `users` (`uid` INTEGER PRIMARY KEY, `username`, `password`, `playername`, `kills` INTEGER DEFAULT 0, `deaths` INTEGER DEFAULT 0)")
  3.  
  4. AddEventListener("OnChat", "Chat")
  5. AddEventListener("OnClientConnect", "ClientConnect")
  6. AddEventListener("OnClientEnter", "ClientEnter")
  7. AddEventListener("OnDie", "Kill")
  8. AddEventListener("OnPlayerJoinTeam", "PlayerJoinTeam")
  9. AddEventListener("OnClientDrop", "Leave")
  10.  
  11. Accounts = {}
  12. KDRate = 0
  13.  
  14. iTick = 0
  15. function Tick(Time, ServerTick)
  16.     iTick = iTick + 1
  17. end
  18. function TickDefered(Time, ServerTick)
  19.  
  20. end
  21. function PostTick(Time, ServerTick)
  22.  
  23. end
  24.  
  25. function EscapeString(str)
  26.     str = tostring(str)
  27.     str = str:replace("\\", "\\\\")
  28.     str = str:replace("'", "''")
  29.     return str
  30. end
  31.  
  32. function CheckAccount(Name)
  33.     result = sql.query(db, "SELECT * FROM `users` WHERE `username` = '" .. EscapeString(Name) .. "'")
  34.     if (result[1] and result[1]["uid"]) then
  35.         return true
  36.     end
  37.     return false
  38. end
  39.  
  40. function GetParameter(Text, Number)
  41.     for i = 1, Number do
  42.         s = Text:find(" ")
  43.         if (s == nil) then
  44.             return nil
  45.         end
  46.         Text = Text:sub(s + 1)
  47.     end
  48.     s = Text:find(" ")
  49.     if (s ~= nil) then
  50.         s = s - 1
  51.     end
  52.     Text = Text:sub(1, s)
  53.     return Text
  54. end
  55.  
  56. function Update(ClientID)
  57.     if (Accounts[ClientID]["uid"] == nil) then
  58.         return
  59.     end
  60.     sql.query(db, "UPDATE `users` SET `playername` = '" .. EscapeString(GetPlayerName(ClientID)) .. "', `kills` = '" .. EscapeString(Accounts[ClientID]["kills"]) .. "', `deaths` = '" .. EscapeString(Accounts[ClientID]["deaths"]) .. "' WHERE `uid` = '" .. Accounts[ClientID]["uid"] .. "'")
  61. end
  62.  
  63. function Register(User, Pass, ClientID)
  64.     if(CheckAccount(User) == true) then
  65.         Login(User, Pass, ClientID)
  66.     else
  67.         sql.query(db, "INSERT INTO `users` (`username`, `password`, `playername`) VALUES ('" .. EscapeString(User) .. "', '" .. EscapeString(Pass) .. "', '" .. EscapeString(GetPlayerName(ClientID)) .. "')")
  68.         Login(User, Pass, ClientID)
  69.     end
  70. end
  71.  
  72. function Login(User, Pass, ClientID)
  73.     result = sql.query(db, "SELECT * FROM `users` WHERE `username` = '" .. EscapeString(User) .. "' AND `password` = '" .. EscapeString(Pass) .. "'")
  74.     if (result[1] and result[1]["uid"]) then
  75.         Accounts[ClientID] = result[1]
  76.         Print("kills", result[1]["kills"])
  77.         Print("deaths", result[1]["deaths"])
  78.         SetPlayerScore(ClientID, Accounts[ClientID]["kills"])
  79.         SetPlayerTeam(ClientID, 0)
  80.     else
  81.         Register(User, Pass, ClientID)
  82.     end
  83. end
  84.  
  85. function Logout(ClientID)
  86.     Update(ClientID)
  87.     Accounts[ClientID] = {}
  88.     SetPlayerTeam(ClientID, -1)
  89. end
  90.  
  91. function GetStats(ID)
  92.     SendChatTarget(ID, "Kills: "..Accounts[ID]["kills"])
  93.     SendChatTarget(ID, "Deaths: "..Accounts[ID]["deaths"])
  94.     if(Accounts[ID]["kills"] < 1 or Accounts[ID]["deaths"] < 1) then
  95.         KDRate = 0
  96.     else
  97.         KDRate = ((Accounts[ID]["kills"])/(Accounts[ID]["deaths"]))
  98.     end
  99.     SendChatTarget(ID, "K/D: "..KDRate)
  100. end
  101.  
  102. function Chat(Text, ID, Team)
  103.     if (Text:sub(1, 1) == "/") then
  104.         if (Text:sub(1, 6) == "/stats" and (Text:len() == 6)) then
  105.             GetStats(ID)
  106.         elseif (Text:sub(1, 8) == "/cmdlist" and (Text:len() == 8)) then
  107.             SendChatTarget(ID, "Servercommands:")
  108.             SendChatTarget(ID, "Commando - Funktion")
  109.             SendChatTarget(ID, "/stats - Gibt deine Stats wieder, Score etc.")
  110.             SendChatTarget(ID, "/info - Info ueber die Mod")
  111.         elseif (Text:sub(1, 5) == "/info" and (Text:len() == 5)) then
  112.             SendChatTarget(ID, "System based on the Lua Accountsystem by Map")
  113.             SendChatTarget(ID, "Death Match with Saved Scores. Have Fun!")
  114.         else
  115.             SendChatTarget(ID, "Unknown command")
  116.             SendChatTarget(ID, "/cmdlist")
  117.         end
  118.         return true
  119.     end
  120. end
  121.  
  122. function Kill(Killer, Victim, Weapon)
  123.     if (Killer ~= Victim) then
  124.         Accounts[Killer]["kills"] = Accounts[Killer]["kills"] + 1
  125.     end
  126.     Accounts[Victim]["deaths"] = Accounts[Killer]["deaths"] + 1
  127. end
  128.  
  129. function Leave(ID, Reason)
  130.     Update(ID)
  131. end
  132.  
  133. function ClientConnect(ClientID)
  134.     Print("Clear", ClientID)
  135.     Accounts[ClientID] = {}
  136.     --SetPlayerTeam(ClientID, -1)
  137. end
  138.  
  139. function ClientEnter(ClientID)
  140.     Register(GetPlayerName(ClientID), GetPlayerClan(ClientID),  ClientID)
  141. end
Add Comment
Please, Sign In to add comment