Advertisement
aLTis

PlayerOfTheDay edit

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