GuildWars Question

02/15/2012 12:07 I don't have a username#1
I'm selecting the top 5 for the score like below:
Code:
        private static Dictionary<byte, string> SelectTopFive()
        {
            Dictionary<byte, string> Select = new Dictionary<byte, string>();
            int count = (Damage.Count > 0) ? (Damage.Count > 5) ? 5 : Damage.Count : 0;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeyValuePair<uint, GuildWarsGuild> sel =
                        Damage.Aggregate((l, r) => l.Value.Score > r.Value.Score ? l : r);
                    Select.Add((byte)i, sel.Value.Guild.Name + ": " + sel.Value.Score);
                }
            }
            return Select;
        }
This is the Damage Dictionary.
Code:
//guildid, gwguild
public static Dictionary<uint, GuildWarsGuild> Damage = new Dictionary<uint, GuildWarsGuild>();
GuildWarsGuild is just containing 2 variables. uint as damage and guild as guild.

It does work, but my question is would there be any better ways to get the top five?

Thanks.
02/15/2012 16:13 pro4never#2
..... and what happens when two guilds have the same score? Do you throw an exception, throw away one record... what?


I'd swap it around so that you have a dictionary holding all Guilds which have competed in the current round... then have their value be damage. Sort by value and display up to top 5 in scoreboard. When pole dies just take top entry from that dict as winner.
02/15/2012 17:47 I don't have a username#3
Quote:
Originally Posted by pro4never View Post
..... and what happens when two guilds have the same score? Do you throw an exception, throw away one record... what?


I'd swap it around so that you have a dictionary holding all Guilds which have competed in the current round... then have their value be damage. Sort by value and display up to top 5 in scoreboard. When pole dies just take top entry from that dict as winner.
It can never throw an exception. It returns the last added.

if you eg. adds 3 values with 1000000.
a 1000000
b 1000000
c 1000000

Output will be:
Code:
c
b
a
If a and c is 1000000 and b 500000, then output would be.
Code:
c
a
b
:)

--
Changed the method a bit, because it could be slightly better xD
02/15/2012 18:20 -impulse-#4
Interesting method. (Though not working, at least not the way you use it.)
I got a couple of questions:
1. Why do you do:
Code:
int count = (Damage.Count > 0) ? (Damage.Count > 5) ? 5 : Damage.Count : 0;
You don't need at least 5 competitors to be able to chose the top 5(at least one is needed to be able to win the GW war).
2. Why use Linq.Aggregate?
I mean it just gives one value, one at a time using the given compare function, but, the way you do it, it will give you the same result all the time (as in one value only...).
I, myself, use the SortedDictionary but you can use directly Damage.OrderBy(p => p.Value.Score);
PHP Code:
private static List<stringSelectTopFive()
{
    List<
string> List = new List<string>();
    var array = 
Damage.OrderBy(=> p.Value.Score);
    for (
int i 0Math.Min(array.Length5); i++)
        List.
Add(array[i].Value.Guild.Name);
    return List;

02/15/2012 18:25 I don't have a username#5
Quote:
Originally Posted by -impulse- View Post
...
I love you Impulse lmfao.
02/15/2012 18:39 .Kinshi#6
Top 5:
Code:
var sorted = (from g in GuildDamages.Values
    orderby g.Points descending
    select g).Take(5);
02/15/2012 18:57 I don't have a username#7
Quote:
Originally Posted by .Kinshi View Post
Top 5:
Code:
var sorted = (from g in GuildDamages.Values
    orderby g.Points descending
    select g).Take(5);
Even better ;o I should do some better research.