Advertisement
VADemon

Follow-up: pastebin.com/KZGtphXG

Jun 20th, 2015
312
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.89 KB | None | 0 0
  1. -- LUA BY CSENDES MARCELL
  2.  
  3. -- MySQL Connection Details:
  4. sql_host = "" -- Host
  5. sql_user = "" -- USER
  6. sql_password = "" --Pass
  7. sql_db_name = "cs2d" -- DBNAME
  8. sql_port = 3306 -- Default Port usually this, do not change it, if you are not sure what is it.
  9.  
  10. -- Adding Hooks
  11. addhook("join", "logPlayer")
  12. addhook("join", "JoinMessage")
  13. addhook("minute", "saveStats")
  14. addhook("say", "logChat")
  15. addhook("say","RegisterLogin")
  16. addhook("say","testsay")
  17.  
  18.  
  19.  
  20. -- Configuration --
  21. server = " CS2D Online" -- server name
  22. version = " 0.1a" -- version of script
  23. LoggedIn = false -- Unregisted user's status, when he/she joins to server.
  24.  
  25.  
  26. color = {
  27.         "©552220000", -- CS2D (yellow) 1
  28.         "©255255255", -- white 2
  29.         "©255000000", -- red 3
  30.         "©000255000", -- green 4
  31.         "©000000255", -- blue 5
  32.         "©220220220", -- light grey 6
  33.         "©064179162", -- PM - light blue 7
  34.         "©255025000", -- T 8
  35.         "©050150255", -- CT 9
  36.         "©100100100" -- pretty dark grey - tag 10
  37. }
  38.  
  39. -- Connection LUA and between MySQL Server
  40. luasql = require "luasql.mysql"
  41. env = assert (luasql.mysql())
  42. con = assert(env:connect(sql_db_name,sql_user,sql_password,sql_host,sql_port))
  43.  
  44. -- Functions
  45.  
  46. function CheckMySQL()
  47.     print("Connecting to sql server")   -- you need most prints BEFORE executing stuff, not after doing that
  48.     if assert(env:connect(sql_db_name,sql_user,sql_password,sql_host,sql_port)) then    -- dont you do that in line 42 already?
  49.     print("©000255000The connection for MySQL server was successfully")
  50.     else
  51.     print("©255000000The connection wasn't successful for MySQL check your server and/or configuration in index.lua")
  52.     end
  53. end
  54.  
  55. CheckMySQL()
  56.    
  57. function JoinMessage(id)
  58.     msg2(id, "©255255255Welcome on the ©55220000"..server.." ©255255255server!")
  59.     msg2(id, "©255255255Our Gamemode running under ©55220000"..version.." ©255255255version.")
  60.     msg2(id, "©000255000To register on our server just use !register <username> <password>")
  61.     msg2(id, "©000255000You will be automatically logged in after you register an account.")
  62. end
  63.  
  64. function DoLogin(id)
  65.  
  66. local usgn = player(id, "usgn")
  67. if usgn > 0 then
  68.     LoggedIn = true
  69.     end
  70. end
  71.  
  72. function _DoLogin(id)
  73.     DoLogin(id)
  74.     if LoggedIn then
  75.         msg2(id, "©000255000You have been successfully logged in.")
  76.     end
  77. end
  78.  
  79. function DoLogout(id)
  80.  
  81. local usgn = player(id, "usgn")
  82.  
  83. if usgn > 0 then
  84.     LoggedIn = false
  85.     end
  86. end
  87.  
  88. function VerifyLogin(id)
  89.  
  90. local usgn = player(id, "usgn")
  91. if usgn > 0 then
  92.     if LoggedIn == false then
  93.     msg2(id, "Please register or log in to play on the server.")
  94.     elseif LoggedIn == true then
  95.         return 0;
  96.     end
  97. end
  98. end
  99.  
  100. function RegisterLogin(id,txt)
  101. print("Received command! ".. tostring(id) .." ; "..tostring(txt))
  102. local usgn = player(id, "usgn")
  103. local space = txt:find(" ") or #txt + 1
  104. local cmd = txt:sub(1, space - 1)
  105.  
  106. if cmd == "!login" then
  107.     print("Command is !login")
  108.         cur = con:execute("select * from users")
  109.         print("cur/row line")
  110.         row = cur:fetch({}, 'a')
  111.         print("row succeeded")
  112.         for k, v in pairs(row) do
  113.         print("pairs...")
  114.         print(k, v)
  115.     end
  116. end
  117.  
  118.  
  119. if cmd=="!register" then
  120.                 local regName, regPW = txt:match("!register ([%a|%d]+) ([%a|%d]+)")
  121.                 local regDate = os.date("%c")
  122.  
  123.                 if usgn > 0 then
  124.                         res = assert (con:execute(string.format([[INSERT INTO users VALUES ('%s', '%s', '%s')]], regName, regPW, regDate)))
  125.                 msg2(id, "©000255000Congratulations! You have been successfully registered!")
  126.                 msg2(id, "©000255000Username: ©255255255"..regName)
  127.                 msg2(id, "©000255000Password: ©255255255"..regPW)
  128.                 msg2(id, "©255255255You will be automatically logged in after 5 second(s)")
  129.                 timer(5000,'parse','lua _DoLogin('..id..')')
  130.         end
  131.         end
  132. end
  133.  
  134.  
  135. function logPlayer(id)
  136. print("logPlayer: " ..tostring(id))
  137. local usgn = player(id, "usgn")
  138. local ip = player(id, "ip")
  139. local name = player(id, "name")
  140. local datime = os.date("%c")
  141.  
  142.     if usgn > 0 then
  143.         print("logPlayer: USGN>0")
  144.         res = assert (con:execute(string.format([[
  145.             INSERT INTO logins
  146.             VALUES ('%s', '%s', '%s', '%s')]], usgn, ip, name, datime)
  147.         ))
  148.         print("logPlayer: USGN>0 STATUS: OK")
  149.     elseif usgn == 0 then
  150.         print("logPlayer: USGN==0")
  151.         res = assert (con:execute(string.format([[
  152.             INSERT INTO bots
  153.             VALUES ('%s', '%s')]], name, datime)
  154.         ))
  155.         print("logPlayer: USGN==0 STATUS: OK")
  156.     end
  157. end
  158.  
  159.        
  160. function logChat(id, msg)
  161. print("logChat")
  162. local usgn = player(id, "usgn")
  163. local ip = player(id, "ip")
  164. local name = player(id, "name")
  165. local datime = os.date("%c")
  166.        
  167.         if usgn > 0 then
  168.         print("logChat USGN>0")
  169.         res = assert (con:execute(string.format([[
  170.                 INSERT INTO chats
  171.                 VALUES ('%s', '%s', '%s', '%s', '%s')]], usgn, ip, name, datime, msg)
  172.         ))
  173.         end
  174.         print("logChat STATUS: OK")
  175. end
  176.  
  177. function saveStats(id)
  178. print("SaveStats")
  179.                 for _, id in pairs(player(0,"table")) do
  180.                 local usgn = player(id, "usgn")
  181.                 local level = player(id, "score")
  182.                 local ip = player(id, "ip")
  183.                 local name = player(id, "name")
  184.                 local datime = os.date("%c")
  185.                 if usgn > 0 then
  186.                 if VerifyLogin(id) then
  187.                 if level > 0 then
  188.                 print("logStats level > 0")
  189.                           res = assert (con:execute(string.format([[
  190.                           INSERT INTO stats
  191.                           VALUES ('%s', '%s', '%s', '%s', '%s')]], usgn, ip, name, datime, level)
  192.                           ))
  193.                 print("©000255000The players's stats have been saved into MYSQL on ©255255255"..datime)
  194.                 else
  195.                     print("logSTATS: return 0")
  196.                         return 0;
  197.                 end
  198.             end
  199.         end
  200.     end
  201.     print("logStats STATUS: OK")
  202. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement