示例#1
0
        /// <summary>
        /// 将事件放入待回答队列
        /// </summary>
        void AddEvent(Event_waitSend t)
        {
            Event_waitAnswer t_a = new Event_waitAnswer();

            t_a.data    = t;
            t_a.endTime = ServerFunction.getTimeStamp_milSeconds() + maxWaitTime_event;
            answerEventList.Add(t.index, t_a);
        }
示例#2
0
        /// <summary>
        /// 解析游戏事件
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="player"></param>
        void ParseGameEvent(string msg, PlayerData player)
        {
            // GE selfIndex@event:(参数) ...
            string[] msgs       = msg.Split(' ');
            int      eventCount = msgs.Length - 1;

            for (int i = 1; i <= eventCount; i++)
            {
                string[]       t     = msgs[i].Split('@');
                ushort         index = currentIndex++;
                Event_waitSend t_e   = new Event_waitSend(t[1], t[0], index, player.Camp, player.client);
                sendEventQueue.Enqueue(t_e);
            }
        }
示例#3
0
        /// <summary>
        /// 游戏
        /// </summary>
        /// <param name="rm"></param>
        public void Game()
        {
            //等待玩家初始化完成
            while (true)
            {
                int t = 0;
                for (int i = 0; i < maxPlayer; i++)
                {
                    if (isReady[i])
                    {
                        t++;
                    }
                }
                if (t == maxPlayer)
                {
                    //开始游戏
                    for (int i = 0; i < maxPlayer; i++)
                    {
                        Players[i].client.Send("BG");
                    }
                    break;
                }
            }

            while (!isFinish)
            {
                //更新玩家数据
                for (int i = 0; i < Players.Length; i++)
                {
                    Players[i].Update(deltaTime);
                }
                //更新单位状态
                if (UpdateBehavious != null)
                {
                    UpdateBehavious(Time, deltaTime);
                }

                //发送玩家事件
                int    c      = 0;
                string data_1 = "BM";
                for (int i = 0; i < Players.Length; i++)
                {
                    if (Players[i].msgQueue.Count > 0)
                    {
                        data_1 += Players[i].getMsg();
                        c++;
                    }
                }
                if (c > 0)
                {
                    BroadcastMsg(data_1);
                }

                //处理待发事件队列
                if (sendEventQueue.Count > 0)
                {
                    string t_msg = "GE";
                    while (sendEventQueue.Count > 0)
                    {
                        Event_waitSend t = sendEventQueue.Dequeue();
                        t_msg += t.ToString();
                        AddEvent(t);
                    }
                    BroadcastMsg(t_msg);
                }

                //处理待回答事件列表
                long currentTimeStamp = ServerFunction.getTimeStamp_milSeconds();
                if (answerEventList.Count > 0)
                {
                    Dictionary <int, Event_waitAnswer> .KeyCollection keysCollect = null;
                    keysCollect = answerEventList.Keys;
                    int[] keys = new int[keysCollect.Count];
                    keysCollect.CopyTo(keys, 0);
                    for (int i = 0; i < keys.Length; i++)
                    {
                        Event_waitAnswer t = answerEventList[keys[i]];
                        //超时
                        if (currentTimeStamp > t.endTime)
                        {
                            //如果超过一半玩家应答则予以回应
                            if (t.getAnswerNum() > maxPlayer * 0.5f)
                            {
                                int    t_answer = t.Answer();
                                string t_data   = string.Format("EA {0}:{1}", t.data.selfIndex, t_answer);
                                t.data.client.Send(t_data);
                            }
                            Console.WriteLine("Timeout");
                            answerEventList.Remove(keys[i]);
                        }
                    }
                }


                //获取玩家信息包
                string data = "PD ";
                for (int i = 0; i < Players.Length; i++)
                {
                    data += (getPlayerData(i));
                    if (i != Players.Length - 1)
                    {
                        data += "#";
                    }
                }

                //发送玩家信息包
                int j = 0;
                for (int i = 0; i < Players.Length; i++)
                {
                    if (Players[i].client.isLink)
                    {
                        Players[i].client.Send(data);
                    }
                    else
                    {
                        j++;
                    }
                }

                //检测是否所有玩家都已掉线
                if (j == maxPlayer)
                {
                    Console.WriteLine(name + "_房间结束游戏");
                    isFinish = true;
                    break;
                }

                //更新时间参数
                deltaTime = (currentTimeStamp - lastTime) / 1000.0f;
                lastTime  = currentTimeStamp;
                Time     += deltaTime;

                //检测是否结束游戏
                int t3 = 0;
                for (int i = 0; i < Players.Length; i++)
                {
                    if (Players[i].isEnd)
                    {
                        t3++;
                    }
                }
                if (t3 == Players.Length)
                {
                    //结束游戏
                    Console.WriteLine(name + "_房间结束游戏");
                    string data_EG = getEndGameData();
                    BroadcastMsg(data_EG);
                    break;
                }

                Thread.Sleep(50);
            }

            while (true)
            {
                //检测是否所有玩家都已退出
                int j = 0;
                for (int i = 0; i < Players.Length; i++)
                {
                    if (!Players[i].client.isLink && !Players[i].isLeftGameRoom)
                    {
                        Players[i].isLeftGameRoom = true;
                    }

                    if (Players[i].isLeftGameRoom)
                    {
                        j++;
                    }
                }

                if (j == maxPlayer)
                {
                    isFinish = true;
                    break;
                }

                Thread.Sleep(1000);
            }
        }