-
Notifications
You must be signed in to change notification settings - Fork 71
/
ItemTransaction.cs
104 lines (94 loc) · 2.27 KB
/
ItemTransaction.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
using RPGCore.Items;
using System;
using System.Collections.Generic;
namespace RPGCore.Inventory.Slots;
public class ItemTransaction : IEquatable<ItemTransaction>
{
public IItem Item;
public IInventory FromInventory;
public IInventory ToInventory;
public int Quantity;
public ItemTransactionType Type => CalculateType(FromInventory, ToInventory);
private static ItemTransactionType CalculateType(IInventory from, IInventory to)
{
if (from == null)
{
if (to == null)
{
return ItemTransactionType.None;
}
else
{
return ItemTransactionType.Add;
}
}
else
{
if (to == null)
{
return ItemTransactionType.Destroy;
}
else
{
return ItemTransactionType.Move;
}
}
}
/// <inheritdoc/>
public override string ToString()
{
if (FromInventory == null)
{
if (ToInventory == null)
{
return "None";
}
else
{
return $"Add {Item} to {ToInventory}";
}
}
else
{
if (ToInventory == null)
{
return $"Destroy {Item} from {FromInventory}";
}
else
{
return $"Move {Item} from {FromInventory} to {ToInventory}";
}
}
}
/// <inheritdoc/>
public override bool Equals(object obj)
{
return Equals(obj as ItemTransaction);
}
public bool Equals(ItemTransaction other)
{
return other != null &&
EqualityComparer<IItem>.Default.Equals(Item, other.Item) &&
EqualityComparer<IInventory>.Default.Equals(FromInventory, other.FromInventory) &&
EqualityComparer<IInventory>.Default.Equals(ToInventory, other.ToInventory) &&
Quantity == other.Quantity;
}
/// <inheritdoc/>
public override int GetHashCode()
{
int hashCode = -894636067;
hashCode = hashCode * -1521134295 + EqualityComparer<IItem>.Default.GetHashCode(Item);
hashCode = hashCode * -1521134295 + EqualityComparer<IInventory>.Default.GetHashCode(FromInventory);
hashCode = hashCode * -1521134295 + EqualityComparer<IInventory>.Default.GetHashCode(ToInventory);
hashCode = hashCode * -1521134295 + Quantity.GetHashCode();
return hashCode;
}
public static bool operator ==(ItemTransaction left, ItemTransaction right)
{
return EqualityComparer<ItemTransaction>.Default.Equals(left, right);
}
public static bool operator !=(ItemTransaction left, ItemTransaction right)
{
return !(left == right);
}
}