Skip to content

Commit

Permalink
Added a "hint token" system for manually redeeming hints
Browse files Browse the repository at this point in the history
  • Loading branch information
Dinopony committed Oct 13, 2023
1 parent 135577e commit d21f7f9
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 49 deletions.
2 changes: 1 addition & 1 deletion HintMachine/Globals.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal static class Globals
/// <summary>
/// The minimal duration between two hint queries obtained from quests
/// </summary>
public const int HintQueueInterval = 2000;
public const int HintQueueInterval = 1000;

/// <summary>
/// The maximal amount of pending hints obtained from quest that can be stored at any given time.
Expand Down
7 changes: 6 additions & 1 deletion HintMachine/HintQuest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ public abstract class HintQuest
public HintQuest()
{}

public abstract bool CheckCompletion();
/// <summary>
/// A method which checks if quest has been completed, decreases the CurrentValue until the quest reaches a
/// "non-completed" state and returns the number of awarded hints.
/// </summary>
/// <returns>the number of obtained hints</returns>
public abstract int CheckAndCommitCompletion();

public abstract void InitComponents(Grid questsGrid);

Expand Down
32 changes: 24 additions & 8 deletions HintMachine/HintQuestCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,18 @@ public long CurrentValue
_lastIncrementTime = now;
}

long absDiff = Math.Abs(_currentValue - value);
if (MaxIncrease != 0 && absDiff > MaxIncrease && absDiff != GoalValue)
return;
// If value is increasing, check if it does not increase more than the defined MaxIncrease
// (this is mostly used to prevent blatant cheating / bugged value readings)
if(value > _currentValue)
{
long diff = _currentValue - value;

// If MaxIncrease is not defined for that quest, use a default value of (2 x GoalValue)
long realMaxIncrease = (MaxIncrease > 0) ? MaxIncrease : GoalValue * 2;

if (diff > realMaxIncrease)
return;
}

_currentValue = value;
}
Expand Down Expand Up @@ -72,13 +81,20 @@ public float GetProgression()
return (float)CurrentValue / GoalValue;
}

public override bool CheckCompletion()
public override int CheckAndCommitCompletion()
{
if (CurrentValue < GoalValue)
return false;
int obtainedHints = 0;

if (GoalValue == 0)
return 0;

while (CurrentValue >= GoalValue)
{
obtainedHints += AwardedHints;
CurrentValue -= GoalValue;
}

CurrentValue -= GoalValue;
return true;
return obtainedHints;
}

public override void InitComponents(Grid questsGrid)
Expand Down
71 changes: 42 additions & 29 deletions HintMachine/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,41 +55,54 @@
<Button Content="🔗 Connect to game" Margin="0,8,0,0" HorizontalAlignment="Center" VerticalAlignment="Top" Width="304" Click="OnConnectToGameButtonClick" Height="37"/>
</StackPanel>

<Border BorderThickness="1" BorderBrush="#FFABADB3" Grid.Column="0" VerticalAlignment="Bottom" RenderTransformOrigin="0.5,0.5">
<Border.Background>
<ImageBrush ImageSource="/Assets/logo_small.png" Stretch="Uniform" TileMode="None" Opacity="0.15"/>
</Border.Background>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel VerticalAlignment="Bottom">
<TextBlock TextWrapping="Wrap" Margin="0,12">
Earn tokens by completing quests in one of the supported games, then redeem those tokens using the following button
to earn a random location hint for your world.
</TextBlock>

<Label Content="Host: " FontWeight="Bold" Grid.Row="0" Grid.Column="0"/>
<TextBlock Name="LabelHost" Grid.Row="0" Grid.Column="1" VerticalAlignment="Center">archipelago.gg:12345</TextBlock>
<TextBlock Name="TextHintTokenCount" TextWrapping="Wrap" FontWeight="Bold">
You currently have 0 hint tokens.
</TextBlock>

<Label Content="Slot: " FontWeight="Bold" Grid.Row="1" Grid.Column="0"/>
<TextBlock Name="LabelSlot" Grid.Row="1" Grid.Column="1" VerticalAlignment="Center">Player</TextBlock>

<Label Content="Game: " FontWeight="Bold" Grid.Row="2" Grid.Column="0"/>
<TextBlock Name="LabelGame" Grid.Row="2" Grid.Column="1" VerticalAlignment="Center">-</TextBlock>

<Grid Grid.Row="3" Grid.ColumnSpan="2" Margin="5">
<Button Name="ButtonRedeemHintToken" Margin="0,12" Height="30" Background="#FFA8E0A9" IsEnabled="False" Click="OnRedeemHintTokenClick">❓ Get a random location hint</Button>

<Border BorderThickness="1" BorderBrush="#FFABADB3" Grid.Column="0" RenderTransformOrigin="0.5,0.5">
<Border.Background>
<ImageBrush ImageSource="/Assets/logo_small.png" Stretch="Uniform" TileMode="None" Opacity="0.15"/>
</Border.Background>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Name="ButtonChangeGame" Content="◀️ Pick another game" Click="OnDisconnectFromGameButtonClick" Grid.Column="0" Margin="0,0,2,0" Visibility="Hidden"/>
<Button Content="❌ Disconnect" Grid.Column="1" Margin="2,0,0,0" Click="OnArchipelagoDisconnectButtonClick"/>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>

<Label Content="Host: " FontWeight="Bold" Grid.Row="0" Grid.Column="0"/>
<TextBlock Name="LabelHost" Grid.Row="0" Grid.Column="1" VerticalAlignment="Center">archipelago.gg:12345</TextBlock>

<Label Content="Slot: " FontWeight="Bold" Grid.Row="1" Grid.Column="0"/>
<TextBlock Name="LabelSlot" Grid.Row="1" Grid.Column="1" VerticalAlignment="Center">Player</TextBlock>

<Label Content="Game: " FontWeight="Bold" Grid.Row="2" Grid.Column="0"/>
<TextBlock Name="LabelGame" Grid.Row="2" Grid.Column="1" VerticalAlignment="Center">-</TextBlock>

<Grid Grid.Row="3" Grid.ColumnSpan="2" Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Button Name="ButtonChangeGame" Content="◀️ Pick another game" Click="OnDisconnectFromGameButtonClick" Grid.Column="0" Margin="0,0,2,0" Visibility="Hidden"/>
<Button Content="❌ Disconnect" Grid.Column="1" Margin="2,0,0,0" Click="OnArchipelagoDisconnectButtonClick"/>
</Grid>
</Grid>
</Grid>
</Border>
</Border>
</StackPanel>

<TabControl Name="TabControl" Margin="12,0,0,0" Grid.Column="1" SelectionChanged="OnTabChange">
<TabItem Header="Message log" Height="22" Margin="-2,-2,-2,0" VerticalAlignment="Top">
Expand Down
50 changes: 40 additions & 10 deletions HintMachine/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading;
using System.Windows.Threading;
using Archipelago.MultiClient.Net.Models;
using System.Collections;

namespace HintMachine
{
Expand All @@ -26,6 +27,8 @@ public partial class MainWindow : Window

private readonly WindowsMediaPlayer _soundPlayer = new WindowsMediaPlayer();

private int _hintTokens = 0;

// ----------------------------------------------------------------------------------

public MainWindow(ArchipelagoHintSession archipelagoSession)
Expand Down Expand Up @@ -159,21 +162,28 @@ private void OnTimerTick()

if (pollSuccessful)
{
int obtainedHintTokens = 0;

// Update hint quests
foreach (HintQuest quest in _game.Quests)
{

if (quest.CheckCompletion())
{
obtainedHintTokens += quest.CheckAndCommitCompletion();
if(obtainedHintTokens > 0)
Console.WriteLine($"Quest {quest.Name} completed");
if (Settings.PlaySoundOnHint)
_soundPlayer.controls.play();
_archipelagoSession.SendMessage($"I just got a hint using HintMachine while playing {_game.Name}!");
Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() => {
quest.UpdateComponents();
}));
}

_archipelagoSession.PendingRandomHints += quest.AwardedHints;
}
if (obtainedHintTokens > 0)
{
if (Settings.PlaySoundOnHint)
_soundPlayer.controls.play();
string hintSingularPlural = (obtainedHintTokens > 1) ? "hints" : "a hint";
_archipelagoSession.SendMessage($"I just got {hintSingularPlural} using HintMachine while playing {_game.Name}!");

Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() => { quest.UpdateComponents(); }));
_hintTokens += obtainedHintTokens;
UpdateHintTokensCount();
}
}
}
Expand All @@ -186,6 +196,15 @@ private void OnTimerTick()
}
}

private void UpdateHintTokensCount()
{
Dispatcher.BeginInvoke(DispatcherPriority.Render, new Action(() =>
{
ButtonRedeemHintToken.IsEnabled = (_hintTokens > 0);
TextHintTokenCount.Text = $"You currently have {_hintTokens} hint tokens.";
}));
}

private void OnConnectToGameButtonClick(object sender, RoutedEventArgs e)
{
lock (_gameLock)
Expand Down Expand Up @@ -331,7 +350,8 @@ private bool ProcessCustomCommands(string v)
#if DEBUG // Debug commands
else if (v == "!gethint")
{
_archipelagoSession.PendingRandomHints += 1;
_hintTokens += 1;
UpdateHintTokensCount();
return true;
}
#endif
Expand Down Expand Up @@ -462,5 +482,15 @@ private void OnReconnectAsPlayerClick(string slotName)
Settings.SaveToFile();
OnArchipelagoSessionChange();
}

private void OnRedeemHintTokenClick(object sender, RoutedEventArgs e)
{
if (_hintTokens <= 0)
return;

_archipelagoSession.PendingRandomHints += 1;
_hintTokens -= 1;
UpdateHintTokensCount();
}
}
}

0 comments on commit d21f7f9

Please sign in to comment.