• Print

Author Topic: Function Add Things By Time ( Megiddo HELP Please Or Who Know )  (Read 4504 times)

0 Members and 1 Guest are viewing this topic.

Offline Schiaffino

  • Jr. Member
  • **
  • Posts: 88
  • Karma: 1
Hi well i have this code ( that people gain exp when they kill an user or an npc or per hit )
BUT I WANT TO KNOW HOW CAN I ADD A LINE TO PEOPLE EARN ( EXP ) BY ONLINE TIME, Who can help me with that ? thanks.


Code: Lua
  1. /*********************************** TODO: Text data? More Powerups? Other player's EXP on screen? More damage?  *******************************************/
  2.  
  3.  
  4. local EXP_INC = 1                       // The experience gained on every shot
  5. local EXP_USENPC = 2                    // 0 = Players as exp objects, 1 = NPCs as exp objects, 2 = both
  6. local EXP_DEATHLOSS = 0.03              // The percentage of exp you lose on death (3 percent in this case)
  7. local EXP_STOPATMAX = 1         // Whether or not player's stop gaining exp at the maximum level (1 for they do)
  8. local EXP_USETXT = 1            // Whether or not to use SQL or .txt files to store the data (1 using .txt)
  9.  
  10.  
  11. // Let's make the files so we can store the exp
  12. sql.Query("CREATE TABLE IF NOT EXISTS exp_pdata('steam' TEXT NOT NULL, 'exp' INTEGER NOT NULL, 'curlevel' INTEGER NOT NULL, 'nextlevel' INTEGER NOT NULL, 'levelnum' INTEGER NOT NULL, PRIMARY KEY('steam'));")
  13. if not file.Exists("exp_store.txt") and EXP_USETXT == 1 then file.Write("exp_store.txt","") end
  14.  
  15.  
  16.  
  17. // Just a quick function to print things into everyone's chat
  18. local function PrintAll(msg) for _,v in ipairs(player.GetAll()) do v:ChatPrint(msg) end end
  19.  
  20.  
  21.  
  22.  
  23. // Some writing/reading functions if you're using text files: still a work in progress
  24. local function WriteData(ply)
  25.         local lines = string.Explode("\n",file.Read("exp_store.txt"))
  26.         local towrite = ""
  27.         for k,v in ipairs(lines) do
  28.                 local values = string.Explode(" ",v)
  29.                 if values[1] != ply:SteamID() then towrite = towrite..v
  30.                 else end
  31.         end
  32.         file.Write("exp_store.txt",
  33.         file.Read("exp_store.txt")..ply:SteamID().." "..ply:GetNWInt("Exp").." "..ply:GetNWInt("CurLevel").." "..ply:GetNWInt("NextLevel").." "..ply:GetNWInt("LevelNum").."\n")
  34. end
  35.  
  36. local function LoadData(ply)
  37.         local lines = string.Explode("\n",file.Read("exp_store.txt"))
  38.         for _,v in ipairs(lines) do
  39.                 local values = string.Explode(" ",v)
  40.                 if values[1] == ply:SteamID() then
  41.                         ply:SetNWInt("Exp",values[2])
  42.                         ply:SetNWInt("CurLevel",values[3])
  43.                         ply:SetNWInt("NextLevel",values[4])
  44.                         ply:SetNWInt("LevelNum",values[5])
  45.                 end
  46.         end
  47. end
  48.  
  49.  
  50. // The markers at which you gain a level (always leave the 0, don't go negative or put something before 0)
  51. levelups = {
  52. 0,
  53. 50,
  54. 100,
  55. 250,
  56. 500,
  57. 1000,
  58. 2500,
  59. 5000,
  60. 10000,
  61. 20000,
  62. 30000,
  63. 40000,
  64. 50000,
  65. 60000,
  66. 70000,
  67. 80000,
  68. 90000,
  69. 100000,
  70. 110000}
  71.  
  72. /*********************************************************** GAMEMODE HOOKS ****************************************************************/
  73.  
  74. // Give experience/level up on hit an npc or player
  75. function AddEXP( ply, hitgroup, dmginfo )
  76.         local attacker = dmginfo:GetAttacker()
  77.         if attacker:IsPlayer() then
  78.                 if EXP_STOPATMAX == 1 and attacker:GetNWInt("CurLevel") == levelups[table.Count(levelups)] then return end
  79.                 attacker:SetNWInt("Exp",attacker:GetNWInt("Exp")+EXP_INC)
  80.                
  81.                 if table.HasValue(levelups,attacker:GetNWInt("Exp")) then
  82.                         local leveln = 1
  83.                         for k,v in ipairs(levelups) do if v == attacker:GetNWInt("Exp") then leveln = k end end
  84.                         if leveln == table.Count(levelups) then PrintAll(attacker:Name().." has reached the maximum level!")
  85.                         else PrintAll(attacker:GetName().." has now reached level "..leveln.."!") end
  86.                         attacker:SetNWInt("NextLevel",levelups[leveln+1])
  87.                         attacker:SetNWInt("CurLevel",levelups[leveln])
  88.                         attacker:SetNWInt("LevelNum",leveln)
  89.                         attacker:SendLua("surface.PlaySound(\"achievements/achievement_earned.mp3\")")
  90.                 end
  91.                
  92.         end
  93. end
  94.        
  95.        
  96.        
  97. // Check if we want to use NPCs or Players as the object of exp
  98. if EXP_USENPC == 1 then hook.Add("ScaleNPCDamage","NPCExp",AddEXP)
  99. elseif EXP_USENPC == 2 then
  100.         hook.Add("ScaleNPCDamage","NPCExp",AddEXP)
  101.         hook.Add("ScalePlayerDamage","PlayerExp",AddEXP)
  102. else hook.Add("ScalePlayerDamage","PlayerExp",AddEXP) end
  103.  
  104.  
  105.  
  106. // Take away experience on death
  107. hook.Add("PlayerDeath","DownEXP",function(ply,wep,killer)
  108.         local exp = ply:GetNWInt("Exp")
  109.         local loss = math.Round(exp-exp*EXP_DEATHLOSS)
  110.         if loss < 0 then ply:SetNWInt("Exp",0)
  111.         elseif loss < ply:GetNWInt("CurLevel") then ply:SetNWInt("Exp",ply:GetNWInt("CurLevel"))
  112.         else ply:SetNWInt("Exp",loss) end
  113. end)
  114.  
  115.  
  116.  
  117. // Save the values in the SQL on disconnect
  118. hook.Add("PlayerDisconnected","UpdateEXP",function(ply)
  119.         local exp = ply:GetNWInt("Exp")
  120.         local cur = ply:GetNWInt("CurLevel")
  121.         local new = ply:GetNWInt("NextLevel")
  122.         local lvl = ply:GetNWInt("LevelNum")
  123.         if EXP_USETXT == 1 then WriteData(ply)
  124.         else
  125.                 sql.Begin()
  126.                 sql.Query("UPDATE exp_pdata SET exp = "..exp.." WHERE steam = "..sql.SQLStr(ply:SteamID())..";")
  127.                 sql.Query("UPDATE exp_pdata SET curlevel = "..cur.." WHERE STEAM = "..sql.SQLStr(ply:SteamID())..";")
  128.                 sql.Query("UPDATE exp_pdata SET nextlevel = "..new.." WHERE STEAM = "..sql.SQLStr(ply:SteamID())..";")
  129.                 sql.Query("UPDATE exp_pdata SET levelnum = "..lvl.." WHERE STEAM = "..sql.SQLStr(ply:SteamID())..";")
  130.                 sql.Commit()
  131.         end
  132. end)
  133.  
  134.  
  135.  
  136. // Load or create the player's values on initial connection
  137. hook.Add("PlayerInitialSpawn","RegisterEXP",function(ply)
  138.         if not tonumber(sql.QueryValue("SELECT exp FROM exp_pdata WHERE steam =  "..sql.SQLStr(ply:SteamID())..";")) then
  139.                 sql.Query("INSERT INTO exp_pdata VALUES("..sql.SQLStr(ply:SteamID())..",".. 0 ..",".. 0 ..","..levelups[2]..",".. 1 ..");")
  140.                 ply:SetNWInt("Exp",0)
  141.                 ply:SetNWInt("CurLevel",0)
  142.                 ply:SetNWInt("NextLevel",levelups[2])
  143.                 ply:SetNWInt("LevelNum",1)
  144.         else
  145.         sql.Begin()
  146.         ply:SetNWInt("Exp",tonumber(sql.QueryValue("SELECT exp FROM exp_pdata WHERE steam =  "..sql.SQLStr(ply:SteamID())..";")))
  147.         ply:SetNWInt("CurLevel",tonumber(sql.QueryValue("SELECT curlevel FROM exp_pdata WHERE steam =  "..sql.SQLStr(ply:SteamID())..";")))
  148.         ply:SetNWInt("NextLevel",tonumber(sql.QueryValue("SELECT nextlevel FROM exp_pdata WHERE steam =  "..sql.SQLStr(ply:SteamID())..";")))
  149.         ply:SetNWInt("LevelNum",tonumber(sql.QueryValue("SELECT levelnum FROM exp_pdata WHERE steam =  "..sql.SQLStr(ply:SteamID())..";")))
  150.         sql.Commit()
  151.         end
  152. end)
  153.  
  154.  
  155.  
  156. /********************************************************** CONCOMMANDS **********************************************************************/
  157.  
  158. local chatcommands = {}
  159.  
  160. // Allow admins to change the exp level of players
  161. concommand.Add("exp_setlevel",function(ply,cmd,args)
  162.         if ply:IsAdmin() then
  163.                 leveln = tonumber(args[2])
  164.                 local tar
  165.                 for _,v in ipairs(player.GetAll()) do if string.match(v:Name(),args[1]) then tar = v end end
  166.                
  167.                 if not tar then ply:ChatPrint("Please use a valid target! (format <player name> <level>") return end
  168.                 if leveln < 1 or leveln > (table.Count(levelups)+1) then ply:ChatPrint("Please select a valid level between 1 - ".. table.Count(levelups) .. ".") return end
  169.                
  170.                 tar:SetNWInt("Exp",levelups[leveln])
  171.                 tar:SetNWInt("CurLevel",levelups[leveln])
  172.                 if leveln == table.Count(levelups) then tar:SetNWInt("NextLevel",levelups[leveln])
  173.                 else tar:SetNWInt("NextLevel",levelups[leveln+1]) end
  174.                 ply:SetNWInt("LevelNum",leveln)
  175.                
  176.                 ply:ChatPrint("You set "..tar:Name().."'s level to "..leveln..".")
  177.         end
  178. end)
  179. table.insert(chatcommands,{"setlevel","exp_setlevel"})
  180.  
  181.  
  182. // Allow admins to change the exp increment per hit via console
  183. concommand.Add("exp_setinc",function(ply,cmd,args)
  184.         if ply:IsAdmin() then
  185.                 local inc = tonumber(args[1])
  186.                 if inc < 1 then ply:ChatPrint("Please use a positive integer above zero.") return end
  187.                 EXP_INC = tonumber(args[1])
  188.         end
  189. end)
  190. table.insert(chatcommands,{"setinc","exp_setinc"})
  191.  
  192.  
  193.  
  194. // Allow anyone to check their current level
  195. concommand.Add("exp_curlevel",function(ply,cmd,args)
  196.         for k,v in ipairs(levelups) do if ply:GetNWInt("CurLevel") == v then ply:ChatPrint("You are on level "..k..".") end end
  197. end)
  198. table.insert(chatcommands,{"curlevel","exp_curlevel"})
  199.  
  200.  
  201. // Allow admins to save everyone's data in the SQL
  202. concommand.Add("exp_savedata",function(ply,cmd,args)
  203.         if ply:IsAdmin() then
  204.                 sql.Begin()
  205.                 for _,v in pairs(player.GetAll()) do
  206.                         local exp = v:GetNWInt("Exp")
  207.                         local cur = v:GetNWInt("CurLevel")
  208.                         local new = v:GetNWInt("NextLevel")
  209.                         local lvl = v:GetNWInt("LevelNum")
  210.                         if EXP_USETXT == 1 then WriteData(v)
  211.                         else
  212.                                 sql.Query("UPDATE exp_pdata SET exp = "..exp.." WHERE steam = "..sql.SQLStr(ply:SteamID())..";")
  213.                                 sql.Query("UPDATE exp_pdata SET curlevel = "..cur.." WHERE STEAM = "..sql.SQLStr(ply:SteamID())..";")
  214.                                 sql.Query("UPDATE exp_pdata SET nextlevel = "..new.." WHERE STEAM = "..sql.SQLStr(ply:SteamID())..";")
  215.                                 sql.Query("UPDATE exp_pdata SET levelnum = "..lvl.." WHERE STEAM = "..sql.SQLStr(ply:SteamID())..";")
  216.                         end
  217.                 end
  218.                 sql.Commit()
  219.                 ply:ChatPrint("Data saved!")
  220.         end
  221. end)
  222. table.insert(chatcommands,{"savedata","exp_savedata"})
  223.  
  224.  
  225. // Just in case the SQL fails, let's check why
  226. concommand.Add("exp_debug",function(ply,cmd,args) if sql.LastError() then ply:ChatPrint(sql.LastError()) else ply:ChatPrint("No SQL errors!") end end)
  227. table.insert(chatcommands,{"debug","exp_debug"})
  228.  
  229.  
  230. // Get the data from the last save
  231. concommand.Add("exp_printsql",function(ply,cmd,args)
  232.         ply:ChatPrint("Data from previous save...")
  233.         ply:ChatPrint("Experience: "..tonumber(sql.QueryValue("SELECT exp FROM exp_pdata WHERE steam =  "..sql.SQLStr(ply:SteamID())..";")))
  234.         ply:ChatPrint("Base Level Experience: "..tonumber(sql.QueryValue("SELECT curlevel FROM exp_pdata WHERE steam =  "..sql.SQLStr(ply:SteamID())..";")))
  235.         ply:ChatPrint("Next Level: "..tonumber(sql.QueryValue("SELECT nextlevel FROM exp_pdata WHERE steam =  "..sql.SQLStr(ply:SteamID())..";")))
  236.         ply:ChatPrint("Level Number: "..tonumber(sql.QueryValue("SELECT levelnum FROM exp_pdata WHERE steam =  "..sql.SQLStr(ply:SteamID())..";")))
  237. end)
  238. table.insert(chatcommands,{"printsql","exp_printsql"})
  239.  
  240.  
  241. // Get the data from right now
  242. concommand.Add("exp_printnw",function(ply,cmd,args)
  243.         ply:ChatPrint("Data at this moment...")
  244.         ply:ChatPrint("Experience: "..ply:GetNWInt("Exp"))
  245.         ply:ChatPrint("Base Level Experience: "..ply:GetNWInt("CurLevel"))
  246.         ply:ChatPrint("Next Level: "..ply:GetNWInt("NextLevel"))
  247.         ply:ChatPrint("Level Number: "..ply:GetNWInt("LevelNum"))
  248. end)
  249. table.insert(chatcommands,{"printnw","exp_printnw"})
  250.  
  251.  
  252. /******************************************************** CHAT COMMANDS **********************************************************************/
  253.  
  254.  
  255. // So the chatcommands will work
  256. hook.Add("PlayerSay","EXP_Chat",function(ply,str,toall)
  257.         if string.Left(str,2) != "**" then return str end
  258.         local cmd = string.Explode(" ",string.sub(str,3,string.len(str)))
  259.         local cmdstr = ""
  260.         for k,v in ipairs(cmd) do if k != 1 then cmdstr = cmdstr .. " ".. v end end
  261.         for _,v in ipairs(chatcommands) do
  262.                 if cmd[1] == v[1] then ply:ConCommand(v[2].." "..cmdstr) end
  263.         end
  264.         return ""
  265. end)
  266.  

Thanks !

Edit by Megiddo: Added code tags
« Last Edit: May 27, 2012, 03:22:05 pm by Megiddo »

Offline Megiddo

  • Ulysses Team Member
  • Hero Member
  • *****
  • Posts: 6214
  • Karma: 394
  • Project Lead
Re: Function Add Things By Time ( Megiddo HELP Please Or Who Know )
« Reply #1 on: May 27, 2012, 03:23:03 pm »
Please post code related things in the developer section and put code in code tags.

You can use the online time API provided by UTime for this purpose...
Experiencing God's grace one day at a time.

  • Print