protected ModuleDataSourceBase ChannelData(CommandContext ctx) { ModuleDataSourceBase retval = MasterDataSingleton.Instance.GetDataSource(ctx.Channel); if (retval == null) { ctx.RespondAsync($"Sorry, I can't find any existing data for {Formatter.Bold(ctx.Channel.Name)}. This will probably throw an error."); } return(retval); }
public async Task LoadData(DiscordChannel channel, string ModuleIdentifier) { ModuleDataSourceBase dataToLoad = null; ulong channelId = channel.Id; string filePath = "WumpusData_" + ModuleIdentifier + "_" + channelId.ToString() + ".bin"; await Task.Run(new Action(delegate() { using (Stream stream = File.Open(filePath, FileMode.Open)) { var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); dataToLoad = (ModuleDataSourceBase)binaryFormatter.Deserialize(stream); } } )); }
public ModuleDataSourceBase GetDataSource(DiscordChannel channel) { ModuleDataSourceBase retval = null; if (_masterDataSource.ContainsKey(channel)) { retval = _masterDataSource[channel]; } //TODO: better exception handling /* * else * { * throw new Exception("No game set for channel " + channel.Name); * } */ return(retval); }
public async Task SaveData(DiscordChannel channel) { if (!DoesDataExistForChannel(channel)) { // do nothing; } else { ModuleDataSourceBase dataToSave = _masterDataSource[channel]; ulong channelId = channel.Id; string filePath = "WumpusData_" + dataToSave.ModuleIdentifier + "_" + channelId.ToString() + ".bin"; await Task.Run(new Action( delegate() { using (Stream stream = File.Open(filePath, FileMode.Create)) { var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); binaryFormatter.Serialize(stream, dataToSave); } } )); } }
public void SetDataSourceForChannel(DiscordChannel channel, ModuleDataSourceBase dataSource, bool safeMode = true) { if (!DoesDataExistForChannel(channel)) { dataSource.HomeChannel = channel.Id; SetNewDataSource(channel, dataSource); } else { if (!safeMode) { dataSource.HomeChannel = channel.Id; ReplaceDataSource(channel, dataSource); } //TODO: better exception handling /* * else * { * throw new Exception("Data already exists for " + channel.Name + " and safeMode was enabled"); * } */ } }
private void ReplaceDataSource(DiscordChannel channel, ModuleDataSourceBase dataSource) { _masterDataSource[channel] = dataSource; }
private void SetNewDataSource(DiscordChannel channel, ModuleDataSourceBase dataSource) { _masterDataSource.Add(channel, dataSource); }