示例#1
0
        public bool AddPlayer(uint playerId, uint sid)
        {
            Debuger.Log(LOG_TAG, "AddPlayer() playerId: {0}, sid: {1}", playerId, sid);

            if (m_State != FSPGameState.Create)
            {
                Debuger.LogError(LOG_TAG, "AddPlayer() 当前状态下无法AddPlayer! State = {0}", m_State);
                return(false);
            }

            // 移除老的
            RemovePlayer(playerId);

            if (m_ListPlayer.Count >= MaxPlayerNum)
            {
                Debuger.LogError(LOG_TAG, "AddPlayer() 玩家数达到上限 {0}", MaxPlayerNum);
                return(false);
            }

            FSPSession session = FSPServer.Instance.AddSession(sid);
            FSPPlayer  player  = new FSPPlayer(playerId, session, m_FSPParam.serverTimeout, OnPlayerReceive);

            m_ListPlayer.Add(player);

            return(true);
        }
示例#2
0
        /// <summary>
        /// 检测游戏是否异常结束
        /// </summary>
        bool CheckGameAbnormalEnd()
        {
            //判断还剩下多少玩家,如果玩家少于2,则表示至少有玩家主动退出
            if (m_ListPlayer.Count < 2)
            {
                //直接进入GameEnd状态
                SetGameState(FSPGameState.GameEnd, (int)FSPGameEndReason.AllOtherExit);
                AddCmdToCurrentFrame(FSPVKeyBase.GAME_END, (int)FSPGameEndReason.AllOtherExit);
                return(true);
            }

            // 检测玩家在线状态
            for (int i = 0; i < m_ListPlayer.Count; i++)
            {
                FSPPlayer player = m_ListPlayer[i];
                if (player.IsLost())
                {
                    m_ListPlayer.RemoveAt(i);
                    FSPServer.Instance.DelSession(player.Sid);
                    player.Dispose();
                    --i;
                }
            }
            //判断还剩下多少玩家,如果玩家少于2,则表示有玩家掉线了
            if (m_ListPlayer.Count < 2)
            {
                //直接进入GameEnd状态
                SetGameState(FSPGameState.GameEnd, (int)FSPGameEndReason.AllOtherLost);
                AddCmdToCurrentFrame(FSPVKeyBase.GAME_END, (int)FSPGameEndReason.AllOtherLost);
                return(true);
            }

            return(false);
        }
示例#3
0
        /// <summary>
        /// 处理来自客户端的 Cmd
        /// 对其中的关键VKey进行处理
        /// 并且收集业务VKey
        /// </summary>
        /// <param name="player"></param>
        /// <param name="cmd"></param>
        protected virtual void HandleClientCmd(FSPPlayer player, FSPVKey cmd)
        {
            // Debuger.Log(LOG_TAG, "HandleClientCmd() cmd: {0}", cmd.ToString());

            uint playerId = player.Id;

            if (!player.HasAuth)
            {
                Debuger.Log(LOG_TAG, "HandleClientCmd() hasAuth = false! Wait for AUTH! the vkey is {0}", cmd.vkey);
                if (cmd.vkey == (int)FSPVKeyBase.AUTH)
                {
                    player.HasAuth = true;
                    // player.HasAuth = cmd.args[0] > 0;
                    Debuger.Log(LOG_TAG, "HandleClientCmd() AUTH, playerId={0}", playerId);
                }
                return;
            }

            switch (cmd.vkey)
            {
            case (int)FSPVKeyBase.GAME_BEGIN:
                Debuger.Log(LOG_TAG, "HandleClientCmd() GAME_BEGIN, playerId = {0}, cmd = {1}", playerId, cmd);
                SetFlag(playerId, ref m_GameBeginFlag, "m_GameBeginFlag");
                break;

            case (int)FSPVKeyBase.ROUND_BEGIN:
                Debuger.Log(LOG_TAG, "HandleClientCmd() ROUND_BEGIN, playerId = {0}, cmd = {1}", playerId, cmd);
                SetFlag(playerId, ref m_RoundBeginFlag, "m_RoundBeginFlag");
                break;

            case (int)FSPVKeyBase.CONTROL_START:
                Debuger.Log(LOG_TAG, "HandleClientCmd() CONTROL_START, playerId = {0}, cmd = {1}", playerId, cmd);
                SetFlag(playerId, ref m_ControlStartFlag, "m_ControlStartFlag");
                break;

            case (int)FSPVKeyBase.ROUND_END:
                Debuger.Log(LOG_TAG, "HandleClientCmd() ROUND_END, playerId = {0}, cmd = {1}", playerId, cmd);
                SetFlag(playerId, ref m_RoundEndFlag, "m_RoundEndFlag");
                break;

            case (int)FSPVKeyBase.GAME_END:
                Debuger.Log(LOG_TAG, "HandleClientCmd() GAME_END, playerId = {0}, cmd = {1}", playerId, cmd);
                SetFlag(playerId, ref m_GameEndFlag, "m_GameEndFlag");
                break;

            case (int)FSPVKeyBase.PLAYER_EXIT:
                Debuger.Log(LOG_TAG, "HandleClientCmd() PLAYER_EXIT, playerId = {0}, cmd = {1}", playerId, cmd);
                HandlePlayerExit(playerId, cmd);
                break;

            default:
                Debuger.Log(LOG_TAG, "HandleClientCmd() other, playerId = {0}, cmd = {1}", playerId, cmd);
                AddCmdToCurrentFrame(playerId, cmd);
                break;
            }
        }
示例#4
0
        public void EnterFrame()
        {
            // 延迟移除,因为还要给些时间发包
            for (int i = 0; i < m_ListPlayersExitOnNextFrame.Count; i++)
            {
                FSPPlayer player = m_ListPlayersExitOnNextFrame[i];
                FSPServer.Instance.DelSession(player.Sid);
                player.Dispose();
            }
            m_ListPlayersExitOnNextFrame.Clear();

            // 处理状态变化
            HandleGameState();

            //经过上面状态处理之后,有可能状态还会发生变化
            if (m_State == FSPGameState.None)
            {
                return;
            }

            if (m_LockedFrame.frameId != 0 || //已经在跑逻辑了
                !m_LockedFrame.IsEmpty()     //可能还在准备阶段
                )
            {
                for (int i = m_ListPlayer.Count - 1; i >= 0; i--)
                {
                    FSPPlayer player = m_ListPlayer[i];
                    player.SendToClient(m_LockedFrame);

                    if (player.WaitForExit)
                    {
                        m_ListPlayer.RemoveAt(i);
                        m_ListPlayersExitOnNextFrame.Add(player);
                    }
                }
            }
            //0帧每个循环需要额外清除掉再重新统计
            if (m_LockedFrame.frameId == 0)
            {
                // 为了保留m_LockedFrame.vkeys,没有用Clear()来清除
                m_LockedFrame = new FSPFrame();
                m_FrameRecords.Add(m_LockedFrame);
            }

            //在这个阶段,帧号才会不停往上加
            if (m_State == FSPGameState.RoundBegin || m_State == FSPGameState.ControlStart)
            {
                m_CurFrameId++;

                m_LockedFrame         = new FSPFrame();
                m_LockedFrame.frameId = m_CurFrameId;

                m_FrameRecords.Add(m_LockedFrame);
            }
        }
示例#5
0
 public FSPPlayer GetPlayer(uint playerId)
 {
     for (int i = 0; i < m_ListPlayer.Count; i++)
     {
         FSPPlayer player = m_ListPlayer[i];
         if (player.Id == playerId)
         {
             return(player);
         }
     }
     return(null);
 }
示例#6
0
        void RemovePlayer(uint playerId)
        {
            for (int i = 0; i < m_ListPlayer.Count; i++)
            {
                FSPPlayer player = m_ListPlayer[i];
                if (player.Id == playerId)
                {
                    m_ListPlayer.RemoveAt(i);

                    FSPServer.Instance.DelSession(player.Sid);
                    player.Dispose();
                    return;
                }
            }
        }
示例#7
0
 public bool IsFlagFull(int flag)
 {
     if (m_ListPlayer.Count > 1)
     {
         for (int i = 0; i < m_ListPlayer.Count; i++)
         {
             FSPPlayer player   = m_ListPlayer[i];
             int       playerId = (int)player.Id;
             if ((flag & (0x01 << (playerId - 1))) == 0)
             {
                 return(false);
             }
         }
         return(true);
     }
     return(false);
 }
示例#8
0
        void HandlePlayerExit(uint playerId, FSPVKey cmd)
        {
            AddCmdToCurrentFrame(playerId, cmd);

            // 退出
            FSPPlayer player = GetPlayer(playerId);

            if (player != null)
            {
                player.WaitForExit = true;

                if (onPlayerExit != null)
                {
                    onPlayerExit(player.Id);
                }
            }
        }
示例#9
0
 void OnPlayerReceive(FSPPlayer player, FSPVKey cmd)
 {
     HandleClientCmd(player, cmd);
 }