|
| 1 | +using System; |
| 2 | +using System.Collections.Concurrent; |
| 3 | +using System.Collections.Generic; |
| 4 | +using System.Data.Entity; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Linq; |
| 7 | +using System.Text; |
| 8 | +using System.Threading; |
| 9 | +using System.Threading.Tasks; |
| 10 | +using ZkData; |
| 11 | + |
| 12 | +namespace Ratings |
| 13 | +{ |
| 14 | + public static class MapRatings |
| 15 | + { |
| 16 | + private static readonly Timer ladderRecalculationTimer = new Timer((t) => { UpdateRatings(); }, null, 15 * 60000, (int)(GlobalConst.LadderUpdatePeriod * 3600 * 1000 + 4242)); |
| 17 | + |
| 18 | + private static ConcurrentDictionary<int, Player> maps = new ConcurrentDictionary<int, Player>(); |
| 19 | + private static ConcurrentDictionary<int, Rating> mapRatings = new ConcurrentDictionary<int, Rating>(); |
| 20 | + private static List<Rating> mapRanking = new List<Rating>(); |
| 21 | + private static int lastPollId = -1; |
| 22 | + private static readonly Rating defaultRating = new Rating(WholeHistoryRating.RatingOffset, float.PositiveInfinity, null, int.MaxValue, 1); |
| 23 | + |
| 24 | + public static void Init() |
| 25 | + { |
| 26 | + |
| 27 | + Task.Factory.StartNew(() => |
| 28 | + { |
| 29 | + UpdateRatings(true); |
| 30 | + }); |
| 31 | + } |
| 32 | + |
| 33 | + public static Rating GetMapRating(int ResourceId) |
| 34 | + { |
| 35 | + Rating rating; |
| 36 | + if (mapRatings.TryGetValue(ResourceId, out rating)) return rating; |
| 37 | + return defaultRating; |
| 38 | + } |
| 39 | + |
| 40 | + public static List<Rating> GetMapRanking() |
| 41 | + { |
| 42 | + return mapRanking; |
| 43 | + } |
| 44 | + |
| 45 | + private static Player GetPlayer(int id) |
| 46 | + { |
| 47 | + lock (maps) |
| 48 | + { |
| 49 | + Player player; |
| 50 | + if (maps.TryGetValue(id, out player)) return player; |
| 51 | + player = new Player(id); |
| 52 | + maps.TryAdd(id, player); |
| 53 | + return player; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + private static void UpdateRatings(bool init = false) |
| 58 | + { |
| 59 | + try |
| 60 | + { |
| 61 | + using (var db = new ZkDataContext()) |
| 62 | + { |
| 63 | + db.MapPollOutcomes.Where(x => x.MapPollID > lastPollId).Include(x => x.MapPollOptions).OrderBy(x => x.MapPollID).AsNoTracking().AsEnumerable().ForEach(poll => |
| 64 | + { |
| 65 | + var opts = poll.MapPollOptions.OrderByDescending(x => x.Votes).ToList(); |
| 66 | + var winners = opts.Where(x => x.Votes == opts[0].Votes); |
| 67 | + var losers = opts.Where(x => x.Votes != opts[0].Votes); |
| 68 | + if (losers.Count() > 0) |
| 69 | + { |
| 70 | + var game = new Game(winners.Select(x => GetPlayer(x.ResourceID)).ToList(), losers.Select(x => GetPlayer(x.ResourceID)).ToList(), true, 0, poll.MapPollID); |
| 71 | + game.whitePlayers.ForEach(x => x.AddGame(game)); |
| 72 | + game.blackPlayers.ForEach(x => x.AddGame(game)); |
| 73 | + } |
| 74 | + lastPollId = poll.MapPollID; |
| 75 | + }); |
| 76 | + } |
| 77 | + if (init) RunIterations(70); |
| 78 | + RunIterations(3); |
| 79 | + using (var db = new ZkDataContext()) |
| 80 | + { |
| 81 | + var newRanking = maps.Values.Select(x => x.days[0]).OrderByDescending(x => x.r).ToList(); |
| 82 | + var ranks = new List<Rating>(); |
| 83 | + for (int i = 0; i < newRanking.Count; i++) |
| 84 | + { |
| 85 | + var rating = new Rating(newRanking[i].GetElo() + WholeHistoryRating.RatingOffset, newRanking[i].GetEloStdev(), db.Resources.FirstOrDefault(r => r.ResourceID == newRanking[i].player.id), i, i / (float)newRanking.Count); |
| 86 | + ranks.Add(rating); |
| 87 | + mapRatings[newRanking[i].player.id] = rating; |
| 88 | + } |
| 89 | + mapRanking = ranks; |
| 90 | + } |
| 91 | + } |
| 92 | + catch (Exception ex) |
| 93 | + { |
| 94 | + Trace.TraceError("Error calculating map ratings: " + ex); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private static void RunIterations(int count) |
| 99 | + { |
| 100 | + for (int i = 0; i < count - 1; i++) |
| 101 | + { |
| 102 | + maps.Values.ForEach(x => x.RunOneNewtonIteration(false)); |
| 103 | + } |
| 104 | + maps.Values.ForEach(x => x.RunOneNewtonIteration(true)); |
| 105 | + } |
| 106 | + |
| 107 | + public class Rating |
| 108 | + { |
| 109 | + public readonly float Elo, EloStdev, Percentile; |
| 110 | + public readonly Resource Map; |
| 111 | + public readonly int Rank; |
| 112 | + |
| 113 | + public Rating(float elo, float stdev, Resource map, int rank, float percentile) |
| 114 | + { |
| 115 | + Elo = elo; |
| 116 | + EloStdev = stdev; |
| 117 | + Map = map; |
| 118 | + Rank = rank; |
| 119 | + Percentile = percentile; |
| 120 | + } |
| 121 | + } |
| 122 | + } |
| 123 | +} |
0 commit comments