void ReadAllFromDB() { Int32 startPost; DateTime endTime; Int32 endPost; _db.GetDayBoundaries(_threadId, _day, out startPost, out endTime, out endPost); StartPost = startPost; EndTime = endTime; EndPost = endPost; SortableBindingList <Voter> livePlayers = new SortableBindingList <Voter>(); IEnumerable <VoterInfo2> voters = _db.GetAllVotes(_threadId, startPost, _endTime.ToUniversalTime(), this); var votersById = voters.GroupBy(x => x.RoleId).Select(y => y.ToList()).ToList(); List <VoterInfo2> hydraVoters = new List <VoterInfo2>(); foreach (var hydra in votersById) { if (hydra.Count > 1) { Hydra h = new Hydra(hydra[0], hydra[1]); hydraVoters.Add(h); } if (hydra.Count == 1) { hydraVoters.Add(hydra[0]); } } voters = hydraVoters; MajorityInfo maj = GetActiveVote(voters, _checkMajority, _lockedVotes); if (maj != null) { MajorityPost = maj.PostNumber; } else { MajorityPost = 0; } foreach (VoterInfo2 vi in voters) { Voter v = new Voter(vi, this); livePlayers.Add(v); } LivePlayers = livePlayers; Int32?maxPost = _db.GetMaxPost(_threadId); if (maxPost != null) { LastPost = maxPost.Value; } else { LastPost = 0; } CreatePostableVoteCount(); // updates counts. }
private MajorityInfo GetActiveVote(IEnumerable <VoterInfo2> voters, Boolean checkMajority, Boolean lockedVotes) { _aliases.Clear(); MajorityInfo rc = null; Int32 majCount = (1 + voters.Count()) / 2; Dictionary <String, String> voterVote = new Dictionary <string, string>(StringComparer.InvariantCultureIgnoreCase); Dictionary <String, HashSet <String> > wagons = new Dictionary <string, HashSet <string> >(StringComparer.InvariantCultureIgnoreCase); Dictionary <String, VoterInfo2> voterInfos = new Dictionary <string, VoterInfo2>(); List <Vote> votes = new List <Vote>(); foreach (var vi in voters) { voterInfos.Add(vi.Name, vi); votes.AddRange(vi.Votes); voterVote.Add(vi.Name, ""); wagons.Add(vi.Name, new HashSet <string>(StringComparer.InvariantCultureIgnoreCase)); foreach (var name in vi.Names) { _aliases.Add(new Alias(name, vi.Name)); } } _aliases.Add(new Alias("unvote", "unvote")); if (_noLynchAllowed) { _aliases.Add(new Alias("no lynch", "no lynch")); } if (!lockedVotes && _noLynchAllowed) { wagons.Add(NoLynch, new HashSet <string>()); } votes.Sort((a, b) => { if (a.PostId == b.PostId) { return(a.BoldPosition.CompareTo(b.BoldPosition)); } return(a.PostId.CompareTo(b.PostId)); }); foreach (var v in votes) { String oldVote = voterVote[v.Voter]; if (wagons.ContainsKey(oldVote)) { if (lockedVotes) { // you can't change votes and previously had a valid vote. continue; } wagons[oldVote].Remove(v.Voter); voterVote[v.Voter] = ""; } String votee = ParseBoldedToVote(v.Bolded, _aliases); voterInfos[v.Voter].SetVote(v.Bolded, v.PostNumber, v.PostTime, v.PostId, v.BoldPosition); if (wagons.ContainsKey(votee)) { voterVote[v.Voter] = votee; wagons[votee].Add(v.Voter); } if (checkMajority) { var lynch = (from w in wagons where (w.Value.Count >= majCount) select w.Key).FirstOrDefault(); if (lynch != null) { rc = new MajorityInfo(v.PostNumber, lynch); return(rc); } } } return(rc); }