Game Command for target player

From GTA Connected
Revision as of 16:56, 3 February 2019 by Mex (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Description

This code adds a green chat message to the chat box for all players, showing the name of the game that a specified player is playing, when typing /game player.
If the specified player is not found, an orange chat message is shown to only the player that typed the command.

Code

Lua, Server-Side:

addCommandHandler('game', function(cmd, args, client)
	local client2 = getClientFromText(args)
	if not client2 then
		messageClient('Player not found', client, COLOUR_ORANGE)
	else
		message(client2.player.name .. ' is playing ' .. getGameNameFromGameId(client2.game), COLOUR_GREEN)
	end
end)

function getGameNameFromGameId(gameId)
	if gameId == GAME_GTA_III then return 'GTA III'
	elseif gameId == GAME_GTA_VC then return 'GTA VC'
	elseif gameId == GAME_GTA_SA then return 'GTA SA'
	elseif gameId == GAME_GTA_UG then return 'GTA Underground'
	elseif gameId == GAME_GTA_IV then return 'GTA IV'
	end
	return 'Unknown Game'
end

function getClientFromText(text)
    local clients = getClients()
    local textLower = text:lower()
    local textLength = text:len()

    -- player id
    local textNumber = tonumber(text, 10)
    if textNumber then
        for i,client in ipairs(clients) do
            if client.player.id == textNumber then
                return client
            end
        end
    end

    -- player full name (case-insensitive)
    for i,client in ipairs(clients) do
        if client.player.name:lower() == textLower then
            return client
        end
    end

    -- player partial name (case-insensitive)
    for i,client in ipairs(clients) do
        if client.player.name:lower():find(textLower, 1, true) then
            return client
        end
    end

    -- nearest player name match (case-insensitive)
    local charMatchCounts = {}
    for i,client in ipairs(clients) do
        local playerNameChars = {}
        local playerNameLower = client.player.name:lower()
        local playerNameLength = client.player.name:len()
        for i2=1, playerNameLength do
            table.insert(playerNameChars, playerNameLower:sub(i2, i2))
        end
        local charMatchCount = 0
        local playerNameCharIndex
        for i2=1, textLength do
            playerNameCharIndex = false
            local textChar = textLower:sub(i2, i2)
            for i3,playerNameChar in ipairs(playerNameChars) do
                if textChar == playerNameChar then
                    charMatchCount = charMatchCount + 1
                    playerNameCharIndex = i3
                    break
                end
            end
            if playerNameCharIndex then
                table.remove(playerNameChars, playerNameCharIndex)
            end
        end
        charMatchCounts[i] = { client, charMatchCount / playerNameLength }
    end
    local highestCharMatchCount = 0
    local highestCharMatchCountClient
    for i,data in ipairs(charMatchCounts) do
        if data[2] > highestCharMatchCount then
            highestCharMatchCount = data[2]
            highestCharMatchCountClient = data[1]
        end
    end
    if highestCharMatchCountClient then
        return highestCharMatchCountClient
    end

    -- no match
    return false
end