Make a Ranked Elo System

First, here is the table we want to sort from.

local EloRankingWithStrings = {
  { elo = 7, name = "Player 1" },
  { elo = 51, name = "Player 2" },
  { elo = 2532, name = "Player 3" },
  { elo = 612631, name = "Player 4" },
  { elo = 3162, name = "Player 5" },
  { elo = 315132, name = "Player 6" }
}

Then, we make a function to sort the Elos from lowest to highest.


function GetSimilarElos(EloRanking)
  -- Sort from lowest to highest Elo rating
  table.sort(EloRanking, function(a, b)
    return a.elo < b.elo
  end)

Then, we create a new table for the players with similar Elos.

 -- Create a new table for the players with similar Elos
  local SortedSimilarElos = {}
-- Iterate over the sorted table with a step size of 2
  for i = 1, #EloRanking, 2 do
    local pair = {}
    -- Create a pair by taking the current and next elements

    for j = 1, 2 do
      if EloRanking[i + j - 1] then
        table.insert(pair, EloRanking[i + j - 1])
      end
    end
    -- Insert the pair into the SortedSimilarElos table

    table.insert(SortedSimilarElos, pair)
  end

  return SortedSimilarElos
end

Lastly, we read out the table nd teleport the players into a ranked game.


local SortedSimilarElos = GetSimilarElos(EloRankingWithStrings)

for _, pair in ipairs(SortedSimilarElos) do
	for _, player in ipairs(pair) do
		print(player.name, player.elo)
	end
	TeleportPlayersToGame(pair[1], pair[2])
	print("---")
end

:wink:

3 Likes