Exemple #1
0
        /// <summary>
        /// 解析事件回答
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="player"></param>
        void ParseEventAnswer(string msg, PlayerData player)
        {
            // EA index@answer
            string[] data   = msg.Split(' ')[1].Split('@');
            int      index  = int.Parse(data[0]);
            int      answer = int.Parse(data[1].Split(':')[1]);

            try
            {
                Event_waitAnswer t = answerEventList[index];
                t.AddAnswer(answer);
                if (t.getAnswerNum() == maxPlayer)
                {
                    int t_answer = t.Answer();
                    answerEventList.Remove(index);
                    string t_data = string.Format("EA {0}:{1}", t.data.selfIndex, t_answer);
                    t.data.client.Send(t_data);
                }
            }
            catch
            {
                Console.WriteLine("Error EA");
            }
        }
Exemple #2
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);
            }
        }