ScriptingExamples/getClientFromText: Difference between revisions

From GTA Connected
Jump to navigation Jump to search
No edit summary
 
(No difference)

Latest revision as of 08:24, 14 August 2018

Description

Fetch a client object from a string of text, checking player IDs and player names, including nearest name match.
This function returns bool false when no match is found.

Code

Lua:

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

JavaScript:

function getClientFromText(text)
{
	var clients = getClients();
	var textLower = text.toLowerCase();
	var textLength = text.length;

	// player id
	var textNumber = parseInt(text, 10);
	if(!isNaN(textNumber))
	{
		for(var i in clients)
		{
			var client = clients[i];
			if(client.player.id == textNumber)
			{
				return client;
			}
		}
	}

	// player full name (case-insensitive)
	for(var i in clients)
	{
		var client = clients[i];
		if(client.player.name.toLowerCase() == textLower)
		{
			return client;
		}
	}

	// player partial name (case-insensitive)
	for(var i in clients)
	{
		var client = clients[i];
		if(client.player.name.toLowerCase().indexOf(textLower) != -1)
		{
			return client;
		}
	}

	// nearest player name match (case-insensitive)
	var charMatchCounts = [];
	for(var i in clients)
	{
		var client = clients[i];
		var playerNameChars = [];
		var playerNameLower = client.player.name.toLowerCase();
		var playerNameLength = client.player.name.length;
		for(var i2=1; i2<=playerNameLength; i2++)
		{
			playerNameChars.push(playerNameLower.substr(i2 - 1, 1));
		}
		var charMatchCount = 0;
		var playerNameCharIndex = null;
		for(var i2=1; i2<=textLength; i2++)
		{
			playerNameCharIndex = null;
			var textChar = textLower.substr(i2 - 1, 1);
			for(var i3 in playerNameChars)
			{
				var playerNameChar = playerNameChars[i3];
				if(textChar == playerNameChar)
				{
					charMatchCount++;
					playerNameCharIndex = i3;
					break;
				}
			}
			if(playerNameCharIndex != null)
			{
				playerNameChars.splice(playerNameCharIndex, 1);
			}
		}
		charMatchCounts[i] = [ client, charMatchCount / playerNameLength ];
	}
	var highestCharMatchCount = 0;
	var highestCharMatchCountClient;
	for(var i in charMatchCounts)
	{
		var data = charMatchCounts[i];
		if(data[2] > highestCharMatchCount)
		{
			highestCharMatchCount = data[2];
			highestCharMatchCountClient = data[1];
		}
	}
	if(highestCharMatchCountClient)
	{
		return highestCharMatchCountClient;
	}

	// no match
	return false;
}