Template:ScriptingExamples/UtilityCode/JavaScript/getClientFromText: Difference between revisions
No edit summary |
(No difference)
|
Latest revision as of 22:07, 27 October 2018
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; }