public async Task LeavePage(int replyId, IotLinkType iotLinkType = IotLinkType.Ideation) { switch (iotLinkType) { case IotLinkType.Form: await Groups.RemoveFromGroupAsync(Context.ConnectionId, $"form - {replyId}"); return; case IotLinkType.Ideation: await Groups.RemoveFromGroupAsync(Context.ConnectionId, $"ideationReply - {replyId}"); break; } }
public void SendDownvote(IotLinkType iotLinkType, int replyId) { var groupName = string.Empty; switch (iotLinkType) { case IotLinkType.Form: groupName = $"form - {replyId}"; break; case IotLinkType.Ideation: groupName = $"ideationReply - {replyId}"; break; } Clients.GroupExcept(groupName, Context.ConnectionId).ReceiveDownvote(); }
public async Task ExecuteAsync(CancellationToken cancellationToken) { Debug.WriteLine("Processing votes.."); var rnd = new Random(); using (var scope = _serviceProvider.CreateScope()) { var voteController = scope.ServiceProvider.GetService <IVoteController>(); var voteHubContext = scope.ServiceProvider.GetService <IHubContext <VoteHub, IVoteHub> >(); var activityHubContext = scope.ServiceProvider.GetService <IHubContext <ActivityHub, IActivityHub> >(); foreach (var iotLinkId in VoteQueues.Keys) { var platform = voteController.GetPlatformByIotLinkId(iotLinkId); User user = null; if (UserQueues.ContainsKey(platform)) { UserQueues[platform].TryDequeue(out user); } int totalTime = 60 * 1000; // 1 minute in millis int votesNextMinute = rnd.Next(Math.Min(11, VoteQueues[iotLinkId].Count)) + 1; // Between 0 and 10 unless queue length is smaller int votesLeft = votesNextMinute; Debug.WriteLine($"Amount of votes in the next minute: {votesNextMinute}"); for (var i = 0; i < votesNextMinute; i++) { int maxInterval = totalTime / votesLeft; // max possible interval in ms int randomizedInterval = rnd.Next(maxInterval); Debug.WriteLine($"Maximum interval: {maxInterval}"); if (VoteQueues[iotLinkId].TryDequeue(out char vote)) { if (vote == '+') { var linkTypeAndIdAndActivity = voteController.VoteUp(iotLinkId, user); IotLinkType linkType = linkTypeAndIdAndActivity.Item1; int id = linkTypeAndIdAndActivity.Item2; BL.Domain.Activity.Activity activity = linkTypeAndIdAndActivity.Item3; if (linkType == IotLinkType.Ideation) { await voteHubContext.Clients.Group($"ideationReply - {id}").ReceiveUpvote(); } var activityVm = new ActivityViewModel(activity); await Task.WhenAll( activityHubContext.Clients.Group($"activity - {activity.Platform.PlatformId}").UpdateActivityFeed(activityVm), activityHubContext.Clients.Group($"activity - all").UpdateActivityFeed(activityVm) ); } if (vote == '-') { var linkTypeAndIdAndActivity = voteController.VoteDown(iotLinkId, user); IotLinkType linkType = linkTypeAndIdAndActivity.Item1; int id = linkTypeAndIdAndActivity.Item2; BL.Domain.Activity.Activity activity = linkTypeAndIdAndActivity.Item3; if (linkType == IotLinkType.Ideation) { await voteHubContext.Clients.Group($"ideationReply - {id}").ReceiveDownvote(); } var activityVm = new ActivityViewModel(activity); await Task.WhenAll( activityHubContext.Clients.Group($"activity - {activity.Platform.PlatformId}").UpdateActivityFeed(activityVm), activityHubContext.Clients.Group($"activity - all").UpdateActivityFeed(activityVm) ); } } Debug.WriteLine($"Waiting {randomizedInterval} ms"); Thread.Sleep(randomizedInterval); Debug.WriteLine("Done waiting..."); Debug.WriteLine(VoteQueues[iotLinkId].Count + " votes left"); totalTime -= randomizedInterval; votesLeft--; } } } Cleanup(); await Task.CompletedTask; }