Guest User

Untitled

a guest
Apr 9th, 2010
1,221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.61 KB | None | 0 0
  1. local availableID = { }   -- Create the table that specifies which id's are available.
  2. local scoreboard
  3. local _dxver
  4. local _initp = true
  5. local _loaded = false
  6.  
  7. function Init ()
  8.     if ( getResourceFromName ( "scoreboard" ) and getResourceState ( getResourceFromName ( "scoreboard" ) ) == "running" ) then
  9.         scoreboard = exports.scoreboard
  10.         _dxver = false
  11.     elseif ( getResourceFromName ( "dxscoreboard" ) and getResourceState ( getResourceFromName ( "dxscoreboard" ) ) == "running" ) then    
  12.         scoreboard = exports.dxscoreboard
  13.         _dxver = true
  14.     else
  15.         outputDebugString ( "No scoreboard resource has been started", 2 )
  16.         _initp = false
  17.     end
  18.     if _initp then
  19.         scoreboard:addScoreboardColumn ( "ID", getRootElement(), 1, 0.05 ) -- Add the ID column
  20.         sortScoreboard( "ID" )
  21.     end
  22.     if not _loaded then
  23.         loadID()
  24.     end
  25. end
  26. addEventHandler ( "onResourceStart", resourceRoot, Init )
  27.  
  28. function loadID ()
  29.     _loaded = true
  30.     local max_players = getMaxPlayers()
  31.     for i = 1, max_players do
  32.         table.insert ( availableID, i, true )  -- Populate the table.
  33.     end
  34.     for _, player in ipairs ( getElementsByType ( "player" ) ) do
  35.         assignPlayerID ( player ) -- Assign an id to all players.
  36.     end
  37. end
  38.  
  39. function checkStoppedResource ( resource )
  40.     local rname = getResourceName ( resource )
  41.     local sres = tostring ( scoreboard ):gsub( "exports.","" )
  42.     if ( rname:lower() == sres:lower() ) then
  43.         outputChatBox ( "falsed" )
  44.         _initp = false
  45.     end
  46. end
  47. addEventHandler ( "onResourceStop", getRootElement(), checkStoppedResource )
  48.  
  49. function checkStartedResource ( resource )
  50.     if ( getResourceName ( resource ) == "scoreboard" or "dxscoreboard" and source ~= getResourceRootElement() ) then
  51.         Init()
  52.     end
  53. end
  54. addEventHandler ( "onResourceStart", getRootElement(), checkStartedResource )--]]
  55.  
  56. function sortScoreboard( column )
  57.     if _dxvera and _initp then
  58.         scoreboard:scoreboardSetSortBy ( column, true, getRootElement() )
  59.     end
  60. end
  61.  
  62. function onJoin () -- Player joined --> assign ID.
  63.     assignPlayerID ( source )
  64. end
  65. addEventHandler ( "onPlayerJoin", getRootElement(), onJoin)
  66.  
  67. function assignPlayerID ( player )
  68.     local s_id = 1 -- start at one
  69.     while ( isIDAvailable ( s_id ) == false ) do -- is it available? if not, +1 & repeat.
  70.         s_id = s_id + 1
  71.     end
  72.     setElementData ( player, "ID", s_id ) -- available -> assign the ID to the player.
  73.     sortScoreboard( "ID" )
  74.     availableID[ s_id ] = false -- set the id as not available.
  75. end    
  76.    
  77. function isIDAvailable ( id ) -- checks if the id is used or not.
  78.     return availableID[id]
  79. end
  80.  
  81. function onLeave () -- player left, remove ID, set as available.
  82.     local s_id = getElementData ( source, "ID" )
  83.     availableID[ s_id ] = true
  84.     removeElementData ( source, "ID" ) 
  85. end
  86. addEventHandler ( "onPlayerQuit", getRootElement(), onLeave )      
  87.  
  88. function removeData ( ) -- resource stopped, clear everything.
  89.     for k, players in ipairs ( getElementsByType ( "player" )) do
  90.         removeElementData ( players, "ID" )
  91.     end
  92.     availableID = { }
  93. end
  94. addEventHandler ( "onResourceStop", resourceRoot, removeData )
  95.  
  96. function getNameMatches ( player_name )
  97. i = 1
  98. local matchingPlayers = { }
  99.     if ( player_name ) then
  100.         for k, player in ipairs ( getElementsByType ( "player" ) ) do
  101.             local player_n = getPlayerName ( player )
  102.             local match = string.find ( string.lower( player_n ), string.lower ( player_name ) )
  103.             if ( match ) then
  104.                 table.insert ( matchingPlayers, i, player )
  105.                 i = i + 1
  106.             end
  107.         end
  108.         if ( #matchingPlayers == 1 ) then
  109.             return true, matchingPlayers[1]
  110.         elseif ( #matchingPlayers > 1 ) then
  111.             return true, matchingPlayers
  112.         else
  113.             return false, "None"
  114.         end
  115.     else
  116.         return false, "Please enter a player name"
  117.     end
  118. end    
  119.  
  120. function getPlayerFromPartialName ( source, player_name, script )
  121.     if ( player_name ) then
  122.         local sucess, value = getNameMatches ( player_name )
  123.             if ( sucess ) then
  124.                 local matches = ( type ( value ) == "table" ) and #value or 1
  125.                 if ( matches == 1 ) then
  126.                     if ( script ) then return value else
  127.                         local player_nick = getPlayerName ( value )
  128.                         local player_id = getElementData ( value, "ID" )
  129.                         outputChatBox ( "(" .. player_id .. ") " .. player_nick, source, 255, 255, 0 )
  130.                     end
  131.                 else   
  132.                     outputChatBox ( "Players matching your search are: ", source, 255, 255, 0 )
  133.                     for k, player in ipairs ( value ) do
  134.                         local player_nick = getPlayerName ( value[k] )
  135.                         local player_id = getElementData ( value[k], "ID" )
  136.                         outputChatBox ( "(" .. player_id .. ") " .. player_nick, source, 255, 255, 0 )
  137.                     end
  138.                     return true, true
  139.                 end    
  140.             else
  141.                 if ( script ) then return false else
  142.                     outputChatBox ( "Players matching your search are: ", source, 255, 255, 0 )
  143.                     outputChatBox ( value, source, 255, 0, 0 )
  144.                 end
  145.             end
  146.     end
  147. end
  148.  
  149. function getPlayerFromID ( id )
  150.     for k, player in ipairs ( getElementsByType ( "player" ) ) do
  151.         local p_id = getElementData ( player, "ID" )
  152.         if ( p_id == tonumber(id) ) then
  153.             player_n = getPlayerName ( player )
  154.             return player, player_n
  155.         end
  156.     end
  157.         return false, "No player has been found with the ID " .. id .. "."
  158. end
  159.  
  160. function getPlayer ( source, input )
  161.     if ( tonumber ( input ) ) then
  162.         local player, playername = getPlayerFromID ( tonumber(input) )
  163.         if ( player ) then
  164.             return player
  165.         else
  166.             outputChatBox ( playername, source, 255, 0, 0 ) -- in this case, playername carries the error. It's just to minimize the amount of code if an error is ecountered.
  167.             return false
  168.         end
  169.     else
  170.         local player, multiple = getPlayerFromPartialName ( source, input, true )
  171.         if ( player and not multiple ) then
  172.             return player
  173.         elseif ( player and multiple ) then
  174.             return false   
  175.         else
  176.             outputChatBox ( "Player not found", source, 255, 0, 0 )
  177.             return false
  178.         end
  179.     end
  180. end
  181.  
  182. function processIDCommands ( source, command, input )
  183.     if ( tonumber ( input ) ) then
  184.         local player, playername = getPlayerFromID ( tonumber(input) )
  185.             if ( player ) then
  186.                 outputChatBox ( "Player that matches that id: ", source, 255, 255, 0 )
  187.                 outputChatBox ( "(" .. input .. ") " .. playername, source, 255, 255, 0, true )
  188.             else
  189.                 outputChatBox ( playername, source, 255, 0, 0 ) -- in this case, playername carries the error. It's just to minimize the amount of code if an error is ecountered.
  190.             end
  191.     else
  192.         local player = getPlayer ( source, input )
  193.         if ( player ) then
  194.             outputChatBox ( "Player that matches that id: ", source, 255, 255, 0 )
  195.             outputChatBox ( "(" .. tostring ( getElementData ( player, "ID" ) ) .. ") " .. getPlayerName ( player ), source, 255, 255, 0, true )
  196.         end
  197.     end
  198. end    
  199. addCommandHandler ( "id", processIDCommands )
Advertisement
Add Comment
Please, Sign In to add comment