public int?SetVote(int?articleId, bool voteValue, string uniqueIdentifier = null) { lock (Sync) { if (_ignoreVotesUntil.HasValue && _ignoreVotesUntil.Value > DateTime.Now) { return(null); } var delay = AppSettings.Get <int>("VoteDelay"); _ignoreVotesUntil = DateTime.Now.AddSeconds(delay); } if (!articleId.HasValue) { articleId = _currentArticleId; } if (!articleId.HasValue) { throw new Exception("articleId needed to set vote"); } int voteId, nextArticleId; Entities.VoteStatistics voteStatistics = null; using (var db = new NpgsqlConnection(_connectionString)) { voteId = db.setVote(articleId.Value, voteValue); nextArticleId = db.GetNextArticleId(); if (uniqueIdentifier != null) { voteStatistics = db.GetStatisticsForVote(voteId); } db.Close(); } // N: next question // S: prev question + answer statistics // O: prev question + answer statistics PublishMessage(_channels.ScreenN, new { articleId = nextArticleId }); PublishMessage(_channels.ScreenS, new { articleId, voteId }); PublishMessage(_channels.ScreenO, new { articleId, voteId }); _currentArticleId = nextArticleId; if (uniqueIdentifier != null && voteStatistics != null) { var payloadConfigKey = voteStatistics.votedCorrect ? "OpenTriggerCorrectAnswerPayload" : "OpenTriggerWrongAnswerPayload"; var payload = AppSettings.Get(payloadConfigKey); var uri = new Uri($"coap://{uniqueIdentifier}/led/RGB"); var request = new Request(Method.PUT) { URI = uri, PayloadString = payload }; request.Send(); } return(nextArticleId); }
public static RightAndWrong GetRightAndWrong(this Entities.VoteStatistics s) { var r = new RightAndWrong(); var notSameVote = s.allVotes - s.sameVote; if (s.votedCorrect) { r.Right = s.sameVote; r.Wrong = notSameVote; } else { r.Wrong = s.sameVote; r.Right = notSameVote; } return(r); }