Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 2d71661

Browse files
committedJan 28, 2025
Voting
1 parent a3ffac0 commit 2d71661

7 files changed

+225
-1
lines changed
 

‎Api/VoteResource.cs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace TNRD.Zeepkist.GTR.Api;
2+
3+
public class VoteResource
4+
{
5+
public string Level { get; set; } = null!;
6+
}

‎Commands/CommandsService.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using TNRD.Zeepkist.GTR.Core;
1+
using TNRD.Zeepkist.GTR.Commands.Voting;
2+
using TNRD.Zeepkist.GTR.Core;
23
using ZeepSDK.ChatCommands;
34

45
namespace TNRD.Zeepkist.GTR.Commands;
@@ -8,5 +9,10 @@ public class CommandsService : IEagerService
89
public CommandsService()
910
{
1011
ChatCommandApi.RegisterLocalChatCommand<WorldRecordCommand>();
12+
13+
ChatCommandApi.RegisterLocalChatCommand<DownvoteCommand>();
14+
ChatCommandApi.RegisterLocalChatCommand<DoubleDownvoteCommand>();
15+
ChatCommandApi.RegisterLocalChatCommand<UpvoteCommand>();
16+
ChatCommandApi.RegisterLocalChatCommand<DoubleUpvoteCommand>();
1117
}
1218
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using TNRD.Zeepkist.GTR.Utilities;
3+
using TNRD.Zeepkist.GTR.Voting;
4+
using ZeepSDK.ChatCommands;
5+
6+
namespace TNRD.Zeepkist.GTR.Commands.Voting;
7+
8+
public class DoubleDownvoteCommand : ILocalChatCommand
9+
{
10+
private readonly VotingService _votingService;
11+
12+
public string Prefix => string.Empty;
13+
public string Command => "--";
14+
public string Description => "Double downvotes the current map";
15+
16+
public DoubleDownvoteCommand()
17+
{
18+
_votingService = ServiceHelper.Instance.GetRequiredService<VotingService>();
19+
}
20+
21+
public void Handle(string arguments)
22+
{
23+
_votingService.DoubleDownvote();
24+
}
25+
}
+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using TNRD.Zeepkist.GTR.Utilities;
3+
using TNRD.Zeepkist.GTR.Voting;
4+
using ZeepSDK.ChatCommands;
5+
6+
namespace TNRD.Zeepkist.GTR.Commands.Voting;
7+
8+
public class DoubleUpvoteCommand : ILocalChatCommand
9+
{
10+
private readonly VotingService _votingService;
11+
12+
public string Prefix => string.Empty;
13+
public string Command => "++";
14+
public string Description => "Double upvotes the current map";
15+
16+
public DoubleUpvoteCommand()
17+
{
18+
_votingService = ServiceHelper.Instance.GetRequiredService<VotingService>();
19+
}
20+
21+
public void Handle(string arguments)
22+
{
23+
_votingService.DoubleUpvote();
24+
}
25+
}

‎Commands/Voting/DownvoteCommand.cs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using Microsoft.Extensions.DependencyInjection;
3+
using TNRD.Zeepkist.GTR.Utilities;
4+
using TNRD.Zeepkist.GTR.Voting;
5+
using ZeepSDK.ChatCommands;
6+
7+
namespace TNRD.Zeepkist.GTR.Commands.Voting;
8+
9+
public class DownvoteCommand : ILocalChatCommand
10+
{
11+
private readonly VotingService _votingService;
12+
13+
public string Prefix => string.Empty;
14+
public string Command => "-";
15+
public string Description => "Downvotes the current map";
16+
17+
public DownvoteCommand()
18+
{
19+
_votingService = ServiceHelper.Instance.GetRequiredService<VotingService>();
20+
}
21+
22+
public void Handle(string arguments)
23+
{
24+
_votingService.Downvote();
25+
}
26+
}

‎Commands/Voting/UpvoteCommand.cs

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using Microsoft.Extensions.DependencyInjection;
2+
using TNRD.Zeepkist.GTR.Utilities;
3+
using TNRD.Zeepkist.GTR.Voting;
4+
using ZeepSDK.ChatCommands;
5+
6+
namespace TNRD.Zeepkist.GTR.Commands.Voting;
7+
8+
public class UpvoteCommand : ILocalChatCommand
9+
{
10+
private readonly VotingService _votingService;
11+
12+
public string Prefix => string.Empty;
13+
public string Command => "+";
14+
public string Description => "Upvotes the current map";
15+
16+
public UpvoteCommand()
17+
{
18+
_votingService = ServiceHelper.Instance.GetRequiredService<VotingService>();
19+
}
20+
21+
public void Handle(string arguments)
22+
{
23+
_votingService.Upvote();
24+
}
25+
}

‎Voting/VotingService.cs

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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

Comments
 (0)
Please sign in to comment.