Beispiel #1
0
        public GameServer()
        {
            Host = new MunchkinHost
            {
                Id   = _deviceInfoService.DeviceId,
                Name = "Game_1"
            };

            ConnectedPlayers = new ConcurrentDictionary <string, PlayerInfo>();
        }
Beispiel #2
0
        public void StartSearchHosts()
        {
            _lanClient.StartListeningBroadcast();

            _hostsSearchSubscription = _lanClient.TcpClientEventSubject.AsObservable()
                                       .TakeUntil(_destroy)
                                       .Where(tcpEvent => tcpEvent.Type == TcpEventType.ReceiveData)
                                       .Where(tcpEvent => tcpEvent.Data != null)
                                       .Where(tcpEvent => ((Packet)tcpEvent.Data).MessageType == MunchkinMessageType.HostFound)
                                       .Finally(() => _gameLogger.Debug("Game host observable end."))
                                       .Select(tcpEvent =>
            {
                var packet     = (Packet)tcpEvent.Data;
                var position   = 3;
                var host       = new MunchkinHost();
                host.IpAddress = packet.SenderIpAdress;

                host.Id   = Encoding.UTF8.GetString(packet.Buffer, position + 1, packet.Buffer[position]);
                position += packet.Buffer[position];
                position++;

                host.Name = Encoding.UTF8.GetString(packet.Buffer, position + 1, packet.Buffer[position]);
                position += packet.Buffer[position];
                position++;

                host.Capacity = packet.Buffer[position++];
                host.Fullness = packet.Buffer[position++];

                _gameLogger.Debug($"GameClient: got new packet with ip [{packet.SenderIpAdress}]");
                return(host);
            })
                                       .Subscribe(host =>
            {
                if (!Hosts.Any(h => h.Id == host.Id))
                {
                    _gameLogger.Debug($"GameClient: added new host name[{host.Name}]");
                    Hosts.Add(host);
                }
                else
                {
                    var hostToUpdate      = Hosts.First(h => h.Id == host.Id);
                    hostToUpdate.Name     = host.Name;
                    hostToUpdate.Capacity = host.Capacity;
                    hostToUpdate.Fullness = host.Fullness;
                }
            });
        }