Esempio n. 1
0
 public DS200_Processor()
 {
     this.fileName   = "";
     this.lines      = new List <string>();
     this.state      = State.Ready;
     this.ballotType = BallotType.Normal;
 }
Esempio n. 2
0
 /// <summary>
 /// poll ticket subscribe
 /// </summary>
 /// <param name="ballotType"></param>
 /// <param name="conditionDelegate"></param>
 public static void Subscribe(BallotType ballotType, FuncOut <object, object, object, object, bool?> conditionDelegate)
 {
     if (!_checkFuncDic.Keys.Contains <BallotType>(ballotType))
     {
         _checkFuncDic.Add(ballotType, new List <FuncOut <object, object, object, object, bool?> >());
     }
     if (_checkFuncDic[ballotType].Contains <FuncOut <object, object, object, object, bool?> >(conditionDelegate))
     {
         throw new Exception(string.Format("Func:{0} for BallotType:{1} reduplicative!", conditionDelegate.Method.Name, ballotType.ToString()));
     }
     _checkFuncDic[ballotType].Add(conditionDelegate);
 }
Esempio n. 3
0
        private static async Task <Ballot> CreateBallot(ApplicationDbContext dbContext, string name, Election election,
                                                        BallotType ballotType)
        {
            var ballot = new Ballot
            {
                Name       = name,
                Date       = election.Date,
                Round      = 0,
                ElectionId = election.ElectionId
            };

            ballot.BallotType = ballotType;
            dbContext.Ballots.Add(ballot);
            await dbContext.SaveChangesAsync();

            return(ballot);
        }
Esempio n. 4
0
        public List <CandidateResult> RetrieveWinners(List <CandidateResult> results,
                                                      BallotType ballotType)
        {
            foreach (var candidateResult in results)
            {
                if (candidateResult.Party == null && candidateResult.PartyName.IsNotEmpty())
                {
                    candidateResult.Party = new Party {
                        Name = candidateResult.PartyName
                    }
                }
                ;
            }
            var groupedWinners = results
                                 .GroupBy(w => w.Party?.Name)
                                 .OrderByDescending(w => w.Count())
                                 .ToList();
            var candidateResults = new List <CandidateResult>();

            foreach (var candidate in groupedWinners)
            {
                var electionMapWinner = candidate.FirstOrDefault();
                var item = new CandidateResult
                {
                    Name     = candidate.Key == null ? "INDEPENDENT" : electionMapWinner?.Party.Name,
                    Party    = electionMapWinner?.Party,
                    CountyId = electionMapWinner.CountyId
                };
                if (ballotType == BallotType.Mayor || ballotType == BallotType.CountyCouncilPresident)
                {
                    item.Votes = candidate.Count();
                }
                else
                {
                    item.Votes = candidate.Sum(c => c.Votes);
                }
                item.TotalSeats = item.SeatsGained = candidate.Sum(c => c.Seats1 + c.Seats2);
                candidateResults.Add(item);
            }

            return(candidateResults);
        }
    }
Esempio n. 5
0
        public List <CandidateResult> RetrieveFirst10Winners(List <CandidateResult> results,
                                                             BallotType ballotType)
        {
            var groupedWinners = results
                                 .GroupBy(w => w.Party?.Name)
                                 .OrderByDescending(w => w.Count())
                                 .ToList();
            var top10            = groupedWinners.Take(10).ToList();
            var candidateResults = new List <CandidateResult>();

            foreach (var candidate in top10)
            {
                var electionMapWinner = candidate.FirstOrDefault();
                candidateResults.Add(new CandidateResult
                {
                    Votes = ballotType == BallotType.Mayor ? candidate.Count() : candidate.Sum(c => c.Votes),
                    Name  = candidate.Key == null ? "INDEPENDENT" : electionMapWinner.Party.Name,
                    Party = electionMapWinner.Party
                });
            }

            return(candidateResults);
        }
Esempio n. 6
0
        public async Task <Result <ElectionResponse> > GetBallotResults(ElectionResultsQuery query)
        {
            using (var dbContext = _serviceProvider.CreateScope().ServiceProvider.GetService <ApplicationDbContext>())
            {
                var ballot = dbContext.Ballots
                             .AsNoTracking()
                             .Include(b => b.Election)
                             .FirstOrDefault(e => e.BallotId == query.BallotId);
                if (query.CountyId != null &&
                    query.CountyId.Value.IsCapitalCity() &&
                    query.Division == ElectionDivision.County &&
                    ballot.Date.Year == 2020)
                {
                    BallotType ballotType = ballot.BallotType;
                    if (ballot.BallotType == BallotType.Mayor)
                    {
                        ballotType = BallotType.CountyCouncilPresident;
                    }
                    if (ballot.BallotType == BallotType.LocalCouncil)
                    {
                        ballotType = BallotType.CountyCouncil;
                    }
                    ballot = dbContext.Ballots
                             .AsNoTracking()
                             .Include(b => b.Election)
                             .FirstOrDefault(e => e.ElectionId == ballot.ElectionId && e.BallotType == ballotType);
                }
                if (ballot == null)
                {
                    throw new Exception($"No results found for ballot id {query.BallotId}");
                }
                var electionResponse = new ElectionResponse();

                var divisionTurnout =
                    await GetDivisionTurnout(query, dbContext, ballot);

                var electionInfo = await GetCandidatesFromDb(query, ballot, dbContext);

                if (electionInfo.TotalVotes > 0)
                {
                    divisionTurnout = new Turnout
                    {
                        EligibleVoters = divisionTurnout.EligibleVoters,
                        CountedVotes   = electionInfo.TotalVotes,
                        TotalVotes     = divisionTurnout.TotalVotes,
                        ValidVotes     = electionInfo.ValidVotes,
                        NullVotes      = electionInfo.NullVotes
                    };
                }
                ElectionResultsResponse results;
                if (divisionTurnout == null)
                {
                    results = new ElectionResultsResponse
                    {
                        TotalVotes     = 0,
                        EligibleVoters = 0,
                        NullVotes      = 0,
                        ValidVotes     = 0,
                        Candidates     = new List <CandidateResponse>()
                    };
                }
                else
                {
                    var parties = await _partiesRepository.GetAllParties();

                    results = ResultsProcessor.PopulateElectionResults(divisionTurnout, ballot, electionInfo.Candidates, parties.ToList());
                }

                electionResponse.Aggregated  = electionInfo.Aggregated;
                electionResponse.Results     = results;
                electionResponse.Observation = await dbContext.Observations.FirstOrDefaultAsync(o => o.BallotId == ballot.BallotId);

                if (divisionTurnout != null)
                {
                    electionResponse.Turnout = new ElectionTurnout
                    {
                        TotalVotes     = divisionTurnout.TotalVotes,
                        EligibleVoters = divisionTurnout.EligibleVoters,
                    };
                    if (query.Division == ElectionDivision.Diaspora ||
                        query.Division == ElectionDivision.Diaspora_Country)
                    {
                        electionResponse.Turnout.EligibleVoters = electionResponse.Turnout.TotalVotes;
                    }
                }

                electionResponse.Scope = await CreateElectionScope(dbContext, query);

                electionResponse.Meta         = CreateElectionMeta(ballot);
                electionResponse.ElectionNews = await GetElectionNews(dbContext, ballot.BallotId, ballot.ElectionId);

                return(electionResponse);
            }
        }
Esempio n. 7
0
        public void ReadLine(string line)
        {
            string[] elements  = this.GetElements(line);
            DateTime timestamp = DateTime.ParseExact(elements[1] + " " + elements[2], "MM/dd/yyyy HH:mm:ss", CultureInfo.InvariantCulture);

            if (this.state == State.Ready)
            {
                if (line.Contains(s_votingStarted))
                {
                    this.startTimestamp = timestamp;
                    this.state          = State.VotingStarted;
                    this.ballotType     = BallotType.Normal;
                }
            }
            else if (this.state == State.VotingStarted)
            {
                if (line.Contains(s_votingComplete))
                {
                    this.WriteSuccessfulRecord(timestamp);
                    this.state = State.Ready;
                }
                else if (line.Contains(s_systemError))
                {
                    this.WriteRecord(timestamp, "System error", false);
                    this.state = State.Ready;
                }
                else if (line.Contains(s_blankBallotRejected))
                {
                    this.WriteRecord(timestamp, "Blank ballot rejected", false);
                    this.state = State.Ready;
                }
                else if (line.Contains(s_overVotedBallotRejected))
                {
                    this.WriteRecord(timestamp, "Overvoted ballot rejected", false);
                    this.state = State.Ready;
                }
                else if (line.Contains(s_ballotJamReinsert) ||
                         line.Contains(s_ballotJamCheckPath) || line.Contains(s_ballotJamCheckPath2))
                {
                    this.WriteRecord(timestamp, "Ballot jam", false);
                    this.state = State.Ready;
                }
                else if (line.Contains(s_multipleBallots))
                {
                    this.WriteRecord(timestamp, "Multiple ballots detected", false);
                    this.state = State.Ready;
                }
                else if (line.Contains(s_reinsertOpposite))
                {
                    this.WriteRecord(timestamp, "Ballot could not be read", false);
                    this.state = State.Ready;
                }
                else if (line.Contains(s_removeStubs))
                {
                    this.WriteRecord(timestamp, "Error scanning ballot", false);
                    this.state = State.Ready;
                }
                else if (line.Contains(s_ballotRemoved))
                {
                    this.WriteRecord(timestamp, "Ballot removed during scan", false);
                    this.state = State.Ready;
                }
                else if (line.Contains(s_notInsertedFarEnough))
                {
                    this.WriteRecord(timestamp, "Ballot not inserted far enough", false);
                    this.state = State.Ready;
                }
                else if (line.Contains(s_machineNotProgrammed))
                {
                    this.WriteRecord(timestamp, "Voting machine not programmed for ballot", false);
                    this.state = State.Ready;
                }
                else if (line.Contains(s_ballotTooShort))
                {
                    this.WriteRecord(timestamp, "Ballot too short", false);
                    this.state = State.Ready;
                }
                else if (line.Contains(s_shutdown))
                {
                    this.WriteRecord(timestamp, "Machine shutdown", false);
                    this.state = State.Ready;
                }
                else if (line.Contains(s_autoRejectBallot))
                {
                    this.WriteRecord(timestamp, "Rejected ballot with unreadable mark", false);
                    this.state = State.Ready;
                }
                else if (line.Contains(s_unprocessedElement))
                {
                    this.WriteRecord(timestamp, "Unprocessed ballot element", false);
                    this.state = State.Ready;
                }
                else if (line.Contains(s_votingStarted))
                {
                    System.Diagnostics.Debug.WriteLine("Unexpected end to voting session!");
                    if (this.fileName != null)
                    {
                        System.Diagnostics.Debug.WriteLine("In file " + this.fileName);
                    }
                    System.Diagnostics.Debug.WriteLine(line);
                    this.startTimestamp = timestamp;
                    this.state          = State.VotingStarted;
                    this.ballotType     = BallotType.Normal;
                }
                else if (line.Contains(s_overVotedBallotAccepted))
                {
                    this.ballotType = BallotType.Overvoted;
                }
                else if (line.Contains(s_blankBallotAccepted))
                {
                    this.ballotType = BallotType.Blank;
                }
            }
        }
 public BallotInstructions(BallotType ballotType)
 {
     this.ballotType = ballotType;
 }
Esempio n. 9
0
        /// <summary>
        /// checking a series conditions.
        /// When voters vote for a ballot type, they do not change anything; they only check some conditions. It means
        /// race condition will not arise. Thus, ballot mechanism can be used by multiple threads at the same time,
        /// without mutual exclusion being needed, that is, no lock is needed.
        /// </summary>
        /// <param name="ballotType"></param>
        /// <param name="ballotRelation"></param>
        /// <param name="arg1"></param>
        /// <param name="arg2"></param>
        /// <param name="arg3"></param>
        /// <param name="arg4"></param>
        /// <returns></returns>
        public static bool?Vote(BallotType ballotType, BallotRelation ballotRelation, object arg1, object arg2, object arg3, out object arg4)
        {
            arg4 = string.Empty;

            if (!_checkFuncDic.Keys.Contains <BallotType>(ballotType))
            {
                return(null);
            }
            bool isCheckPass = default(bool);

            switch (ballotRelation)
            {
            case BallotRelation.AND:
                foreach (var item in _checkFuncDic[ballotType])
                {
                    bool?result = item(arg1, arg2, arg3, out arg4);
                    if (!result.HasValue)
                    {
                        continue;
                    }
                    if (!result.Value)
                    {
                        return(false);
                    }
                }
                isCheckPass = true;
                break;

            case BallotRelation.NONE:
                foreach (var item in _checkFuncDic[ballotType])
                {
                    bool?result = item(arg1, arg2, arg3, out arg4);
                    if (!result.HasValue)
                    {
                        continue;
                    }
                    if (result.Value)
                    {
                        return(false);
                    }
                }
                isCheckPass = true;
                break;

            case BallotRelation.OR:
                foreach (var item in _checkFuncDic[ballotType])
                {
                    bool?result = item(arg1, arg2, arg3, out arg4);
                    if (!result.HasValue)
                    {
                        continue;
                    }
                    if (result.Value)
                    {
                        return(true);
                    }
                }
                isCheckPass = false;
                break;
            }

            return(isCheckPass);
        }
Esempio n. 10
0
        /// <summary>
        /// checking a series conditions
        /// </summary>
        /// <param name="ballotType"></param>
        /// <param name="ballotRelation"></param>
        /// <returns></returns>
        public static bool?Vote(BallotType ballotType, BallotRelation ballotRelation)
        {
            object result = string.Empty;

            return(Vote(ballotType, ballotRelation, null, null, null, out result));
        }
Esempio n. 11
0
        /// <summary>
        /// checking a series conditions
        /// </summary>
        /// <param name="ballotType"></param>
        /// <param name="ballotRelation"></param>
        /// <param name="arg1"></param>
        /// <param name="arg2"></param>
        /// <param name="arg3"></param>
        /// <returns></returns>
        public static bool?Vote(BallotType ballotType, BallotRelation ballotRelation, object arg1, object arg2, object arg3)
        {
            object result = string.Empty;

            return(Vote(ballotType, ballotRelation, arg1, arg2, arg3, out result));
        }