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.
Adding Basic Give/Remove GP Commands
- Loading branch information
Showing
10 changed files
with
320 additions
and
2 deletions.
There are no files selected for viewing
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
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 int _giveMultiplier = 100; | ||
private int _removeMultiplier = 100; | ||
private bool _allowModOverride = true; | ||
private bool _giveGpEnabled = false; | ||
private bool _removeGpEnabled = false; | ||
|
||
public bool GiveGpEnabled // TODO: Add to UI | ||
{ | ||
get => _giveGpEnabled; | ||
set | ||
{ | ||
_giveGpEnabled = value; | ||
OnPropertyChanged(); | ||
} | ||
} | ||
public bool RemoveGpEnabled // TODO: Add to UI | ||
{ | ||
get => _removeGpEnabled; | ||
set | ||
{ | ||
_removeGpEnabled = value; | ||
OnPropertyChanged(); | ||
} | ||
} | ||
public int GiveMultiplier // TODO: Add to UI | ||
{ | ||
get => _giveMultiplier; | ||
set | ||
{ | ||
_giveMultiplier = value; | ||
OnPropertyChanged(); | ||
} | ||
} | ||
public int RemoveMultiplier // TODO: Add to UI | ||
{ | ||
get => _removeMultiplier; | ||
set | ||
{ | ||
_removeMultiplier = value; | ||
OnPropertyChanged(); | ||
} | ||
} | ||
public bool AllowModOverride // TODO: Add to UI | ||
{ | ||
get => _allowModOverride; | ||
set | ||
{ | ||
_allowModOverride = value; | ||
OnPropertyChanged(); | ||
} | ||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
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,59 @@ | ||
using InteractiveSeven.Core; | ||
using InteractiveSeven.Core.Diagnostics.Memory; | ||
using InteractiveSeven.Core.Emitters; | ||
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; | ||
} | ||
|
||
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); | ||
} | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/InteractiveSeven.Twitch/Commands/RemovePlayerGpCommand.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.Settings; | ||
using InteractiveSeven.Twitch.Model; | ||
using InteractiveSeven.Twitch.Payments; | ||
using System.Linq; | ||
using TwitchLib.Client.Interfaces; | ||
|
||
namespace InteractiveSeven.Twitch.Commands | ||
{ | ||
public class RemovePlayerGpCommand : BaseCommand | ||
{ | ||
private readonly PaymentProcessor _paymentProcessor; | ||
private readonly ITwitchClient _twitchClient; | ||
private readonly IGpAccessor _gpAccessor; | ||
private readonly IStatusHubEmitter _statusHubEmitter; | ||
private PlayerGpSettings GpSettings => Settings.EquipmentSettings.PlayerGpSettings; | ||
|
||
public RemovePlayerGpCommand(PaymentProcessor paymentProcessor, ITwitchClient twitchClient, | ||
IGpAccessor gpAccessor, IStatusHubEmitter statusHubEmitter) | ||
: base(x => x.RemovePlayerGpCommandWords, | ||
x => x.EquipmentSettings.PlayerGpSettings.RemoveGpEnabled) | ||
{ | ||
_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 take from the player, {commandData.User.Username}?"); | ||
return; | ||
} | ||
|
||
uint currentGp = _gpAccessor.GetGp(); | ||
if (amount > currentGp) | ||
{ | ||
// TODO: Adjust their request to remove all gp. | ||
_twitchClient.SendMessage(commandData.Channel, | ||
$"Player doesn't have {amount} gp."); | ||
return; | ||
} | ||
|
||
int gilCost = amount * GpSettings.RemoveMultiplier; | ||
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.RemoveGp(amount); | ||
string message = $"Removed {amount} gp from 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