forked from MrOkiDoki/BattleBit-Community-Server-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
448 lines (395 loc) · 14.3 KB
/
Program.cs
File metadata and controls
448 lines (395 loc) · 14.3 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
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
using System.Text.Json;
using BattleBitAPI;
using BattleBitAPI.Common;
using BattleBitAPI.Server;
using CommunityServerAPI;
using CommunityServerAPI.server_utilities;
internal class MyProgram
{
private static void Main(string[] args)
{
Console.WriteLine("Starting API");
var listener = new ServerListener<MyPlayer, MyGameServer>();
listener.OnCreatingGameServerInstance += OnCreatingGameServerInstance;
listener.OnCreatingPlayerInstance += OnCreatingPlayerInstance;
listener.Start(55669);
Thread.Sleep(-1);
}
private static MyPlayer OnCreatingPlayerInstance()
{
return new MyPlayer();
}
private static MyGameServer OnCreatingGameServerInstance()
{
return new MyGameServer();
}
}
public class MyPlayer : Player<MyPlayer>
{
public int Level;
}
public class MyGameServer : GameServer<MyPlayer>
{
private const string AdminJson = "./config/admins.json";
private const string SteamIdJson = "./config/streamer_steamids.json";
private readonly List<ulong> mAdmins = new();
private readonly List<ApiCommand> mChatCommands = new()
{
new HealCommand(),
new KillCommand(),
new TeleportCommand(),
new GrenadeCommand(),
new ForceStartCommand(),
new SpeedCommand(),
new HelpCommand(),
new RevealCommand(),
new ChangeDamageCommand(),
new ChangeReceivedDamageCommand(),
new ChangeAmmoCommand(),
new SetStreamerCommand(),
new RemoveStreamerCommand(),
new OpCommand(),
new DeopCommand(),
new NextGameModeCommand(),
new SetGameModeCommand(),
new GetGameModeCommand(),
new TogglePlaylistCommand()
};
private readonly List<GameMode> mGameModes;
private readonly List<ulong> mListedStreamers = new();
private GameMode mCurrentGameMode;
private bool mCyclePlaylist;
private int mGameModeIndex;
//public CommandQueue queue = new();
public MyGameServer()
{
mGameModes = new List<GameMode>
{
new GunGame(this),
new TeamGunGame(this),
new LifeSteal(this),
new Swap(this),
new Hardcore(this),
new MeleeOnly(this),
new Csgo(this)
};
mGameModeIndex = 0;
mCurrentGameMode = mGameModes[mGameModeIndex];
FetchStreamers();
FetchAdmins();
}
public override async Task OnConnected()
{
await Console.Out.WriteLineAsync(GameIP + " Connected");
}
public override Task OnReconnected()
{
Console.Out.WriteLine($"Current GameMode: {mCurrentGameMode.Name}");
return base.OnReconnected();
}
//modular GameModes: CHECK if new Gamemodes need more passthrough
public override Task OnAPlayerDownedAnotherPlayer(OnPlayerKillArguments<MyPlayer> args)
{
args = mCurrentGameMode.OnAPlayerDownedAnotherPlayer(args);
return base.OnAPlayerDownedAnotherPlayer(args);
}
public override Task OnPlayerGivenUp(MyPlayer player)
{
player = mCurrentGameMode.OnPlayerGivenUp(player);
return base.OnPlayerGivenUp(player);
}
public override Task OnPlayerSpawned(MyPlayer player)
{
player = mCurrentGameMode.OnPlayerSpawned(player);
return base.OnPlayerSpawned(player);
}
public override Task<OnPlayerSpawnArguments> OnPlayerSpawning(MyPlayer player, OnPlayerSpawnArguments request)
{
var re = mCurrentGameMode.OnPlayerSpawning(player, request);
return base.OnPlayerSpawning(re.Player, re.SpawnArguments);
}
public override Task OnRoundEnded()
{
if (mCyclePlaylist) mGameModeIndex = (mGameModeIndex + 1) % mGameModes.Count;
mCurrentGameMode.OnRoundEnded();
return base.OnRoundEnded();
}
public override Task OnRoundStarted()
{
mCurrentGameMode = mGameModes[mGameModeIndex];
mCurrentGameMode.OnRoundStarted();
return base.OnRoundStarted();
}
//basic Functionality
public override Task OnDisconnected()
{
Console.WriteLine("Server disconnected");
return base.OnDisconnected();
}
public override async Task OnPlayerConnected(MyPlayer player)
{
SayToChat("<color=green>" + player.Name + " joined the game!</color>");
player.Message($"Current GameMode is: {mCurrentGameMode.Name}", 4f);
await Console.Out.WriteLineAsync("Connected: " + player);
player.JoinSquad(Squads.Alpha);
}
public override Task OnPlayerDisconnected(MyPlayer player)
{
Console.WriteLine($"{player.Name} disconnected");
SayToChat($"<color=red>{player.Name} left the game!</color>");
player = mCurrentGameMode.OnPlayerDisconnected(player);
return base.OnPlayerDisconnected(player);
}
public override Task OnPlayerJoiningToServer(ulong steamId, PlayerJoiningArguments args)
{
args.Stats.Progress.Rank = 200;
args.Stats.Progress.Prestige = 10;
var re = mCurrentGameMode.OnPlayerJoiningToServer(steamId, args);
return base.OnPlayerJoiningToServer(re.SteamId, re.JoiningArguments);
}
public override async Task<bool> OnPlayerTypedMessage(MyPlayer player, ChatChannel channel, string msg)
{
if (msg.StartsWith("!fetch"))
{
player.Message("Fetching Admins and Streamers", 2f);
FetchStreamers();
FetchAdmins();
}
if (!mAdmins.Contains(player.SteamID)) return true;
var splits = msg.Split(" ");
foreach (var command in mChatCommands)
if (splits[0] == command.CommandPrefix)
{
var c = command.ChatCommand(player, channel, msg);
await HandleCommand(c);
return false;
}
var re = await mCurrentGameMode.OnPlayerTypedMessage(player, channel, msg);
return await base.OnPlayerTypedMessage(re.Player, re.Channel, re.Msg);
}
public void SaveStreamers()
{
try
{
var newJson =
JsonSerializer.Serialize(mListedStreamers, new JsonSerializerOptions { WriteIndented = true });
// Write the JSON to the file, overwriting its content
File.WriteAllText(SteamIdJson, newJson);
Console.WriteLine("Steam IDs updated and saved to the file.");
}
catch (Exception ex)
{
Console.WriteLine("Steam IDs couldn't be updated and saved to the file." + ex);
}
}
public void SaveAdmins()
{
try
{
var newJson =
JsonSerializer.Serialize(mAdmins, new JsonSerializerOptions { WriteIndented = true });
// Write the JSON to the file, overwriting its content
File.WriteAllText(AdminJson, newJson);
Console.WriteLine("Admins updated and saved to the file.");
}
catch (Exception ex)
{
Console.WriteLine("Admins couldn't be updated and saved to the file." + ex);
}
}
public void FetchStreamers()
{
Console.Out.WriteLine("Fetching configs");
try
{
// Read the entire JSON file as a string
var jsonFilePath = SteamIdJson;
var jsonString = File.ReadAllText(jsonFilePath);
// Parse the JSON array using System.Text.Json
var steamIds = JsonSerializer.Deserialize<ulong[]>(jsonString);
foreach (var steamId in steamIds) mListedStreamers.Add(steamId);
Console.Out.WriteLine("Fetching streamers succeeded");
}
catch (Exception ex)
{
Console.Out.WriteLine("Fetching streamers failed: " + ex);
}
}
public void FetchAdmins()
{
try
{
// Read the entire JSON file as a string
var jsonFilePath = AdminJson;
var jsonString = File.ReadAllText(jsonFilePath);
// Parse the JSON array using System.Text.Json
var steamIds = JsonSerializer.Deserialize<ulong[]>(jsonString);
foreach (var steamId in steamIds) mAdmins.Add(steamId);
Console.Out.WriteLine("Fetching admins succeeded");
}
catch (Exception ex)
{
Console.Out.WriteLine("Fetching admins failed: " + ex);
}
}
public async Task HandleCommand(Command c)
{
// need testing if blocking
foreach (var player in AllPlayers)
{
if (!mListedStreamers.Contains(player.SteamID)) continue;
if (player.SteamID != c.StreamerId) continue;
switch (c.Action)
{
case ActionType.Heal:
{
player.Heal(c.Amount);
player.Message($"{c.ExecutorName} has healed you for {c.Amount}", 2f);
break;
}
case ActionType.Kill:
{
player.Kill();
player.Message($"{c.ExecutorName} has killed you", 2f);
break;
}
case ActionType.Grenade:
{
//can't spawn stuff, also no playerPOS
player.Message($"{c.ExecutorName} has spawned a grenade on you", 2f);
break;
}
case ActionType.Teleport:
{
//relative teleport????
player.Message($"{c.ExecutorName} has teleported you {c.Data}", 2f);
break;
}
case ActionType.Speed:
{
player.Modifications.RunningSpeedMultiplier = c.Amount;
player.Message($"{c.ExecutorName} has set your speed to {c.Amount}x", 2f);
break;
}
case ActionType.Reveal:
{
//set marker on Map
player.Message($"{c.ExecutorName} has revealed your Position", 2f);
break;
}
case ActionType.ChangeAmmo:
{
player.Message($"{c.ExecutorName} has set your Ammo to {c.Amount}", 2f);
break;
}
case ActionType.Start:
{
player.Message("Forcing start ", 2f);
ForceStartGame();
break;
}
case ActionType.Help:
{
foreach (var command in mChatCommands) SayToChat($"{command.CommandPrefix} {command.Help}");
break;
}
case ActionType.ChangeDamage:
{
player.Modifications.GiveDamageMultiplier = c.Amount;
player.Message($"{c.ExecutorName} has set your Dmg Multiplier to {c.Amount}", 2f);
break;
}
case ActionType.ChangeReceivedDamage:
{
player.Modifications.ReceiveDamageMultiplier = c.Amount;
player.Message($"{c.ExecutorName} has set your recieve Dmg Multiplier to {c.Amount}", 2f);
break;
}
case ActionType.SetStreamer:
{
player.Message("You are now a streamer", 2f);
mListedStreamers.Add(player.SteamID);
SaveStreamers();
break;
}
case ActionType.RemoveStreamer:
{
player.Message("You are no longer a streamer", 2f);
mListedStreamers.Remove(player.SteamID);
SaveStreamers();
break;
}
case ActionType.GrantOp:
{
player.Message("You are now an admin", 2f);
mAdmins.Add(player.SteamID);
SaveAdmins();
break;
}
case ActionType.RevokeOp:
{
player.Message("You are no longer an Admin", 2f);
mAdmins.Remove(player.SteamID);
SaveAdmins();
break;
}
case ActionType.NextGameMode:
{
mCurrentGameMode.Reset();
mGameModeIndex = (mGameModeIndex + 1) % mGameModes.Count;
mCurrentGameMode = mGameModes[mGameModeIndex];
mCurrentGameMode.Init();
AnnounceShort($"GameMode is now {mCurrentGameMode.Name}");
Console.WriteLine($"GameMode is now {mCurrentGameMode.Name}");
break;
}
case ActionType.SetGameMode:
{
try
{
mCurrentGameMode.Reset();
}
catch (Exception ex)
{
Console.WriteLine($"ERROR resetting GM: {ex}");
}
foreach (var gameMode in mGameModes)
if (gameMode.Name == c.ExecutorName)
{
mCurrentGameMode = gameMode;
mGameModeIndex = mGameModes.IndexOf(gameMode);
}
try
{
mCurrentGameMode.Init();
}
catch (Exception ex)
{
Console.WriteLine($"ERROR initializing GM: {ex}");
}
AnnounceShort($"GameMode is now {mCurrentGameMode.Name}");
break;
}
case ActionType.GetGameMode:
{
AnnounceShort($"GameMode is {mCurrentGameMode.Name}");
break;
}
case ActionType.TogglePlaylist:
{
mCyclePlaylist = !mCyclePlaylist;
AnnounceShort($"GameModePlaylist is now {mCyclePlaylist}");
break;
}
// Add more cases for other ActionType values as needed
}
}
}
/*
set admin role
if (steamID == ID)
{
stats.Roles = Roles.Admin;
}
*/
}