public void AddAmount(UserDataModel user, int amount) { if (!user.IsCurrencyRankExempt && amount > 0) { UserRankViewModel prevRank = this.GetRankForPoints(this.GetAmount(user)); this.SetAmount(user, this.GetAmount(user) + amount); UserRankViewModel newRank = this.GetRankForPoints(this.GetAmount(user)); } }
public void SetAmount(UserDataModel user, UserInventoryItemModel item, int amount) { if (!user.InventoryAmounts.ContainsKey(this.ID)) { user.InventoryAmounts[this.ID] = new Dictionary <Guid, int>(); } user.InventoryAmounts[this.ID][item.ID] = Math.Min(Math.Max(amount, 0), item.HasMaxAmount ? item.MaxAmount : this.DefaultMaxAmount); if (ChannelSession.Settings != null) { ChannelSession.Settings.UserData.ManualValueChanged(user.ID); } }
public CurrencyWindowViewModel() { if (ChannelSession.Settings.Currency.All(c => !c.Value.IsPrimary)) { this.IsPrimary = true; } this.OnlineRate = CurrencyAcquireRateTypeEnum.Minutes; this.OfflineRate = CurrencyAcquireRateTypeEnum.Disabled; this.AutomaticResetRate = CurrencyResetRateEnum.Never; this.AddRankCommand = this.CreateCommand(async(parameter) => { if (string.IsNullOrEmpty(this.NewRankName)) { await DialogHelper.ShowMessage(Resources.RankRequired); return; } if (this.NewRankAmount < 0) { await DialogHelper.ShowMessage(Resources.MinimumAmountRequired); return; } if (this.Ranks.Any(r => r.Name.Equals(this.NewRankName) || r.Amount == this.NewRankAmount)) { await DialogHelper.ShowMessage(Resources.UniqueRankNameAndMinimumAmountRequired); return; } RankModel newRank = new RankModel(this.NewRankName, this.NewRankAmount); this.Ranks.Add(newRank); var tempRanks = this.Ranks.ToList(); this.Ranks.ClearAndAddRange(tempRanks.OrderBy(r => r.Amount)); this.NewRankName = string.Empty; this.NewRankAmount = 0; }); this.ManualResetCommand = this.CreateCommand(async(parameter) => { if (await DialogHelper.ShowConfirmation(string.Format(Resources.ResetCurrencyRankPointsPrompt, this.CurrencyRankIdentifierString))) { if (this.Currency != null) { await this.Currency.Reset(); } } }); this.RetroactivelyGivePointsCommand = this.CreateCommand(async(parameter) => { if (await DialogHelper.ShowConfirmation(string.Format(Resources.RetroactivelyGivePointsPrompt1 + Environment.NewLine + Environment.NewLine + Resources.RetroactivelyGivePointsPrompt2 + Environment.NewLine + Environment.NewLine + Resources.RetroactivelyGivePointsPrompt3, this.CurrencyRankIdentifierString))) { if (this.Currency != null && this.Currency.AcquireInterval > 0) { if (this.Currency.SpecialTracking != CurrencySpecialTrackingEnum.None) { await DialogHelper.ShowMessage(Resources.RetroactiveUnsupported); return; } await this.Currency.Reset(); foreach (MixItUp.Base.Model.User.UserDataModel userData in ChannelSession.Settings.UserData.Values) { int intervalsToGive = userData.ViewingMinutes / this.Currency.AcquireInterval; this.Currency.AddAmount(userData, this.Currency.AcquireAmount * intervalsToGive); if (userData.TwitchUserRoles.Contains(UserRoleEnum.Mod) || userData.TwitchUserRoles.Contains(UserRoleEnum.ChannelEditor)) { this.Currency.AddAmount(userData, this.Currency.ModeratorBonus * intervalsToGive); } else if (userData.TwitchUserRoles.Contains(UserRoleEnum.Subscriber)) { this.Currency.AddAmount(userData, this.Currency.SubscriberBonus * intervalsToGive); } ChannelSession.Settings.UserData.ManualValueChanged(userData.ID); } } } }); this.ImportFromFileCommand = this.CreateCommand(async(parameter) => { this.userImportData.Clear(); if (await DialogHelper.ShowConfirmation(string.Format(Resources.ImportPointsPrompt1 + Environment.NewLine + Environment.NewLine + Resources.ImportPointsPrompt2, this.CurrencyRankIdentifierString))) { try { string filePath = ChannelSession.Services.FileService.ShowOpenFileDialog(); if (!string.IsNullOrEmpty(filePath)) { string fileContents = await ChannelSession.Services.FileService.ReadFile(filePath); string[] lines = fileContents.Split(new string[] { Environment.NewLine, "\n" }, StringSplitOptions.RemoveEmptyEntries); if (lines.Count() > 0) { foreach (string line in lines) { long id = 0; string username = null; int amount = 0; string[] segments = line.Split(new string[] { " ", "\t", "," }, StringSplitOptions.RemoveEmptyEntries); if (segments.Count() == 2) { if (!int.TryParse(segments[1], out amount)) { throw new InvalidOperationException("File is not in the correct format"); } if (!long.TryParse(segments[0], out id)) { username = segments[0]; } } else if (segments.Count() == 3) { if (!long.TryParse(segments[0], out id)) { throw new InvalidOperationException("File is not in the correct format"); } if (!int.TryParse(segments[2], out amount)) { throw new InvalidOperationException("File is not in the correct format"); } } else { throw new InvalidOperationException("File is not in the correct format"); } UserViewModel user = null; if (amount > 0) { if (id > 0) { MixItUp.Base.Model.User.UserDataModel userData = ChannelSession.Settings.GetUserDataByTwitchID(id.ToString()); if (userData != null) { user = new UserViewModel(userData); } else { UserModel twitchUser = await ChannelSession.TwitchUserConnection.GetNewAPIUserByID(id.ToString()); if (twitchUser != null) { user = new UserViewModel(twitchUser); } } } else if (!string.IsNullOrEmpty(username)) { UserModel twitchUser = await ChannelSession.TwitchUserConnection.GetNewAPIUserByLogin(username); if (twitchUser != null) { user = new UserViewModel(twitchUser); } } } if (user != null) { if (!this.userImportData.ContainsKey(user.ID)) { this.userImportData[user.ID] = amount; } this.userImportData[user.ID] = Math.Max(this.userImportData[user.ID], amount); this.ImportFromFileText = string.Format("{0} {1}...", this.userImportData.Count(), MixItUp.Base.Resources.Imported); } } foreach (var kvp in this.userImportData) { if (ChannelSession.Settings.UserData.ContainsKey(kvp.Key)) { MixItUp.Base.Model.User.UserDataModel userData = ChannelSession.Settings.UserData[kvp.Key]; this.Currency.SetAmount(userData, kvp.Value); } } this.ImportFromFileText = MixItUp.Base.Resources.ImportFromFile; return; } } } catch (Exception ex) { Logger.Log(ex); } await DialogHelper.ShowMessage(Resources.CurrencyImportFailed + Environment.NewLine + Environment.NewLine + "<USERNAME> <AMOUNT>" + Environment.NewLine + Environment.NewLine + "<USER ID> <AMOUNT>" + Environment.NewLine + Environment.NewLine + "<USER ID> <USERNAME> <AMOUNT>"); this.ImportFromFileText = MixItUp.Base.Resources.ImportFromFile; } }); this.ExportToFileCommand = this.CreateCommand(async(parameter) => { string filePath = ChannelSession.Services.FileService.ShowSaveFileDialog(this.Currency.Name + " Data.txt"); if (!string.IsNullOrEmpty(filePath)) { StringBuilder fileContents = new StringBuilder(); foreach (MixItUp.Base.Model.User.UserDataModel userData in ChannelSession.Settings.UserData.Values.ToList()) { fileContents.AppendLine(string.Format("{0} {1} {2}", userData.TwitchID, userData.Username, this.Currency.GetAmount(userData))); } await ChannelSession.Services.FileService.SaveFile(filePath, fileContents.ToString()); } }); this.HelpCommand = this.CreateCommand((parameter) => { ProcessHelper.LaunchLink("https://github.com/SaviorXTanren/mixer-mixitup/wiki/Currency,-Rank,-&-Inventory"); return(Task.FromResult(0)); }); }
public CurrencyWindowViewModel() { if (ChannelSession.Settings.Currency.All(c => !c.Value.IsPrimary)) { this.IsPrimary = true; } this.OnlineRate = CurrencyAcquireRateTypeEnum.Minutes; this.OfflineRate = CurrencyAcquireRateTypeEnum.Disabled; this.AutomaticResetRate = CurrencyResetRateEnum.Never; this.AddRankCommand = this.CreateCommand(async(parameter) => { if (string.IsNullOrEmpty(this.NewRankName)) { await DialogHelper.ShowMessage("A rank name must be specified"); return; } if (this.NewRankAmount < 0) { await DialogHelper.ShowMessage("A minimum amount must be specified"); return; } if (this.Ranks.Any(r => r.Name.Equals(this.NewRankName) || r.Amount == this.NewRankAmount)) { await DialogHelper.ShowMessage("Every rank must have a unique name and minimum amount"); return; } RankModel newRank = new RankModel(this.NewRankName, this.NewRankAmount); this.Ranks.Add(newRank); var tempRanks = this.Ranks.ToList(); this.Ranks.ClearAndAddRange(tempRanks.OrderBy(r => r.Amount)); this.NewRankName = string.Empty; this.NewRankAmount = 0; }); this.ManualResetCommand = this.CreateCommand(async(parameter) => { if (await DialogHelper.ShowConfirmation(string.Format("Do you want to reset all {0} points?", this.CurrencyRankIdentifierString))) { if (this.Currency != null) { await this.Currency.Reset(); } } }); this.RetroactivelyGivePointsCommand = this.CreateCommand(async(parameter) => { if (await DialogHelper.ShowConfirmation(string.Format("This option will reset all {0} points for this {0} & assign an amount to each user that directly equals the SAVED online rate, not the currently edited online rate. Before using this option, please save all edits to this {0}, re-edit it, then select this option." + Environment.NewLine + Environment.NewLine + "EX: If the Online Rate is \"1 Per Hour\" and a user has 16 viewing hours, then that user's {0} points will be set to 16." + Environment.NewLine + Environment.NewLine + "This process may take some time; are you sure you wish to do this?", this.CurrencyRankIdentifierString))) { if (this.Currency != null && this.Currency.AcquireInterval > 0) { if (this.Currency.SpecialTracking != CurrencySpecialTrackingEnum.None) { await DialogHelper.ShowMessage("The rate type for this currency does not support retroactively giving points."); return; } await this.Currency.Reset(); foreach (MixItUp.Base.Model.User.UserDataModel userData in ChannelSession.Settings.UserData.Values) { int intervalsToGive = userData.ViewingMinutes / this.Currency.AcquireInterval; this.Currency.AddAmount(userData, this.Currency.AcquireAmount * intervalsToGive); if (userData.TwitchUserRoles.Contains(UserRoleEnum.Mod) || userData.TwitchUserRoles.Contains(UserRoleEnum.ChannelEditor)) { this.Currency.AddAmount(userData, this.Currency.ModeratorBonus * intervalsToGive); } else if (userData.TwitchUserRoles.Contains(UserRoleEnum.Subscriber)) { this.Currency.AddAmount(userData, this.Currency.SubscriberBonus * intervalsToGive); } ChannelSession.Settings.UserData.ManualValueChanged(userData.ID); } } } }); this.ImportFromFileCommand = this.CreateCommand(async(parameter) => { this.userImportData.Clear(); if (await DialogHelper.ShowConfirmation(string.Format("This will allow you to import the total amounts that each user had, assign them to this {0}, and will overwrite any amounts that each user has." + Environment.NewLine + Environment.NewLine + "This process may take some time; are you sure you wish to do this?", this.CurrencyRankIdentifierString))) { try { string filePath = ChannelSession.Services.FileService.ShowOpenFileDialog(); if (!string.IsNullOrEmpty(filePath)) { string fileContents = await ChannelSession.Services.FileService.ReadFile(filePath); string[] lines = fileContents.Split(new string[] { Environment.NewLine, "\n" }, StringSplitOptions.RemoveEmptyEntries); if (lines.Count() > 0) { foreach (string line in lines) { long id = 0; string username = null; int amount = 0; string[] segments = line.Split(new string[] { " ", "\t", "," }, StringSplitOptions.RemoveEmptyEntries); if (segments.Count() == 2) { if (!int.TryParse(segments[1], out amount)) { throw new InvalidOperationException("File is not in the correct format"); } if (!long.TryParse(segments[0], out id)) { username = segments[0]; } } else if (segments.Count() == 3) { if (!long.TryParse(segments[0], out id)) { throw new InvalidOperationException("File is not in the correct format"); } if (!int.TryParse(segments[2], out amount)) { throw new InvalidOperationException("File is not in the correct format"); } } else { throw new InvalidOperationException("File is not in the correct format"); } UserViewModel user = null; if (amount > 0) { if (id > 0) { MixItUp.Base.Model.User.UserDataModel userData = ChannelSession.Settings.GetUserDataByTwitchID(id.ToString()); if (userData != null) { user = new UserViewModel(userData); } else { UserModel twitchUser = await ChannelSession.TwitchUserConnection.GetNewAPIUserByID(id.ToString()); if (twitchUser != null) { user = new UserViewModel(twitchUser); } } } else if (!string.IsNullOrEmpty(username)) { UserModel twitchUser = await ChannelSession.TwitchUserConnection.GetNewAPIUserByLogin(username); if (twitchUser != null) { user = new UserViewModel(twitchUser); } } } if (user != null) { if (!this.userImportData.ContainsKey(user.ID)) { this.userImportData[user.ID] = amount; } this.userImportData[user.ID] = Math.Max(this.userImportData[user.ID], amount); this.ImportFromFileText = string.Format("{0} {1}...", this.userImportData.Count(), MixItUp.Base.Resources.Imported); } } foreach (var kvp in this.userImportData) { if (ChannelSession.Settings.UserData.ContainsKey(kvp.Key)) { MixItUp.Base.Model.User.UserDataModel userData = ChannelSession.Settings.UserData[kvp.Key]; this.Currency.SetAmount(userData, kvp.Value); } } this.ImportFromFileText = MixItUp.Base.Resources.ImportFromFile; return; } } } catch (Exception ex) { Logger.Log(ex); } await DialogHelper.ShowMessage("We were unable to import the data. Please ensure your file is in one of the following formats:" + Environment.NewLine + Environment.NewLine + "<USERNAME> <AMOUNT>" + Environment.NewLine + Environment.NewLine + "<USER ID> <AMOUNT>" + Environment.NewLine + Environment.NewLine + "<USER ID> <USERNAME> <AMOUNT>"); this.ImportFromFileText = MixItUp.Base.Resources.ImportFromFile; } }); this.ExportToFileCommand = this.CreateCommand(async(parameter) => { string filePath = ChannelSession.Services.FileService.ShowSaveFileDialog(this.Currency.Name + " Data.txt"); if (!string.IsNullOrEmpty(filePath)) { StringBuilder fileContents = new StringBuilder(); foreach (MixItUp.Base.Model.User.UserDataModel userData in ChannelSession.Settings.UserData.Values.ToList()) { fileContents.AppendLine(string.Format("{0} {1} {2}", userData.TwitchID, userData.Username, this.Currency.GetAmount(userData))); } await ChannelSession.Services.FileService.SaveFile(filePath, fileContents.ToString()); } }); this.HelpCommand = this.CreateCommand((parameter) => { ProcessHelper.LaunchLink("https://github.com/SaviorXTanren/mixer-mixitup/wiki/Currency,-Rank,-&-Inventory"); return(Task.FromResult(0)); }); }
public void ResetAmount(UserDataModel user) { user.InventoryAmounts[this.ID] = new Dictionary <Guid, int>(); ChannelSession.Settings.UserData.ManualValueChanged(user.ID); }
public bool HasAmount(UserDataModel user, UserInventoryItemModel item, int amount) { return(user.IsCurrencyRankExempt || this.GetAmount(user, item) >= amount); }
public UserRankViewModel GetRank(UserDataModel user) { return(this.GetRankForPoints(this.GetAmount(user))); }
public void ResetAmount(UserDataModel user) { this.SetAmount(user, 0); }
public bool HasAmount(UserDataModel user, int amount) { return(user.IsCurrencyRankExempt || this.GetAmount(user) >= amount); }