I am making a browser-based game using ruby on rails and I am having
trouble implementing a ranking system for my game. Each player has
five skills: intelligence, creativity, strength, charisma and
technological. I want the player to have a rank for each skill. The
ranking I have in place right now gives the same rank if two players
have the same amount of skill.
SELECT COUNT(*) AS [rank] FROM players "+
"WHERE intelligence >= "+
"(SELECT intelligence FROM players WHERE id = #{@player.id})
I want each player to have a different rank even if they have the same
amount of skill as another player but I can’t seem to figure out how
to implement that. I have an intelligence column and an irank column,
the same for the rest of the skills, and I was thinking that I could
first sort the intelligence column and then according to that sorting
assign a number to the irank column from 1 and so on but I can’t
figure out how to exactly implement this. I was thinking maybe a for
loop or a .each do loop but I hit a roadblock. I know I can do
Player.all(:order => ‘intelligence DESC’) to sort it but after that
I’m stumped.
I would appreciate any hints or help that anyone could provide me if
willing. Thanks.
what if:
ranked_players = Array.new
Player.all(:order => ‘intelligence’).each_with_index do |player|
ranked_players << “#{index + 1}. #{player[:name]} with
#{player[:intelligence]} intelligence”
end
#now you have ranked players list
ranked_players.each do |player|
puts player
end
hey, so I put the code in my website and this is what it outputted for
me. I used ranked_players[0] and so on to get the output.
nascargurly with 30 intelligence 1. nascargurly48 with 30
creativity 1. nascargurly48 with 10 strength 1. nascargurly48 with 10
charisma 1. nascargurly48 with 30 technological
2 2. nascargurly48 with 90 intelligence 2. nascargurly with 30
creativity 2. nascargurly with 10 strength 2. nascargurly with 10
charisma 2. nascargurly with 90 technological
It shows nascargurly with 30 intelligence higher than nascargurly48
with 90 intelligence when it should be the other way around. Its like
its ordering by the way things are added to array then by the order of
intelligence. I added nascargurly to the website after nascargurly48.
the command prompt says its ordering by intelligence but the array
doesnt show it. Any ideas?
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.