Example #1
0
        public async Task <IActionResult> SemiAutoMatchmake(
            [FromBody] SemiAutoMatchmake.Request req, [FromServices] GeneralManager gm)
        {
            var res = new SemiAutoMatchmake.Response
            {
                matchId = req.matchId,
                entries = new List <SemiAutoMatchmake.Entry>(),
            };

            var matchmaker = new Logic.Matchmaker2();

            await gm.EnqueueJob(() =>
            {
                var match = gm.MatchManager.GetMatch(req.matchId);
                if (match == null || match.State != MatchState.Matching)
                {
                    return;
                }

                var entries = new List <IBattleEntry>();
                req.entryIds.ForEach(id =>
                {
                    var entry = gm.BattleEntryManager.GetEntry(id);
                    if (entry != null)
                    {
                        entries.Add(entry);
                    }
                });

                var result = matchmaker.Matchmake(1, entries, Battle.MatchPlayersNum);
                if (1 <= result.Count)
                {
                    result[0].Elements.ForEach(element => res.entries.Add(new SemiAutoMatchmake.Entry
                    {
                        entryId = element.Entry.EntryId,
                        side    = element.Side,
                    }));
                }
            });

            return(Ok(res));
        }
        public void Start()
        {
            PgInvitationManager = new PgInvitationManager();
            PremadeGroupManager = new PremadeGroupManager();
            BattleEntryManager  = new BattleEntryManager();
            MatchManager        = new MatchManager();

            var matchMaker = new Logic.Matchmaker2();


            var sessionCheckTimer       = new IntervalTimer(5000);
            var autoMatchmakeTimer      = new IntervalTimer(30000);
            var generateSessionCntTimer = new IntervalTimer(5000);
            var LogSessionCntTimer      = new IntervalTimer(30000);


            Task.Run(async() =>
            {
                while (true)
                {
                    if (Settings.MatchingArea != MatchingArea.Unknown)
                    {
                        WatchDogSession = new WatchDogSession(Settings.MatchingArea);
                        break;
                    }

                    await Task.Delay(1000);
                }

                var enabledMatchesCnt = 0;
                var entriedPlayersCnt = 0;

                while (true)
                {
                    // -----------------------------------------
                    // Session有効確認
                    // -----------------------------------------
                    if (sessionCheckTimer.Timeup())
                    {
                        var tmp = EnqueueJob(async() =>
                        {
                            BattleEntryManager.CheckSessions(WatchDogSession.Alive);

                            MatchManager.CheckSessions(WatchDogSession.Alive);

                            PgInvitationManager.ClearExpiredInvitations();                            //Clear!! ExpiredInvitations

                            var changedPlayers = PremadeGroupManager.CheckSessions(WatchDogSession.Alive);

                            for (int i = 0; i < changedPlayers.Count; i++)
                            {
                                var player = changedPlayers[i];

                                await new evolib.Kvs.Models.ConnectionQueue(player.SessionId).EnqueueAsync(
                                    this.CreatePremadeGroupQueueData(player.PlayerId)
                                    );
                            }

                            sessionCheckTimer.Start();
                        });
                    }

                    // -----------------------------------------
                    // Auto Matchmake
                    // -----------------------------------------
                    if (autoMatchmakeTimer.Timeup())
                    {
                        var tmp = EnqueueJob(async() =>
                        {
                            var enabledMatches =
                                MatchManager.GatherMatches(m =>
                            {
                                return
                                (m.State == MatchState.Matching && m.Server.AutoMatchmakeTarget);
                            });

                            enabledMatchesCnt = enabledMatches.Count;
                            entriedPlayersCnt = BattleEntryManager.EntriedPlayersCount;


                            if (VersionChecker.Get(VersionChecker.CheckTarget.EnabledMatchmake).Check())
                            {
                                var limitPackagerVer = VersionChecker.Valued(
                                    VersionChecker.ReferenceSrc.PackageVersion,
                                    VersionChecker.LimitPackageVersion(VersionChecker.CheckTarget.Matchmake)
                                    );

                                var minMatchmakeEntriedPlayersNumber = MinMatchmakeEntriedPlayersNumber.Pop(null);
                                using (var common2Db = DbContextFactory.CreateCommon2())
                                {
                                    minMatchmakeEntriedPlayersNumber = MinMatchmakeEntriedPlayersNumber.Pop(
                                        await common2Db.GenericDatas.FindAsync(GenericData.Type.MinMatchmakeEntriedPlayersNumber)
                                        );
                                }


                                var result = matchMaker.MatchmakeForCBT1(
                                    enabledMatches.Count,
                                    BattleEntryManager.GetEntries(e =>
                                {
                                    return(limitPackagerVer <= e.MinPackageVersion);
                                },
                                                                  5000),
                                    minMatchmakeEntriedPlayersNumber
                                    );

                                // Sort by entryTime.
                                enabledMatches.Sort((a, b) => (a.Server.EntryTime < b.Server.EntryTime) ? -1 : 1);

                                for (int i = 0; i < result.Count; i++)
                                {
                                    var match = enabledMatches[i];

                                    var matchmake = result[i];

                                    var players = new Dictionary <string, long>();

                                    foreach (var e in matchmake.Elements)
                                    {
                                        if (MatchManager.AssignPlayers(
                                                match.MatchId,
                                                e.Entry.Players,
                                                e.Side))
                                        {
                                            BattleEntryManager.Cancel(e.Entry.EntryId, match.MatchId, evolib.BattleEntry.Type.Matching);

                                            foreach (var p in e.Entry.Players)
                                            {
                                                players[p.SessionId] = p.PlayerId;
                                            }
                                        }
                                    }

                                    foreach (var p in players)
                                    {
                                        await new ConnectionQueue(p.Key).EnqueueAsync(
                                            match.JoinBattleServer(p.Value)
                                            );
                                    }

                                    await new ConnectionQueue(match.Server.SessionId).EnqueueAsync(
                                        match.MatchInfo()
                                        );
                                }
                            }

                            autoMatchmakeTimer.Start();
                        });
                    }


                    // -----------------------------------------
                    // 同時接続数出力
                    // -----------------------------------------
                    if (generateSessionCntTimer.Timeup())
                    {
                        var now      = DateTime.UtcNow;
                        var areaName = Settings.MatchingArea.ToString();

                        var total     = 0;
                        var breakDown = "{";
                        foreach (var cnt in WatchDogSession.ClientCounts)
                        {
                            total += cnt.Value;

                            breakDown += $"{cnt.Key}:{cnt.Value},";
                        }
                        breakDown += "}";

                        var currentSessionCount                     = new CurrentSessionCount(Settings.MatchingArea);
                        currentSessionCount.Model.areaName          = areaName;
                        currentSessionCount.Model.count             = total;
                        currentSessionCount.Model.date              = now;
                        currentSessionCount.Model.breakDown         = breakDown;
                        currentSessionCount.Model.enabledMatchesCnt = enabledMatchesCnt;
                        currentSessionCount.Model.entriedPlayersCnt = entriedPlayersCnt;
                        await currentSessionCount.SaveAsync(
                            TimeSpan.FromMilliseconds(generateSessionCntTimer.Interval * 2)
                            );

                        if (LogSessionCntTimer.Timeup())
                        {
                            Logger.Logging(
                                new LogObj().AddChild(new LogModels.CurrentSessionCount
                            {
                                AreaName          = areaName,
                                Count             = total,
                                BreakDown         = breakDown,
                                Date              = now,
                                entriedPlayersCnt = entriedPlayersCnt,
                                enabledMatchesCnt = enabledMatchesCnt,
                            })
                                );

                            LogSessionCntTimer.Start();
                        }

                        generateSessionCntTimer.Start();
                    }

                    await Task.Delay(100);
                }
            });
        }