private List <Action> ImportActions(ExcelPackage package) { try { ExcelWorksheet workSheet = package.Workbook.Worksheets["Action"]; int lastRow = workSheet.Dimension.End.Row; for (int row = 2; row <= lastRow; row++) { var code = workSheet.Cells[row, 1].GetValue <string>(); var name = workSheet.Cells[row, 2].GetValue <string>(); var points = workSheet.Cells[row, 3].GetValue <int>(); var limitsPerDay = workSheet.Cells[row, 4].GetValue <int>(); var throttleSeconds = workSheet.Cells[row, 5].GetValue <int>(); var isEnabled = workSheet.Cells[row, 6].GetValue <bool>(); var requiresVerification = workSheet.Cells[row, 7].GetValue <bool>(); var description = workSheet.Cells[row, 8].GetValue <string>(); if (string.IsNullOrEmpty(code)) { continue; } var action = new Action { Id = Guid.NewGuid(), Code = code, Name = name, Description = description, IsEnabled = isEnabled, LimitPerDay = limitsPerDay, Points = points, ThrottleSeconds = throttleSeconds, RequiresVerification = requiresVerification, CreatedById = Constants.SystemUserId, CreatedOn = DateTimeOffset.Now, LastModifiedById = Constants.SystemUserId, LastModifiedOn = DateTimeOffset.Now }; _context.Action.Add(action); _actions.Add(action); } } catch (Exception ex) { LogError(ex, $"Exception in {nameof(ImportActions)}()"); } return(_actions); }
private List <Action> ImportUserActions(ExcelPackage package) { try { ExcelWorksheet workSheet = package.Workbook.Worksheets["UserAction"]; int lastRow = workSheet.Dimension.End.Row; for (int row = 2; row <= lastRow; row++) { var code = workSheet.Cells[row, 1].GetValue <string>(); var createdOn = ConvertToDateTimeOffsetorNull(workSheet, row, 2); var player = workSheet.Cells[row, 3].GetValue <string>(); var score = ConvertToDoubleNullable(workSheet, row, 4); if (string.IsNullOrEmpty(code) || createdOn == null || score == null) { continue; } var actionByCode = _actions.ToDictionary(i => i.Code); Action action = null; if (actionByCode.TryGetValue(code, out action)) { var usersByDisplayName = _users.ToDictionary(i => i.DisplayName); User userPlayer = null; if (usersByDisplayName.TryGetValue(player, out userPlayer) && score != null) { var userAction = new UserAction { Id = Guid.NewGuid(), ActionId = action.Id, UserId = userPlayer.Id, Score = score.Value, CreatedOn = createdOn.Value, }; _context.UserAction.Add(userAction); } } } } catch (Exception ex) { LogError(ex, $"Exception in {nameof(ImportUserActions)}()"); } return(_actions); }
private List <Challenge> ImportChallenges(ExcelPackage package) { var actionByCode = _actions.ToDictionary(i => i.Code); List <Challenge> items = new List <Challenge>(); try { ExcelWorksheet workSheet = package.Workbook.Worksheets["Challenge"]; int lastRow = workSheet.Dimension.End.Row; for (int row = 3; row <= lastRow; row++) { //var id = workSheet.Cells[row, 1].GetValue<string>(); var name = workSheet.Cells[row, 1].GetValue <string>(); var actions = workSheet.Cells[row, 2].GetValue <string>(); if (string.IsNullOrEmpty(name)) { continue; } var challenge = new Challenge { Id = Guid.NewGuid(), Name = name, CreatedById = Constants.SystemUserId, CreatedOn = DateTimeOffset.Now, LastModifiedById = Constants.SystemUserId, LastModifiedOn = DateTimeOffset.Now }; _context.Challenge.Add(challenge); List <string> codes = actions.Split(',').Select(p => p.Trim()).ToList(); foreach (var code in codes) { Action foundAction = null; if (actionByCode.TryGetValue(code.Trim(), out foundAction)) { var challengeAction = new ChallengeAction { Id = Guid.NewGuid(), Action = foundAction, Challenge = challenge }; _context.ChallengeAction.Add(challengeAction); } } #region Create Levels for (int levelCounter = 0; levelCounter < 3; levelCounter++) { int levelStartColIndex = 3 + levelCounter * 2; var levelName = workSheet.Cells[row, levelStartColIndex].GetValue <string>(); var triggerPoints = workSheet.Cells[row, levelStartColIndex + 1].GetValue <int>(); var challengeLevel = new ChallengeLevel() { Challenge = challenge, Id = Guid.NewGuid(), Name = levelName, TriggerPoint = triggerPoints, IconUrl = string.Empty, Description = string.Empty, LockedIconUrl = string.Empty, Hyperlink = string.Empty }; _context.ChallengeLevel.Add(challengeLevel); } #endregion items.Add(challenge); } } catch (Exception ex) { LogError(ex, $"Exception in {nameof(ImportChallenges)}()"); } return(items); }
private void UpdateUserChallenges() { var challengeLevels = _context.ChallengeLevel.ToList(); foreach (var userAction in _context.UserAction.Include(i => i.Action).Include(i => i.Action.ChallengeAction)) { Action action = userAction.Action; if (action.IsEnabled) { int countOfThisActionToday = 0; //TODO: Calculate this if (countOfThisActionToday < action.LimitPerDay) { DateTime dateTimeOfLastSameAction = DateTime.MinValue; //TODO: Calculate This double lastTimeOfSameAction = (DateTime.Now - dateTimeOfLastSameAction).TotalSeconds; if (lastTimeOfSameAction > action.ThrottleSeconds) { //var userAction = new UserAction //{ // Id = Guid.NewGuid(), // ActionId = action.Id, // UserId = userPlayer.Id, // CreatedOn = createdOn.Value, //}; //_context.UserActions.Add(userAction); } } } //Fetch all the “Enabled” Challenge this Action belongs to var challengeActions = _context.ChallengeAction.Where(i => i.ActionId == action.Id).ToList(); foreach (var challengeAction in challengeActions) { UserChallenge userChallenge = _context.UserChallenge.Include(i => i.ChallengeLevel).Where(i => i.UserId == userAction.UserId && i.ChallengeId == challengeAction.ChallengeId).FirstOrDefault(); if (userChallenge == null) { userChallenge = _context.UserChallenge.Local.Where(i => i.UserId == userAction.UserId && i.ChallengeId == challengeAction.ChallengeId).FirstOrDefault(); } if (userChallenge == null) { //var firstLevel = challengeLevels.Where(i => i.ChallengeId == challenge.Id).OrderBy(i => i.TriggerPoint).First(); userChallenge = new UserChallenge { Id = Guid.NewGuid(), ChallengeId = challengeAction.ChallengeId, UserId = userAction.UserId, //PointsTotal = action.Points, //PointsThisWeek = action.Points, //ChallengeLevelId = firstLevel.Id }; _context.UserChallenge.Add(userChallenge); } userChallenge.PointsTotal += action.Points; userChallenge.PointsThisWeek += action.Points; #region Update Challenge Level var levels = challengeLevels.Where(i => i.ChallengeId == challengeAction.ChallengeId).OrderBy(i => i.TriggerPoint).ToList(); ChallengeLevel userChallengeLevel = null; foreach (var level in levels) { if (userChallenge.PointsTotal >= level.TriggerPoint) { userChallengeLevel = level; break; } } if (userChallenge.ChallengeLevelId != userChallengeLevel.Id) { //leveled up! userChallenge.ChallengeLevelId = userChallengeLevel.Id; } #endregion Update Challenge Level } } _context.SaveChanges(); }