public void AllowEnter(ref MyEventEnterGame msg) { var playerList = msg.ConnectedPlayers; bool allConnected = MyMultiplayerPeers.Static.Players.All(s => playerList.Contains(s.UserId)); if (allConnected) { // Generate game user id and send back var response = new MyEventEnterGameResponse(); response.Allowed = true; response.PlayerId = GeneratePlayerId(); var newPlayer = new MyPlayerRemote(new StringBuilder(msg.PlayerInfo.DisplayName), msg.PlayerInfo.UserId, response.PlayerId); newPlayer.Connection = msg.SenderConnection; msg.SenderConnection.Tag = newPlayer; newPlayer.Faction = MyMwcObjectBuilder_FactionEnum.None; MyMultiplayerPeers.Static.Players.Add(newPlayer); Peers.NetworkClient.Send(ref response, msg.SenderConnection, NetDeliveryMethod.ReliableOrdered, 0); } else { SendPlayerList(msg.SenderConnection); } }
void m_peer_PeerDisconnected(NetConnection connection) { if (connection == null) { return; } MyPlayerRemote player = connection.Tag as MyPlayerRemote; if (connection == m_serverConnection) { NetworkClient.RelayServerConnection = null; var handler = ServerDisconnected; if (handler != null) { handler(connection); } } else if (player != null && Players.Contains(player)) { Players.Remove(player); var handler = PlayerDisconnected; if (handler != null) { handler(player); } } }
void OnNewPlayer(ref MyEventNewPlayer msg) { Log("OnNewPlayer"); if (NewPlayer != null) { NewPlayer(); } if (IsHost) { SendCheckpoint(msg.SenderConnection); var playerLeftMsg = new MyEventPlayerStateChanged(); playerLeftMsg.UserId = msg.PlayerInfo.UserId; playerLeftMsg.NewState = MyMultiplayerStateEnum.Playing; Peers.SendServer(ref playerLeftMsg); UpdateMission(); } else { var player = new MyPlayerRemote(new StringBuilder(msg.PlayerInfo.DisplayName), msg.PlayerInfo.UserId, msg.PlayerInfo.PlayerId); player.Connection = msg.SenderConnection; player.Connection.Tag = player; Peers.Players.Add(player); Notify(MyTextsWrapperEnum.MP_XHasJoined, msg.PlayerInfo.DisplayName); } LogPlayers(); }
private MyPlayerInfo RemotePlayerToInfo(MyPlayerRemote player) { return(new MyPlayerInfo() { DisplayName = player.GetDisplayName().ToString(), PlayerId = player.PlayerId, UserId = player.UserId, Faction = player.Faction, }); }
void OnStatsUpdate(ref MyEventStatsUpdate msg) { MyPlayerRemote sender = (MyPlayerRemote)msg.SenderConnection.Tag; if (sender != null) { sender.Statistics = new MyPlayerStatistics(); sender.Statistics.Init(msg.StatsBuilder); } }
void Peers_PlayerDisconnected(MyPlayerRemote player) { if (player.UserId == Peers.HostUserId) { // TODO: Connection to host lost, shutdown var handler = OnShutdown; if (handler != null) { handler(); } } else { // Close all entities of disconnected player foreach (var e in MyEntities.GetEntities()) { if (e.EntityId.HasValue && e.EntityId.Value.PlayerId == player.PlayerId) { e.MarkForClose(); } } } if (player.Ship != null) { player.Ship.MarkForClose(); player.Ship = null; } if (IsHost) { ClearLocks(player.PlayerId); var playerLeftMsg = new MyEventPlayerStateChanged(); playerLeftMsg.UserId = player.UserId; playerLeftMsg.NewState = MyMultiplayerStateEnum.Disconnected; Peers.SendServer(ref playerLeftMsg); } Notify(MyTextsWrapperEnum.MP_XHasLeft, player.GetDisplayName()); }
/// <summary> /// True when message should be passed to handler. /// False to prevent handler. /// </summary> bool MessageFilter(NetConnection connection, IPEndPoint endpoint, MyEventEnum eventType) { MyPlayerRemote player = connection.Tag as MyPlayerRemote; if (player != null) { // Accept known players return(true); } else if (connection == m_serverConnection) { // Accept server messages return(true); } else if (connection != null && (eventType == MyEventEnum.NEW_PLAYER || eventType == MyEventEnum.ENTER_GAME || eventType == MyEventEnum.GET_PLAYER_LIST || eventType == MyEventEnum.GET_PLAYER_LIST_RESPONSE)) { // Accept incoming new players return(true); } return(false); }
void OnGetPlayerListResponse(ref MyEventGetPlayerListResponse msg) { MyMultiplayerGameplay.LogPlayerList(ref msg); // Disconnect from players (connection to host is not dropped) foreach (var player in MyMultiplayerPeers.Static.Players) { if (player.UserId != MyMultiplayerPeers.Static.HostUserId) { player.Connection.Disconnect(String.Empty); } } MyMultiplayerPeers.Static.Players.Clear(); // Start connecting players again foreach (var playerInfo in msg.PlayerList) { var playerRemote = new MyPlayerRemote(new StringBuilder(playerInfo.DisplayName), playerInfo.UserId, playerInfo.PlayerId); if (playerRemote.UserId == MyMultiplayerPeers.Static.HostUserId) { playerRemote.Connection = msg.SenderConnection; msg.SenderConnection.Tag = playerRemote; } MyMultiplayerPeers.Static.Players.Add(playerRemote); } MyMultiplayerGameplay.Log("JOIN 4.a - Player list received (#OnGetPlayerListResponse)"); m_natIntroduction = new MyNatIntroduction(MyMultiplayerPeers.Static.NetworkClient); m_natIntroduction.SetRequiredPlayers(MyMultiplayerPeers.Static.Players); ClearCallbacks(); var userListCopy = MyMultiplayerPeers.Static.Players.Where(s => s != null).Select(s => s.UserId).ToList(); StartClientIntroductionTimer(userListCopy); RequestPlayerIntroduction(userListCopy, false); UpdateLobby(); }
public bool TryGetPlayer(int userId, out MyPlayerRemote player) { player = Players.FirstOrDefault(p => p.UserId == userId); return(player != null); }
private void AddPlayer(MyPlayerRemote player, bool isFaction = false) { AddPlayer(player.GetDisplayName(), (int)(player.Connection.AverageRoundtripTime * 1000), player.Statistics.Deaths, player.Statistics.PlayersKilled, (byte)player.PlayerId, player.Faction, isFaction); }