-
Notifications
You must be signed in to change notification settings - Fork 0
/
KitchenPool.cs
91 lines (75 loc) · 2.32 KB
/
KitchenPool.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TerrariaKitchen
{
public class KitchenPool
{
public Dictionary<string, int> Contributions { get; set; }
public string PoolName { get; set; }
public string? Customer { get; set; }
public KitchenConfig.KitchenEntry? TargetEntry { get; set; }
public KitchenEvent? TargetEvent { get; set; }
public int Index { get; private set; }
public static int PoolIdx = 0;
private KitchenPool()
{
Contributions = new Dictionary<string, int>();
Index = ++PoolIdx;
PoolName = $"Pool {Index}";
}
public KitchenPool(KitchenConfig.KitchenEntry entry) : this()
{
TargetEntry = entry;
PoolName = $"Pool {Index} - {entry.MobName}";
}
public KitchenPool(KitchenEvent kitchenEvent) : this()
{
TargetEvent = kitchenEvent;
PoolName = $"Event Pool {Index} - {kitchenEvent.EventName}";
}
public int TargetValue()
{
if (TargetEntry != null)
{
return TargetEntry.Price;
}
if (TargetEvent != null)
{
return TargetEvent.Price;
}
return -1;
}
public bool TargetReached => TotalContributions() >= TargetValue();
public int TotalContributions()
{
return Contributions.Sum(c => c.Value);
}
public int Contribute(string userName, int amount)
{
lock (Contributions)
{
amount = Math.Min(amount, TargetValue() - TotalContributions());
if (Contributions.ContainsKey(userName))
{
if (amount + Contributions[userName] <= 0)
{
return 0;
}
Contributions[userName] += amount;
}
else
{
if (amount <= 0)
{
return 0;
}
Contributions[userName] = amount;
}
return amount;
}
}
}
}