public int VotesCount(VoteKey mask) { lock (s_votesSync) { return((from VoteKey key in _votes.Keys where key.CheckMask(mask) select(int) _votes[key]).Sum()); } }
public int GetTotalVotesCount(string electionId) { var key = new VoteKey { ElectionNum = electionId }; return(VotesCount(key)); }
public void SetNewVotesValue(VoteKey voteKey, int value) { _votes[voteKey] = value; _logger.LogInfo( Message.VotingResult_SetVotesValue, voteKey.ToString(), 0, value, GetCounterValue(voteKey.ScannerSerialNumber)); }
public int GetBulletinCountForScanner(int scannerSerialNumber) { var key = new VoteKey { ScannerSerialNumber = scannerSerialNumber, BlankType = BlankType.All }; return(VotesCount(key)); }
public void AddVote(VoteKey voteKey) { lock (s_votesSync) { if (_votes.ContainsKey(voteKey)) UpdateVotesValue(voteKey, (int)_votes[voteKey] + 1); else SetNewVotesValue(voteKey, 1); } }
public void ClearTestData() { lock (s_votesSync) { var keys = new VoteKey[_votes.Keys.Count]; _votes.Keys.CopyTo(keys, 0); foreach (var key in keys.Where(key => key.VotingMode == VotingMode.Test)) { _votes.Remove(key); _logger.LogInfo(Message.VotingResult_ClearTestData); } } }
public void AddVote(VoteKey voteKey) { lock (s_votesSync) { if (_votes.ContainsKey(voteKey)) { UpdateVotesValue(voteKey, (int)_votes[voteKey] + 1); } else { SetNewVotesValue(voteKey, 1); } } }
public int GetAboveCandidateVotesCount(Election election) { if (!election.NoneAboveExists) { return(0); } var key = new VoteKey { ElectionNum = election.ElectionId, CandidateId = election.NoneAboveCandidate.Id }; return(VotesCount(key)); }
public bool CheckMask(VoteKey mask) { if (mask.BlankType.HasValue) { if (!BlankType.HasValue) { return(false); } switch (mask.BlankType.Value) { case Voting.BlankType.AllButBad: if (BlankType.Value == Voting.BlankType.Bad || BlankType.Value == Voting.BlankType.BadMode) { return(false); } break; case Voting.BlankType.NotValid: if (BlankType.Value == Voting.BlankType.Valid || BlankType.Value == Voting.BlankType.Bad || BlankType.Value == Voting.BlankType.BadMode) { return(false); } break; default: if ( // нужен бланк конкретного типа mask.BlankType.Value != Voting.BlankType.All && mask.BlankType.Value != BlankType.Value) { return(false); } break; } } if (mask.CandidateId != null && mask.CandidateId != CandidateId || mask.ElectionNum != null && mask.ElectionNum != ElectionNum || mask.BlankId != null && mask.BlankId != BlankId || mask.ScannerSerialNumber.HasValue && mask.ScannerSerialNumber.Value != ScannerSerialNumber || mask.VotingMode.HasValue && mask.VotingMode.Value != VotingMode) { return(false); } return(true); }
private void UpdateVotesValue(VoteKey voteKey, int newValue) { var currentValue = (int)_votes[voteKey]; if (currentValue >= newValue) { _logger.LogInfo( Message.VotingResult_TryToSetIncorrectVotesValue, voteKey.ToString(), currentValue, newValue, GetCounterValue(voteKey.ScannerSerialNumber)); return; } _votes[voteKey] = newValue; _logger.LogInfo( Message.VotingResult_SetVotesValue, voteKey.ToString(), currentValue, newValue, GetCounterValue(voteKey.ScannerSerialNumber)); }
public int GetAboveCandidateVotesCount(Election election) { if (!election.NoneAboveExists) return 0; var key = new VoteKey { ElectionNum = election.ElectionId, CandidateId = election.NoneAboveCandidate.Id }; return VotesCount(key); }
private void WriteElectionInfo(XmlTextWriter xmlWriter, Voting.Election election) { xmlWriter.WriteStartElement("Election"); xmlWriter.WriteAttributeString("n", election.ExternalNumber); xmlWriter.WriteAttributeString("parentid", election.ParentComittee.ParentID); xmlWriter.WriteAttributeString("e-mail", election.ParentComittee.EMail); if (!_isPreliminaryProtocol) { foreach (var line in election.Protocol.Lines) { xmlWriter.WriteStartElement("Line"); xmlWriter.WriteAttributeString("n", XmlConvert.ToString(line.Num)); if (!string.IsNullOrEmpty(line.AdditionalNum)) xmlWriter.WriteAttributeString("a", line.AdditionalNum); xmlWriter.WriteString(XmlConvert.ToString(line.Value.GetValueOrDefault(0))); xmlWriter.WriteEndElement(); } } var showDisabledCandidates = !string.IsNullOrEmpty(election.Protocol.DisabledString); foreach (var candidate in election.Candidates) { if (candidate.Disabled && !showDisabledCandidates) continue; xmlWriter.WriteStartElement("Candidate"); xmlWriter.WriteAttributeString("n", candidate.Id); if (candidate.Disabled) xmlWriter.WriteAttributeString("disabled", XmlConvert.ToString(true)); var mask = new VoteKey { ElectionNum = election.ElectionId, CandidateId = candidate.Id }; var votesCount = VotingResults.VotesCount(mask); xmlWriter.WriteString(XmlConvert.ToString(votesCount)); xmlWriter.WriteEndElement(); } xmlWriter.WriteEndElement(); }
private void WriteScannersInfo(XmlTextWriter xmlWriter, Voting.Election election) { var scannerArr = _scannersInfo.GetScannerInfos(); foreach (var scanner in scannerArr) { var intScannerSerialNumber = Convert.ToInt32(scanner.SerialNumber); xmlWriter.WriteStartElement("Scanner"); xmlWriter.WriteAttributeString("n", scanner.SerialNumber); var mask = new VoteKey { ScannerSerialNumber = intScannerSerialNumber, BlankType = BlankType.Bad, }; var nufCount = VotingResults.VotesCount(mask); xmlWriter.WriteAttributeString("nuf", XmlConvert.ToString(nufCount)); if (election != null) WriteAttendInfo(xmlWriter, intScannerSerialNumber, election); else { foreach (var el in _electionManager.SourceData.Elections) WriteAttendInfo(xmlWriter, intScannerSerialNumber, el); } xmlWriter.WriteEndElement(); } }
public void SetCounterValueKeys(VoteKey[] keys) { CodeContract.Requires(keys != null && keys.Length > 0); lock (s_votesSync) { _counterKeys = keys; if (!_addBadBlankToCounterValue) { foreach (var key in _counterKeys.Where(key => key.BlankType.HasValue && key.BlankType.Value == BlankType.All)) { key.BlankType = BlankType.AllButBad; } } } }
public int GetTotalVotesCount(string electionId) { var key = new VoteKey { ElectionNum = electionId }; return VotesCount(key); }
public int GetBulletinCountForScanner(int scannerSerialNumber) { var key = new VoteKey { ScannerSerialNumber = scannerSerialNumber, BlankType = BlankType.All }; return VotesCount(key); }
public bool CheckMask(VoteKey mask) { if (mask.BlankType.HasValue) { if (!BlankType.HasValue) return false; switch (mask.BlankType.Value) { case Voting.BlankType.AllButBad: if (BlankType.Value == Voting.BlankType.Bad || BlankType.Value == Voting.BlankType.BadMode) return false; break; case Voting.BlankType.NotValid: if (BlankType.Value == Voting.BlankType.Valid || BlankType.Value == Voting.BlankType.Bad || BlankType.Value == Voting.BlankType.BadMode) return false; break; default: if (// нужен бланк конкретного типа mask.BlankType.Value != Voting.BlankType.All && mask.BlankType.Value != BlankType.Value) return false; break; } } if (mask.CandidateId != null && mask.CandidateId != CandidateId || mask.ElectionNum != null && mask.ElectionNum != ElectionNum || mask.BlankId != null && mask.BlankId != BlankId || mask.ScannerSerialNumber.HasValue && mask.ScannerSerialNumber.Value != ScannerSerialNumber || mask.VotingMode.HasValue && mask.VotingMode.Value != VotingMode) return false; return true; }
private void WriteAttendInfo(XmlTextWriter xmlWriter, int scannerSerialNumber, Voting.Election election) { foreach (var blank in _electionManager.SourceData.Blanks) { if (election != null && _electionManager.SourceData.GetBlankIdByElectionNumber(election.ElectionId) != blank.Id) continue; var mask = new VoteKey { ScannerSerialNumber = scannerSerialNumber, BlankId = blank.Id }; xmlWriter.WriteStartElement("Bulletin"); xmlWriter.WriteAttributeString("id", blank.Id); xmlWriter.WriteAttributeString("n", XmlConvert.ToString(blank.Marker)); mask.BlankType = BlankType.Valid; xmlWriter.WriteAttributeString("Valid", XmlConvert.ToString(VotingResults.VotesCount(mask))); mask.BlankType = BlankType.NoMarks; xmlWriter.WriteAttributeString("NoMarks", XmlConvert.ToString(VotingResults.VotesCount(mask))); mask.BlankType = BlankType.TooManyMarks; xmlWriter.WriteAttributeString("TooManyMarks", XmlConvert.ToString(VotingResults.VotesCount(mask))); mask.BlankType = BlankType.BadMode; xmlWriter.WriteAttributeString("BadMode", XmlConvert.ToString(VotingResults.VotesCount(mask))); xmlWriter.WriteEndElement(); } }
public NextActivityKey ReceivedBulletinsCountInCurrentModeMoreThenZero( WorkflowExecutionContext context, ActivityParameterDictionary parameters) { var key = new VoteKey { BlankType = BlankType.All, VotingMode = _electionManager.CurrentVotingMode, ScannerSerialNumber = _scannerManager.IntSerialNumber }; var count = _votingResultManager.VotingResults.VotesCount(key); return count > 0 ? BpcNextActivityKeys.Yes : BpcNextActivityKeys.No; }
private void UpdateVotesValue(VoteKey voteKey, int newValue) { var currentValue = (int) _votes[voteKey]; if (currentValue >= newValue) { _logger.LogInfo( Message.VotingResult_TryToSetIncorrectVotesValue, voteKey.ToString(), currentValue, newValue, GetCounterValue(voteKey.ScannerSerialNumber)); return; } _votes[voteKey] = newValue; _logger.LogInfo( Message.VotingResult_SetVotesValue, voteKey.ToString(), currentValue, newValue, GetCounterValue(voteKey.ScannerSerialNumber)); }
public int VotesCount(VoteKey mask) { lock (s_votesSync) { return (from VoteKey key in _votes.Keys where key.CheckMask(mask) select (int) _votes[key]).Sum(); } }
private List<BasePlainElement> CreateProtocolBodyTable( ProtocolText protocolTemplate, Election election, bool final, bool printResults) { int lineNumber = election.Protocol.GetLatestLineNumber(final); string disabled = election.Protocol.DisabledString; bool showDisabled = !string.IsNullOrEmpty(disabled); bool delimiter = false; var table = new List<BasePlainElement>(); foreach (var voteLine in protocolTemplate.VoteLines) { var mask = new VoteKey { ElectionNum = election.ElectionId }; switch (voteLine.Type) { case VoteLineType.Vote: foreach (Candidate currentCand in election.Candidates) { if ((currentCand.Id == voteLine.ID) && (!currentCand.Disabled || showDisabled)) { lineNumber++; mask.CandidateId = currentCand.Id; int votesCount = _votingResultManager.VotingResults.VotesCount(mask); table.Add(new LineClause( new[] { lineNumber.ToString(), currentCand.GetInitials(!currentCand.NoneAbove), currentCand.Disabled ? disabled : (printResults ? votesCount.ToString() : "0"), currentCand.Disabled ? "" : (printResults ? "(" + CustomRusNumber.Str(votesCount, true).Trim() + ")" : CustomRusNumber.Str(0, true).Trim()) }, voteLine.FontSize, voteLine.Bold, voteLine.Italic, delimiter)); delimiter = false; break; } } break; case VoteLineType.Line: if (string.CompareOrdinal(voteLine.ID, VoteTextLine.TOTAL_RECEIVED_VOTETEXTLINE_ID) == 0) { var value = _votingResultManager.VotingResults.VotesCount( new VoteKey( BlankType.AllButBad, null, null, null, null, _electionManager.SourceData.GetBlankIdByElectionNumber(election.ElectionId))); var text = string.IsNullOrEmpty(voteLine.Text) ? VoteTextLine.TOTAL_RECEIVED_VOTETEXTLINE_DEFAULT_TEXT : voteLine.Text; table.Add(new LineClause( new[] { "", text, printResults ? value.ToString() : "0", printResults ? "(" + CustomRusNumber.Str(value, true).Trim() + ")" : CustomRusNumber.Str(0, true).Trim() }, voteLine.FontSize, voteLine.Bold, voteLine.Italic, delimiter)); } else { foreach (Line currentLine in election.Protocol.Lines) { if (currentLine.Id == voteLine.ID) { int value = final && currentLine.Value.HasValue ? currentLine.Value.Value : 0; table.Add(new LineClause( new[] { currentLine.Num + currentLine.AdditionalNum, currentLine.Name, printResults ? value.ToString() : "0", printResults ? "(" + CustomRusNumber.Str(value, true).Trim() + ")" : CustomRusNumber.Str(0, true).Trim() }, voteLine.FontSize, voteLine.Bold, voteLine.Italic, delimiter)); break; } } } delimiter = false; break; case VoteLineType.Delimiter: delimiter = true; break; } } return table; }
private List<KeyValuePair<VoteKey, bool>> GetVoteKeysForAdding( VotingResult votingResult, VotingMode votingMode, int scannerSerialNumber) { Logger.LogVerbose(Message.Common_DebugCall); var keys = new List<KeyValuePair<VoteKey, bool>>(); var sourceData = _electionManager.SourceData; var blank = (0 <= votingResult.BulletinNumber && votingResult.BulletinNumber < sourceData.Blanks.Length) ? sourceData.Blanks[votingResult.BulletinNumber] : null; var bulletinVote = new VoteKey { ScannerSerialNumber = scannerSerialNumber, VotingMode = votingMode, BlankId = (blank != null ? blank.Id : votingResult.BulletinNumber.ToString()), BlankType = votingResult.BlankType, }; if (bulletinVote.BlankType == BlankType.Valid) { if (votingResult.SectionsMarks == null || votingResult.SectionsValidity == null) { bulletinVote.BlankType = BlankType.NoMarks; } else { var invalid = true; for (var sectionIndex = 0; sectionIndex <= votingResult.SectionsMarks.GetUpperBound(0); sectionIndex++) { if (votingResult.SectionsValidity[sectionIndex] && votingResult.SectionsMarks[sectionIndex] != null && votingResult.SectionsMarks[sectionIndex].Length > 0) { invalid = false; break; } } if (invalid) bulletinVote.BlankType = BlankType.NoMarks; } } if (bulletinVote.BlankType == BlankType.Valid) { bool isBulletinAdded = false; for (var sectionIndex = 0; sectionIndex <= votingResult.SectionsMarks.GetUpperBound(0); sectionIndex++) { if (!votingResult.SectionsValidity[sectionIndex]) continue; for (var markIndex = 0; markIndex < votingResult.SectionsMarks[sectionIndex].Length; markIndex++) { VoteKey candidateVote; if (!isBulletinAdded && markIndex == 0) { candidateVote = bulletinVote; isBulletinAdded = true; } else candidateVote = new VoteKey { ScannerSerialNumber = scannerSerialNumber, BlankId = blank.Id, VotingMode = votingMode }; candidateVote.ElectionNum = blank.Sections[sectionIndex]; candidateVote.CandidateId = sourceData.GetElectionByNum(blank.Sections[sectionIndex]). Candidates[votingResult.SectionsMarks[sectionIndex][markIndex]].Id; keys.Add(new KeyValuePair<VoteKey, bool>(candidateVote, false)); } } } else { keys.Add(new KeyValuePair<VoteKey, bool>(bulletinVote, false)); } return keys; }