public bool AddBalances(string input) { /* * SAMPLE: * * Game Night 02-24 * Bob 10 * Fred 8 * George 8 * Objective "Beat Me at Chess" * Fred 5 * * Each balance entry should use the plain text note preceding it as a note * Convention is to use a tab delimiter for balance entries and no tab in the note */ bool fullSuccess = true; string currentNote = ""; DateTime currentTime = DateTime.Now; input = input.Replace("\r\n", "\r"); List <String> balanceEntries = input.Split(new string[] { "\r" }, StringSplitOptions.RemoveEmptyEntries).ToList(); Regex userBalance = new Regex("^(-?[^\t]+)\t+([0-9]+)$"); Regex note = new Regex("^([^\t]+)$"); for (int i = 0; i < balanceEntries.Count; i++) { if (note.Match(balanceEntries[i]).Success) { currentNote = note.Match(balanceEntries[i]).Groups[1].Value; } else if (userBalance.Match(balanceEntries[i]).Success) { Match userBalanceMatch = userBalance.Match(balanceEntries[i]); AppUser user = GetRequestedUser(userBalanceMatch.Groups[1].Value.Trim(), true); int points = Int32.Parse(userBalanceMatch.Groups[2].Value, System.Globalization.NumberStyles.AllowLeadingSign); user.CreateBalanceEntry(currentNote, points, currentTime); UserManager.Update(user); } else { fullSuccess = false; } } // If all rows have successfully been parsed, then persist! if (fullSuccess) { unitOfWork.Save(); } return(fullSuccess); }
public List <String> AddBalance(string[] userNickNames) { int points = 0; List <String> updatedUsers = new List <String>(); DateTime time = DateTime.Now; foreach (string userNickname in userNickNames) { string nickname = userNickname; if (char.IsNumber(nickname[0]) == true || nickname[0] == '-') { string numSubString = String.Empty; int k = 0; // easy way to check the first part of the string to see if it's a negative number if (nickname[0] == '-') { k = 1; numSubString = "-"; } for (int i = k; i < nickname.Length - 1; i++) { if (char.IsNumber(nickname[i])) { numSubString += nickname[i]; } else { break; } } nickname = nickname.Substring(numSubString.Length); Int32.TryParse(numSubString, out points); } AppUser user = GetUserByNickName(nickname); if (user != null) { user.CreateBalanceEntry("Admin adjusted balance update", points, time); UserManager.Update(user); updatedUsers.Add(user.UserName + ": " + points + "\n"); } } unitOfWork.Save(); return(updatedUsers); }
public async Task TransferPoints(int points, string userId) { AppUser donor = await GetCurrentUserNoProperties(); AppUser recipient = await UserManager.FindByIdAsync(userId); if (donor.Balance - donor.ReservedBalance() >= points) { donor.CreateBalanceEntry("Transfer of points to " + recipient.UserName, 0 - points, DateTime.Now); recipient.CreateBalanceEntry("Gift of points from " + donor.UserName, points, DateTime.Now); UserManager.Update(donor); UserManager.Update(recipient); unitOfWork.Save(); } }
public bool TransferPoints(AppUser sender, AppUser recipient, int points) { if (sender.Balance - sender.ReservedBalance() >= points) { sender.CreateBalanceEntry("Transfer of points to " + recipient.UserName, 0 - points, DateTime.Now); recipient.CreateBalanceEntry("Gift of points from " + sender.UserName, points, DateTime.Now); UserManager.Update(sender); UserManager.Update(recipient); unitOfWork.Save(); return(true); } return(false); }
public List <String> AddBalanceForObjective(Objective objective, string[] userNickNames, int objectiveReward) { List <String> userRewards = new List <String>(); int numCompletions = 1; //default 1 DateTime time = DateTime.Now; foreach (string userNickname in userNickNames) { string nickname = userNickname; if (char.IsNumber(nickname[0]) == true) { string numSubString = String.Empty; for (int i = 0; i < nickname.Length - 1; i++) { if (char.IsNumber(nickname[i])) { numSubString = numSubString += nickname[i]; } else { break; } } nickname = nickname.Substring(numSubString.Length); Int32.TryParse(numSubString, out numCompletions); } AppUser user = GetUserByNickName(nickname); if (user != null) { for (int i = 0; i < numCompletions; i++) { user.CreateBalanceEntry(objective, time); } UserManager.Update(user); userRewards.Add(user.UserName + ": " + objectiveReward * numCompletions + "\n"); } } unitOfWork.Save(); return(userRewards); }