Skip to content

Commit

Permalink
Merge pull request #94 from jonathan-robertson/dev
Browse files Browse the repository at this point in the history
Dialog Shop: Live-Update Prices on Change, Improve Logging on Purchase
  • Loading branch information
jonathan-robertson authored Jul 4, 2023
2 parents ca46e12 + 5384f73 commit 5f25386
Show file tree
Hide file tree
Showing 7 changed files with 105 additions and 44 deletions.
Binary file modified Amnesia.dll
Binary file not shown.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [2.0.2] - 2023-07-04

- improve logging, simplify logic for purchase
- `[Amnesia.Utilities.DialogShop] player 171 (Kanaverum | EOS_000[truncated for example]) purchased Therapy from trader: 12442+0 = 12442 - 600 = 11842`
- `[Amnesia.Utilities.DialogShop] player 171 (Kanaverum | EOS_000[truncated for example]) requested Treatment from trader but doesn't have a Fragile Memory to heal.`
- *these kinds of important actions are now always logged even when debug-mode is disabled (they're real important, after all)*
- live-update prices when admin changes them
- *even when players are looking right at the price in the trader dialog menu!*

## [2.0.1] - 2023-06-30

- update to support a21 b324 (stable)
Expand Down
34 changes: 32 additions & 2 deletions Config/gameevents.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,48 @@
<action class="RemoveItems">
<property name="items_location" value="Backpack" />
<property name="items_tags" value="amnesiaCurrency" />
<property name="phase" value="1" />
</action>
</action_sequence>
<action_sequence name="amnesia_pay_from_blt">
<action_sequence name="amnesia_pay_from_bag_with_change">
<action class="RemoveItems">
<property name="items_location" value="Backpack" />
<property name="items_tags" value="amnesiaCurrency" />
<property name="phase" value="1" />
</action>
<action class="AddBuff">
<property name="buff_name" value="buffAmnesiaRequestChangeCallback" />
<property name="check_already_exists" value="false" />
<property name="phase" value="2" />
</action>
</action_sequence>
<action_sequence name="amnesia_pay_from_all">
<action class="RemoveItems">
<property name="items_location" value="Backpack" />
<property name="items_tags" value="amnesiaCurrency" />
<property name="phase" value="1" />
</action>
<action class="RemoveItems">
<property name="items_location" value="Toolbelt" />
<property name="items_tags" value="amnesiaCurrency" />
<property name="phase" value="2" />
</action>
</action_sequence>
<action_sequence name="amnesia_request_change">
<action_sequence name="amnesia_pay_from_all_with_change">
<action class="RemoveItems">
<property name="items_location" value="Backpack" />
<property name="items_tags" value="amnesiaCurrency" />
<property name="phase" value="1" />
</action>
<action class="RemoveItems">
<property name="items_location" value="Toolbelt" />
<property name="items_tags" value="amnesiaCurrency" />
<property name="phase" value="2" />
</action>
<action class="AddBuff">
<property name="buff_name" value="buffAmnesiaRequestChangeCallback" />
<property name="check_already_exists" value="false" />
<property name="phase" value="3" />
</action>
</action_sequence>

Expand Down
2 changes: 1 addition & 1 deletion ModInfo.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<xml>
<Name value="kanaverum-amnesia" />
<DisplayName value="Amnesia" />
<Version value="2.0.1" />
<Version value="2.0.2" />
<Description value="Reset player progress after dying under a configurable set of circumstances" />
<Author value="Jonathan Robertson (Kanaverum)" />
<Website value="https://github.com/jonathan-robertson/amnesia" />
Expand Down
16 changes: 16 additions & 0 deletions src/Data/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ public static bool SetTreatmentCostBase(int value)
}
TreatmentCostBase = Math.Max(0, value);
_ = Save();
foreach (var player in GameManager.Instance.World.Players.list)
{
DialogShop.UpdatePrices(player);
}
return true;
}

Expand All @@ -177,6 +181,10 @@ public static bool SetTreatmentCostMultiplier(int value)
}
TreatmentCostMultiplier = Math.Max(0, value);
_ = Save();
foreach (var player in GameManager.Instance.World.Players.list)
{
DialogShop.UpdatePrices(player);
}
return true;
}

Expand All @@ -192,6 +200,10 @@ public static bool SetTherapyCostBase(int value)
}
TherapyCostBase = Math.Max(0, value);
_ = Save();
foreach (var player in GameManager.Instance.World.Players.list)
{
DialogShop.UpdatePrices(player);
}
return true;
}

Expand All @@ -207,6 +219,10 @@ public static bool SetTherapyCostMultiplier(int value)
}
TherapyCostMultiplier = Math.Max(0, value);
_ = Save();
foreach (var player in GameManager.Instance.World.Players.list)
{
DialogShop.UpdatePrices(player);
}
return true;
}

Expand Down
3 changes: 1 addition & 2 deletions src/Data/Values.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ internal class Values

// game_events
public static string GameEventPayFromBag { get; private set; } = "amnesia_pay_from_bag";
public static string GameEventPayFromBlt { get; private set; } = "amnesia_pay_from_blt";
public static string GameEventRequestChg { get; private set; } = "amnesia_request_change";
public static string GameEventPayFromAll { get; private set; } = "amnesia_pay_from_all";
public static string GameEventShopCannotAfford { get; private set; } = "amnesia_dialog_shop_cannot_afford";
public static string GameEventShopUnnecessary { get; private set; } = "amnesia_dialog_shop_unnecessary";
public static string GameEventShopPurchased { get; private set; } = "amnesia_dialog_shop_purchased";
Expand Down
85 changes: 46 additions & 39 deletions src/Utilities/DialogShop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ internal class DialogShop
private static readonly ItemValue CASINO_COIN_ITEM_VALUE = ItemClass.GetItem("casinoCoin", false);
private static readonly FastTags moneyTag = FastTags.Parse("amnesiaCurrency");

public static Dictionary<int, int> BagMoney { get; private set; } = new Dictionary<int, int>();
public static Dictionary<int, int> BltMoney { get; private set; } = new Dictionary<int, int>();
public static Dictionary<int, int> BagMoney { get; private set; } = new Dictionary<int, int>();
public static Dictionary<int, int> Change { get; private set; } = new Dictionary<int, int>();

public static void UpdateMoneyTracker(int entityId, ItemStack[] ___toolbelt = null, ItemStack[] ___bag = null)
Expand All @@ -39,7 +39,7 @@ public static void UpdateMoneyTracker(int entityId, ItemStack[] ___toolbelt = nu
BagMoney[entityId] = bagMoney;
bag = bagMoney;
}
if (changed)
if (changed && ModApi.DebugMode)
{
_log.Debug($"Held Money changed for player {entityId} (blt: {blt}, bag: {bag}, total: {blt + bag})");
}
Expand Down Expand Up @@ -92,22 +92,31 @@ public static void TryBuyTreatment(EntityAlive entity)
if (PlayerHelper.TryGetClientInfoAndEntityPlayer(entity.world, entity.entityId, out var clientInfo, out var player))
{
player.Buffs.RemoveBuff(Values.BuffTryBuyTreatment);

var playerName = player.GetDebugName();
var userId = ClientInfoHelper.GetUserIdentifier(clientInfo);

if (!player.Buffs.HasBuff(Values.BuffFragileMemory))
{
_log.Trace($"player {player.GetDebugName()} requested Treatment from trader but doesn't have the necessary money {Values.BuffFragileMemory}.");
_log.Info($"player {player.entityId} ({playerName} | {userId}) requested Treatment from trader but doesn't have a Fragile Memory to heal.");
PlayerHelper.OpenWindow(clientInfo, Values.WindowShopTreatmentUnnecessary);
PlayerHelper.TriggerGameEvent(clientInfo, player, Values.GameEventShopUnnecessary);
return;
}
else if (DialogShop.TryPurchase(clientInfo, player, DialogShop.GetCost(player.Progression.Level, Product.Treatment)))

_ = BltMoney.TryGetValue(player.entityId, out var blt);
_ = BagMoney.TryGetValue(player.entityId, out var bag);
var cost = GetCost(player.Progression.Level, Product.Treatment);
if (TryPurchase(clientInfo, player, blt, bag, cost, out var change))
{
_log.Trace($"player {player.GetDebugName()} purchased Treatment from trader.");
_log.Info($"player {player.entityId} ({playerName} | {userId}) purchased Treatment from trader: {blt}+{bag} = {blt + bag} - {cost} = {change}");
player.Buffs.RemoveBuff(Values.BuffFragileMemory);
PlayerHelper.OpenWindow(clientInfo, Values.WindowShopTreatmentComplete);
PlayerHelper.TriggerGameEvent(clientInfo, player, Values.GameEventShopPurchased);
}
else
{
_log.Trace($"player {player.GetDebugName()} requested Treatment from trader but doesn't have enough money.");
_log.Info($"player {player.entityId} ({playerName} | {userId}) requested Treatment from trader but doesn't have enough money: {blt}+{bag} = {blt + bag} < {cost}");
PlayerHelper.OpenWindow(clientInfo, Values.WindowShopCannotAfford);
PlayerHelper.TriggerGameEvent(clientInfo, player, Values.GameEventShopCannotAfford);
}
Expand All @@ -121,16 +130,23 @@ public static void TryBuyTherapy(EntityAlive entity)
if (PlayerHelper.TryGetClientInfoAndEntityPlayer(entity.world, entity.entityId, out var clientInfo, out var player))
{
player.Buffs.RemoveBuff(Values.BuffTryBuyTherapy);
if (DialogShop.TryPurchase(clientInfo, player, DialogShop.GetCost(player.Progression.Level, Product.Therapy)))

var playerName = player.GetDebugName();
var userId = ClientInfoHelper.GetUserIdentifier(clientInfo);

_ = BltMoney.TryGetValue(player.entityId, out var blt);
_ = BagMoney.TryGetValue(player.entityId, out var bag);
var cost = GetCost(player.Progression.Level, Product.Therapy);
if (TryPurchase(clientInfo, player, blt, bag, cost, out var change))
{
_log.Trace($"player {player.GetDebugName()} purchased Therapy from trader.");
_log.Info($"player {player.entityId} ({playerName} | {userId}) purchased Therapy from trader: {blt}+{bag} = {blt + bag} - {cost} = {change}");
PlayerHelper.Respec(player);
PlayerHelper.OpenWindow(clientInfo, Values.WindowShopTherapyComplete);
PlayerHelper.TriggerGameEvent(clientInfo, player, Values.GameEventShopPurchased);
}
else
{
_log.Trace($"player {player.GetDebugName()} requested Therapy from trader but doesn't have enough money.");
_log.Info($"player {player.entityId} ({playerName} | {userId}) requested Therapy from trader but doesn't have enough money: {blt}+{bag} = {blt + bag} < {cost}");
PlayerHelper.OpenWindow(clientInfo, Values.WindowShopCannotAfford);
PlayerHelper.TriggerGameEvent(clientInfo, player, Values.GameEventShopCannotAfford);
}
Expand Down Expand Up @@ -166,44 +182,35 @@ private static int GetCost(int level, Product product)
return -1;
}

private static bool TryPurchase(ClientInfo clientInfo, EntityPlayer player, int cost)
private static bool TryPurchase(ClientInfo clientInfo, EntityPlayer player, int _blt, int _bag, int _cost, out int change)
{
if (CanAfford(player.entityId, cost))
if (!CanAfford(player.entityId, _cost))
{
_log.Trace($"player {player.GetDebugName()} could afford {cost}");
Pay(clientInfo, player, cost);
return true;
_log.Trace($"player {player.GetDebugName()} could NOT afford {_cost}");
change = -1;
return false;
}
_log.Trace($"player {player.GetDebugName()} could NOT afford {cost}");
return false;
}

private static void Pay(ClientInfo clientInfo, EntityPlayer player, int cost)
{
_log.Trace($"player {player.GetDebugName()} charge attempt for {cost}");
if (BagMoney.TryGetValue(clientInfo.entityId, out var bag))
_log.Trace($"player {player.GetDebugName()} could afford {_cost}");
string eventName;
if (_bag >= _cost)
{
PlayerHelper.TriggerGameEvent(clientInfo, player, Values.GameEventPayFromBag);
if (bag >= cost)
{
if (bag > cost)
{
Change.Add(player.entityId, bag - cost);
PlayerHelper.TriggerGameEvent(clientInfo, player, Values.GameEventRequestChg);
}
return;
}
cost -= bag;
change = _bag - _cost;
eventName = Values.GameEventPayFromBag;
}
if (BltMoney.TryGetValue(clientInfo.entityId, out var blt))
else
{
PlayerHelper.TriggerGameEvent(clientInfo, player, Values.GameEventPayFromBlt);
if (blt > cost)
{
Change.Add(player.entityId, blt - cost);
PlayerHelper.TriggerGameEvent(clientInfo, player, Values.GameEventRequestChg);
}
change = _bag + _blt - _cost;
eventName = Values.GameEventPayFromAll;
}

if (change > 0)
{
Change.Add(player.entityId, change);
eventName += "_with_change";
}
PlayerHelper.TriggerGameEvent(clientInfo, player, eventName);
return true;
}

private static bool CanAfford(int entityId, int amount)
Expand Down

0 comments on commit 5f25386

Please sign in to comment.