forked from Fydar/RPGCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInventoryTransaction.cs
107 lines (87 loc) · 2.34 KB
/
InventoryTransaction.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Collections.Generic;
using System.Text;
namespace RPGCore.Inventory.Slots;
/// <summary>
/// <para>Represents a tranfer of items between inventories.</para>
/// </summary>
/// <remarks>
/// <para>When a game client wants to move items from one inventory to another, they construct their desired transaction.</para>
/// <para>A transaction can consist of adding, removing, moving or destroying items.</para>
/// </remarks>
public class InventoryTransaction : IEquatable<InventoryTransaction>
{
public static readonly InventoryTransaction None = new(TransactionStatus.None, Array.Empty<ItemTransaction>());
public TransactionStatus Status { get; set; }
public IReadOnlyList<ItemTransaction> Items { get; set; }
public InventoryTransaction()
{
}
public InventoryTransaction(TransactionStatus status, IReadOnlyList<ItemTransaction> items)
{
Status = status;
Items = items;
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
return Equals(obj as InventoryTransaction);
}
public bool Equals(InventoryTransaction other)
{
if (Status != other.Status)
{
return false;
}
if ((Items?.Count ?? 0) != (other.Items?.Count ?? 0))
{
return false;
}
for (int i = 0; i < Items.Count; i++)
{
var thisItem = Items[i];
var otherItem = other.Items[i];
if (thisItem != otherItem)
{
return false;
}
}
return true;
}
/// <inheritdoc/>
public override int GetHashCode()
{
int hashCode = 2143614870;
hashCode = hashCode * -1521134295 + Status.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<IReadOnlyList<ItemTransaction>>.Default.GetHashCode(Items);
return hashCode;
}
/// <inheritdoc/>
public override string ToString()
{
var sb = new StringBuilder();
sb.Append(nameof(InventoryTransaction));
sb.Append("(");
sb.Append("Status: ");
sb.Append(Status.ToString());
sb.Append(", [");
for (int i = 0; i < Items.Count; i++)
{
sb.Append(Items[i]);
if (i != Items.Count - 1)
{
sb.Append(", ");
}
}
sb.Append("])");
return sb.ToString();
}
public static bool operator ==(InventoryTransaction left, InventoryTransaction right)
{
return EqualityComparer<InventoryTransaction>.Default.Equals(left, right);
}
public static bool operator !=(InventoryTransaction left, InventoryTransaction right)
{
return !(left == right);
}
}