//============================================================ /// <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); }
//======================================================================================= /// <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; } }
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); }
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); } } }
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(); }
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); }
//=================================================================== 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); }
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; } } }
//--------------------------------------------------------- //收到客户端Player的Cmd private void OnRecvFromPlayer(FSPPlayer player, FSPMessage msg) { HandleClientCmd(player, msg); }