예제 #1
0
        private static async Task SetStatsForAllPlayers(List <Player> players)
        {
            var playerStatsRetriever = new PlayerStatsRetriever(new DocumentProfileLoader());

            await Task.WhenAll(
                players.Select(async player =>
            {
                var playerStats    = await playerStatsRetriever.Retrieve(player.Name);
                player.PlayerStats = playerStats;
            }));
        }
예제 #2
0
        public async Task Start()
        {
            var device = IPAddress.Parse("5.42.181.0").GetDefaultCaptureDevice();

            device.Open(DeviceMode.Promiscuous, 1000);
            device.Filter = "ip and tcp";

            var handlers = new List <IPlayerHandler> {
                new OpponentHandler(), new MyPlayerHandler()
            };
            var playerStatsRetriever = new PlayerStatsRetriever(new DocumentProfileLoader());

            var stopwatch = new Stopwatch();

            do
            {
                var rawCapture = device.GetNextPacket();
                if (rawCapture == null)
                {
                    continue;
                }

                var packet = Packet.ParsePacket(rawCapture.LinkLayerType, rawCapture.Data);
                if (!(packet.PayloadPacket.PayloadPacket is TcpPacket tcpPacket))
                {
                    continue;
                }
                if (!tcpPacket.IsInIpRange())
                {
                    continue;
                }

                foreach (var handler in handlers)
                {
                    if (handler.Accepts(tcpPacket.Bytes))
                    {
                        stopwatch.Start();
                        var players = handler.Handle(tcpPacket.Bytes).ToImmutableList();
                        await Task.WhenAll(players.Select(async player =>
                        {
                            var playerStats    = await playerStatsRetriever.Retrieve(player.Name);
                            player.PlayerStats = playerStats;
                        }));

                        foreach (var observer in _observers)
                        {
                            observer.OnNext(players);
                        }
                    }
                }

                if (stopwatch.Elapsed >= TimeSpan.FromSeconds(5))
                {
                    foreach (var observer in _observers)
                    {
                        observer.OnCompleted();
                    }
                    return;
                }
            } while (true);
        }