internal static StrategyBase GetInstance(StrategyID strategyID, ClientConnection clientConnection, GameInfo gameInfo, string botAuthenticationGuid, string playerName, short sideIndex, bool isGameController, bool isCommander) { StrategyBase returnValue = null; if (_strategyTypesByStrategyID == null) { var types = from type in Assembly.GetAssembly(typeof(StrategyBase)).GetTypes() where typeof(StrategyBase).IsAssignableFrom(type) && type.IsAbstract == false select type; _strategyTypesByStrategyID = types .ToDictionary(p => ((StrategyBase)Activator.CreateInstance(p)).StrategyID, r => r); } if (_strategyTypesByStrategyID.ContainsKey(strategyID) == false) { throw new NotSupportedException(strategyID.ToString()); } returnValue = (StrategyBase)Activator.CreateInstance(_strategyTypesByStrategyID[strategyID]); returnValue.Attach(clientConnection, gameInfo, botAuthenticationGuid, playerName, sideIndex, isGameController, isCommander); return(returnValue); }
public void Attach(AllegianceInterop.ClientConnection client, GameInfo gameInfo, string botAuthenticationGuid, string playerName, short sideIndex, bool isGameController, bool isCommander) { ClientConnection = client; PlayerName = playerName; SideIndex = sideIndex; IsGameController = isGameController; IsCommander = isCommander; BotAuthenticationGuid = botAuthenticationGuid; GameInfo = gameInfo; _messageReceiver = new MessageReceiver(playerName); client.OnAppMessage += _messageReceiver.OnAppMessage; _messageReceiver.FMD_S_JOIN_SIDE += _messageReceiver_FMD_S_JOIN_SIDE; _messageReceiver.FMD_S_SHIP_STATUS += _messageReceiver_FMD_S_SHIP_STATUS; //_messageReceiver.FMD_S_EXPORT += _messageReceiver_FMD_S_EXPORT; _messageReceiver.FMD_S_CLUSTERINFO += _messageReceiver_FMD_S_CLUSTERINFO; _messageReceiver.FMD_CS_PING += _messageReceiver_FMD_CS_PING; // We'll get one of these every 5 seconds, using this to drive "less frequent" updates. _messageReceiver.FMD_S_DOCKED += _messageReceiver_FMD_S_DOCKED; _messageReceiver.FMD_S_MISSION_STAGE += _messageReceiver_FMD_S_MISSION_STAGE; _messageReceiver.FMD_S_BALLOT += _messageReceiver_FMD_S_BALLOT; _messageReceiver.FMD_S_GAME_OVER += _messageReceiver_FMD_S_GAME_OVER; AttachMessages(_messageReceiver, botAuthenticationGuid, playerName, sideIndex, isGameController, isCommander); Log($"Starting strategy: {StrategyID.ToString()}"); Start(); }
public void Log(string message) { Console.WriteLine($"{StrategyID.ToString()} {PlayerName}: {message}"); string filename = $"c:\\1\\Logs\\{PlayerName}_strategy.txt"; Exception lastException = null; for (int i = 0; i < 30; i++) { try { File.AppendAllText(filename, $"{DateTime.Now} {StrategyID.ToString()} {PlayerName}: {message}\n"); lastException = null; break; } catch (Exception ex) { lastException = ex; Thread.Sleep(10); } } if (lastException != null) { throw lastException; } }
//public Dictionary<StrategyID, Type> LoadPlugins(string assemblyName) //{ // var assemb = Assembly.LoadFrom(assemblyName); // var types = from type in assemb.GetTypes() // where typeof(StrategyBase).IsAssignableFrom(type) // select type; // //Dictionary<StrategyID, StrategyInstanceCreationDelegate> instances = types.Select( // // v => new StrategyInstanceCreationDelegate(() => // // { // // return (StrategyBase)Activator.CreateInstance(v); // // })) // // .ToDictionary(p => ((StrategyBase)Activator.CreateInstance(p)).StrategyID, r => r); // Dictionary<StrategyID, Type> instances = types // .ToDictionary(p => ((StrategyBase)Activator.CreateInstance(p)).StrategyID, r => r); // return instances; //} public StrategyBase CreateInstance(StrategyID strategyID) { Type strategyType; if (_strategyTypesByStrategyID.TryGetValue(strategyID, out strategyType) == false) { throw new NotSupportedException(strategyID.ToString()); } return((StrategyBase)Activator.CreateInstance(strategyType)); }