Untitled

From Red Motmot, 10 Years ago, written in Plain Text, viewed 681 times.
URL https://paste.godclan.hu/view/3Zb32I4- Embed
Download Paste or View Raw
  1. --
  2. -- toptimes_server.lua
  3. --
  4.  
  5. SToptimesManager = {}
  6. SToptimesManager.__index = SToptimesManager
  7. SToptimesManager.instances = {}
  8.  
  9.  
  10. ---------------------------------------------------------------------------
  11. -- Server
  12. -- Handle events from Race
  13. --
  14. -- This is the 'interface' from Race
  15. --
  16. ---------------------------------------------------------------------------
  17.  
  18. addEvent('onMapStarting')
  19. addEventHandler('onMapStarting', g_Root,
  20.         function(mapInfo, mapOptions, gameOptions)
  21.                 if g_SToptimesManager then
  22.                         g_SToptimesManager:setModeAndMap( mapInfo.modename, mapInfo.name, gameOptions.statsKey )
  23.                 end
  24.         end
  25. )
  26.     g_Root = getRootElement()
  27.      
  28.     addEvent('onPlayerPickUpRacePickup',true)
  29.     addEventHandler('onPlayerPickUpRacePickup', g_Root,
  30.      function(number, sort, model)
  31.          if sort == "vehiclechange" then
  32.             if model == 425 then
  33.         outputChatBox ("#FF6347[TOPTIMES] #ffffff" .. getPlayerName(source) .. " #ffffffhas got the Hunter", getRoot, 255, 255, 255, true )
  34.            if g_SToptimesManager then
  35.             g_SToptimesManager:playerFinished( source, exports.race:getTimePassed())
  36.         end
  37.        end
  38.       end
  39.      end
  40.     )
  41.  
  42. addEvent('onPlayerFinish')
  43. addEventHandler('onPlayerFinish', g_Root,
  44.         function(rank, time)
  45.                 if g_SToptimesManager then
  46.                         g_SToptimesManager:playerFinished( source, time)
  47.                 end
  48.         end
  49. )
  50.  
  51. addEventHandler('onResourceStop', g_ResRoot,
  52.         function()
  53.                 if g_SToptimesManager then
  54.                         g_SToptimesManager:unloadingMap()
  55.                 end
  56.         end
  57. )
  58.  
  59. addEventHandler('onPlayerQuit', g_Root,
  60.         function()
  61.                 if g_SToptimesManager then
  62.                         g_SToptimesManager:removePlayerFromUpdateList(source)
  63.                         g_SToptimesManager:unqueueUpdate(source)
  64.                 end
  65.         end
  66. )
  67.  
  68. addEventHandler('onResourceStart', g_ResRoot,
  69.         function()
  70.                 local raceInfo = getRaceInfo()
  71.                 if raceInfo and g_SToptimesManager then
  72.                         g_SToptimesManager:setModeAndMap( raceInfo.mapInfo.modename, raceInfo.mapInfo.name, raceInfo.gameOptions.statsKey )
  73.                 end
  74.         end
  75. )
  76.  
  77. function getRaceInfo()
  78.         local raceResRoot = getResourceRootElement( getResourceFromName( "race" ) )
  79.         return raceResRoot and getElementData( raceResRoot, "info" )
  80. end
  81.  
  82. ---------------------------------------------------------------------------
  83. --
  84. -- Events fired from here
  85. --
  86. ---------------------------------------------------------------------------
  87.  
  88. addEvent("onPlayerToptimeImprovement")
  89.  
  90. ---------------------------------------------------------------------------
  91.  
  92.  
  93. ---------------------------------------------------------------------------
  94. --
  95. -- SToptimesManager:create()
  96. --
  97. -- Create a SToptimesManager instance
  98. --
  99. ---------------------------------------------------------------------------
  100. function SToptimesManager:create()
  101.         local id = #SToptimesManager.instances + 1
  102.         SToptimesManager.instances[id] = setmetatable(
  103.                 {
  104.                         id = id,
  105.                         playersWhoWantUpdates   = {},
  106.                         updateQueue                      = {},
  107.                         serviceQueueTimer               = nil,
  108.                         displayTopCount          = 12,          -- Top number of times to display
  109.                         mapTimes                                = nil,          -- SMaptimes:create()
  110.                         serverRevision                  = 0,            -- To prevent redundant updating to clients
  111.                 },
  112.                 self
  113.         )
  114.         SToptimesManager.instances[id]:postCreate()
  115.         return SToptimesManager.instances[id]
  116. end
  117.  
  118.  
  119. ---------------------------------------------------------------------------
  120. --
  121. -- SToptimesManager:destroy()
  122. --
  123. -- Destroy a SToptimesManager instance
  124. --
  125. ---------------------------------------------------------------------------
  126. function SToptimesManager:destroy()
  127.         SToptimesManager.instances[self.id] = nil
  128.         self.id = 0
  129. end
  130.  
  131.  
  132. ---------------------------------------------------------------------------
  133. --
  134. -- SToptimesManager:postCreate()
  135. --
  136. --
  137. --
  138. ---------------------------------------------------------------------------
  139. function SToptimesManager:postCreate()
  140.         cacheSettings()
  141.         self.displayTopCount = g_Settings.numtimes
  142. end
  143.  
  144.  
  145. ---------------------------------------------------------------------------
  146. --
  147. -- SToptimesManager:setModeAndMap()
  148. --
  149. -- Called when a new map has been loaded
  150. --
  151. ---------------------------------------------------------------------------
  152. function SToptimesManager:setModeAndMap( raceModeName, mapName, statsKey )
  153.         outputDebug( 'TOPTIMES', 'SToptimesManager:setModeAndMap ' .. raceModeName .. '<>' .. mapName )
  154.  
  155.         -- Reset updatings from the previous map
  156.         self.playersWhoWantUpdates = {}
  157.         self.updateQueue = {}
  158.         if self.serviceQueueTimer then
  159.                 killTimer(self.serviceQueueTimer)
  160.         end
  161.         self.serviceQueueTimer = nil
  162.  
  163.         -- Remove old map times
  164.         if self.mapTimes then
  165.                 self.mapTimes:flush()   -- Ensure last stuff is saved
  166.                 self.mapTimes:destroy()
  167.         end
  168.  
  169.         -- Get map times for this map
  170.         self.mapTimes = SMaptimes:create( raceModeName, mapName, statsKey )
  171.         self.mapTimes:load()
  172.  
  173.         -- Get the toptimes data ready to send
  174.         self:updateTopText()
  175. end
  176.  
  177.  
  178. ---------------------------------------------------------------------------
  179. --
  180. -- SToptimesManager:unloadingMap()
  181. --
  182. -- Called when unloading
  183. --
  184. ---------------------------------------------------------------------------
  185. function SToptimesManager:unloadingMap()
  186.         if self.mapTimes then
  187.                 self.mapTimes:flush()   -- Ensure last stuff is saved
  188.         end
  189. end
  190.  
  191.  
  192. ---------------------------------------------------------------------------
  193. --
  194. -- SToptimesManager:playerFinished()
  195. --
  196. -- If time is good enough, insert into database
  197. --
  198. ---------------------------------------------------------------------------
  199. function SToptimesManager:playerFinished( player, newTime, dateRecorded )
  200.  
  201.         -- Check if top time recording is disabled for this player
  202.         if getElementData ( player, "toptimes" ) == "off" then
  203.                 return
  204.         end
  205.  
  206.         if not self.mapTimes then
  207.                 outputDebug( 'TOPTIMES', 'SToptimesManager:playerFinished - self.mapTimes == nil' )
  208.                 return
  209.         end
  210.  
  211.         dateRecorded = dateRecorded or getRealDateTimeNowString()
  212.  
  213.         local oldTime   = self.mapTimes:getTimeForPlayer( player )      -- Can be false if no previous time
  214.         local newPos    = self.mapTimes:getPositionForTime( newTime, dateRecorded )
  215.  
  216.         -- See if time is an improvement for this player
  217.         if not oldTime or newTime < oldTime then
  218.  
  219.                 local oldPos    = self.mapTimes:getIndexForPlayer( player )
  220.                 triggerEvent("onPlayerToptimeImprovement", player, newPos, newTime, oldPos, oldTime, self.displayTopCount, self.mapTimes:getValidEntryCount() )
  221.  
  222.                 -- See if its in the top display
  223.                 if newPos <= self.displayTopCount then
  224.                         outputDebug( 'TOPTIMES', getPlayerName(player) .. ' got toptime position ' .. newPos )
  225.                 end
  226.  
  227.                 if oldTime then
  228.                         outputDebug( 'TOPTIMES', getPlayerName(player) .. ' new personal best ' .. newTime .. ' ' .. oldTime - newTime )
  229.                 end
  230.  
  231.                 self.mapTimes:setTimeForPlayer( player, newTime, dateRecorded )
  232.  
  233.                 -- updateTopText if database was changed
  234.                 if newPos <= self.displayTopCount then
  235.                         self:updateTopText()
  236.                 end
  237.         end
  238.  
  239.         outputDebug( 'TOPTIMES', '++ SToptimesManager:playerFinished ' .. tostring(getPlayerName(player)) .. ' time:' .. tostring(newTime) )
  240. end
  241.  
  242.  
  243. ---------------------------------------------------------------------------
  244. --
  245. -- SToptimesManager:updateTopText()
  246. --
  247. -- Update the toptimes client data for the current map
  248. --
  249. ---------------------------------------------------------------------------
  250. function SToptimesManager:updateTopText()
  251.         if not self.mapTimes then return end
  252.         -- Update data
  253.  
  254.         -- Read top rows from map toptimes table and send to all players who want to know
  255.         self.toptimesDataForMap = self.mapTimes:getToptimes( self.displayTopCount )
  256.         self.serverRevision = self.serverRevision + 1
  257.  
  258.         -- Queue send to all players
  259.         for i,player in ipairs(self.playersWhoWantUpdates) do
  260.                 self:queueUpdate(player)
  261.         end
  262. end
  263.  
  264.  
  265. ---------------------------------------------------------------------------
  266. --
  267. -- SToptimesManager:onServiceQueueTimer()
  268. --
  269. -- Pop a player off the updateQueue and send them an update
  270. --
  271. ---------------------------------------------------------------------------
  272. function SToptimesManager:onServiceQueueTimer()
  273.         outputDebug( 'TOPTIMES', 'SToptimesManager:onServiceQueueTimer()' )
  274.         -- Process next player
  275.         if #self.updateQueue > 0 and self.mapTimes then
  276.                 local player = self.updateQueue[1]
  277.                 local playerPosition = self.mapTimes:getIndexForPlayer( player )
  278.                 clientCall( player, 'onServerSentToptimes', self.toptimesDataForMap, self.serverRevision, playerPosition );
  279.         end
  280.         table.remove(self.updateQueue,1)
  281.         -- Stop timer if end of update queue
  282.         if #self.updateQueue < 1 then
  283.                 killTimer(self.serviceQueueTimer)
  284.                 self.serviceQueueTimer = nil
  285.         end
  286. end
  287.  
  288.  
  289. ---------------------------------------------------------------------------
  290. --
  291. -- SToptimesManager:addPlayerToUpdateList()
  292. --
  293. --
  294. --
  295. ---------------------------------------------------------------------------
  296. function SToptimesManager:addPlayerToUpdateList( player )
  297.         if not table.find( self.playersWhoWantUpdates, player) then
  298.                 table.insert( self.playersWhoWantUpdates, player )
  299.                 outputDebug( 'TOPTIMES', 'playersWhoWantUpdates : ' .. #self.playersWhoWantUpdates )
  300.         end
  301. end
  302.  
  303. function SToptimesManager:removePlayerFromUpdateList( player )
  304.         table.removevalue( self.playersWhoWantUpdates, player )
  305. end
  306.  
  307.  
  308. ---------------------------------------------------------------------------
  309. --
  310. -- SToptimesManager:queueUpdate()
  311. --
  312. --
  313. --
  314. ---------------------------------------------------------------------------
  315. function SToptimesManager:queueUpdate( player )
  316.         if not table.find( self.updateQueue, player) then
  317.                 table.insert( self.updateQueue, player )
  318.         end
  319.  
  320.         if not self.serviceQueueTimer then
  321.                 self.serviceQueueTimer = setTimer( function() self:onServiceQueueTimer() end, 100, 0 )
  322.         end
  323. end
  324.  
  325.  
  326. function SToptimesManager:unqueueUpdate( player )
  327.         table.removevalue( self.updateQueue, player )
  328. end
  329.  
  330.  
  331. ---------------------------------------------------------------------------
  332. --
  333. -- SToptimesManager:doOnClientRequestToptimesUpdates()
  334. --
  335. --
  336. --
  337. ---------------------------------------------------------------------------
  338. function SToptimesManager:doOnClientRequestToptimesUpdates( player, bOn, clientRevision )
  339.         outputDebug( 'TOPTIMES', 'SToptimesManager:onClientRequestToptimesUpdates: '
  340.                         .. tostring(getPlayerName(player)) .. '<>' .. tostring(bOn) .. '< crev:'
  341.                         .. tostring(clientRevision) .. '< srev:' .. tostring(self.serverRevision) )
  342.         if bOn then
  343.                 self:addPlayerToUpdateList(player)
  344.                 if clientRevision ~= self.serverRevision then
  345.                         outputDebug( 'TOPTIMES', 'queueUpdate for'..getPlayerName(player) )
  346.                         self:queueUpdate(player)
  347.                 end
  348.         else
  349.                 self:removePlayerFromUpdateList(player)
  350.                 self:unqueueUpdate(player)
  351.         end
  352.  
  353. end
  354.  
  355.  
  356. addEvent('onClientRequestToptimesUpdates', true)
  357. addEventHandler('onClientRequestToptimesUpdates', getRootElement(),
  358.         function( bOn, clientRevision )
  359.                 g_SToptimesManager:doOnClientRequestToptimesUpdates( source, bOn, clientRevision )
  360.         end
  361. )
  362.  
  363.  
  364. ---------------------------------------------------------------------------
  365. --
  366. -- Commands and binds
  367. --
  368. --
  369. --
  370. ---------------------------------------------------------------------------
  371.  
  372. addCommandHandler( "deltime",
  373.         function( player, cmd, place )
  374.                 if not _TESTING and not isPlayerInACLGroup(player, g_Settings.admingroup) then
  375.                         return
  376.                 end
  377.                 outputChatBox( "Top time"..placeText.." from '" .. tostring(row.playerName) .. "' deleted by " .. getPlayerName(player), 255,55,00 )
  378.                 if g_SToptimesManager and g_SToptimesManager.mapTimes then
  379.                         local row = g_SToptimesManager.mapTimes:deletetime(place)
  380.                         if row then
  381.                                 g_SToptimesManager:updateTopText()
  382.                                 local mapName = tostring(g_SToptimesManager.mapTimes.mapName)
  383.                                 local placeText = place and " #" .. tostring(place) or ""
  384.                                 outputChatBox( "Top time"..placeText.." from '" .. tostring(row.playerName) .. "' deleted by " .. getPlayerName(player), 255,55,00 )
  385.                                 outputServerLog( "INFO: Top time"..placeText.." from '" ..tostring(row.playerName).. "' (" ..tostring(row.timeText).. " in " ..mapName.. ") deleted by " .. getAdminNameForLog(player) )
  386.                         end
  387.                 end
  388.         end
  389. )
  390.  
  391.  
  392. ---------------------------------------------------------------------------
  393. --
  394. -- Settings
  395. --
  396. --
  397. --
  398. ---------------------------------------------------------------------------
  399. function cacheSettings()
  400.         g_Settings = {}
  401.         g_Settings.numtimes             = getNumber('numtimes',8)
  402.         g_Settings.startshow    = getBool('startshow',false)
  403.         g_Settings.gui_x                = getNumber('gui_x',0.56)
  404.         g_Settings.gui_y                = getNumber('gui_y',0.02)
  405.         g_Settings.admingroup   = getString("admingroup","Admin")
  406. end
  407.  
  408. -- React to admin panel changes
  409. addEvent ( "onSettingChange" )
  410. addEventHandler('onSettingChange', g_ResRoot,
  411.         function(name, oldvalue, value, playeradmin)
  412.                 outputDebug( 'MISC', 'Setting changed: ' .. tostring(name) .. '  value:' .. tostring(value) .. '  value:' .. tostring(oldvalue).. '  by:' .. tostring(player and getPlayerName(player) or 'n/a') )
  413.                 cacheSettings()
  414.                 -- Update here
  415.                 if g_SToptimesManager then
  416.                         g_SToptimesManager.displayTopCount = g_Settings.numtimes
  417.                         g_SToptimesManager:updateTopText()
  418.                 end
  419.                 -- Update clients
  420.                 clientCall(g_Root,'updateSettings', g_Settings, playeradmin)
  421.         end
  422. )
  423.  
  424. -- New player joined
  425. addEvent('onLoadedAtClient_tt', true)
  426. addEventHandler('onLoadedAtClient_tt', g_Root,
  427.         function()
  428.                 -- Tell newly joined client current settings
  429.                 clientCall(source,'updateSettings', g_Settings)
  430.  
  431.                 -- This could also be the toptimes resource being restarted, so send some mapinfo
  432.                 local raceInfo = getRaceInfo()
  433.                 if raceInfo then
  434.                     triggerClientEvent('onClientSetMapName', source, raceInfo.mapInfo.name )
  435.                 end
  436.         end
  437. )
  438.  
  439.  
  440. ---------------------------------------------------------------------------
  441. -- Global instance
  442. ---------------------------------------------------------------------------
  443. g_SToptimesManager = SToptimesManager:create()

Reply to "Untitled"

Here you can reply to the paste above