summaryrefslogtreecommitdiff
path: root/Impostor-dev/src/Impostor.Server/Net/State/Game.Api.cs
blob: f395be29234b3a12c86a4eed8dc4694a14dbd0bf (plain)
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
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Threading.Tasks;
using Impostor.Api;
using Impostor.Api.Games;
using Impostor.Api.Innersloth;
using Impostor.Api.Net;
using Impostor.Api.Net.Inner;
using Impostor.Api.Net.Inner.Objects;
using Impostor.Server.Net.Inner;

namespace Impostor.Server.Net.State
{
    internal partial class Game : IGame
    {
        IClientPlayer IGame.Host => Host;

        IGameNet IGame.GameNet => GameNet;

        public void BanIp(IPAddress ipAddress)
        {
            _bannedIps.Add(ipAddress);
        }

        public async ValueTask SyncSettingsAsync()
        {
            if (Host.Character == null)
            {
                throw new ImpostorException("Attempted to set infected when the host was not spawned.");
            }

            using (var writer = StartRpc(Host.Character.NetId, RpcCalls.SyncSettings))
            {
                // Someone will probably forget to do this, so we include it here.
                // If this is not done, the host will overwrite changes later with the defaults.
                Options.IsDefaults = false;

                await using (var memory = new MemoryStream())
                await using (var writerBin = new BinaryWriter(memory))
                {
                    Options.Serialize(writerBin, GameOptionsData.LatestVersion);
                    writer.WriteBytesAndSize(memory.ToArray());
                }

                await FinishRpcAsync(writer);
            }
        }

        public async ValueTask SetInfectedAsync(IEnumerable<IInnerPlayerControl> players)
        {
            if (Host.Character == null)
            {
                throw new ImpostorException("Attempted to set infected when the host was not spawned.");
            }

            using (var writer = StartRpc(Host.Character.NetId, RpcCalls.SetInfected))
            {
                writer.Write((byte)Host.Character.NetId);

                foreach (var player in players)
                {
                    writer.Write((byte)player.PlayerId);
                }

                await FinishRpcAsync(writer);
            }
        }
    }
}