コード例 #1
0
ファイル: FSPGame.cs プロジェクト: swordlegend/SGF
        //============================================================

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

            // 检测玩家在线状态
            for (int i = 0; i < m_ListPlayer.Count; i++)
            {
                FSPPlayer player = m_ListPlayer[i];
                if (player.IsLose())
                {
                    m_ListPlayer.RemoveAt(i);
                    player.Release();
                    --i;
                }
            }

            //判断还剩下多少玩家,如果玩家少于1,则表示至少有玩家主动退出
            if (m_ListPlayer.Count < 1)
            {
                //直接进入GameEnd状态
                SetGameState(FSPGameState.GameEnd, (int)FSPGameEndReason.AllOtherLost);
                AddBasicCmdToCurrentFrame(FSPBasicCmd.GAME_END, (int)FSPGameEndReason.AllOtherLost);
                return(true);
            }


            return(false);
        }
コード例 #2
0
ファイル: FSPGame.cs プロジェクト: sodatencent/Snake-update
        //---------------------------------------------------------
        /// <summary>
        /// 驱动游戏状态
        /// </summary>
        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())
            {
                //将当前帧扔给Player
                for (int i = 0; i < m_ListPlayer.Count; i++)
                {
                    FSPPlayer player = m_ListPlayer[i];
                    player.SendToClient(m_LockedFrame);
                    if (player.WaitForExit)
                    {
                        m_ListPlayersExitOnNextFrame.Add(player);
                        m_ListPlayer.RemoveAt(i);
                        --i;
                    }
                }
            }

            //0帧每个循环需要额外清除掉再重新统计
            if (m_LockedFrame.frameId == 0)
            {
                m_LockedFrame = new FSPFrame();
                //防止GC
                if (UseDelayGC)
                {
                    m_ListObjectsForDelayGC.Add(m_LockedFrame);
                }
            }


            //在这个阶段,帧号才会不停往上加
            if (m_State == FSPGameState.RoundBegin || m_State == FSPGameState.ControlStart)
            {
                m_CurFrameId++;
                m_LockedFrame         = new FSPFrame();
                m_LockedFrame.frameId = m_CurFrameId;
                //防止GC
                if (UseDelayGC)
                {
                    m_ListObjectsForDelayGC.Add(m_LockedFrame);
                }
            }
        }
コード例 #3
0
        /// <summary>
        /// check if there are player exit abnormally
        /// </summary>
        private bool CheckGameAbnormalEnd()
        {
            //check current player,if less than 2
            if (mPlayerList.Count < 2)
            {
                //enter GameEnd state
                SetGameState(FSPGameState.GameEnd, (int)FSPGameEndState.AllOtherExit);
                AddCmdToCurrentFrame(FSPVKeyBase.GAME_END, (int)FSPGameEndState.AllOtherExit);
                return(true);
            }

            // check player connection status
            for (int i = 0; i < mPlayerList.Count; i++)
            {
                FSPPlayer player = mPlayerList[i];
                if (player.IsLose())
                {
                    mPlayerList.RemoveAt(i);
                    FSPServer.Instance.DelSession(player.Sid);
                    player.Dispose();
                    --i;
                }
            }

            //check again, if player num is less than 2
            if (mPlayerList.Count < 2)
            {
                //enter GameEnd state directly
                SetGameState(FSPGameState.GameEnd, (int)FSPGameEndState.AllOtherLost);
                AddCmdToCurrentFrame(FSPVKeyBase.GAME_END, (int)FSPGameEndState.AllOtherLost);
                return(true);
            }

            return(false);
        }
コード例 #4
0
ファイル: FSPGame.cs プロジェクト: sodatencent/Snake-update
        /// <summary>
        /// 检测游戏是否异常结束
        /// </summary>
        private 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.IsLose())
                {
                    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);
        }
コード例 #5
0
ファイル: FSPGame.cs プロジェクト: swordlegend/SGF
        //=======================================================================================

        /// <summary>
        /// 驱动游戏状态
        /// </summary>
        public void EnterFrame()
        {
            for (int i = 0; i < m_ListPlayersExitOnNextFrame.Count; i++)
            {
                var player = m_ListPlayersExitOnNextFrame[i];
                player.Release();
            }
            m_ListPlayersExitOnNextFrame.Clear();



            //处理游戏状态切换
            HandleGameState();


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



            if (m_LockedFrame.frameId != 0 || !m_LockedFrame.IsEmpty())
            {
                //将当前帧扔级Player
                for (int i = 0; i < m_ListPlayer.Count; i++)
                {
                    FSPPlayer player = m_ListPlayer[i];
                    player.SendToClient(m_LockedFrame);

                    if (player.WaitForExit)
                    {
                        m_ListPlayersExitOnNextFrame.Add(player);
                        m_ListPlayer.RemoveAt(i);
                        --i;
                    }
                }
            }


            //0帧每个循环需要额外清除掉再重新统计
            if (m_LockedFrame.frameId == 0)
            {
                m_LockedFrame = new FSPFrame();
            }


            //在这个阶段,帧号才会不停往上加
            if (m_State == FSPGameState.RoundBegin || m_State == FSPGameState.ControlStart)
            {
                m_CurFrameId++;
                m_LockedFrame         = new FSPFrame();
                m_LockedFrame.frameId = m_CurFrameId;
            }
        }
コード例 #6
0
ファイル: FSPGame.cs プロジェクト: sodatencent/Snake-update
        //---------------------------------------------------------
        //收到客户端Player的Cmd
        private void OnPlayerReceive(FSPPlayer player, FSPVKey cmd)
        {
            //防止GC
            if (UseDelayGC)
            {
                m_ListObjectsForDelayGC.Add(cmd);
            }

            HandleClientCmd(player, cmd);
        }
コード例 #7
0
ファイル: FSPGame.cs プロジェクト: swordlegend/SGF
        private FSPPlayer GetPlayer(uint playerId)
        {
            FSPPlayer player = null;

            for (int i = 0; i < m_ListPlayer.Count; i++)
            {
                player = m_ListPlayer[i];
                if (player.id == playerId)
                {
                    return(player);
                }
            }
            return(null);
        }
コード例 #8
0
ファイル: FSPGame.cs プロジェクト: sodatencent/Snake-update
        private void HandleGameExit(uint playerId, FSPVKey cmd)
        {
            AddCmdToCurrentFrame(playerId, cmd);
            FSPPlayer player = GetPlayer(playerId);

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

                if (onGameExit != null)
                {
                    onGameExit(player.Id);
                }
            }
        }
コード例 #9
0
ファイル: FSPGame.cs プロジェクト: swordlegend/SGF
        private void HandleGameExit(uint playerId, FSPMessage msg)
        {
            AddCmdToCurrentFrame(playerId, msg);

            FSPPlayer player = GetPlayer(playerId);

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

                if (onGameExit != null)
                {
                    onGameExit(player.id);
                }
            }
        }
コード例 #10
0
ファイル: FSPGame.cs プロジェクト: swordlegend/SGF
        public void Release()
        {
            SetGameState(FSPGameState.None);

            for (int i = 0; i < m_ListPlayer.Count; i++)
            {
                FSPPlayer player = m_ListPlayer[i];
                player.Release();
            }
            m_ListPlayer.Clear();
            m_ListPlayersExitOnNextFrame.Clear();

            onGameExit = null;
            onGameEnd  = null;

            Debuger.Log();
        }
コード例 #11
0
ファイル: FSPGame.cs プロジェクト: sodatencent/Snake-update
        public void Dispose()
        {
            SetGameState(FSPGameState.None);
            for (int i = 0; i < m_ListPlayer.Count; i++)
            {
                FSPPlayer player = m_ListPlayer[i];
                FSPServer.Instance.DelSession(player.Sid);
                player.Dispose();
            }
            m_ListPlayer.Clear();
            m_ListObjectsForDelayGC.Clear();
            GC.Collect();
            onGameExit = null;
            onGameEnd  = null;

            Debuger.Log(LOG_TAG, "Dispose()");
        }
コード例 #12
0
ファイル: FSPGame.cs プロジェクト: swordlegend/SGF
        public bool IsFlagFull(int flag)
        {
            if (m_ListPlayer.Count > 0)
            {
                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);
        }
コード例 #13
0
ファイル: FSPGame.cs プロジェクト: sodatencent/Snake-update
        //---------------------------------------------------------
        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);
            }

            FSPPlayer player = null;

            for (int i = 0; i < m_ListPlayer.Count; i++)
            {
                player = m_ListPlayer[i];
                if (player.Id == playerId)
                {
                    Debuger.LogWarning(LOG_TAG, "AddPlayer() PlayerId已经存在!用新的替代旧的! PlayerId = " + playerId);
                    m_ListPlayer.RemoveAt(i);
                    FSPServer.Instance.DelSession(player.Sid);
                    player.Dispose();
                    break;
                }
            }

            if (m_ListPlayer.Count >= MaxPlayerNum)
            {
                Debuger.LogError(LOG_TAG, "AddPlayer() 已经达到最大玩家数了! MaxPlayerNum = {0}", MaxPlayerNum);
                return(false);
            }

            FSPSession session = FSPServer.Instance.AddSession(sid);

            player = new FSPPlayer(playerId, m_FSPParam.serverTimeout, session, OnPlayerReceive);
            m_ListPlayer.Add(player);

            return(true);
        }
コード例 #14
0
        //---------------------------------------------------------
        public bool AddPlayer(uint playerId, uint sid)
        {
            MyLogger.Log(LOG_TAG, "AddPlayer() playerId:{0}, sid:{1}", playerId.ToString(), sid);

            if (mState != FSPGameState.Create)
            {
                MyLogger.LogError(LOG_TAG, "AddPlayer() cannot create player in current state! State = {0}", mState.ToString());
                return(false);
            }

            FSPPlayer player = null;

            for (int i = 0; i < mPlayerList.Count; i++)
            {
                player = mPlayerList[i];
                if (player.Id == playerId)
                {
                    MyLogger.LogWarning(LOG_TAG, "AddPlayer()", " PlayerId used!replace with new id! PlayerId = " + playerId);
                    mPlayerList.RemoveAt(i);
                    FSPServer.Instance.DelSession(player.Sid);
                    player.Dispose();
                    break;
                }
            }

            if (mPlayerList.Count >= MaxPlayerNum)
            {
                MyLogger.LogError(LOG_TAG, "AddPlayer() maximum player reached! MaxPlayerNum = {0}", MaxPlayerNum.ToString());
                return(false);
            }

            FSPSession session = FSPServer.Instance.AddSession(sid);

            player = new FSPPlayer(playerId, mFSPParam.serverTimeout, session, OnPlayerReceive);
            mPlayerList.Add(player);

            return(true);
        }
コード例 #15
0
ファイル: FSPGame.cs プロジェクト: swordlegend/SGF
        //===================================================================

        public FSPPlayer AddPlayer(uint playerId, FSPSession session)
        {
            Debuger.Log("playerId:{0}", playerId);

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

            FSPPlayer player = null;

            for (int i = 0; i < m_ListPlayer.Count; i++)
            {
                player = m_ListPlayer[i];
                if (player.id == playerId)
                {
                    Debuger.LogWarning("PlayerId已经存在!用新的替代旧的! PlayerId = " + playerId);
                    m_ListPlayer.RemoveAt(i);
                    player.Release();
                    break;
                }
            }

            if (m_ListPlayer.Count >= MaxPlayerNum)
            {
                Debuger.LogError("已经达到最大玩家数了! MaxPlayerNum = {0}", MaxPlayerNum);
                return(null);
            }


            player = new FSPPlayer();
            player.Create(playerId, m_authId, session, OnRecvFromPlayer);
            m_ListPlayer.Add(player);

            return(player);
        }
コード例 #16
0
ファイル: FSPGame.cs プロジェクト: sodatencent/Snake-update
        //---------------------------------------------------------

        /// <summary>
        /// 处理来自客户端的 Cmd
        /// 对其中的关键VKey进行处理
        /// 并且收集业务VKey
        /// </summary>
        /// <param name="player"></param>
        /// <param name="cmd"></param>
        protected virtual void HandleClientCmd(FSPPlayer player, FSPVKey cmd)
        {
            uint playerId = player.Id;

            //处理鉴权
            if (!player.HasAuth)
            {
                Debuger.Log(LOG_TAG, "HandleClientCmd() hasAuth = false! Wait AUTH!");
                if (cmd.vkey == FSPVKeyBase.AUTH)
                {
                    Debuger.Log(LOG_TAG, "HandleClientCmd() AUTH, playerId={0}", playerId);
                    player.SetAuth(cmd.args[0]);
                }
                return;
            }


            switch (cmd.vkey)
            {
            case 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 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 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 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 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 FSPVKeyBase.GAME_EXIT:
            {
                Debuger.Log(LOG_TAG, "HandleClientCmd() GAME_EXIT, playerId = {0}, cmd = {1}", playerId, cmd);
                HandleGameExit(playerId, cmd);
                break;
            }

            default:
            {
                Debuger.Log(LOG_TAG, "HandleClientCmd() playerId = {0}, cmd = {1}", playerId, cmd);
                AddCmdToCurrentFrame(playerId, cmd);
                break;
            }
            }
        }
コード例 #17
0
        //---------------------------------------------------------
        /// <summary>
        /// enter frame
        /// </summary>
        public void EnterFrame()
        {
            //delete exited players
            for (int i = 0; i < mPlayersExitOnNextFrameList.Count; i++)
            {
                FSPPlayer player = mPlayersExitOnNextFrameList[i];
                FSPServer.Instance.DelSession(player.Sid);
                player.Dispose();
            }
            mPlayersExitOnNextFrameList.Clear();

            //hand game state
            HandleGameState();

            //check if game state changes
            if (mState == FSPGameState.None)
            {
                return;
            }

            if (mLockedFrame.frameId != 0 || !mLockedFrame.IsEmpty())
            {
                //send frame to players
                for (int i = 0; i < mPlayerList.Count; i++)
                {
                    FSPPlayer player = mPlayerList[i];
                    player.SendToClient(mLockedFrame);
                    if (player.WaitForExit)
                    {
                        mPlayersExitOnNextFrameList.Add(player);
                        mPlayerList.RemoveAt(i);
                        --i;
                    }
                }
            }

            //clear frame 0 in each iteration
            if (mLockedFrame.frameId == 0)
            {
                mLockedFrame = new FSPFrame();
                //delay GC
                if (UseDelayGC)
                {
                    mListObjectsForDelayGC.Add(mLockedFrame);
                }
            }


            //add up frame id
            if (mState == FSPGameState.RoundBegin || mState == FSPGameState.ControlStart)
            {
                mCurFrameId++;
                mLockedFrame         = new FSPFrame();
                mLockedFrame.frameId = mCurFrameId;
                //防止GC
                if (UseDelayGC)
                {
                    mListObjectsForDelayGC.Add(mLockedFrame);
                }
            }
        }
コード例 #18
0
ファイル: FSPGame.cs プロジェクト: swordlegend/SGF
        protected virtual void HandleClientCmd(FSPPlayer player, FSPMessage msg)
        {
            uint playerId = player.id;

            //处理鉴权
            if (!player.HasAuthed)
            {
                if (msg.cmd == FSPBasicCmd.AUTH)
                {
                    player.SetAuth(msg.args[0]);
                }
                else
                {
                    Debuger.LogWarning("当前Player未鉴权,无法处理该Cmd:{0}", msg.cmd);
                }
                return;
            }

            switch (msg.cmd)
            {
            case FSPBasicCmd.GAME_BEGIN:
            {
                Debuger.Log("GAME_BEGIN, playerId = {0}, cmd = {1}", playerId, msg);
                SetFlag(playerId, ref m_GameBeginFlag, "m_GameBeginFlag");
                break;
            }

            case FSPBasicCmd.ROUND_BEGIN:
            {
                Debuger.Log("ROUND_BEGIN, playerId = {0}, cmd = {1}", playerId, msg);
                SetFlag(playerId, ref m_RoundBeginFlag, "m_RoundBeginFlag");
                break;
            }

            case FSPBasicCmd.CONTROL_START:
            {
                Debuger.Log("CONTROL_START, playerId = {0}, cmd = {1}", playerId, msg);
                SetFlag(playerId, ref m_ControlStartFlag, "m_ControlStartFlag");
                break;
            }

            case FSPBasicCmd.ROUND_END:
            {
                Debuger.Log("ROUND_END, playerId = {0}, cmd = {1}", playerId, msg);
                SetFlag(playerId, ref m_RoundEndFlag, "m_RoundEndFlag");
                break;
            }

            case FSPBasicCmd.GAME_END:
            {
                Debuger.Log("GAME_END, playerId = {0}, cmd = {1}", playerId, msg);
                SetFlag(playerId, ref m_GameEndFlag, "m_GameEndFlag");
                break;
            }

            case FSPBasicCmd.GAME_EXIT:
            {
                Debuger.Log("GAME_EXIT, playerId = {0}, cmd = {1}", playerId, msg);
                HandleGameExit(playerId, msg);
                break;
            }

            default:
            {
                Debuger.Log("playerId = {0}, cmd = {1}", playerId, msg);
                AddCmdToCurrentFrame(playerId, msg);
                break;
            }
            }
        }
コード例 #19
0
ファイル: FSPGame.cs プロジェクト: swordlegend/SGF
 //---------------------------------------------------------
 //收到客户端Player的Cmd
 private void OnRecvFromPlayer(FSPPlayer player, FSPMessage msg)
 {
     HandleClientCmd(player, msg);
 }