-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKitchenCreditsStore.cs
287 lines (246 loc) · 8.64 KB
/
KitchenCreditsStore.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using Microsoft.Data.Sqlite;
using TwitchLib.Api.Helix.Models.Chat.GetChatters;
namespace TerrariaKitchen
{
public class KitchenCreditsStore
{
private static readonly string connectionString = "Data Source=terrariaKitchen.db";
private Dictionary<string, int> Credits;
public HashSet<string> CurrentChatters { get; private set; }
public List<KitchenPool> Pools { get; private set; }
public List<KitchenWave> Waves { get; private set; }
private int? CurrentWorld;
private readonly KitchenConfig Config;
public KitchenCreditsStore(KitchenConfig config)
{
Config = config;
Credits = new Dictionary<string, int>();
CurrentChatters = new HashSet<string>();
Pools = new List<KitchenPool>();
Waves = new List<KitchenWave>();
SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_e_sqlite3());
using (var connection = new SqliteConnection(connectionString))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText =
@"
CREATE TABLE IF NOT EXISTS kitchenMonies (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
world INTEGER NOT NULL,
amount INTEGER NOT NULL,
UNIQUE(name, world)
);
";
command.ExecuteNonQuery();
}
}
public void ChangeWorld(int? newWorld)
{
if (newWorld == CurrentWorld)
{
return;
}
UpdatePlayersToDb();
CurrentWorld = newWorld;
ResetInternal();
if (CurrentWorld == null)
{
return;
}
using (var connection = new SqliteConnection(connectionString))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText =
@"
SELECT name, amount FROM kitchenMonies WHERE world = $world;
";
command.Parameters.AddWithValue("$world", CurrentWorld);
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
var chatterName = reader.GetString(0);
var amount = reader.GetInt32(1);
Credits[chatterName] = amount;
}
}
}
Console.WriteLine($"(Terraria Kitchen) Credits Loaded from DB ({Credits.Count})");
}
public void UpdatePlayersToDb()
{
if (CurrentWorld == null)
{
return;
}
using (var connection = new SqliteConnection(connectionString))
{
connection.Open();
lock (Credits)
{
using (var transaction = connection.BeginTransaction())
{
foreach (var balance in Credits)
{
var command = connection.CreateCommand();
command.CommandText = @"INSERT OR REPLACE INTO kitchenMonies (name, world, amount)
VALUES($name, $world, $amount);";
command.Parameters.AddWithValue("$name", balance.Key);
command.Parameters.AddWithValue("$world", CurrentWorld);
command.Parameters.AddWithValue("$amount", balance.Value);
command.ExecuteNonQuery();
}
transaction.Commit();
}
}
}
Console.WriteLine("(Terraria Kitchen) Credits Saved to DB...");
}
public void Income(object? state)
{
DistributeIncome(Config.Income);
}
public void DistributeIncome(int amount)
{
lock (Credits)
{
foreach (var player in CurrentChatters)
{
if (!Credits.ContainsKey(player))
{
Credits[player] = Config.StartingMoney;
}
Credits[player] += amount;
}
}
}
public void ModBalance(string chatter, int amount)
{
lock (Credits)
{
if (!Credits.ContainsKey(chatter))
{
Credits[chatter] = Config.StartingMoney;
}
Credits[chatter] += amount;
if (Credits[chatter] < 0)
{
Credits[chatter] = 0;
}
}
}
public int GetBalance(string chatter)
{
if (!Credits.ContainsKey(chatter))
{
Credits[chatter] = Config.StartingMoney;
}
return Credits[chatter] - Pools.Sum(p => p.Contributions.Where(c => c.Key == chatter).Sum(c => c.Value)) - Waves.Sum(p => p.Contributions.Where(c => c.Key == chatter).Sum(c => c.Value));
}
public KitchenPool? GetPool(int PoolIndex)
{
return Pools.FirstOrDefault(p => p.Index == PoolIndex);
}
public int ContributePool(string chatter, int amount, int PoolIndex)
{
if (GetPool(PoolIndex) is KitchenPool pool)
{
var amt = pool.Contribute(chatter, amount);
return amt;
}
return 0;
}
public void ResolvePoolPayment(KitchenPool pool)
{
lock (Pools)
{
Pools.Remove(pool);
foreach (var contribution in pool.Contributions)
{
if (!Credits.ContainsKey(contribution.Key))
{
Credits[contribution.Key] = Config.StartingMoney - contribution.Value;
}
else
{
Credits[contribution.Key] -= contribution.Value;
}
}
}
}
public void ResolveWavePayment(KitchenWave wave)
{
lock (Waves)
{
Waves.Remove(wave);
foreach (var contribution in wave.Contributions)
{
if (!Credits.ContainsKey(contribution.Key))
{
Credits[contribution.Key] = Config.StartingMoney - contribution.Value;
}
else
{
Credits[contribution.Key] -= contribution.Value;
}
}
}
}
public void ResetInternal()
{
lock (Credits)
{
Credits.Clear();
}
lock (Pools)
{
KitchenPool.PoolIdx = 0;
Pools.Clear();
}
}
public void ResetChatterBalance()
{
ResetInternal();
if (CurrentWorld == null)
{
return;
}
using (var connection = new SqliteConnection(connectionString))
{
connection.Open();
var command = connection.CreateCommand();
command.CommandText = @"DELETE FROM kitchenMonies WHERE world = $world;";
command.Parameters.AddWithValue("$world", CurrentWorld);
command.ExecuteNonQuery();
}
}
public bool Give(string chatter, string target, string amount)
{
lock (Credits)
{
if (!Credits.ContainsKey(chatter))
{
Credits[chatter] = Config.StartingMoney;
}
if (!CurrentChatters.Contains(target))
{
return false;
}
if (!Credits.ContainsKey(target))
{
Credits[target] = Config.StartingMoney;
}
if (int.TryParse(amount, out var amt) && amt > 0 && Credits[chatter] >= amt)
{
Credits[chatter] -= amt;
Credits[target] += amt;
return true;
}
return false;
}
}
}
}