11using System ;
22using System . Net . Http ;
3+ using System . Threading ;
34using JetBrains . Annotations ;
45using Microsoft . Extensions . Logging ;
6+ using Steamworks ;
57using TNRD . Zeepkist . GTR . Api ;
8+ using TNRD . Zeepkist . GTR . Configuration ;
69using TNRD . Zeepkist . GTR . Core ;
710using TNRD . Zeepkist . GTR . PlayerLoop ;
811using ZeepkistClient ;
912using ZeepSDK . Chat ;
1013using ZeepSDK . External . Cysharp . Threading . Tasks ;
14+ using ZeepSDK . External . FluentResults ;
1115using ZeepSDK . Level ;
1216using ZeepSDK . Messaging ;
1317using ZeepSDK . Multiplayer ;
@@ -23,43 +27,79 @@ public class VotingService : IEagerService
2327 private readonly PlayerLoopService _playerLoopService ;
2428 private readonly ILogger < VotingService > _logger ;
2529 private readonly ApiHttpClient _apiHttpClient ;
30+ private readonly ConfigService _configService ;
31+ private readonly VotingGraphqlService _votingGraphqlService ;
2632
2733 private string _previousTimeLeft ;
34+ private int _reminderRequestVersion ;
2835
2936 public VotingService ( PlayerLoopService playerLoopService , ILogger < VotingService > logger ,
30- ApiHttpClient apiHttpClient )
37+ ApiHttpClient apiHttpClient , ConfigService configService , VotingGraphqlService votingGraphqlService )
3138 {
3239 _playerLoopService = playerLoopService ;
3340 _logger = logger ;
3441 _apiHttpClient = apiHttpClient ;
42+ _configService = configService ;
43+ _votingGraphqlService = votingGraphqlService ;
3544 _playerLoopService . SubscribeUpdate ( OnUpdate ) ;
3645 }
3746
3847 private void OnUpdate ( )
3948 {
4049 if ( ! MultiplayerApi . IsPlayingOnline )
50+ {
51+ if ( _previousTimeLeft != null )
52+ {
53+ _previousTimeLeft = null ;
54+ _reminderRequestVersion ++ ;
55+ }
4156 return ;
57+ }
4258
4359 string currentTimeLeft = ZeepkistNetwork . CurrentLobby . timeLeftString ;
4460
4561 if ( currentTimeLeft == TIME_LEFT && _previousTimeLeft != TIME_LEFT )
62+ ShowVoteReminderAsync ( ++ _reminderRequestVersion ) . Forget ( ) ;
63+
64+ _previousTimeLeft = currentTimeLeft ;
65+ }
66+
67+ private async UniTaskVoid ShowVoteReminderAsync ( int requestVersion )
68+ {
69+ string currentHash = LevelApi . CurrentHashV2 ? . Hash ;
70+ VoteSummary summary = VoteSummary . Unknown ;
71+
72+ if ( string . IsNullOrEmpty ( currentHash ) )
4673 {
47- ChatApi . AddLocalMessage (
48- "<size=80%><color=#FFFF00>Cast your vote for ZeepCentraal:</color></size><br>" +
49- "<size=75%>" +
50- "<size=50%><i>(hated it)</i></size> " +
51- "<b><color=#FF0000>--</color></b> " +
52- "<b><color=#FF8000>-</color></b> " +
53- "<b><color=#FFFF00>-+</color>/<color=#FFFF00>+-</color></b> " +
54- "<b><color=#80FF00>+</color></b> " +
55- "<b><color=#00FF00>++</color></b> " +
56- "<size=50%><i>(loved it)</i></size>" +
57- "</size>"
58- ) ;
74+ _logger . LogError ( "Unable to get vote summary because current level hash is empty" ) ;
75+ }
76+ else
77+ {
78+ Result < VoteSummary > result = await _votingGraphqlService . GetVoteSummary (
79+ currentHash ,
80+ SteamClient . SteamId . Value ,
81+ CancellationToken . None ) ;
82+ if ( result . IsSuccess )
83+ {
84+ summary = result . Value ;
85+ }
86+ else
87+ {
88+ _logger . LogError ( "Failed to get vote summary: {Result}" , result ) ;
89+ }
90+ }
5991
92+ if ( requestVersion != _reminderRequestVersion ||
93+ ! MultiplayerApi . IsPlayingOnline ||
94+ ! string . Equals ( currentHash , LevelApi . CurrentHashV2 ? . Hash , StringComparison . Ordinal ) )
95+ {
96+ return ;
6097 }
6198
62- _previousTimeLeft = currentTimeLeft ;
99+ if ( ! VoteReminderFormatter . ShouldShow ( _configService . ShowVoteReminderAfterVoting . Value , summary ) )
100+ return ;
101+
102+ ChatApi . AddLocalMessage ( VoteReminderFormatter . Format ( summary ) ) ;
63103 }
64104
65105 private void OnVoteSuccess ( string vote )
0 commit comments