Ejemplo n.º 1
0
        public void Process()
        {
            KeyValuePair<int, byte[][]>[] whispers = new KeyValuePair<int, byte[][]>[_receivedWhispers.Count];
            lock (_sync_whispers)
            {
                _sendWhispers.CopyTo(whispers, 0);
                _sendWhispers.Clear();
            }
            foreach (var whisper in whispers)
            {
                var playerid = BitConverter.ToInt32(whisper.Value[0],0);
                switch ((DDZActions)BitConverter.ToInt32(whisper.Value[1],0))
                {
                    case DDZActions.C_能否进入:
                        处理_能否进入(playerid);
                        break;
                    case DDZActions.C_进入:
                        处理_进入(playerid);
                        break;
                    case DDZActions.C_准备:
                        处理_准备(playerid);
                        break;
                    case DDZActions.C_叫地主:
                        处理_叫地主(playerid);
                        break;
                    case DDZActions.C_不叫:
                        处理_不叫(playerid);
                        break;
                    case DDZActions.C_出牌:
                        处理_出牌(playerid, whisper.Value);
                        break;
                    case DDZActions.C_Pass:
                        处理_Pass(playerid);
                        break;
                    case DDZActions.GM_请求服务数据:
                        处理_GM_请求服务数据();
                        break;
                }
            }
            #region 游戏状态管理
            if (serviceState == ServiceStates.等待客户端进入)
            {
                var h = _currentStateHander as IWatingJoin;
                if (h.JoinSuccess(_players))
                {
                    foreach (var player in _players)
                    {
                        player.Value.超时_准备超时 = GameLooper.Counter + 30; //30秒准备时间
                    }
                    serviceState = ServiceStates.等待客户端准备好;
                }
            }
            else if (serviceState == ServiceStates.等待客户端准备好)
            {
                var h = _currentStateHander as IWatingReady;

                if (!_players.IsInit)
                {
                    List<int> tempPlayerIDs = new List<int>();
                    foreach (var tempPlayer in _players)
                    {
                        tempPlayerIDs.Add(tempPlayer.Key);
                    }
                    _players.顺序索引(0).前面的玩家 = _players.顺序索引(2);
                    _players.顺序索引(0).后面的玩家 = _players.顺序索引(1);
                    _players.顺序索引(1).前面的玩家 = _players.顺序索引(0);
                    _players.顺序索引(1).后面的玩家 = _players.顺序索引(2);
                    _players.顺序索引(2).前面的玩家 = _players.顺序索引(1);
                    _players.顺序索引(2).后面的玩家 = _players.顺序索引(0);
                    _players.IsInit = true;
                }

                if (h.EveryOneIsReady(_players))
                {
                    发出_开始游戏();
                    serviceState = ServiceStates.正在游戏;
                }
            }
            else if (serviceState == ServiceStates.正在游戏)
            {

            }
            #endregion
        }
Ejemplo n.º 2
0
        public void Process()
        {
            #region 处理消息
            KeyValuePair<int, byte[][]>[] whispers;
            lock (_sync_whispers)
            {
                whispers = new KeyValuePair<int, byte[][]>[_receivedWhispers.Count];
                _receivedWhispers.CopyTo(whispers, 0);
                _receivedWhispers.Clear();
            }
            foreach (var whisper in whispers)
            {
                var id = whisper.Key;
                var rollAction = (RollActions)BitConverter.ToInt32(whisper.Value[0], 0);
                switch (rollAction)
                {
                    case RollActions.C_能否进入:
                        处理_能进否(id);
                        break;
                    case RollActions.C_进入:
                        处理_进入(id);
                        break;
                    case RollActions.C_准备:
                        处理_准备(id);
                        break;
                    case RollActions.C_投掷:
                        处理_投掷(id);
                        break;
                }
            }

            #region 处理超时

            // 根据当前游戏的进展,分别判断玩家的超时并处理(踢除或强制状态改变)
            lock (_sync_players)
            {
                var counter = GameLooper.Counter;
                var removeIds = new List<int>();
                var autoThrowPlayers = new List<KeyValuePair<int,Character>>();
                foreach (var kvp in _players)
                {
                    var player = kvp.Value.Value;
                    var playerId = kvp.Key;
                    if (player.clientState == ClientStates.已发_能进否)
                    {
                        if (player.超时_进入超时 <= counter) removeIds.Add(playerId);
                    }
                    else if ((player.clientState == ClientStates.已发_要求进入 || player.clientState == ClientStates.已发_已掷骰子) && serviceState == ServiceStates.等待客户端准备好)
                    {
                        if (player.超时_准备超时 <= counter) removeIds.Add(playerId);
                    }
                    else if (player.clientState == ClientStates.已发_已准备好 && serviceState == ServiceStates.正在游戏)
                    {
                        if (player.超时_投掷超时 <= counter) autoThrowPlayers.Add(new KeyValuePair<int,Character>(playerId, player));
                    }
                }
                foreach (var playerId in removeIds)
                {
                    _players.Remove(playerId); //删除进入超时,准备超时玩家
                    发出_踢出(playerId);
                    w.WL("踢出 " + playerId.ToString() + Environment.NewLine);
                }
                foreach (var player in autoThrowPlayers)
                {

                    _currentStateHander.Throw(player.Value);//为投掷超时玩家自动投掷
                    发出_点数(player.Key);
                    player.Value.clientState = ClientStates.已发_已掷骰子;
                    w.WL("玩家 " + player.Key + "投掷超时,服务器已自动投掷骰子,点数为 " + player.Value.Num.ToString() + Environment.NewLine);
                }
            }

            #endregion
            #endregion
            #region 游戏进度指示
            if (serviceState == ServiceStates.等待客户端进入)
            {
                var h = _currentStateHander as IWatingJoin;
                if (h.JoinSuccess(_players))
                {
                    serviceState = ServiceStates.等待客户端准备好;
                }
            }
            else if (serviceState == ServiceStates.等待客户端准备好)
            {
                var h = _currentStateHander as IWatingReady;
                if (h.EveryOneIsReady(_players))
                {
                    foreach (var player in _players)
                    {
                        发出_请投掷(player.Value.Key);
                        player.Value.Value.超时_投掷超时 = GameLooper.Counter + 30;
                    }
                    serviceState = ServiceStates.正在游戏;
                }
            }
            else if (serviceState == ServiceStates.正在游戏)
            {
                var h = _currentStateHander as IWatingThrow;
                if (h.EveryOneIsThrew(_players))
                {
                    foreach (var player in _players)
                    {
                        发出_请准备(player.Value.Key);
                        player.Value.Value.超时_准备超时 = GameLooper.Counter + 30;
                    }
                    发出_结果();
                    serviceState = ServiceStates.等待客户端准备好;
                }
            }
            #endregion
        }
Ejemplo n.º 3
0
 private void OnServiceStateChanged(ServiceStates state)
 {
     if (this.ServiceStateChanged != null)
     {
         Debug.WriteLine("ObjectDetectionStateChange: " + this.ServiceState);
         this.ServiceStateChanged(this, new ServiceStateEventArgs() { State = state });
     }
 }
Ejemplo n.º 4
0
        }        //ProcessEngineEvents()

        //
        //
        //
        // *****************************************************************
        // ****					Process FrontEndRequest					****
        // *****************************************************************
        /// <summary>
        /// These are my internal requests, used to create new GUI displays, etc.
        /// </summary>
        /// <param name="request"></param>
        private void ProcessFrontEndRequest(FrontEndRequest request)
        {
            EngineEventArgs e;

            Log.NewEntry(LogLevel.Minor, "FrontEndRequest {0}", request.ToString());
            switch (request.Type)
            {
            case FrontEndRequest.Request.Start:
                // ************************************
                // ***          Start               ***
                // ************************************
                m_ServiceState = ServiceStates.Running;                 // We are ready to run.
                OnServiceStateChanged();
                break;

            case FrontEndRequest.Request.Stop:
                // ************************************
                // ***          Stop                ***
                // ************************************
                if (m_ServiceState != ServiceStates.Stopping)
                {                                            // Shut down displays
                    m_ServiceState = ServiceStates.Stopping; // Trying to stop.
                    OnServiceStateChanged();
                    // Tell displays to shut down.
                    foreach (ClusterDisplay display in m_ClusterDisplays.Values)
                    {
                        if (display != null && (!display.IsDisposed))
                        {
                            try
                            {
                                display.Invoke((System.Threading.ThreadStart) delegate() { display.Close(); });
                            }
                            catch (Exception)
                            {
                            }
                        }
                    }
                }
                // Verify we can stop.
                if (m_ClusterDisplays.Count == 0)
                {       // All displays are removed and shutdown.  We can exit now.
                    m_ServiceState = ServiceStates.Stopped;
                    OnServiceStateChanged();
                    base.Stop();
                }
                else
                {
                    Log.NewEntry(LogLevel.Major, "Shutting down. {0} displays remaining.", m_ClusterDisplays.Count);
                    m_PendingRequests.Enqueue(request);
                }
                break;

            case FrontEndRequest.Request.AddService:
                // ************************************
                // ***          Add Service         ***
                // ************************************
                // This is called internally, each time a new service is added
                // to the application.  We attempt to get information about it.
                if (request.ObjectList == null)
                {
                    return;
                }
                for (int i = 0; i < request.ObjectList.Count; ++i)
                {
                    string serviceName = (string)request.ObjectList[i];
                    bool   isGoodToAdd = true;
                    // First check whether this type of engine is we want to ignore.
                    foreach (string pattern in m_IgnoreEngineNamePatterns)
                    {
                        if (serviceName.Contains(pattern))
                        {
                            isGoodToAdd = false;
                            break;
                        }
                    }
                    if (isGoodToAdd == false)
                    {
                        return;
                    }
                    IService iService;
                    if (m_AppServices.TryGetService(serviceName, out iService) && iService is IEngineHub)
                    {       // We have discovered a new EngineHub
                        if (!m_RemoteEngineHubs.ContainsKey(serviceName))
                        {   // This new engineHub isunknown to us.
                            IEngineHub iEngine = (IEngineHub)iService;
                            m_RemoteEngineHubs.Add(serviceName, iEngine);
                            Log.NewEntry(LogLevel.Major, "Adding new EngineHub {0}.  Requesting all controls.", serviceName);
                            iEngine.EngineChanged += new EventHandler(this.HubEventEnqueue);
                            e = EngineEventArgs.RequestAllControls(serviceName);
                            iEngine.HubEventEnqueue(e);
                        }
                    }
                }
                break;

            case FrontEndRequest.Request.AddDisplay:
                // ************************************
                // ***          Add Display         ***
                // ************************************
                // When a new display is requested, we search for the GuiTemplates already
                // stored for that new display, create the display and hand them to the display.
                List <string> engineHubNames = new List <string>();
                if (request.ObjectList == null || (request.ObjectList.Count == 1 && request.ObjectList[0] == null))
                {
                    engineHubNames.AddRange(m_RemoteEngineHubs.Keys);
                }
                else
                {
                    foreach (object o in request.ObjectList)
                    {
                        if (o is string)
                        {
                            engineHubNames.Add((string)o);
                        }
                    }
                }
                // Open displays for each hub name provided.
                foreach (string engineName in engineHubNames)
                {
                    Dictionary <int, GuiTemplates.EngineContainerGui> engineHubTemplates = null;
                    IEngineHub iEngineHub = null;
                    if (m_RemoteEngineHubs.TryGetValue(engineName, out iEngineHub) && m_EngineHubTemplates.TryGetValue(engineName, out engineHubTemplates))
                    {
                        Log.NewEntry(LogLevel.Major, "AddDisplay requesting new ClusterDisplay for {0}.", engineName);
                        List <GuiTemplates.EngineContainerGui> templates = new List <GuiTemplates.EngineContainerGui>(engineHubTemplates.Values);
                        Utilities.GuiCreator creator = Utilities.GuiCreator.Create(typeof(ClusterDisplay), this, iEngineHub, templates);
                        creator.FormCreated += new EventHandler(this.HubEventEnqueue);          // push created form on to my usual queue.
                        creator.Start();
                    }
                    else
                    {
                        Log.NewEntry(LogLevel.Major, "AddDisplay request rejected for {0}.  No templates found.", engineName);
                    }
                }
                break;

            case FrontEndRequest.Request.RemoveDisplay:
                // ************************************
                // ***			Remove Display		***
                // ************************************
                ClusterDisplay display2 = (ClusterDisplay)request.ObjectList[0];
                int            n        = display2.ID;
                if (m_ClusterDisplays.ContainsKey(n))
                {
                    m_ClusterDisplays.Remove(n);
                    Log.NewEntry(LogLevel.Major, "RemoveDisplay id {1}. Remaining {0}.", m_ClusterDisplays.Count, n);
                }
                break;

            default:
                Log.NewEntry(LogLevel.Warning, "Unknown request.");
                break;
            }    //requestType switch
        }        //ProcessFrontEndRequest().
Ejemplo n.º 5
0
        public void Process()
        {
            KeyValuePair <int, byte[][]>[] whispers = new KeyValuePair <int, byte[][]> [_receivedWhispers.Count];
            lock (_sync_whispers)
            {
                _sendWhispers.CopyTo(whispers, 0);
                _sendWhispers.Clear();
            }
            foreach (var whisper in whispers)
            {
                var playerid = BitConverter.ToInt32(whisper.Value[0], 0);
                switch ((DDZActions)BitConverter.ToInt32(whisper.Value[1], 0))
                {
                case DDZActions.C_能否进入:
                    处理_能否进入(playerid);
                    break;

                case DDZActions.C_进入:
                    处理_进入(playerid);
                    break;

                case DDZActions.C_准备:
                    处理_准备(playerid);
                    break;

                case DDZActions.C_叫地主:
                    处理_叫地主(playerid);
                    break;

                case DDZActions.C_不叫:
                    处理_不叫(playerid);
                    break;

                case DDZActions.C_出牌:
                    处理_出牌(playerid, whisper.Value);
                    break;

                case DDZActions.C_Pass:
                    处理_Pass(playerid);
                    break;

                case DDZActions.GM_请求服务数据:
                    处理_GM_请求服务数据();
                    break;
                }
            }
            #region 游戏状态管理
            if (serviceState == ServiceStates.等待客户端进入)
            {
                var h = _currentStateHander as IWatingJoin;
                if (h.JoinSuccess(_players))
                {
                    foreach (var player in _players)
                    {
                        player.Value.超时_准备超时 = GameLooper.Counter + 30; //30秒准备时间
                    }
                    serviceState = ServiceStates.等待客户端准备好;
                }
            }
            else if (serviceState == ServiceStates.等待客户端准备好)
            {
                var h = _currentStateHander as IWatingReady;

                if (!_players.IsInit)
                {
                    List <int> tempPlayerIDs = new List <int>();
                    foreach (var tempPlayer in _players)
                    {
                        tempPlayerIDs.Add(tempPlayer.Key);
                    }
                    _players.顺序索引(0).前面的玩家 = _players.顺序索引(2);
                    _players.顺序索引(0).后面的玩家 = _players.顺序索引(1);
                    _players.顺序索引(1).前面的玩家 = _players.顺序索引(0);
                    _players.顺序索引(1).后面的玩家 = _players.顺序索引(2);
                    _players.顺序索引(2).前面的玩家 = _players.顺序索引(1);
                    _players.顺序索引(2).后面的玩家 = _players.顺序索引(0);
                    _players.IsInit        = true;
                }

                if (h.EveryOneIsReady(_players))
                {
                    发出_开始游戏();
                    serviceState = ServiceStates.正在游戏;
                }
            }
            else if (serviceState == ServiceStates.正在游戏)
            {
            }
            #endregion
        }
Ejemplo n.º 6
0
        }     // HubEventHandler()

        //
        //
        //
        //
        // *****************************************************************
        // ****             ProcessServiceStateRequests()               ****
        // *****************************************************************
        private void ProcessServiceStateRequests(RequestEventArg <RequestCode> eventArg)
        {
            ServiceStates prevState      = m_ServiceState;                  // store the current state.
            ServiceStates requestedState = (ServiceStates)eventArg.Data[0]; // get state requested

            Log.NewEntry(LogLevel.Minor, "ProcessServiceStateRequests: Current={0} Requested={1}", m_ServiceState, requestedState);

            switch (m_ServiceState)                         // switch on CURRENT state.
            {
            case ServiceStates.Unstarted:                   // We are starting for first time.
                // Complete what ever initialization is needed to move to Started state.
                //      * Create (or find) communication services needed.
                bool isReadyToStart = true;
                if (isReadyToStart)
                {
                    m_ServiceState = ServiceStates.Started;
                    HubEventEnqueue(m_Requests.Get(RequestCode.ServiceStateChange, ServiceStates.Running));     // automatically try to advance
                }
                break;

            case ServiceStates.Started:
                // Complete initialization needed to switch to running state.
                if (requestedState == ServiceStates.Running)
                {       // We are trying to advance to full running state.
                        // TODO: Check whatever conditions we need to run.
                    bool isReadyToRun = true;
                    if (isReadyToRun)
                    {
                        m_ServiceState = ServiceStates.Running;

                        // TEMP FOR DEBUGGING ONLY
                        //object[] p = new object[2];
                        //p[0] = ExecutionHub.tempXML;            // objects to make
                        //p[1] = string.Empty;
                        //HubEventEnqueue(m_Requests.Get(RequestCode.CreateNewEngineContainer, p)); // automatically try to advance
                        // END OF DEBUG
                    }
                }
                else if (requestedState == ServiceStates.Stopped || requestedState == ServiceStates.Stopping)
                {                                                                                           // Something in start up procedure failed.  We are trying to stop.
                    m_ServiceState = ServiceStates.Stopping;
                    HubEventEnqueue(m_Requests.Get(RequestCode.ServiceStateChange, ServiceStates.Stopped)); // automatically try to advance
                }
                break;

            case ServiceStates.Running:
                if (requestedState == ServiceStates.Stopped || requestedState == ServiceStates.Stopping)
                {                                                                                           // We want to move to Stopped state.
                    m_ServiceState = ServiceStates.Stopping;                                                // mark our desire to stop.
                    HubEventEnqueue(m_Requests.Get(RequestCode.ServiceStateChange, ServiceStates.Stopped)); // automatically try to advance
                }
                break;

            case ServiceStates.Stopping:
                // Complete whatever shutdown procedure is needed.
                // Dispose of resources, etc.
                bool isReadyToStop = true;
                if (isReadyToStop)
                {
                    m_ServiceState = ServiceStates.Stopped;
                    // this could cause an issue, we probably need to do some checking after the stop in the order engine
                    // has been called to make sure we don't recieve and fills at the last second or something that goes
                    // unreported.  For now this works however.
                    foreach (Dictionary <int, ExecutionContainer> execContainerDict in m_ExecutionContainers.Values)
                    {
                        foreach (ExecutionContainer execContainer in execContainerDict.Values)
                        {
                            execContainer.IOrderEngine.Stop();
                        }
                    }
                    base.Stop();                                        // In this state we stop immediately.
                }
                break;

            case ServiceStates.Stopped:
                // This state will never be called.
                break;

            default:
                break;
            }//switch ServiceState
            //
            // Exit
            //
            // Trigger state change.
            if (m_ServiceState != prevState)
            {
                OnServiceStateChanged(prevState, m_ServiceState);
            }
            m_Requests.Recycle(eventArg);
        }//ProcessServiceStateRequests()
 /// <summary>
 /// The constructor that ensures the proper creation of the eventArgs.
 /// </summary>
 public ServiceStateEventArgs(IService iService, ServiceStates current, ServiceStates previous)
 {
     this.ServiceName   = iService.ServiceName;
     this.CurrentState  = current;
     this.PreviousState = previous;
 }