-
Notifications
You must be signed in to change notification settings - Fork 1
/
CurrencySystem.cs
241 lines (189 loc) · 7.54 KB
/
CurrencySystem.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
using BattleBitAPI.Common;
using BattleBitAPI.Common.Threading;
using BattleBitAPI.Features;
using BBRAPIModules;
using Microsoft.Data.Sqlite;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
namespace BBRModules
{
[Module("A module with a currency system, including saving, loading, and more. Uses SQLite.", "1.0.0")]
[RequireModule(typeof(PlaceholderLib))]
public class CurrencySystem : BattleBitModule
{
public static CurrencyConfiguration Configuration { get; set; } = null!;
public CurrencyDatabase CurrencyDatabase { get; set; }
public List<CurrencyPlayer> CurrencyPlayers { get; private set; }
public List<CurrencyPlayer> TeamACurrencyPlayers { get => CurrencyPlayers.Where((p) => p.Player.Team == Team.TeamA).ToList(); }
public List<CurrencyPlayer> TeamBCurrencyPlayers { get => CurrencyPlayers.Where((p) => p.Player.Team == Team.TeamB).ToList(); }
public override async Task OnConnected()
{
CurrencyPlayers = new();
CurrencyDatabase = new();
await CurrencyDatabase.Open();
await CurrencyDatabase.CreateCurrencyTable();
lock (Server.AllPlayers)
{
foreach (RunnerPlayer player in Server.AllPlayers)
{
CurrencyPlayer currencyPlayer = new(this, player);
CurrencyPlayers.Add(currencyPlayer);
}
}
}
public override Task OnPlayerConnected(RunnerPlayer player)
{
CurrencyPlayer currencyPlayer = new(this, player);
CurrencyPlayers.Add(currencyPlayer);
return Task.CompletedTask;
}
public override Task OnPlayerDisconnected(RunnerPlayer player)
{
CurrencyPlayers.RemoveAll((p) => p.Player.SteamID == player.SteamID);
return Task.CompletedTask;
}
public CurrencyPlayer GetCurrencyPlayer(RunnerPlayer player) => CurrencyPlayers.Where(p => p.Player.SteamID == player.SteamID).Single();
}
public class CurrencyPlayer
{
public RunnerPlayer Player;
public CurrencySystem System;
public int CurrencyAmount;
public CurrencyPlayer(CurrencySystem system, RunnerPlayer player)
{
Player = player;
System = system;
SqliteConnection connection = System.CurrencyDatabase.GetConnection();
SqliteCommand create = new("INSERT OR IGNORE INTO currencyStore (steamId, currency) VALUES ($steamId, $default)", connection);
create.Parameters.AddWithValue("$steamId", Player.SteamID.ToString());
create.Parameters.AddWithValue("$default", CurrencySystem.Configuration.StartingAmount);
create.ExecuteNonQuery();
SqliteCommand get = new("SELECT currency FROM currencyStore WHERE steamId=$steamId", connection);
get.Parameters.AddWithValue("$steamId", Player.SteamID.ToString());
object? value = get.ExecuteScalar();
if (value != null)
CurrencyAmount = int.Parse(value.ToString());
System.CurrencyDatabase.OnChangedEvent += OnChanged;
}
public CurrencyPlayer Set(int newAmount)
{
CurrencyAmount = newAmount;
CurrencyChangedArgs args = new();
args.NewValue = CurrencyAmount;
System.CurrencyDatabase.OnChanged(args);
return this;
}
public int GetCurrency() => CurrencyAmount;
public CurrencyPlayer Increment(int amount)
{
CurrencyAmount += amount;
CurrencyChangedArgs args = new();
args.NewValue = CurrencyAmount;
System.CurrencyDatabase.OnChanged(args);
return this;
}
public CurrencyPlayer Decrement(int amount)
{
CurrencyAmount -= amount;
CurrencyChangedArgs args = new();
args.NewValue = CurrencyAmount;
System.CurrencyDatabase.OnChanged(args);
return this;
}
public CurrencyPlayer Multiply(int multiplicand)
{
CurrencyAmount *= multiplicand;
CurrencyChangedArgs args = new();
args.NewValue = CurrencyAmount;
System.CurrencyDatabase.OnChanged(args);
return this;
}
public CurrencyPlayer Divide(int divisor)
{
CurrencyAmount /= divisor;
CurrencyChangedArgs args = new();
args.NewValue = CurrencyAmount;
System.CurrencyDatabase.OnChanged(args);
return this;
}
public void OnChanged(object? e, CurrencyChangedArgs args)
{
Console.WriteLine("Updated!");
CurrencyAmount = args.NewValue;
}
public async Task SaveAsync()
{
SqliteConnection connection = System.CurrencyDatabase.GetConnection();
SqliteCommand command = new("UPDATE currencyStore SET currency = $amount WHERE steamId=$steamId", connection);
command.Parameters.AddWithValue("$steamId", Player.SteamID);
command.Parameters.AddWithValue("$amount", CurrencyAmount);
await command.ExecuteNonQueryAsync();
}
public void Destroy()
{
System.CurrencyDatabase.OnChangedEvent -= OnChanged;
}
}
public class CurrencyConfiguration : ModuleConfiguration
{
public string CurrencyFormat { get; set; } = "{amount} {currency}";
public string Currency { get; set; } = "points";
public int StartingAmount { get; set; } = 0;
}
public class CurrencyDatabase
{
public static SqliteConnection Connection { get; private set; } = null!;
public CurrencyDatabase()
{
string directoryPath = Path.Combine(Directory.GetCurrentDirectory(), "data");
string path = Path.Combine(directoryPath, "CurrencySystem.db");
if (!Directory.Exists(directoryPath))
Directory.CreateDirectory(directoryPath);
if (!File.Exists(path))
File.Create(path);
}
public async Task CreateCurrencyTable()
{
var cmd = Connection.CreateCommand();
cmd.CommandText =
@"CREATE TABLE IF NOT EXISTS currencyStore (steamId INT PRIMARY KEY, currency INT)";
await cmd.ExecuteNonQueryAsync();
}
public async Task Open()
{
string path = Path.Combine(Directory.GetCurrentDirectory(), "Data", "CurrencySystem.db");
string connectionString = new SqliteConnectionStringBuilder()
{
Mode = SqliteOpenMode.ReadWriteCreate,
DataSource = path,
Cache = SqliteCacheMode.Shared
}.ToString();
Connection = new SqliteConnection(connectionString);
await Connection.OpenAsync();
}
public async Task Close()
{
await Connection.CloseAsync();
}
public SqliteConnection GetConnection()
{
return Connection;
}
public virtual void OnChanged(CurrencyChangedArgs e)
{
EventHandler<CurrencyChangedArgs> handler = OnChangedEvent;
if (handler != null)
{
handler(this, e);
}
}
public event EventHandler<CurrencyChangedArgs> OnChangedEvent;
}
public class CurrencyChangedArgs
{
public int NewValue { get; set; }
}
}