forked from DevChatter/InteractiveSeven
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request DevChatter#78 from DevChatter/benrick/bug-fixing
Bug Fixes and GP Add/Remove Commands
- Loading branch information
Showing
14 changed files
with
408 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/InteractiveSeven.Core/Diagnostics/Memory/GpAccessor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
using InteractiveSeven.Core.FinalFantasy; | ||
using InteractiveSeven.Core.Settings; | ||
using Microsoft.Extensions.Logging; | ||
using System; | ||
|
||
namespace InteractiveSeven.Core.Diagnostics.Memory | ||
{ | ||
public class GpAccessor : IGpAccessor | ||
{ | ||
private readonly IMemoryAccessor _memoryAccessor; | ||
private readonly ILogger<GpAccessor> _logger; | ||
private ApplicationSettings Settings => ApplicationSettings.Instance; | ||
private static readonly object Padlock = new object(); | ||
|
||
public GpAccessor(IMemoryAccessor memoryAccessor, ILogger<GpAccessor> logger) | ||
{ | ||
_memoryAccessor = memoryAccessor; | ||
_logger = logger; | ||
} | ||
|
||
public ushort GetGp() | ||
{ | ||
try | ||
{ | ||
lock (Padlock) | ||
{ | ||
var buffer = new byte[Addresses.Gp.NumBytes]; | ||
_memoryAccessor.ReadMem(Settings.ProcessName, Addresses.Gp.Address, buffer); | ||
return BitConverter.ToUInt16(buffer); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.LogError(e, "Error setting player GP."); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
public void AddGp(in ushort amount) | ||
{ | ||
try | ||
{ | ||
lock (Padlock) | ||
{ | ||
var buffer = new byte[Addresses.Gp.NumBytes]; | ||
_memoryAccessor.ReadMem(Settings.ProcessName, Addresses.Gp.Address, buffer); | ||
ushort currentBalance = BitConverter.ToUInt16(buffer); | ||
currentBalance += amount; | ||
_memoryAccessor.WriteMem(Settings.ProcessName, Addresses.Gp.Address, | ||
BitConverter.GetBytes(currentBalance)); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.LogError(e, "Error adding GP to player."); | ||
} | ||
} | ||
|
||
public void RemoveGp(ushort amount) | ||
{ | ||
try | ||
{ | ||
lock (Padlock) | ||
{ | ||
var buffer = new byte[Addresses.Gp.NumBytes]; | ||
_memoryAccessor.ReadMem(Settings.ProcessName, Addresses.Gp.Address, buffer); | ||
ushort currentBalance = BitConverter.ToUInt16(buffer); | ||
if (currentBalance >= amount) | ||
{ | ||
currentBalance -= amount; | ||
} | ||
else | ||
{ | ||
currentBalance = 0; | ||
} | ||
_memoryAccessor.WriteMem(Settings.ProcessName, Addresses.Gp.Address, | ||
BitConverter.GetBytes(currentBalance)); | ||
} | ||
} | ||
catch (Exception e) | ||
{ | ||
_logger.LogError(e, "Error adding GP to player."); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace InteractiveSeven.Core.Diagnostics.Memory | ||
{ | ||
public interface IGpAccessor | ||
{ | ||
ushort GetGp(); | ||
void AddGp(in ushort amount); | ||
void RemoveGp(ushort amount); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,6 @@ | |
public static class FF7Const | ||
{ | ||
public const byte Empty = 0xFF; | ||
public const ushort MaxGp = 10_000; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
namespace InteractiveSeven.Core.Settings | ||
{ | ||
public class PlayerGpSettings : ObservableSettingsBase | ||
{ | ||
private ushort _giveMultiplier = 100; | ||
private ushort _removeMultiplier = 100; | ||
private bool _allowModOverride = true; | ||
private bool _giveGpEnabled = false; | ||
private bool _removeGpEnabled = false; | ||
|
||
public bool GiveGpEnabled | ||
{ | ||
get => _giveGpEnabled; | ||
set | ||
{ | ||
_giveGpEnabled = value; | ||
OnPropertyChanged(); | ||
} | ||
} | ||
public bool RemoveGpEnabled | ||
{ | ||
get => _removeGpEnabled; | ||
set | ||
{ | ||
_removeGpEnabled = value; | ||
OnPropertyChanged(); | ||
} | ||
} | ||
public ushort GiveMultiplier | ||
{ | ||
get => _giveMultiplier; | ||
set | ||
{ | ||
_giveMultiplier = value; | ||
OnPropertyChanged(); | ||
} | ||
} | ||
public ushort RemoveMultiplier | ||
{ | ||
get => _removeMultiplier; | ||
set | ||
{ | ||
_removeMultiplier = value; | ||
OnPropertyChanged(); | ||
} | ||
} | ||
public bool AllowModOverride | ||
{ | ||
get => _allowModOverride; | ||
set | ||
{ | ||
_allowModOverride = value; | ||
OnPropertyChanged(); | ||
} | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/InteractiveSeven.Twitch/Commands/GivePlayerGpCommand.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
using InteractiveSeven.Core; | ||
using InteractiveSeven.Core.Diagnostics.Memory; | ||
using InteractiveSeven.Core.Emitters; | ||
using InteractiveSeven.Core.FinalFantasy.Constants; | ||
using InteractiveSeven.Core.Settings; | ||
using InteractiveSeven.Twitch.Model; | ||
using InteractiveSeven.Twitch.Payments; | ||
using System.Linq; | ||
using TwitchLib.Client.Interfaces; | ||
|
||
namespace InteractiveSeven.Twitch.Commands | ||
{ | ||
public class GivePlayerGpCommand : BaseCommand | ||
{ | ||
private readonly PaymentProcessor _paymentProcessor; | ||
private readonly ITwitchClient _twitchClient; | ||
private readonly IGpAccessor _gpAccessor; | ||
private readonly IStatusHubEmitter _statusHubEmitter; | ||
private PlayerGpSettings GpSettings => Settings.EquipmentSettings.PlayerGpSettings; | ||
|
||
public GivePlayerGpCommand(PaymentProcessor paymentProcessor, ITwitchClient twitchClient, | ||
IGpAccessor gpAccessor, IStatusHubEmitter statusHubEmitter) | ||
: base(x => x.GivePlayerGpCommandWords, | ||
x => x.EquipmentSettings.PlayerGpSettings.GiveGpEnabled) | ||
{ | ||
_paymentProcessor = paymentProcessor; | ||
_twitchClient = twitchClient; | ||
_gpAccessor = gpAccessor; | ||
_statusHubEmitter = statusHubEmitter; | ||
} | ||
|
||
public override void Execute(in CommandData commandData) | ||
{ | ||
ushort amount = commandData.Arguments.FirstOrDefault().SafeUshortParse(); | ||
|
||
if (amount <= 0) | ||
{ | ||
_twitchClient.SendMessage(commandData.Channel, | ||
$"How much gp do you want to give to the player, {commandData.User.Username}?"); | ||
return; | ||
} | ||
|
||
ushort currentGp = _gpAccessor.GetGp(); | ||
if (FF7Const.MaxGp - currentGp > amount) | ||
{ | ||
_twitchClient.SendMessage(commandData.Channel, | ||
$"Max GP is {FF7Const.MaxGp:N0}. Current GP: {currentGp:N0}."); | ||
return; | ||
} | ||
|
||
int gilCost = amount * GpSettings.GiveMultiplier; | ||
GilTransaction gilTransaction = _paymentProcessor.ProcessPayment( | ||
commandData, gilCost, GpSettings.AllowModOverride); | ||
|
||
if (!gilTransaction.Paid) | ||
{ | ||
_twitchClient.SendMessage(commandData.Channel, | ||
$"You don't have {gilCost} gil, {commandData.User.Username}."); | ||
return; | ||
} | ||
|
||
_gpAccessor.AddGp(amount); | ||
string message = $"Added {amount} GP for player."; | ||
_twitchClient.SendMessage(commandData.Channel, message); | ||
_statusHubEmitter.ShowEvent(message); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.