|
| 1 | +using System; |
| 2 | +using System.Net.Http; |
| 3 | +using JetBrains.Annotations; |
| 4 | +using Microsoft.Extensions.Logging; |
| 5 | +using TNRD.Zeepkist.GTR.Api; |
| 6 | +using TNRD.Zeepkist.GTR.Core; |
| 7 | +using TNRD.Zeepkist.GTR.PlayerLoop; |
| 8 | +using ZeepkistClient; |
| 9 | +using ZeepSDK.Chat; |
| 10 | +using ZeepSDK.External.Cysharp.Threading.Tasks; |
| 11 | +using ZeepSDK.Level; |
| 12 | +using ZeepSDK.Messaging; |
| 13 | +using ZeepSDK.Multiplayer; |
| 14 | +using ZeepSDK.Racing; |
| 15 | + |
| 16 | +namespace TNRD.Zeepkist.GTR.Voting; |
| 17 | + |
| 18 | +[UsedImplicitly] |
| 19 | +public class VotingService : IEagerService |
| 20 | +{ |
| 21 | + private const string TIME_LEFT = "00:30"; |
| 22 | + |
| 23 | + private readonly PlayerLoopService _playerLoopService; |
| 24 | + private readonly ILogger<VotingService> _logger; |
| 25 | + private readonly ApiHttpClient _apiHttpClient; |
| 26 | + |
| 27 | + private string _previousTimeLeft; |
| 28 | + |
| 29 | + public VotingService(PlayerLoopService playerLoopService, ILogger<VotingService> logger, |
| 30 | + ApiHttpClient apiHttpClient) |
| 31 | + { |
| 32 | + _playerLoopService = playerLoopService; |
| 33 | + _logger = logger; |
| 34 | + _apiHttpClient = apiHttpClient; |
| 35 | + _playerLoopService.SubscribeUpdate(OnUpdate); |
| 36 | + } |
| 37 | + |
| 38 | + private void OnUpdate() |
| 39 | + { |
| 40 | + if (!MultiplayerApi.IsPlayingOnline) |
| 41 | + return; |
| 42 | + |
| 43 | + string currentTimeLeft = ZeepkistNetwork.CurrentLobby.timeLeftString; |
| 44 | + |
| 45 | + if (currentTimeLeft == TIME_LEFT && _previousTimeLeft != TIME_LEFT) |
| 46 | + { |
| 47 | + ChatApi.AddLocalMessage("Time to cast your vote! (-- - + ++)"); |
| 48 | + } |
| 49 | + |
| 50 | + _previousTimeLeft = currentTimeLeft; |
| 51 | + } |
| 52 | + |
| 53 | + public void DoubleDownvote() |
| 54 | + { |
| 55 | + VoteAsync("ddownvote", |
| 56 | + () => { MessengerApi.LogSuccess("--'d successfully"); }, |
| 57 | + () => { MessengerApi.LogError("Failed to --"); }) |
| 58 | + .Forget(); |
| 59 | + } |
| 60 | + |
| 61 | + public void DoubleUpvote() |
| 62 | + { |
| 63 | + VoteAsync("dupvote", |
| 64 | + () => { MessengerApi.LogSuccess("++'d successfully"); }, |
| 65 | + () => { MessengerApi.LogError("Failed to ++"); }) |
| 66 | + .Forget(); |
| 67 | + } |
| 68 | + |
| 69 | + public void Downvote() |
| 70 | + { |
| 71 | + VoteAsync("downvote", |
| 72 | + () => { MessengerApi.LogSuccess("-'d successfully"); }, |
| 73 | + () => { MessengerApi.LogError("Failed to -"); }) |
| 74 | + .Forget(); |
| 75 | + } |
| 76 | + |
| 77 | + public void Upvote() |
| 78 | + { |
| 79 | + VoteAsync("upvote", |
| 80 | + () => { MessengerApi.LogSuccess("+'d successfully"); }, |
| 81 | + () => { MessengerApi.LogError("Failed to +"); }) |
| 82 | + .Forget(); |
| 83 | + } |
| 84 | + |
| 85 | + private async UniTaskVoid VoteAsync(string path, Action onSuccess, Action onFail) |
| 86 | + { |
| 87 | + string currentHash = LevelApi.CurrentHash; |
| 88 | + if (string.IsNullOrEmpty(currentHash)) |
| 89 | + { |
| 90 | + _logger.LogError("Unable to vote because current level hash is empty"); |
| 91 | + onFail(); |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + HttpResponseMessage response = |
| 96 | + await _apiHttpClient.PostAsync($"voting/{path}", new VoteResource { Level = currentHash }); |
| 97 | + |
| 98 | + try |
| 99 | + { |
| 100 | + response.EnsureSuccessStatusCode(); |
| 101 | + } |
| 102 | + catch (Exception e) |
| 103 | + { |
| 104 | + _logger.LogError(e, "Failed to vote"); |
| 105 | + onFail(); |
| 106 | + return; |
| 107 | + } |
| 108 | + |
| 109 | + onSuccess(); |
| 110 | + } |
| 111 | +} |
0 commit comments