-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInventory.cs
168 lines (154 loc) · 6.34 KB
/
Inventory.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
namespace econrpg
{
public class Inventory
{
private int agentId;
private List<InventoryItem> inventoryItems = new List<InventoryItem>();
private double money;
public List<InventoryItem> InventoryItems
{
get { return this.inventoryItems; }
}
public double Money
{
get { return this.money; }
}
public Inventory(int AgentId, RoleProductionRules roleProductionRules)
{
this.money = Globals.inventoryStartMoney;
this.agentId = AgentId;
this.startInventory(roleProductionRules);
}
public void increaseMoney(double value)
{
this.money += value;
}
public void decreaseMoney(double value)
{
this.money -= value;
}
// public void printInventory(RoleCommodities roleCommodities)
// {
// Console.WriteLine("This inventory have '" + this.money + "' of money");
// Console.WriteLine("These are the commodities in this inventory");
// Console.WriteLine("Name\tQntd\tId\tThreshold");
// foreach (InventoryItem item in this.inventoryItems)
// {
// RoleCommodity roleCommodity = roleCommodities.FindRoleCommodityById(item.getCommodityId());
// Console.WriteLine($"{item.getCommodityName()}\t{item.getInventoryLevel()}\t{item.getCommodityId()}\t{roleCommodity.getThreshold()}");
// }
// }
public int[] getItemPriceBelief(int itemId)
{
return this.findItemById(itemId)?.getPriceBeliefs();
}
private void startInventory(RoleProductionRules roleProductionRules)
{
inventoryItems.RemoveAll(item => true);
List<Commodity> agentCommodities = new List<Commodity>();
ProductionRule oneProductionRule = roleProductionRules.rulesByOutputAmount.First();
agentCommodities.Add(Commodities.getOneById(oneProductionRule.OutputId));
foreach (RecipeItem recipeItem in oneProductionRule.Resources)
{
agentCommodities.Add(Commodities.getOneById(recipeItem.CommodityId));
}
foreach (Commodity commodity in agentCommodities)
{
InventoryItem newItem = new InventoryItem(commodity);
newItem.increaseQuantity(Globals.inventoryItemStartAmount);
this.addInventoryItem(newItem);
}
}
private void addInventoryItem(InventoryItem newItem)
{
this.inventoryItems.Add(newItem);
}
public InventoryItem addCommodityToInventory(Commodity commodity)
{
InventoryItem newItem = new InventoryItem(commodity);
this.addInventoryItem(newItem);
return newItem;
}
public InventoryItem? findItemById(int inventoryItemId)
{
InventoryItem? foundItem = inventoryItems.Find(item => item.getCommodityId() == inventoryItemId);
if (foundItem is null) return null;
return foundItem;
}
public InventoryItem? findItemByName(String inventoryItemName)
{
InventoryItem? foundItem = inventoryItems.Find(item => item.getCommodityName() == inventoryItemName);
if (foundItem is null) return null;
return foundItem;
}
public int getInventoryItemLevel(int inventoryItemId)
{
InventoryItem? foundItem = this.findItemById(inventoryItemId);
if (foundItem is null) return 0;
return foundItem.getInventoryLevel();
}
public int decreaseInventoryItemLevel(int commodityId, int decreaseAmount)
{
InventoryItem? foundItem = this.findItemById(commodityId);
if (foundItem is null) return 0;
return foundItem.decreaseQuantity(decreaseAmount);
}
public int increaseInventoryItemLevel(int commodityId, int increaseAmount)
{
InventoryItem? foundItem = this.findItemById(commodityId);
if (foundItem is null)
{
Commodity foundCommodity = Commodities.getOneById(commodityId);
foundItem = this.addCommodityToInventory(foundCommodity);
return foundItem.increaseQuantity(increaseAmount);
};
return foundItem.increaseQuantity(increaseAmount);
}
public void adjustInventoryLevelsAccordingToProductionRule(ProductionRule rule)
{
foreach (RecipeItem recipeItem in rule.Resources)
{
decreaseInventoryItemLevel(recipeItem.CommodityId, recipeItem.Amount);
}
increaseInventoryItemLevel(rule.OutputId, rule.OutputAmount);
}
private int getAmountBeyondThreshold(int inventoryLevel, int threshold, bool isProduced)
{
int amount;
if (isProduced)
{
amount = inventoryLevel - threshold;
}
else
{
amount = threshold - inventoryLevel;
}
return amount;
}
private Offer generateOneOffer(int commodityId, String offerType)
{
InventoryItem item = this.findItemById(commodityId);
if (item is null) throw new Exception("Inventory item with id " + commodityId + " was not found.");
if (!offerType.Equals("ask") && !offerType.Equals("bid")) throw new Exception("offerType must be bid or ask");
return new Offer(
offerType,
commodityId,
// TODO: Need to get the amount beyond threshold for type "bid"
// See previous version of Inventory class in git history.
item.getInventoryLevel(),
item.getValueFromPriceBeliefs(),
this.agentId
);
}
public List<Offer> generateOffers(ProductionRule productionRule)
{
List<Offer> itemsToTrade = new List<Offer>();
itemsToTrade.Add(generateOneOffer(productionRule.OutputId, "ask"));
foreach (RecipeItem recipeItem in productionRule.Resources)
{
itemsToTrade.Add(generateOneOffer(recipeItem.CommodityId, "bid"));
}
return itemsToTrade;
}
}
}