Exemplo n.º 1
0
        public bool AddChallenge(Challenge ao_challenge, int ai_userGroup)
        {
            try {
                IQueryable<Community> lo_homecomquery = from coms in db.Communities
                                              where coms.home == true
                                              select coms;

                Community lo_homecom = lo_homecomquery.First();

                List<KeyValuePair<string, bool>> lo_results = new List<KeyValuePair<string, bool>>();
                foreach (CommunityChallenge lo_comchal in ao_challenge.communityChallenges)
                {
                    RemoteChallengeWS.ChallengeData lo_chaldata = new RemoteChallengeWS.ChallengeData();
                    lo_chaldata.categoryUnitId = ao_challenge.categoryUnitId;
                    if (ao_challenge.endTime != null)
                    {
                        lo_chaldata.endTime = (DateTime)ao_challenge.endTime;
                    }
                    lo_chaldata.startTime = ao_challenge.startTime;
                    lo_chaldata.name = ao_challenge.name;
                    lo_chaldata.id = ao_challenge.Id;

                    int li_id = lo_comchal.communityId;
                    IQueryable<Community> query = from coms in db.Communities
                                                  where coms.Id == li_id
                                                  select coms;

                    Community lo_com = query.First();
                    if (string.IsNullOrWhiteSpace(lo_com.authtoken)) return false;
                    lo_chaldata.authtoken = lo_com.authtoken;
                    GoAberChallengesWSSoapClient lo_service = GetSOAPClient(lo_com);

                    bool lb_res = lo_service.RecieveChallenge(lo_chaldata, ai_userGroup);
                    lo_results.Add(new KeyValuePair<string, bool>(lo_chaldata.name, lb_res));
                }

                for (int i = 0; i < lo_results.Count; i++)
                {
                    if (!lo_results[i].Value) return false;
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates a result object based on activity data and the challenge.
        /// </summary>
        /// <param name="as_challengeid"></param>
        /// <param name="db"></param>
        /// <param name="ao_challenge"></param>
        /// <returns></returns>
        public static Result CreateResult(string as_challengeid, ApplicationDbContext db, Challenge ao_challenge = null)
        {
            if (ao_challenge == null)
            {
                IQueryable<Challenge> lo_query = from c in db.Challenges
                                                 where c.Id == as_challengeid
                                                 select c;
                ao_challenge = lo_query.First();
            }

            ao_challenge.complete = true;
            db.Entry(ao_challenge).State = EntityState.Modified;
            db.SaveChanges();

            IQueryable<int?> lo_query_result = from a in db.ActivityDatas
                                               where
                                               (a.date > ao_challenge.startTime && a.date < ao_challenge.endTime)
                                               && (a.categoryUnitId == ao_challenge.categoryUnitId)
                                               select a.value;

            int? li_result = lo_query_result.ToArray().Sum();

            int li_catunitid = ao_challenge.categoryUnitId;
            IQueryable<CategoryUnit> lo_catunitquery = from c in db.CategoryUnits
                                                       where c.Id == li_catunitid
                                                       select c;
            CategoryUnit lo_catunit = lo_catunitquery.First();

            IQueryable<Community> lo_comquery = from c in db.Communities
                                                where c.home == true
                                                select c;
            Community lo_community = lo_comquery.First();

            Result lo_result = new Result();
            lo_result.challenge = ao_challenge;
            lo_result.challengeId = ao_challenge.Id;
            lo_result.community = lo_community;
            lo_result.communityId = lo_community.Id;
            lo_result.categoryUnit = lo_catunit;
            lo_result.categoryUnitId = lo_catunit.Id;
            lo_result.value = li_result;
            return lo_result;
        }
Exemplo n.º 3
0
        public bool RecieveChallenge(ChallengeData challenge, int userGroup)
        {
            try {
                Challenge lo_chalmod = new Challenge();
                lo_chalmod.categoryUnitId = challenge.categoryUnitId;
                lo_chalmod.endTime = challenge.endTime;
                lo_chalmod.name = challenge.name;
                lo_chalmod.startTime = challenge.startTime;
                lo_chalmod.Id = challenge.id;
                io_challengeService.createChallenge(lo_chalmod);

                string ls_authtoken = challenge.authtoken;
                IQueryable<Community> lo_comquery = from c in db.Communities
                                                    where c.authtoken == ls_authtoken
                                                    select c;
                Community lo_community = lo_comquery.First();

                IQueryable<Community> lo_homecomquery = from c in db.Communities
                                                        where c.home == true
                                                        select c;

                Community lo_homecom = lo_homecomquery.First();

                List<string> lo_errors = new List<string>();
                io_challengeService.addChallengeToCommunities(lo_chalmod, new string[] { lo_community.Id.ToString(), lo_homecom.Id.ToString() }, 0, ref lo_errors, false);
                if (lo_errors.Count > 0)
                {
                    for (int i = 0; i < lo_errors.Count; i++)
                    {
                        Debug.WriteLine(lo_errors[i]);
                    }
                    return false;
                }
                return true;
            } catch (Exception ex)
            {
                return false;
            }
        }
Exemplo n.º 4
0
        public bool addChallengeToCommunities(Challenge challenge, string[] communities, int usersGroup, ref List<string> errors, bool local = true)
        {
            try {
                foreach (string item in communities)
                {
                    CommunityChallenge communityChallenge = new CommunityChallenge()
                    {
                        communityId = Int32.Parse(item),
                        challengeId = challenge.Id
                    };

                    if (Int32.Parse(item) == usersGroup)
                    {
                        communityChallenge.startedChallenge = true;
                    }
                    db.CommunityChallenges.Add(communityChallenge);
                    db.SaveChanges();

                    if (local)
                    {
                        bool lb_goremote = false;

                        for (int i = 0; i < communities.Length; i++)
                        {
                            if (int.Parse(communities[i]) != usersGroup)
                            {
                                lb_goremote = true;
                            }
                        }

                        if (!lb_goremote) return true;

                        GoAberChallengeWSConsumer lo_chalconsumer = GoAberChallengeWSConsumer.GetInstance();

                        if (!lo_chalconsumer.AddChallenge(challenge, usersGroup))
                        {
                            return false;
                        }
                    }

            }
            if (usersGroup > 0)
            {
                if (!communities.Contains(usersGroup.ToString()))
                {
                    CommunityChallenge communityChallenge = new CommunityChallenge()
                    {
                        communityId = usersGroup,
                        challengeId = challenge.Id,
                        startedChallenge = true
                    };
                    db.CommunityChallenges.Add(communityChallenge);
                    db.SaveChanges();
                }
            }
            return true;
            }
            catch (Exception e)
            {
                Debug.Write(e.StackTrace);
                return false;
            }
        }
Exemplo n.º 5
0
        public void setupRemoteChallenge(Challenge challenge, string[] communities, int usersGroup, ref List<string> errors, bool local = true)
        {
            createChallenge(challenge);
            if (addChallengeToCommunities(challenge, communities, usersGroup, ref errors, local)) return;

            //Try to rollback if request doesn't work.
            //The remote challenge may have been set wrong so make sure we delete that as well if it exists.
            try {
                IQueryable<Challenge> query = from c in db.Challenges
                                              where c.Id == challenge.Id
                                              select c;
                List<Challenge> challenges = query.ToList();
                for (int i = 0; i < challenges.Count; i++)
                {
                    db.Challenges.Remove(challenges[i]);
                }
                db.SaveChanges();
            } catch (Exception ex)
            {
                Debug.Write(ex.StackTrace);
            }
        }
Exemplo n.º 6
0
        public IEnumerable<LeaderViewModel> getGroupChallengeLeaders(Challenge challenge)
        {
            ActivityDataService dataService = new ActivityDataService();

            // first get all acitivty data matching the unit
            var activityData = dataService.GetAllActivityData()
                                .Where(a => a.categoryunit.unit.Id == challenge.categoryUnit.unit.Id)
                                .ToList();

            var data = activityData.Where(a => challenge.userchallenges.Any(x => x.User.Id.Equals(a.User.Id)));

            IEnumerable <LeaderViewModel> model = challenge.groupchallenges.Select(c => new LeaderViewModel
            {
                Name = c.group.name,
                Total = data.Where(a => a.User.TeamId == c.group.Id).Sum(a => a.value).GetValueOrDefault(),
                NumMembers = data.Where(a => a.User.TeamId == c.group.Id).GroupBy(a => a.User.Id).Count()
            });

            return model.OrderByDescending(m => m.Total);
        }
Exemplo n.º 7
0
 public void editChallenge(Challenge challenge)
 {
     db.Entry(challenge).State = EntityState.Modified;
     db.SaveChanges();
 }
Exemplo n.º 8
0
        public void createChallenge(Challenge challenge)
        {
            //If the challenge comes to us via the soap web service then there will already be an Id attached.
            if (String.IsNullOrWhiteSpace(challenge.Id))
            {
                challenge.Id = Guid.NewGuid().ToString();
            }

            db.Challenges.Add(challenge);
            db.SaveChanges();

            Job lo_cjob = new Job();
            lo_cjob.tasktype = JobType.Challenge;
            lo_cjob.schedtype = ScheduleType.Once;
            lo_cjob.id = challenge.Id + "_job";

            //TimeSpan lo_elapsedTime = ((DateTime)challenge.endTime).Subtract(DateTime.Now);
            DateTimeOffset ldao_date = DateTime.SpecifyKind((DateTime)challenge.endTime, DateTimeKind.Utc);

            lo_cjob.date = ldao_date;

            //string[] ls_args = new string[];
            if (ScheduleJobs.AddJob(lo_cjob, new string[] { challenge.Id.ToString() }))
            {
                db.Jobs.Add(lo_cjob);
                db.SaveChanges();
            }
        }
Exemplo n.º 9
0
        public void addChallengeToGroups(Challenge challenge, string[] groupChallenges, int usersGroup)
        {
            foreach (string item in groupChallenges)
            {
                GroupChallenge groupChallenge = new GroupChallenge()
                {
                    groupId = Int32.Parse(item),
                    challengeId = challenge.Id
                };
                if (Int32.Parse(item) == usersGroup)
                {
                    groupChallenge.startedChallenge = true;
                }
                db.GroupChallenges.Add(groupChallenge);
                db.SaveChanges();
            }

            if(!groupChallenges.Contains(usersGroup.ToString()))
            {
                GroupChallenge groupChallenge = new GroupChallenge()
                {
                    groupId = usersGroup,
                    challengeId = challenge.Id,
                    startedChallenge = true
                };
                db.GroupChallenges.Add(groupChallenge);
                db.SaveChanges();
            }
        }