Advertisement
aLTis

PlayerOfTheDay edit2

Apr 29th, 2016
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.65 KB | None | 0 0
  1. -- Player of the Day by 002, edited by aLTis (altis94@gmail.com)
  2.  
  3. --  Change log:
  4. --  2016-04-26: messages are now sent to the console instead of chat
  5. --  2016-04-29: added STAT_TIME variable, the console messages will be sent every second for that amount of time
  6.  
  7. -- Frequency to display these messages in X seconds...
  8. STAT_FREQUENCY = 60
  9.  
  10. -- Time that messages stay on screen in seconds (it will stay a bit longer than this value depending on player's fps)
  11. STAT_TIME = 6
  12.  
  13. -- These are the messages that will be displayed every STAT_FREQUENCY seconds:
  14. --  $name       - This is substituted for the player's name.
  15. --  $stat       - This is substituted for the relevant stat.
  16. --  $pl_s       - This is substituted for a plural "s" when $stat is not 1.
  17. --  $pl_es      - This is substituted for a plural "es" when $stat is not 1.
  18. MOST_KILLS_TEXT     = "The player with the most kills today is $name ($stat kill$pl_s)"
  19. MOST_KILLS_ENABLED  = true
  20. MOST_FLAGS_TEXT     = "The player with the most flag captures today is $name ($stat cap$pl_s)"
  21. MOST_FLAGS_ENABLED  = true
  22. MOST_DEATHS_TEXT    = "The player with the most deaths today is $name ($stat death$pl_s)"
  23. MOST_DEATHS_ENABLED = true
  24.  
  25. -- End of configuration
  26.  
  27. api_version = "1.9.0.0"
  28.  
  29. stats = {}
  30. stat_timer = 0
  31. last_date = 0
  32.  
  33. function ClearConsole(i)--                  Clears player's console from any messages
  34.     for j=1,30 do
  35.         rprint(i," ")
  36.     end
  37. end
  38.  
  39. function OnScriptLoad()
  40.     if MOST_KILLS_ENABLED then
  41.         register_callback(cb["EVENT_KILL"],"OnKill")
  42.     end
  43.     if MOST_DEATHS_ENABLED then
  44.         register_callback(cb["EVENT_DIE"],"OnDie")
  45.     end
  46.     if MOST_FLAGS_ENABLED then
  47.         register_callback(cb["EVENT_SCORE"],"OnScore")
  48.     end
  49.     ResetStats()
  50.     timer(1000 * STAT_FREQUENCY,"CheckStats", STAT_TIME)
  51. end
  52.  
  53. function CheckStats(stat_time_left)
  54.     stat_time_left = (stat_time_left - 1)
  55.     for i=1,16 do
  56.         ClearConsole(i)
  57.     end
  58.     if MOST_KILLS_ENABLED then
  59.         local name,stat = BestOfStat("kills")
  60.         if name ~= nil then
  61.             execute_command("rprint * \""..FormatString(MOST_KILLS_TEXT,name,stat))
  62.         end
  63.     end
  64.     if MOST_DEATHS_ENABLED then
  65.         local name,stat = BestOfStat("deaths")
  66.         if name ~= nil then
  67.             execute_command("rprint * \""..FormatString(MOST_DEATHS_TEXT,name,stat))
  68.         end
  69.     end
  70.     if MOST_FLAGS_ENABLED then
  71.         local name,stat = BestOfStat("caps")
  72.         if name ~= nil then
  73.             execute_command("rprint * \""..FormatString(MOST_FLAGS_TEXT,name,stat))
  74.         end
  75.     end
  76.     if os.date("*t",os.time()).day ~= last_date then
  77.         ResetStats()
  78.     end
  79.     if(stat_time_left > 0) then
  80.         timer(1000 ,"CheckStats", stat_time_left)
  81.         return false
  82.     end
  83.     timer(1000 * STAT_FREQUENCY - STAT_TIME,"CheckStats", STAT_TIME)
  84. end
  85.  
  86. function ResetStats()
  87.     stats = {
  88.         ["kills"] = {},
  89.         ["deaths"] = {},
  90.         ["caps"] = {}
  91.     }
  92.     last_date = os.date("*t",os.time()).day
  93. end
  94.  
  95. function BestOfStat(StatName)
  96.     local stat = 0
  97.     local name = nil
  98.     for k,v in pairs(stats[StatName]) do
  99.         if v.stat > stat then
  100.             stat = v.stat
  101.             name = v.name
  102.         end
  103.     end
  104.     if stat == 0 then return nil,nil end
  105.     return name,stat
  106. end
  107.  
  108. function RegisterStat(PlayerHash,PlayerName,StatName,StatCount)
  109.     for k,v in pairs(stats[StatName]) do
  110.         if PlayerHash == k then
  111.             stats[StatName][k].stat = stats[StatName][k].stat + StatCount
  112.             stats[StatName][k].name = PlayerName
  113.             return
  114.         end
  115.     end
  116.     stats[StatName][PlayerHash] = {
  117.         ["name"] = PlayerName,
  118.         ["stat"] = StatCount
  119.     }
  120. end
  121.  
  122. function OnScriptUnload()
  123. end
  124.  
  125. function FormatString(BaseString,Name,Stat)
  126.     local formatted_string = BaseString
  127.     if Stat == 1 then
  128.         formatted_string = string.gsub(formatted_string,"$pl_s","")
  129.         formatted_string = string.gsub(formatted_string,"$pl_es","")
  130.     else
  131.         formatted_string = string.gsub(formatted_string,"$pl_s","s")
  132.         formatted_string = string.gsub(formatted_string,"$pl_es","es")
  133.     end
  134.     formatted_string = string.gsub(formatted_string,"$stat",Stat)
  135.     formatted_string = string.gsub(formatted_string,"$name",Name)
  136.     return formatted_string
  137. end
  138.  
  139. function OnKill(PlayerIndex)
  140.     RegisterStat(get_var(PlayerIndex,"$hash"),get_var(PlayerIndex,"$name"),"kills",1)
  141. end
  142.  
  143. function OnDie(PlayerIndex)
  144.     RegisterStat(get_var(PlayerIndex,"$hash"),get_var(PlayerIndex,"$name"),"deaths",1)
  145. end
  146.  
  147. function OnScore(PlayerIndex)
  148.     if get_var(PlayerIndex,"$gt") ~= "ctf" then return end
  149.     RegisterStat(get_var(PlayerIndex,"$hash"),get_var(PlayerIndex,"$name"),"caps",1)
  150. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement