Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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();
        }