forked from MrOkiDoki/BattleBit-Community-Server-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyGameServer.cs
More file actions
92 lines (73 loc) · 3.33 KB
/
MyGameServer.cs
File metadata and controls
92 lines (73 loc) · 3.33 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
using BattleBitAPI.Common;
using BattleBitAPI.Server;
using DatabaseExample.Models;
using DatabaseExample.Repositories;
using Microsoft.Extensions.DependencyInjection;
namespace DatabaseExample;
internal class MyGameServer : GameServer<MyPlayer>
{
private readonly IServiceProvider _services;
public MyGameServer(IServiceProvider services)
{
_services = services;
}
public override async Task<bool> OnPlayerTypedMessage(MyPlayer author, ChatChannel channel, string message)
{
// Here we make commands like "!banweapon M4A1" etc. to ban and unban weapons.
// These commands use our repository to put and remove them from the database.
// returning true means putting the message in chat, false for not putting it in chat.
if (author.SteamID != 76561198173566107 || !message.StartsWith("!"))
return true; // Whatever checks you want to do.
var words = message.Split(" ");
using var scope = _services.CreateScope();
var bannedWeapons = scope.ServiceProvider.GetRequiredService<BannedWeaponRepository>();
switch (words[0])
{
case "!banweapon":
if (!await bannedWeapons.ExistsAsync(words[1]))
await bannedWeapons.CreateAsync(new BannedWeapon { Name = words[1] });
break;
case "!unbanweapon":
if (await bannedWeapons.ExistsAsync(words[1]))
await bannedWeapons.DeleteAsync(new BannedWeapon { Name = words[1] });
break;
}
return false;
}
public override async Task OnSavePlayerStats(ulong steamId, PlayerStats stats)
{
using var scope = _services.CreateScope();
var players = scope.ServiceProvider.GetRequiredService<PlayerRepository>();
var player = new ServerPlayer { SteamId = steamId, Stats = stats };
// Check if there's already an entry in the DB, if so, update it, otherwise, create one.
if (await players.ExistsAsync(steamId))
await players.UpdateAsync(player);
else
await players.CreateAsync(player);
}
public override async Task OnPlayerJoiningToServer(ulong steamId, PlayerJoiningArguments args)
{
using var scope = _services.CreateScope();
var players = scope.ServiceProvider.GetRequiredService<PlayerRepository>();
// Here we try to get the player out of the database. Return a new PlayersStats() if null, otherwise
// we will put player in a variable and return its stats.
args.Stats = await players.FindAsync(steamId) switch
{
null => new PlayerStats(),
var player => player.Stats
};
}
public override async Task<OnPlayerSpawnArguments?> OnPlayerSpawning(MyPlayer player,
OnPlayerSpawnArguments request)
{
using var scope = _services.CreateScope();
var bannedWeapons = scope.ServiceProvider.GetRequiredService<BannedWeaponRepository>();
// Check if the it's in the banned weapons table, if so, we don't allow it.
if (await bannedWeapons.ExistsAsync(request.Loadout.PrimaryWeapon.Tool.Name))
{
player.Message($"Cannot use banned weapon {request.Loadout.PrimaryWeapon.Tool.Name}!", 1f);
return null; // Deny spawn request.
}
return request;
}
}