-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathBasicProgression.cs
More file actions
257 lines (194 loc) · 7.36 KB
/
Copy pathBasicProgression.cs
File metadata and controls
257 lines (194 loc) · 7.36 KB
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
using BattleBitAPI.Common;
using BBRAPIModules;
using System;
using System.IO;
using System.Threading.Tasks;
using static BattleBitAPI.Common.PlayerStats;
namespace BattleBitBaseModules;
[Module("Provide basic persistent progression for players", "1.1.1")]
public class BasicProgression : BattleBitModule
{
public BasicProgressionConfiguration Configuration { get; set; } = null!;
private string dataDir => this.Configuration.PerServer ? Path.Combine(this.Configuration.DataDirectory, $"{this.Server.GameIP}:{this.Server.GamePort}") : this.Configuration.DataDirectory;
public override Task OnConnected()
{
if (!Directory.Exists(dataDir))
{
Directory.CreateDirectory(dataDir);
}
return Task.CompletedTask;
}
public override async Task OnPlayerJoiningToServer(ulong steamID, PlayerJoiningArguments args)
{
if (this.Configuration.ApplyInitialStatsOnEveryJoin)
{
args.Stats = this.Configuration.InitialStats?.ToPlayerStats() ?? args.Stats;
return;
}
string playerFileName = getPlayerFileName(steamID);
for (int i = 0; i < 5; i++)
{
try
{
args.Stats = File.Exists(playerFileName) ? new PlayerStats(File.ReadAllBytes(playerFileName)) : (this.Configuration.InitialStats?.ToPlayerStats() ?? args.Stats);
return;
}
catch (Exception ex)
{
this.Logger.Error($"Tried {i} times to read from file {playerFileName} but failed:{ex}");
}
await Task.Delay(250);
}
this.Logger.Error("Giving up trying to read.");
}
public override async Task OnSavePlayerStats(ulong steamID, PlayerStats stats)
{
for (int i = 0; i < 5; i++)
{
try
{
File.WriteAllBytes(getPlayerFileName(steamID), stats.SerializeToByteArray());
return;
}
catch (Exception ex)
{
this.Logger.Error($"Tried {i} times to write to file {getPlayerFileName(steamID)} but failed:{ex}");
}
await Task.Delay(250);
}
this.Logger.Error("Giving up trying to save.");
}
private string getPlayerFileName(ulong steamId)
{
return Path.Combine(dataDir, $"{steamId}.bin");
}
}
public class BasicProgressionConfiguration : ModuleConfiguration
{
public string DataDirectory { get; set; } = "./data/PersistentProgressionFiles";
public bool PerServer { get; set; } = false;
public bool ApplyInitialStatsOnEveryJoin { get; set; } = false;
public BasicPlayerStats? InitialStats { get; set; } = new BasicPlayerStats()
{
Achievements = new byte[0],
IsBanned = false,
Progress = new BasicPlayerProgress(),
Roles = Roles.None,
Selections = new byte[0],
ToolProgress = new byte[0]
};
}
public class BasicPlayerStats
{
public bool IsBanned { get; set; }
public Roles Roles { get; set; }
public BasicPlayerProgress Progress { get; set; } = new();
public byte[] ToolProgress { get; set; } = Array.Empty<byte>();
public byte[] Achievements { get; set; } = Array.Empty<byte>();
public byte[] Selections { get; set; } = Array.Empty<byte>();
public PlayerStats ToPlayerStats()
{
return new()
{
IsBanned = this.IsBanned,
Roles = this.Roles,
Progress = this.Progress.ToPlayerProgress(),
ToolProgress = this.ToolProgress,
Achievements = this.Achievements,
Selections = this.Selections
};
}
}
public class BasicPlayerProgress
{
public uint KillCount { get; set; }
public uint LeaderKills { get; set; }
public uint AssaultKills { get; set; }
public uint MedicKills { get; set; }
public uint EngineerKills { get; set; }
public uint SupportKills { get; set; }
public uint ReconKills { get; set; }
public uint DeathCount { get; set; }
public uint WinCount { get; set; }
public uint LoseCount { get; set; }
public uint FriendlyShots { get; set; }
public uint FriendlyKills { get; set; }
public uint Revived { get; set; }
public uint RevivedTeamMates { get; set; }
public uint Assists { get; set; }
public uint Prestige { get; set; }
public uint Rank { get; set; }
public uint EXP { get; set; }
public uint ShotsFired { get; set; }
public uint ShotsHit { get; set; }
public uint Headshots { get; set; }
public uint ObjectivesComplated { get; set; }
public uint HealedHPs { get; set; }
public uint RoadKills { get; set; }
public uint Suicides { get; set; }
public uint VehiclesDestroyed { get; set; }
public uint VehicleHPRepaired { get; set; }
public uint LongestKill { get; set; }
public uint PlayTimeSeconds { get; set; }
public uint LeaderPlayTime { get; set; }
public uint AssaultPlayTime { get; set; }
public uint MedicPlayTime { get; set; }
public uint EngineerPlayTime { get; set; }
public uint SupportPlayTime { get; set; }
public uint ReconPlayTime { get; set; }
public uint LeaderScore { get; set; }
public uint AssaultScore { get; set; }
public uint MedicScore { get; set; }
public uint EngineerScore { get; set; }
public uint SupportScore { get; set; }
public uint ReconScore { get; set; }
public uint TotalScore { get; set; }
public PlayerProgess ToPlayerProgress()
{
return new()
{
KillCount = this.KillCount,
LeaderKills = this.LeaderKills,
AssaultKills = this.AssaultKills,
MedicKills = this.MedicKills,
EngineerKills = this.EngineerKills,
SupportKills = this.SupportKills,
ReconKills = this.ReconKills,
DeathCount = this.DeathCount,
WinCount = this.WinCount,
LoseCount = this.LoseCount,
FriendlyShots = this.FriendlyShots,
FriendlyKills = this.FriendlyKills,
Revived = this.Revived,
RevivedTeamMates = this.RevivedTeamMates,
Assists = this.Assists,
Prestige = this.Prestige,
Rank = this.Rank,
EXP = this.EXP,
ShotsFired = this.ShotsFired,
ShotsHit = this.ShotsHit,
Headshots = this.Headshots,
ObjectivesComplated = this.ObjectivesComplated,
HealedHPs = this.HealedHPs,
RoadKills = this.RoadKills,
Suicides = this.Suicides,
VehiclesDestroyed = this.VehiclesDestroyed,
VehicleHPRepaired = this.VehicleHPRepaired,
LongestKill = this.LongestKill,
PlayTimeSeconds = this.PlayTimeSeconds,
LeaderPlayTime = this.LeaderPlayTime,
AssaultPlayTime = this.AssaultPlayTime,
MedicPlayTime = this.MedicPlayTime,
EngineerPlayTime = this.EngineerPlayTime,
SupportPlayTime = this.SupportPlayTime,
ReconPlayTime = this.ReconPlayTime,
LeaderScore = this.LeaderScore,
AssaultScore = this.AssaultScore,
MedicScore = this.MedicScore,
EngineerScore = this.EngineerScore,
SupportScore = this.SupportScore,
ReconScore = this.ReconScore,
TotalScore = this.TotalScore
};
}
}