예제 #1
0
        private void Players_PlayerRequesting(PlayerRequestArgs args)
        {
            if (args.PlayerId.SerialId == 0)
                return;

            if (!CanSpawnMoreBots(args.PlayerId))
                args.Cancel = true;
            else
                Bots.TotalBotCount++;
        }
        static void OnNewPlayerRequest(ref NewPlayerRequestMsg msg, MyNetworkClient sender)
        {
            if (msg.ClientSteamId != sender.SteamUserId)
            {
                Debug.Assert(false, "A client requested player for another client!");
                return;
            }

            var playerId = new MyPlayer.PlayerId(msg.ClientSteamId, msg.PlayerSerialId);
            if (Sync.Players.m_players.ContainsKey(playerId))
                return;
            if (Sync.Players.PlayerRequesting != null)
            {
                var args = new PlayerRequestArgs(playerId);
                Sync.Players.PlayerRequesting(args);
                if (args.Cancel)
                {
                    var failMsg = new NewPlayerFailureMsg();
                    failMsg.ClientSteamId = msg.ClientSteamId;
                    failMsg.PlayerSerialId = msg.PlayerSerialId;

                    Sync.Layer.SendMessage(ref failMsg, sender.SteamUserId, MyTransportMessageEnum.Failure);
                    return;
                }
            } 

            var identity = Sync.Players.TryGetPlayerIdentity(playerId);
            if (identity == null)
            {
                identity = Sync.Players.RespawnComponent.CreateNewIdentity(msg.DisplayName, playerId, msg.CharacterModel);
            }

            Sync.Players.CreateNewPlayer(identity, playerId, msg.DisplayName);

            var response = new NewPlayerSuccessMsg();
            response.ClientSteamId = msg.ClientSteamId;
            response.PlayerSerialId = msg.PlayerSerialId;

            Sync.Layer.SendMessage(ref response, sender.SteamUserId);
        }
        static void OnNewPlayerRequest(ulong clientSteamId, int playerSerialId, string displayName, [Serialize(MyObjectFlags.Nullable)] string characterModel)
        {
            if (!MyEventContext.Current.IsLocallyInvoked && clientSteamId != MyEventContext.Current.Sender.Value)
            {
                Debug.Assert(false, "A client requested player for another client!");
                return;
            }

            var playerId = new MyPlayer.PlayerId(clientSteamId, playerSerialId);
            if (Sync.Players.m_players.ContainsKey(playerId))
                return;
            if (Sync.Players.PlayerRequesting != null)
            {
                var args = new PlayerRequestArgs(playerId);
                Sync.Players.PlayerRequesting(args);
                if (args.Cancel)
                {
                    if (MyEventContext.Current.IsLocallyInvoked)
                        OnNewPlayerFailure(clientSteamId, playerSerialId);
                    else
                        MyMultiplayer.RaiseStaticEvent(s => MyPlayerCollection.OnNewPlayerFailure, clientSteamId, playerSerialId, MyEventContext.Current.Sender);

                    return;
                }
            }

            var identity = Sync.Players.TryGetPlayerIdentity(playerId);
            if (identity == null)
            {
                identity = Sync.Players.RespawnComponent.CreateNewIdentity(displayName, playerId, characterModel);
            }

            Sync.Players.CreateNewPlayer(identity, playerId, identity.DisplayName);

            if (MyEventContext.Current.IsLocallyInvoked)
                OnNewPlayerSuccess(clientSteamId, playerSerialId);
            else
                MyMultiplayer.RaiseStaticEvent(s => MyPlayerCollection.OnNewPlayerSuccess, clientSteamId, playerSerialId, MyEventContext.Current.Sender);
        }