public override void Initialize() { base.Initialize(); //Show only the virtual controllers supported on this platform VirtualControllerTypes[] vcTypes = EnumUtility.GetValues <VirtualControllerTypes> .EnumValues; for (int i = 0; i < vcTypes.Length; i++) { VirtualControllerTypes vControllerType = vcTypes[i]; //Continue if not supported if (VControllerHelper.IsVControllerSupported(vControllerType, TRBotOSPlatform.CurrentOS) == false) { continue; } CachedVCTypesStr += vControllerType.ToString(); if (i < (vcTypes.Length - 1)) { CachedVCTypesStr += ", "; } } }
/// <summary> /// Returns a list of Settings objects containing recommended default values. /// </summary> /// <returns>A list of Settings objects with their default values.</returns> public static List <Settings> GetDefaultSettings() { List <Settings> defaultSettings = new List <Settings>() { SettingsHelper(MAIN_THREAD_SLEEP, 100L), SettingsHelper(CREDITS_NAME, "Credits"), SettingsHelper(CREDITS_GIVE_TIME, 120000L), SettingsHelper(CREDITS_GIVE_AMOUNT, 100L), SettingsHelper(DUEL_TIMEOUT, 60000L), SettingsHelper(GROUP_BET_TOTAL_TIME, 120000L), SettingsHelper(GROUP_BET_MIN_PARTICIPANTS, 3L), SettingsHelper(CHATBOT_ENABLED, false), SettingsHelper(CHATBOT_SOCKET_PATH, "ChatterBotSocket"), SettingsHelper(CHATBOT_SOCKET_PATH_IS_RELATIVE, true), SettingsHelper(BINGO_ENABLED, false), SettingsHelper(BINGO_PIPE_PATH, "BingoPipe"), SettingsHelper(BINGO_PIPE_PATH_IS_RELATIVE, true), SettingsHelper(CLIENT_SERVICE_TYPE, (long)ClientServiceTypes.Twitch), SettingsHelper(AUTO_PROMOTE_ENABLED, true), SettingsHelper(AUTO_PROMOTE_LEVEL, (long)PermissionLevels.Whitelisted), SettingsHelper(AUTO_PROMOTE_INPUT_REQ, 50L), SettingsHelper(BOT_MSG_CHAR_LIMIT, 500L), SettingsHelper(PERIODIC_MSG_TIME, 1800000L), SettingsHelper(MESSAGE_COOLDOWN, 1000L), SettingsHelper(CONNECT_MESSAGE, "Your friendly Twitch Plays bot has connected :D ! Use !help to display a list of commands and !tutorial to see how to play! Original input parser by Jdog, aka TwitchPlays_Everything, rewritten and improved over time by the community."), SettingsHelper(RECONNECTED_MESSAGE, "Successfully reconnected to chat!"), SettingsHelper(PERIODIC_MESSAGE, "Hi! I'm your friendly Twitch Plays bot :D ! Use !help to display a list of commands!"), SettingsHelper(AUTOPROMOTE_MESSAGE, "{0} has been promoted to {1}! New commands are available."), SettingsHelper(NEW_USER_MESSAGE, "Welcome to the stream, {0} :D ! We hope you enjoy your stay!"), SettingsHelper(BEING_HOSTED_MESSAGE, "Thank you for hosting, {0}!!"), SettingsHelper(NEW_SUBSCRIBER_MESSAGE, "Thank you for subscribing, {0} :D !!"), SettingsHelper(RESUBSCRIBER_MESSAGE, "Thank you for subscribing for {1} months, {0} :D !!"), SettingsHelper(SOURCE_CODE_MESSAGE, "This bot is free software licensed under the AGPL v3.0. The code repository and full license terms are at https://github.com/teamradish/TRTwitchPlaysBot - You have the right to obtain source code for the streamer's deployed version of the software."), SettingsHelper(GAME_MESSAGE, "This is a game message."), SettingsHelper(GAME_MESSAGE_PATH, "GameMessage.txt"), SettingsHelper(GAME_MESSAGE_PATH_IS_RELATIVE, true), SettingsHelper(INFO_MESSAGE, "Welcome to the channel! You can play games by submitting messages in chat. Type !inputs to see all available buttons."), SettingsHelper(TUTORIAL_MESSAGE, "Hi {0}, here's how to play: https://github.com/teamradish/TRTwitchPlaysBot/Wiki/Syntax-Walkthrough.md"), SettingsHelper(DOCUMENTATION_MESSAGE, "Hi {0}, here's documentation on TRBot: https://github.com/teamradish/TRTwitchPlaysBot/Wiki/Home.md"), SettingsHelper(PERIODIC_INPUT_ENABLED, 0L), SettingsHelper(PERIODIC_INPUT_TIME, 1000 * 60 * 5), SettingsHelper(PERIODIC_INPUT_PORT, 0L), SettingsHelper(PERIODIC_INPUT_VALUE, string.Empty), SettingsHelper(TEAMS_MODE_ENABLED, 0L), SettingsHelper(TEAMS_MODE_MAX_PORT, 3L), SettingsHelper(TEAMS_MODE_NEXT_PORT, 0L), SettingsHelper(DEFAULT_INPUT_DURATION, 200L), SettingsHelper(MAX_INPUT_DURATION, 60000L), SettingsHelper(GLOBAL_MID_INPUT_DELAY_ENABLED, 0L), SettingsHelper(GLOBAL_MID_INPUT_DELAY_TIME, 34L), SettingsHelper(LAST_CONSOLE, 1L), SettingsHelper(LAST_VCONTROLLER_TYPE, (long)VControllerHelper.GetDefaultVControllerTypeForPlatform(TRBotOSPlatform.CurrentOS)), SettingsHelper(JOYSTICK_COUNT, 1L), SettingsHelper(GLOBAL_INPUT_LEVEL, (long)PermissionLevels.User), }; return(defaultSettings); }
private void HandleReloadBoth() { //Check if the periodic input value changed, and enable/disable the routine if we should long periodicEnabled = DataHelper.GetSettingInt(SettingsConstants.PERIODIC_INPUT_ENABLED, 0L); if (periodicEnabled == 0) { RoutineHandler.FindRoutine(RoutineConstants.PERIODIC_INPUT_ROUTINE_ID, out int rIndex); //Remove the routine if it exists if (rIndex >= 0) { RoutineHandler.RemoveRoutine(rIndex); } } else { RoutineHandler.FindRoutine(RoutineConstants.PERIODIC_INPUT_ROUTINE_ID, out int rIndex); //Add the routine if it doesn't exist if (rIndex < 0) { RoutineHandler.AddRoutine(new PeriodicInputRoutine()); } } //Check if the virtual controller type was changed if (LastVControllerTypeChanged() == true) { VirtualControllerTypes lastVControllerType = (VirtualControllerTypes)DataHelper.GetSettingInt(SettingsConstants.LAST_VCONTROLLER_TYPE, 0L); VirtualControllerTypes supportedVCType = VControllerHelper.ValidateVirtualControllerType(lastVControllerType, TRBotOSPlatform.CurrentOS); //Show a message saying the previous value wasn't supported if (VControllerHelper.IsVControllerSupported(lastVControllerType, TRBotOSPlatform.CurrentOS) == false) { MsgHandler.QueueMessage($"Current virtual controller {lastVControllerType} is not supported by the {TRBotOSPlatform.CurrentOS} platform. Switched it to the default of {supportedVCType} for this platform."); using (BotDBContext context = DatabaseManager.OpenContext()) { Settings lastVControllerSetting = DataHelper.GetSettingNoOpen(SettingsConstants.LAST_VCONTROLLER_TYPE, context); lastVControllerSetting.ValueInt = (long)supportedVCType; context.SaveChanges(); } } ChangeVControllerType(supportedVCType); return; } ReinitVControllerCount(); }
public override void ExecuteCommand(EvtChatCommandArgs args) { List <string> arguments = args.Command.ArgumentsAsList; long lastVControllerType = DataHelper.GetSettingInt(SettingsConstants.LAST_VCONTROLLER_TYPE, 0L); VirtualControllerTypes curVCType = (VirtualControllerTypes)lastVControllerType; //See the virtual controller if (arguments.Count == 0) { QueueMessage($"The current virtual controller is {(VirtualControllerTypes)lastVControllerType}. To set the virtual controller, add one as an argument: {CachedVCTypesStr}"); return; } //Invalid number of arguments if (arguments.Count > 1) { QueueMessage(UsageMessage); return; } using (BotDBContext context = DatabaseManager.OpenContext()) { //Check if the user has the ability to set the type User user = DataHelper.GetUserNoOpen(args.Command.ChatMessage.Username, context); if (user != null && user.HasEnabledAbility(PermissionConstants.SET_VCONTROLLER_TYPE_ABILITY) == false) { QueueMessage("You don't have the ability to set the virtual controller type!"); return; } } string vControllerStr = arguments[0]; //Parse if (EnumUtility.TryParseEnumValue(vControllerStr, out VirtualControllerTypes parsedVCType) == false) { QueueMessage($"Please enter a valid virtual controller: {CachedVCTypesStr}"); return; } //Same type if (parsedVCType == curVCType && DataContainer.ControllerMngr.Initialized == true) { QueueMessage($"The current virtual controller is already {curVCType}!"); return; } //Make sure this virtual controller is supported on this platform if (VControllerHelper.IsVControllerSupported(parsedVCType, TRBotOSPlatform.CurrentOS) == false) { QueueMessage($"{parsedVCType} virtual controllers are not supported on {TRBotOSPlatform.CurrentOS} platforms."); return; } using (BotDBContext context = DatabaseManager.OpenContext()) { //Set the value and save Settings lastVControllerSetting = DataHelper.GetSettingNoOpen(SettingsConstants.LAST_VCONTROLLER_TYPE, context); lastVControllerSetting.ValueInt = (long)parsedVCType; context.SaveChanges(); } //Stop and halt all inputs InputHandler.StopAndHaltAllInputs(); try { //Assign the new controller manager IVirtualControllerManager controllerMngr = VControllerHelper.GetVControllerMngrForType(parsedVCType); if (controllerMngr == null) { QueueMessage($"Virtual controller manager of new type {parsedVCType} failed to initialize. This indicates an invalid {SettingsConstants.LAST_VCONTROLLER_TYPE} setting in the database or an unimplemented platform."); return; } //Dispose the controller manager DataContainer.ControllerMngr.Dispose(); DataContainer.SetCurVControllerType(parsedVCType); DataContainer.SetControllerManager(controllerMngr); DataContainer.ControllerMngr.Initialize(); //Ensure we clamp the controller count to the correct value for this virtual controller manager int minControllerCount = DataContainer.ControllerMngr.MinControllers; int maxControllerCount = DataContainer.ControllerMngr.MaxControllers; int joystickCount = (int)DataHelper.GetSettingInt(SettingsConstants.JOYSTICK_COUNT, 0L); int newJoystickCount = joystickCount; if (joystickCount < minControllerCount) { QueueMessage($"Controller count of {joystickCount} is invalid. Clamping to the min of {minControllerCount}."); newJoystickCount = minControllerCount; } else if (joystickCount > maxControllerCount) { QueueMessage($"Controller count of {joystickCount} is invalid. Clamping to the max of {maxControllerCount}."); newJoystickCount = maxControllerCount; } if (joystickCount != newJoystickCount) { using (BotDBContext context = DatabaseManager.OpenContext()) { //Adjust the joystick count setting Settings joystickCountSetting = DataHelper.GetSettingNoOpen(SettingsConstants.JOYSTICK_COUNT, context); joystickCountSetting.ValueInt = newJoystickCount; context.SaveChanges(); } } int acquiredCount = DataContainer.ControllerMngr.InitControllers(newJoystickCount); QueueMessage($"Set virtual controller to {parsedVCType} with {acquiredCount} controller(s) and reset all running inputs!"); } catch (Exception e) { DataContainer.MessageHandler.QueueMessage($"Error changing virtual controller type: {e.Message}"); return; } finally { //Resume inputs InputHandler.ResumeRunningInputs(); } }
private void HandleReloadBoth() { //Check for changes in the log level Serilog.Events.LogEventLevel logLevel = (Serilog.Events.LogEventLevel)DataHelper.GetSettingInt(SettingsConstants.LOG_LEVEL, (long)Serilog.Events.LogEventLevel.Information); if (logLevel != TRBotLogger.MinLoggingLevel) { TRBotLogger.Logger.Information($"Detected change in logging level - changing the logging level from {TRBotLogger.MinLoggingLevel} to {logLevel}"); TRBotLogger.SetLogLevel(logLevel); } //Check if the periodic input value changed, and enable/disable the routine if we should long periodicEnabled = DataHelper.GetSettingInt(SettingsConstants.PERIODIC_INPUT_ENABLED, 0L); if (periodicEnabled == 0) { RoutineHandler.FindRoutine(RoutineConstants.PERIODIC_INPUT_ROUTINE_ID, out int rIndex); //Remove the routine if it exists if (rIndex >= 0) { RoutineHandler.RemoveRoutine(rIndex); } } else { RoutineHandler.FindRoutine(RoutineConstants.PERIODIC_INPUT_ROUTINE_ID, out int rIndex); //Add the routine if it doesn't exist if (rIndex < 0) { RoutineHandler.AddRoutine(new PeriodicInputRoutine()); } } //Check if the virtual controller type was changed if (LastVControllerTypeChanged() == true) { VirtualControllerTypes lastVControllerType = (VirtualControllerTypes)DataHelper.GetSettingInt(SettingsConstants.LAST_VCONTROLLER_TYPE, 0L); VirtualControllerTypes supportedVCType = VControllerHelper.ValidateVirtualControllerType(lastVControllerType, TRBotOSPlatform.CurrentOS); //Show a message saying the previous value wasn't supported if (VControllerHelper.IsVControllerSupported(lastVControllerType, TRBotOSPlatform.CurrentOS) == false) { MsgHandler.QueueMessage($"Current virtual controller {lastVControllerType} is not supported by the {TRBotOSPlatform.CurrentOS} platform. Switched it to the default of {supportedVCType} for this platform."); using (BotDBContext context = DatabaseManager.OpenContext()) { Settings lastVControllerSetting = DataHelper.GetSettingNoOpen(SettingsConstants.LAST_VCONTROLLER_TYPE, context); lastVControllerSetting.ValueInt = (long)supportedVCType; context.SaveChanges(); } } ChangeVControllerType(supportedVCType); } else { ReinitVControllerCount(); } //Handle message throttling changes MessageThrottlingOptions msgThrottle = (MessageThrottlingOptions)DataHelper.GetSettingInt(SettingsConstants.MESSAGE_THROTTLE_TYPE, 0L); long msgTime = DataHelper.GetSettingInt(SettingsConstants.MESSAGE_COOLDOWN, 30000L); long msgThrottleCount = DataHelper.GetSettingInt(SettingsConstants.MESSAGE_THROTTLE_COUNT, 20L); if (msgThrottle != MsgHandler.CurThrottleOption) { TRBotLogger.Logger.Information("Detected change in message throttling type - changing message throttler."); } MsgHandler.SetMessageThrottling(msgThrottle, new MessageThrottleData(msgTime, msgThrottleCount)); }
public void Initialize() { if (Initialized == true) { return; } //Use invariant culture Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture; //Set up the logger string logPath = Path.Combine(LoggingConstants.LogFolderPath, LoggingConstants.LOG_FILE_NAME); if (Utilities.FileHelpers.ValidatePathForFile(logPath) == false) { Console.WriteLine("Logger path cannot be validated. This is a problem! Double check the path is correct."); } //Set up the logger //Cap the size at 10 MB TRBotLogger.SetupLogger(logPath, Serilog.Events.LogEventLevel.Verbose, Serilog.RollingInterval.Day, 1024L * 1024L * 10L, TimeSpan.FromSeconds(60d)); //Initialize database string databasePath = Path.Combine(DataConstants.DataFolderPath, DataConstants.DATABASE_FILE_NAME); TRBotLogger.Logger.Information($"Validating database at: {databasePath}"); if (Utilities.FileHelpers.ValidatePathForFile(databasePath) == false) { TRBotLogger.Logger.Error($"Cannot create database path at {databasePath}. Check if you have permission to write to this directory. Aborting."); return; } TRBotLogger.Logger.Information("Database path validated! Initializing database and importing migrations."); DatabaseManager.SetDatabasePath(databasePath); DatabaseManager.InitAndMigrateContext(); TRBotLogger.Logger.Information("Checking to initialize default values for missing database entries."); //Check for and initialize default values if the database was newly created or needs updating int addedDefaultEntries = DataHelper.InitDefaultData(); if (addedDefaultEntries > 0) { TRBotLogger.Logger.Information($"Added {addedDefaultEntries} additional entries to the database."); } //Set the logger's log level long logLevel = DataHelper.GetSettingInt(SettingsConstants.LOG_LEVEL, (long)Serilog.Events.LogEventLevel.Information); TRBotLogger.SetLogLevel((Serilog.Events.LogEventLevel)logLevel); TRBotLogger.Logger.Information("Initializing client service"); //Initialize client service InitClientService(); //If the client service doesn't exist, we can't continue if (ClientService == null) { TRBotLogger.Logger.Error("Client service failed to initialize; please check your settings. Aborting."); return; } //Set client service and message cooldown MsgHandler.SetClientService(ClientService); MessageThrottlingOptions msgThrottleOption = (MessageThrottlingOptions)DataHelper.GetSettingInt(SettingsConstants.MESSAGE_THROTTLE_TYPE, 0L); long msgCooldown = DataHelper.GetSettingInt(SettingsConstants.MESSAGE_COOLDOWN, 30000L); long msgThrottleCount = DataHelper.GetSettingInt(SettingsConstants.MESSAGE_THROTTLE_COUNT, 20L); MsgHandler.SetMessageThrottling(msgThrottleOption, new MessageThrottleData(msgCooldown, msgThrottleCount)); //Subscribe to events UnsubscribeEvents(); SubscribeEvents(); DataContainer.SetMessageHandler(MsgHandler); DataContainer.SetDataReloader(DataReloader); TRBotLogger.Logger.Information("Setting up virtual controller manager."); VirtualControllerTypes lastVControllerType = (VirtualControllerTypes)DataHelper.GetSettingInt(SettingsConstants.LAST_VCONTROLLER_TYPE, 0L); VirtualControllerTypes curVControllerType = VControllerHelper.ValidateVirtualControllerType(lastVControllerType, TRBotOSPlatform.CurrentOS); //Show a message saying the previous value wasn't supported and save the changes if (VControllerHelper.IsVControllerSupported(lastVControllerType, TRBotOSPlatform.CurrentOS) == false) { MsgHandler.QueueMessage($"Current virtual controller {lastVControllerType} is not supported by the {TRBotOSPlatform.CurrentOS} platform. Switched it to the default of {curVControllerType} for this platform."); using (BotDBContext context = DatabaseManager.OpenContext()) { Settings lastVControllerSetting = DataHelper.GetSettingNoOpen(SettingsConstants.LAST_VCONTROLLER_TYPE, context); lastVControllerSetting.ValueInt = (long)curVControllerType; context.SaveChanges(); } } DataContainer.SetCurVControllerType(curVControllerType); IVirtualControllerManager controllerMngr = VControllerHelper.GetVControllerMngrForType(curVControllerType); DataContainer.SetControllerManager(controllerMngr); int controllerCount = 0; //Clamp the controller count to the min and max allowed by the virtual controller manager using (BotDBContext context = DatabaseManager.OpenContext()) { Settings joystickCountSetting = DataHelper.GetSettingNoOpen(SettingsConstants.JOYSTICK_COUNT, context); int minCount = DataContainer.ControllerMngr.MinControllers; int maxCount = DataContainer.ControllerMngr.MaxControllers; //Validate controller count if (joystickCountSetting.ValueInt < minCount) { MsgHandler.QueueMessage($"Controller count of {joystickCountSetting.ValueInt} in database is invalid. Clamping to the min of {minCount}."); joystickCountSetting.ValueInt = minCount; context.SaveChanges(); } else if (joystickCountSetting.ValueInt > maxCount) { MsgHandler.QueueMessage($"Controller count of {joystickCountSetting.ValueInt} in database is invalid. Clamping to the max of {maxCount}."); joystickCountSetting.ValueInt = maxCount; context.SaveChanges(); } controllerCount = (int)joystickCountSetting.ValueInt; } DataContainer.ControllerMngr.Initialize(); int acquiredCount = DataContainer.ControllerMngr.InitControllers(controllerCount); TRBotLogger.Logger.Information($"Setting up virtual controller {curVControllerType} and acquired {acquiredCount} controllers!"); CmdHandler = new CommandHandler(); CmdHandler.Initialize(DataContainer, RoutineHandler); DataReloader.SoftDataReloadedEvent -= OnSoftReload; DataReloader.SoftDataReloadedEvent += OnSoftReload; DataReloader.HardDataReloadedEvent -= OnHardReload; DataReloader.HardDataReloadedEvent += OnHardReload; //Initialize routines InitRoutines(); //Cache our parser InputParser = new Parser(); Initialized = true; }