Пример #1
0
 private void CheckOnline(Player self)
 {
     if (!self.isOnline && TimeHelper.ClientNowMilliSeconds() > self.disconnectTime)
     {
         NetworkHelper.DisconnectPlayer(self);
     }
 }
Пример #2
0
        private async ETTask StartToPing(long waitTime, Action <Exception> action)
        {
            var timerComponent = Game.Scene.GetComponent <TimerComponent>();

            var session = this.GetParent <Session>();

            while (!this.IsDisposed)
            {
                try
                {
                    _sendTimer = TimeHelper.ClientNowMilliSeconds();
                    await session.Call(_request);

                    _receiveTimer = TimeHelper.ClientNowMilliSeconds();
                    // 計算延遲時間
                    Ping = ((_receiveTimer - _sendTimer) / 2) < 0 ? 0 : (_receiveTimer - _sendTimer) / 2;
                    onPingChanged?.Invoke(Ping);
                    //Log.Info($"_receiveTimer{_receiveTimer}, _sendTimer{_sendTimer}, Ping:{Ping}");
                }
                catch (Exception e)
                {
                    // 執行斷線後操作
                    action?.Invoke(e);
                    Log.Info("[Ping]網路斷線");
                }
                await timerComponent.WaitForMilliSecondAsync(waitTime);
            }
        }
Пример #3
0
 public static void SetOnline(this Player self, bool isOnline)
 {
     if (!isOnline && self.isOnline != isOnline)
     {
         self.disconnectTime = TimeHelper.ClientNowMilliSeconds() + 30000;
     }
     self.isOnline = isOnline;
 }
Пример #4
0
 public override void Awake(ActorLocationSender self)
 {
     self.LastRecvTime = TimeHelper.ClientNowMilliSeconds();
     self.Tcs          = null;
     self.FailTimes    = 0;
     self.ActorId      = 0;
     self.WaitingTasks.Clear();
 }
Пример #5
0
        private static async ETTask RunTask(this ActorLocationSender self, ActorTask task)
        {
            ActorMessageSender actorMessageSender = Game.Scene.GetComponent <ActorMessageSenderComponent>().Get(self.ActorId);
            IActorResponse     response           = await actorMessageSender.Call(task.ActorRequest);

            // 发送成功
            switch (response.Error)
            {
            case ErrorCode.ERR_NotFoundActor:
                // 如果没找到Actor,重试
                ++self.FailTimes;

                // 失败MaxFailTimes次则清空actor发送队列,返回失败
                if (self.FailTimes > ActorLocationSender.MaxFailTimes)
                {
                    // 失败直接删除actorproxy
                    Log.Info($"actor send message fail, actorid: {self.Id}");
                    self.RunError(response.Error);
                    self.GetParent <ActorLocationSenderComponent>().Remove(self.Id);
                    return;
                }

                // 等待0.5s再发送
                await Game.Scene.GetComponent <TimerComponent>().WaitForMilliSecondAsync(500);

                self.ActorId = await Game.Scene.GetComponent <LocationProxyComponent>().Get(self.Id);

                self.AllowGet();
                return;

            case ErrorCode.ERR_ActorNoMailBoxComponent:
                self.RunError(response.Error);
                self.GetParent <ActorLocationSenderComponent>().Remove(self.Id);
                return;

            default:
                self.LastRecvTime = TimeHelper.ClientNowMilliSeconds();
                self.FailTimes    = 0;
                self.WaitingTasks.Dequeue();

                // 如果所有的发送消息都得到了返回,发送任务完成,那么删除这个ActorLocationSender,及时回收发送对象
                if (self.WaitingTasks.Count == 0)
                {
                    self.GetParent <ActorLocationSenderComponent>().Remove(self.Id);
                }

                if (task.Tcs != null)
                {
                    IActorLocationResponse actorLocationResponse = response as IActorLocationResponse;
                    if (actorLocationResponse == null)
                    {
                        task.Tcs.SetException(new Exception($"actor location respose is not IActorLocationResponse, but is: {response.GetType().Name}"));
                    }
                    task.Tcs.SetResult(actorLocationResponse);
                }
                return;
            }
        }
        public static async void LoadingFinish(RoomTeamComponent roomTeamComponent)
        {
            if (roomTeamComponent.DataIsLoaded)
            {
                return;
            }

            roomTeamComponent.DataIsLoaded = true;

            //等3秒給Client Lerp
            await Game.Scene.GetComponent <TimerComponent>().WaitForSecondAsync(3.0f);

            //設定比賽開始結束時間
            var roadSetting = Game.Scene.GetComponent <ConfigComponent>().Get
                                  (typeof(RoadSetting), roomTeamComponent.RoomEntity.info.RoadSettingId) as RoadSetting;

            if (roadSetting != null)
            {
                var timerComponent = Game.Scene.GetComponent <TimerComponent>();
                roomTeamComponent.RoomRunStartTimeMs = TimeHelper.ClientNowMilliSeconds();
                roomTeamComponent.RoadDistance_km    = RoadHelper.GetDistance_km(roadSetting);
                roomTeamComponent.RoadDistance_m     = RoadHelper.GetDistance_m(roadSetting);
            }
            else
            {
                Log.Error($"找不到該路徑資料, RoadSettingId:{roomTeamComponent.RoomEntity.info.RoadSettingId}");
            }

            //設定所有Player StartRoom
            List <MapUnit> mapUnits   = roomTeamComponent.RoomEntity.GetAll();
            var            proxy      = Game.Scene.GetComponent <CacheProxyComponent>();
            var            playerSync = proxy.GetMemorySyncSolver <Player>();

            for (int i = 0; i < mapUnits.Count; i++)
            {
                Player target = playerSync.Get <Player>(mapUnits[i].Uid);
                if (target != null)
                {
                    target.StartRoom();
                    await playerSync.Update(target);
                }
            }

            //廣播給所有玩家 所有人載入完成
            M2C_TeamGoBattleProgressAllDone m2c_TeamGoBattleProgressAllDone = new M2C_TeamGoBattleProgressAllDone()
            {
                StartUTCTick = DateTime.UtcNow.Ticks
            };

            MapMessageHelper.BroadcastRoom(roomTeamComponent.RoomEntity.Id, m2c_TeamGoBattleProgressAllDone);
        }
Пример #7
0
        public override void Update(MapUnitFsmMoveComponent self)
        {
            if (!self.Enable)
            {
                return;
            }
            if (self.MapUnit == null)
            {
                return;
            }
            long nowTime = TimeHelper.ClientNowMilliSeconds();

            UpdateSpeed(self, nowTime);
            UpdateMove(self, nowTime);
            UpdateSync(self, nowTime);
        }
 private static void UpdateNPC(this NPC self)
 {
     if (self._mapUnit != null)
     {
         if (TimeHelper.ClientNowMilliSeconds() > self._rideAfterTime_ms)
         {
             self.DestroyNPC();
         }
     }
     else
     {
         if (TimeHelper.ClientNowMilliSeconds() > self._restAfterTime_ms)
         {
             self.CreateNPC();
         }
     }
 }
        // 每10s扫描一次过期的actorproxy进行回收,过期时间是1分钟
        // 可能由于bug或者进程挂掉,导致ActorLocationSender发送的消息没有确认,结果无法自动删除,每一分钟清理一次这种ActorLocationSender
        public async ETVoid StartAsync(ActorLocationSenderComponent self)
        {
            List <long> timeoutActorProxyIds = new List <long>();

            while (true)
            {
                await Game.Scene.GetComponent <TimerComponent>().WaitForMilliSecondAsync(10000);

                if (self.IsDisposed)
                {
                    return;
                }

                timeoutActorProxyIds.Clear();

                long timeNow = TimeHelper.ClientNowMilliSeconds();
                foreach (long id in self.ActorLocationSenders.Keys)
                {
                    ActorLocationSender actorLocationMessageSender = self.ActorLocationSenders[id];
                    if (actorLocationMessageSender == null)
                    {
                        continue;
                    }

                    if (timeNow < actorLocationMessageSender.LastRecvTime + 60 * 1000)
                    {
                        continue;
                    }

                    timeoutActorProxyIds.Add(id);
                }

                foreach (long id in timeoutActorProxyIds)
                {
                    self.Remove(id);
                }
            }
        }
Пример #10
0
        public static async Task Send(this BenchmarkComponent self, Session session, int j)
        {
            try
            {
                await session.Call(new C2R_Ping());

                ++self.k;

                if (self.k % 100000 != 0)
                {
                    return;
                }

                long time2 = TimeHelper.ClientNowMilliSeconds();
                long time  = time2 - self.time1;
                self.time1 = time2;
                Log.Info($"Benchmark k: {self.k} 每10W次耗时: {time} ms {session.Network.Count}");
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
        }
Пример #11
0
        public override void Update(RoomTeamComponent self)
        {
            if (self.RoomEntity != null)
            {
                switch (self.RoomEntity.State)
                {
                case RoomState.Run:
                {
                    //判斷是否正在載入比賽
                    if (!self.DataIsLoaded)
                    {
                        if (TimeHelper.ClientNowMilliSeconds() > self.RoomRunLoadingOutTimeMs)
                        {
                            //踢掉還沒載入的
                            for (int i = 0; i < RoomTeamComponent.MEMBER_MAX; i++)
                            {
                                if (self.MemberDatas[i] == null)
                                {
                                    continue;
                                }

                                if (!self.MemberDatas[i].LoadingDone)
                                {
                                    MapUnit kickMember = self.RoomEntity.GetMapUnitByUid(self.MemberDatas[i].Uid);
                                    RoomTeamHelper.KickMember(self, kickMember, TeamLoseType.LoadingTimeOut);
                                }
                            }
                            //讓隊伍進入比賽
                            RoomTeamHelper.LoadingFinish(self);
                        }
                    }

                    //判斷是否結束比賽
                    if (self.IsEnd())
                    {
                        for (int i = 0; i < self.RoomEntity.MapUnitList.Count; i++)
                        {
                            self.BattleLeaderboardUnitInfos.Add(new BattleLeaderboardUnitInfo()
                                {
                                    Uid  = self.RoomEntity.MapUnitList[i].Uid,
                                    Name = self.RoomEntity.MapUnitList[i].Info.Name,
                                    DistanceTravelledTarget = self.RoomEntity.MapUnitList[i].Info.DistanceTravelled,
                                    Location = self.RoomEntity.MapUnitList[i].Info.Location,
                                });

                            //設定結束騎乘時間
                            self.RoomEntity.MapUnitList[i].TrySetEndTime();
                        }

                        //排序 依照DistanceTravelledTarget(大到小)
                        for (int i = 0; i < self.BattleLeaderboardUnitInfos.Count; i++)
                        {
                            for (int m = i + 1; m < self.BattleLeaderboardUnitInfos.Count; m++)
                            {
                                if (self.BattleLeaderboardUnitInfos[m].DistanceTravelledTarget >
                                    self.BattleLeaderboardUnitInfos[i].DistanceTravelledTarget)
                                {
                                    var temp = self.BattleLeaderboardUnitInfos[i];
                                    self.BattleLeaderboardUnitInfos[i] = self.BattleLeaderboardUnitInfos[m];
                                    self.BattleLeaderboardUnitInfos[m] = temp;
                                }
                            }
                        }

                        //寫入排名
                        for (int i = 0; i < self.BattleLeaderboardUnitInfos.Count; i++)
                        {
                            for (int m = 0; m < self.RoomEntity.MapUnitList.Count; m++)
                            {
                                if (self.RoomEntity.MapUnitList[m].Uid == self.BattleLeaderboardUnitInfos[i].Uid)
                                {
                                    self.RoomEntity.MapUnitList[m].SetRank(i + 1);
                                    break;
                                }
                            }
                        }

                        // 紀錄隊伍資訊
                        var teamId = RideInfoHelper.SaveRideTeamRecord(self.BattleLeaderboardUnitInfos);

                        // 紀錄騎乘資訊
                        for (int i = 0; i < self.RoomEntity.MapUnitList.Count; i++)
                        {
                            RideInfoHelper.SaveRoadAllInfo(self.RoomEntity.MapUnitList[i], teamId);
                        }

                        //傳送排行榜
                        var m2c_BattleLeaderboard = new M2C_BattleLeaderboard();
                        m2c_BattleLeaderboard.BattleLeaderboardUnitInfos.AddRange(self.BattleLeaderboardUnitInfos);
                        MapMessageHelper.BroadcastRoom(self.RoomEntity.info.RoomId, m2c_BattleLeaderboard);

                        //結束
                        self.RoomEntity.SwitchState(RoomState.End);

                        Game.Scene.GetComponent <RoomComponent>().Update(self.RoomEntity).Coroutine();
                    }
                }
                break;
                }
            }
        }