Beispiel #1
0
        public IDictionary <ModuleType, IModule> CreateModules()
        {
            Cmcl.Fixtures = CmclFixtures;
            Cmcl.Places   = CmclPlaces;

            var playersModule = new PlayersModule(Configuration.GameEngine, Players, Teams);

            //	Need to create just a Teams list object. Selecting the Teams Module has to refresh the potentially changed team.
            foreach (var team in Teams.Values)
            {
                team.Players      = new List <Player>(Players.Where(p => p.TeamName == team.TeamName));
                team.Formation[0] = team.Players[0];
                team.Formation[1] = team.Players[1];
            }
            var teamModule = new TeamModule(Teams);

            var competitionModule = new CompetitionsModule(new[] { Cmcl });
            var fixturesModule    = new FixturesModule(competitionModule.Competitions);
            var matchModule       = new MatchModule(new [] { Cmcl });

            Config.Configuration.GlobalWeek = () => Competition.GlobalWeek(new[] { Cmcl });

            return(new Dictionary <ModuleType, IModule>
            {
                { ModuleType.Team, teamModule },
                { ModuleType.SelectTeam, teamModule },
                { ModuleType.Fixtures, fixturesModule },
                { ModuleType.Competitions, competitionModule },
                { ModuleType.Match, matchModule },
                { ModuleType.Players, playersModule }
            });
        }
Beispiel #2
0
        internal void Start(int player, string endpoint, int seed, GameVersion version)
        {
            Stop();

            GameVersion = version;

            if (seed < 0)
            {
                seed = Guid.NewGuid().GetHashCode() ^ DateTime.UtcNow.GetHashCode();
            }

            Rng = new Random(seed);

            PlayerNumber = player;
            Log          = new Log(Path.Combine(Path.GetDirectoryName(Process.GetCurrentProcess().MainModule.FileName), $"{Name} {PlayerNumber}.log"));

            InfoModule = new InfoModule()
            {
                BotInternal = this
            };
            MapModule = new MapModule()
            {
                BotInternal = this
            };
            PlayersModule = new PlayersModule()
            {
                BotInternal = this
            };
            UnitsModule = new UnitsModule()
            {
                BotInternal = this
            };
            ResearchModule = new ResearchModule()
            {
                BotInternal = this
            };
            MicroModule = new MicroModule()
            {
                BotInternal = this
            };

            BotThread = new Thread(() => Run(endpoint))
            {
                IsBackground = true
            };
            BotThread.Start();
        }
Beispiel #3
0
        private void Run(string endpoint)
        {
            Log.Info($"Started");

            var channel    = new Channel(endpoint, ChannelCredentials.Insecure);
            var module_api = new AIModuleAPIClient(channel);
            var api        = new ExpertAPIClient(channel);

            if (GameVersion == GameVersion.AOC)
            {
                DatFile = module_api.GetGameDataFilePath(new Protos.GetGameDataFilePathRequest()).Result;
            }

            Tick = 0;
            var sw       = new Stopwatch();
            var commands = new List <Command>();
            var previous = DateTime.UtcNow;

            while (!Stopping)
            {
                Log.Info($"Tick {Tick}");

                sw.Restart();
                commands.Clear();

                // update

                if (Tick > 0)
                {
                    commands.AddRange(Update().Where(c => c.HasMessages));
                }

                commands.AddRange(MicroModule.RequestUpdateInternal().Where(c => c.Messages.Count > 0));
                commands.AddRange(ResearchModule.RequestUpdateInternal().Where(c => c.Messages.Count > 0));
                commands.AddRange(UnitsModule.RequestUpdateInternal().Where(c => c.Messages.Count > 0));
                commands.AddRange(PlayersModule.RequestUpdateInternal().Where(c => c.Messages.Count > 0));
                commands.AddRange(MapModule.RequestUpdateInternal().Where(c => c.Messages.Count > 0));
                commands.AddRange(InfoModule.RequestUpdateInternal().Where(c => c.Messages.Count > 0));

                commands.AddRange(GameElementUpdates.Values);

                // don't send commands if it's been more than 5 seconds since previous update

                if ((DateTime.UtcNow - previous) > TimeSpan.FromSeconds(5))
                {
                    commands.Clear();
                    GameElementUpdates.Clear();

                    Log.Info("Clearing commands (more than 5 seconds since previous)");
                }

                // set up api call

                var commandlist = new CommandList()
                {
                    PlayerNumber = PlayerNumber
                };

                foreach (var command in commands)
                {
                    foreach (var message in command.Messages)
                    {
                        commandlist.Commands.Add(Any.Pack(message));
                    }
                }

                Log.Info($"RequestUpdate took {sw.ElapsedMilliseconds} ms");

                // make the call

                sw.Restart();

                CommandResultList resultlist;
                try
                {
                    var aw = api.ExecuteCommandListAsync(commandlist);
                    //GC.Collect();
                    resultlist = aw.GetAwaiter().GetResult();
                }
                catch (Exception e)
                {
                    Log.Info($"{e.Message}");
                    resultlist = null;
                }

                Log.Info($"Call took {sw.ElapsedMilliseconds} ms");

                if (resultlist == null)
                {
                    foreach (var command in commands)
                    {
                        command.Reset();
                    }
                }
                else
                {
                    sw.Restart();

                    // update the results to the commands

                    Debug.Assert(commands.Sum(c => c.Messages.Count) == resultlist.Results.Count);

                    var offset = 0;
                    foreach (var command in commands)
                    {
                        command.Responses.Clear();

                        for (int i = 0; i < command.Messages.Count; i++)
                        {
                            command.Responses.Add(resultlist.Results[offset + i]);
                        }

                        offset += command.Responses.Count;
                    }

                    if (commands.Count > 0)
                    {
                        Tick++;
                    }

                    // perform update

                    foreach (var element in GameElementUpdates.Keys)
                    {
                        element.Update();
                    }
                    GameElementUpdates.Clear();

                    InfoModule.UpdateInternal();
                    MapModule.UpdateInternal();
                    PlayersModule.UpdateInternal();
                    UnitsModule.UpdateInternal();
                    ResearchModule.UpdateInternal();
                    MicroModule.UpdateInternal();

                    previous = DateTime.UtcNow;

                    Log.Info($"Update took {sw.ElapsedMilliseconds} ms");
                }
            }

            channel.ShutdownAsync().Wait();

            Log.Info($"Stopped");
        }