ModernUO/Projects/UOContent/Items/Special/Veteran Rewards/TreeStump.cs
Kamron Batman 9b554f69b0
feat(timers): Adds timer pooling, fixes timer related bugs, and changes timer api (#667)
### Changes/Fixes:
* Adds timer pooling.
* Allows pool to be configurable in ModernUO.json
* Pool replenishes itself asynchronously if depleted.
* Fixes an issue with barkeeps and town criers
* Fixes an issue with incognito buff icons not being removed
* Fixes an issue with polymorph name mod not being removed
* Fixes several places where timers go on forever even after an object is deleted, keeping a reference (memory leak)
* Eliminates the timer for MiningCart altogether.
* Deletes `AcidSlime` since it is a duplicate of `PoolOfAcid`
* Fixes HonorableExecution and standardizes the code for other Bushido moves.

## Changes to the Timer API:
```cs
public class Timer
{
  // Creates a timer that will be returned to the pool once execution stops.
  public static void StartTimer(Action callback);
  public static void StartTimer(TimeSpan delay, Action callback);
  public static void StartTimer(TimeSpan delay, TimeSpan interval, Action callback);
  public static void StartTimer(TimeSpan interval, int count, Action callback);
  public static void StartTimer(TimeSpan delay, TimeSpan interval, int count, Action callback);

  // Creates a timer and returns a token for more control. Requires manual cancellation in order for the timer to be returned to the pool.
  // If the token is dereferenced, the timer will be dereferenced too. While not returning a timer to the pool is not considered hazardous, it does defeat the purpose of pooled timers.
  public static void StartTimer(Action callback, out TimerExecutionToken token);
  public static void StartTimer(TimeSpan delay, Action callback, out TimerExecutionToken token);
  public static void StartTimer(TimeSpan delay, TimeSpan interval, Action callback, out TimerExecutionToken token);
  public static void StartTimer(TimeSpan interval, int count, Action callback, out TimerExecutionToken token);
  public static void StartTimer(TimeSpan delay, TimeSpan interval, int count, Action callback, out TimerExecutionToken token);

  // If you aren't sure how to use the API above, or you don't care about performance, then you can use the old RunUO Timer.DelayCall
  public static DelayCallTimer DelayCall(Action callback);
  public static DelayCallTimer DelayCall(TimeSpan delay, Action callback);
  public static DelayCallTimer DelayCall(TimeSpan delay, TimeSpan interval, Action callback);
  public static DelayCallTimer DelayCall(TimeSpan interval, int count, Action callback);
  public static DelayCallTimer DelayCall(TimeSpan delay, TimeSpan interval, int count, Action callback);
}

public struct TimerExecutionToken
{
  public bool Running { get; }
  public int Index { get; }
  public int RemainingCount { get; }
  public DateTime Next { get; }
}
```

## When to use `TimerExecutionToken`?
Use tokens when you want to gain the performance benefit of using a pooled timer, but you need one of the following:
* Access to the next time the timer will tick:`token.Next`
* Access to which interval, how many intervals there are, or how many are remaining: `token.Index`, `token.Count`, and `token.RemainingCount`
* Stop a timer manually.
* Determine if the timer is running: `timer.Running`
* See notes below about requirements for using tokens!

## Notes about using the TimerExecutionToken:
When you opt-in to receive a token, you must call `Cancel()` to return the timer. This can be done inside of the callback, or outside of the callback at any time.
If this is not called and your timer is an infinite interval, then you will create a potential memory leak, or null pointer exception in your callback.
If the timer ends and is stopped, but cancel is not called, then the timer will never return to the pool and stay referenced until the token is deleted or cancel is called. (Memory leak)

## Is this thread safe?
No. The ModernUO timer system is not thread safe at all. If you require a thread safe timer system, contact me and I'll help adapt this system. Keep in mind that there is a massive performance hit to make this thread safe when there are literally no use cases for it.

If you need to synchronize execution, meaning you want to execute code from another thread on the core thread. Let's say you have a discord bot that is pushing commands to the game server. Then use `EventLoopContext.Post(SendOrPostCallback callback, object state);`.
2021-08-07 14:33:35 -07:00

301 lines
8.6 KiB
C#

using System;
using Server.Engines.VeteranRewards;
using Server.Gumps;
using Server.Multis;
using Server.Network;
namespace Server.Items
{
public class TreeStump : BaseAddon, IRewardItem
{
private bool m_IsRewardItem;
private int m_Logs;
private TimerExecutionToken _timerToken;
[Constructible]
public TreeStump(int itemID)
{
AddComponent(new AddonComponent(itemID), 0, 0, 0);
Timer.StartTimer(TimeSpan.FromDays(1), TimeSpan.FromDays(1), GiveLogs, out _timerToken);
}
public TreeStump(Serial serial) : base(serial)
{
}
public override BaseAddonDeed Deed
{
get
{
var deed = new TreeStumpDeed();
deed.IsRewardItem = m_IsRewardItem;
deed.Logs = m_Logs;
return deed;
}
}
[CommandProperty(AccessLevel.GameMaster)]
public int Logs
{
get => m_Logs;
set
{
m_Logs = value;
InvalidateProperties();
}
}
[CommandProperty(AccessLevel.GameMaster)]
public bool IsRewardItem
{
get => m_IsRewardItem;
set
{
m_IsRewardItem = value;
InvalidateProperties();
}
}
private void GiveLogs()
{
m_Logs = Math.Min(100, m_Logs + 10);
}
public override void OnAfterDelete()
{
_timerToken.Cancel();
}
public override void OnComponentUsed(AddonComponent c, Mobile from)
{
var house = BaseHouse.FindHouseAt(this);
/*
* Unique problems have unique solutions. OSI does not have a problem with 1000s of mining carts
* due to the fact that they have only a miniscule fraction of the number of 10 year vets that a
* typical RunUO shard will have (RunUO's scaled down account aging system makes this a unique problem),
* and the "freeness" of free accounts. We also dont have mitigating factors like inactive (unpaid)
* accounts not gaining veteran time.
*
* The lack of high end vets and vet rewards on OSI has made testing the *exact* ranging/stacking
* behavior of these things all but impossible, so either way its just an estimation.
*
* If youd like your shard's carts/stumps to work the way they did before, simply replace the check
* below with this line of code:
*
* if (!from.InRange(GetWorldLocation(), 2)
*
* However, I am sure these checks are more accurate to OSI than the former version was.
*
*/
if (!from.InRange(GetWorldLocation(), 2) || !from.InLOS(this) || !(from.Z - Z > -3 && from.Z - Z < 3))
{
from.LocalOverheadMessage(MessageType.Regular, 0x3B2, 1019045); // I can't reach that.
}
else if (house?.HasSecureAccess(from, SecureLevel.Friends) == true)
{
if (m_Logs > 0)
{
var logs = Utility.Random(7) switch
{
0 => new Log(),
1 => new AshLog(),
2 => new OakLog(),
3 => new YewLog(),
4 => new HeartwoodLog(),
5 => new BloodwoodLog(),
_ => new FrostwoodLog()
};
var amount = Math.Min(10, m_Logs);
logs.Amount = amount;
if (!from.PlaceInBackpack(logs))
{
logs.Delete();
from.SendLocalizedMessage(1078837); // Your backpack is full! Please make room and try again.
}
else
{
m_Logs -= amount;
PublicOverheadMessage(MessageType.Regular, 0, 1094719, m_Logs.ToString()); // Logs: ~1_COUNT~
}
}
else
{
from.SendLocalizedMessage(1094720); // There are no more logs available.
}
}
else
{
from.SendLocalizedMessage(1061637); // You are not allowed to access this.
}
}
public override void Serialize(IGenericWriter writer)
{
base.Serialize(writer);
writer.WriteEncodedInt(0); // version
writer.Write(m_IsRewardItem);
writer.Write(m_Logs);
if (_timerToken.Running)
{
writer.Write(_timerToken.Next);
}
else
{
writer.Write(Core.Now + TimeSpan.FromDays(1));
}
}
public override void Deserialize(IGenericReader reader)
{
base.Deserialize(reader);
var version = reader.ReadEncodedInt();
m_IsRewardItem = reader.ReadBool();
m_Logs = reader.ReadInt();
var next = reader.ReadDateTime();
if (next < Core.Now)
{
next = Core.Now;
}
Timer.StartTimer(next - Core.Now, TimeSpan.FromDays(1), GiveLogs, out _timerToken);
}
}
public class TreeStumpDeed : BaseAddonDeed, IRewardItem, IRewardOption
{
private bool m_IsRewardItem;
private int m_ItemID;
private int m_Logs;
[Constructible]
public TreeStumpDeed() => LootType = LootType.Blessed;
public TreeStumpDeed(Serial serial) : base(serial)
{
}
public override int LabelNumber => 1080406; // a deed for a tree stump decoration
public override BaseAddon Addon
{
get
{
var addon = new TreeStump(m_ItemID);
addon.IsRewardItem = m_IsRewardItem;
addon.Logs = m_Logs;
return addon;
}
}
[CommandProperty(AccessLevel.GameMaster)]
public int Logs
{
get => m_Logs;
set
{
m_Logs = value;
InvalidateProperties();
}
}
[CommandProperty(AccessLevel.GameMaster)]
public bool IsRewardItem
{
get => m_IsRewardItem;
set
{
m_IsRewardItem = value;
InvalidateProperties();
}
}
public void GetOptions(RewardOptionList list)
{
list.Add(1, 1080403); // Tree Stump with Axe West
list.Add(2, 1080404); // Tree Stump with Axe North
list.Add(3, 1080401); // Tree Stump East
list.Add(4, 1080402); // Tree Stump South
}
public void OnOptionSelected(Mobile from, int option)
{
m_ItemID = option switch
{
1 => 0xE56,
2 => 0xE58,
3 => 0xE57,
4 => 0xE59,
_ => m_ItemID
};
if (!Deleted)
{
base.OnDoubleClick(from);
}
}
public override void GetProperties(ObjectPropertyList list)
{
base.GetProperties(list);
if (m_IsRewardItem)
{
list.Add(1076223); // 7th Year Veteran Reward
}
}
public override void OnDoubleClick(Mobile from)
{
if (m_IsRewardItem && !RewardSystem.CheckIsUsableBy(from, this))
{
return;
}
if (IsChildOf(from.Backpack))
{
from.CloseGump<RewardOptionGump>();
from.SendGump(new RewardOptionGump(this));
}
else
{
from.SendLocalizedMessage(1062334); // This item must be in your backpack to be used.
}
}
public override void Serialize(IGenericWriter writer)
{
base.Serialize(writer);
writer.WriteEncodedInt(0); // version
writer.Write(m_IsRewardItem);
writer.Write(m_Logs);
}
public override void Deserialize(IGenericReader reader)
{
base.Deserialize(reader);
var version = reader.ReadEncodedInt();
m_IsRewardItem = reader.ReadBool();
m_Logs = reader.ReadInt();
}
}
}