public static List <MicroModule> GetMicroModules()
        {
            List <MicroModule> MicroModuleList  = new List <MicroModule>();
            DataTable          MicroModuleTable = MicroModuleDataAccess.GetInstance.GetMicroModules();

            foreach (DataRow dr in MicroModuleTable.Rows)
            {
                MicroModule TheMicroModule = DataRowToObject(dr);

                MicroModuleList.Add(TheMicroModule);
            }

            return(MicroModuleList);
        }
        public static MicroModule DataRowToObject(DataRow dr)
        {
            MicroModule TheMicroModule = new MicroModule
            {
                ModuleID       = int.Parse(dr["ModuleID"].ToString()),
                ModuleName     = dr["ModuleName"].ToString(),
                ModuleMenuText = dr["ModuleMenuText"].ToString(),
                ParentModuleID = int.Parse(MicroGlobals.ReturnZeroIfNull(dr["ParentModuleID"].ToString())),
                IsActive       = bool.Parse(dr["IsActive"].ToString()),
                IsDeleted      = bool.Parse(dr["IsDeleted"].ToString())
            };

            return(TheMicroModule);
        }
예제 #3
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();
        }
        public static MicroModule GetMicroModuleByName(string moduleName)
        {
            List <MicroModule> MicroModuleList = GetMicroModules();
            MicroModule        TheMicroModule  = new MicroModule();

            if (MicroModuleList.Count > 0)
            {
                var MicroModuleFilteredList = (from TheModule in MicroModuleList
                                               where TheModule.ModuleMenuText == moduleName
                                               select TheModule).ToList();

                foreach (MicroModule EachMicroModule in MicroModuleFilteredList)
                {
                    TheMicroModule = EachMicroModule;
                }
            }

            return(TheMicroModule);
        }
예제 #5
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");
        }