Example #1
0
        public void ConnectToServer(IPAddress hostname, int port, Action onSuccess = null, Action onFail = null)
        {
            if (StartupTask != null)
            {
                throw new Exception("Trying to connect to server while network is already trying to start something else");
            }

            if (Role != NetworkRole.None)
            {
                Disconnect();
            }

            Role      = NetworkRole.Client;
            GameState = NetworkGameState.Connecting;

            client = new Client(new IPEndPoint(hostname, port));

            client.Connected += (s, e) =>
            {
                GameState = NetworkGameState.InLobby;
                RaiseMessageEvent(NetworkMessage.MessageType.ConnectedToServer);
                onSuccess?.Invoke();
            };

            client.PlayerListChanged += (s, e) => { PlayerListChanged?.Invoke(s, e); };

            client.Start();
        }
Example #2
0
        private void OnPlayerInfoRemoval(PlayerInfoRemovalMessage ev)
        {
            if (_playerList == null)
            {
                _playerList = new();
            }

            _playerList.Remove(ev.NetUserId);
            PlayerListChanged?.Invoke(_playerList.Values.ToList());
        }
Example #3
0
        private void OnPlayerInfoChanged(PlayerInfoChangedEvent ev)
        {
            if (ev.PlayerInfo == null)
            {
                return;
            }

            if (_playerList == null)
            {
                _playerList = new();
            }

            _playerList[ev.PlayerInfo.SessionId] = ev.PlayerInfo;
            PlayerListChanged?.Invoke(_playerList.Values.ToList());
        }
Example #4
0
        private void OnRoundRestartCleanup(RoundRestartCleanupEvent msg, EntitySessionEventArgs args)
        {
            if (_playerList == null)
            {
                return;
            }

            foreach (var(id, playerInfo) in _playerList.ToArray())
            {
                if (playerInfo.Connected)
                {
                    continue;
                }
                _playerList.Remove(id);
            }
            PlayerListChanged?.Invoke(_playerList.Values.ToList());
        }
Example #5
0
        private void HandleNewPlayer(NetConnection sender, LobbyPlayerGreeting player)
        {
            long remoteId = sender == null ? -1 : sender.RemoteUniqueIdentifier;

            PlayersByConnection[remoteId] = new PlayerConnection
            {
                Connection = sender,
                PlayerName = player.PlayerName
            };

            var lobbyPlayerList = new LobbyPlayerList {
                PlayerNames = PlayersByConnection.Values.Select(p => p.PlayerName).ToArray()
            };

            PlayerListChanged?.Invoke(this, lobbyPlayerList);
            Broadcast(lobbyPlayerList);
        }
Example #6
0
        private void OnData(object data)
        {
            if (data is LobbyPlayerList)
            {
                PlayerListChanged?.Invoke(this, data as LobbyPlayerList);
            }

            if (data is GameStartInfo)
            {
                HandleMatchStart(data as GameStartInfo);
            }

            if (data is Session.NetSessionState)
            {
                Ballz.The().Match.ApplyState(data as Session.NetSessionState);
            }

            // Entities
            var entity = data as Entity;

            if (entity != null)
            {
                // Same entity already exists?
                var localEntity = Ballz.The().Match.World.EntityById(entity.ID);
                if (localEntity != null)
                {
                    ObjectSync.Sync.SyncState(entity, localEntity);
                }
                else
                {
                    Ballz.The().Match.World.AddEntity(entity);
                }
                return;
            }

            var terrainModification = data as Terrain.TerrainModification;

            if (terrainModification != null)
            {
                Ballz.The().Match.World.StaticGeometry.ApplyModification(terrainModification);
                return;
            }
        }
Example #7
0
 private void OnPlayerListChanged(FullPlayerListEvent msg)
 {
     _playerList = msg.PlayersInfo.ToDictionary(x => x.SessionId, x => x);
     PlayerListChanged?.Invoke(msg.PlayersInfo);
 }