Ejemplo n.º 1
0
        private Court ProcessCourtData(string courtData)
        {
            Court court = null;

            if (!string.IsNullOrEmpty(courtData))
            {
                CourtJson courtJson = JsonSerializer.Current.DeserializeObject <CourtJson>(courtData);

                if (null != courtJson && !string.IsNullOrEmpty(courtJson.name))
                {
                    court = this.DB.Court.Where(c => c.CourtId == courtJson.GuidId).FirstOrDefault();

                    if (null == court)
                    {
                        court           = new Court();
                        court.CourtId   = courtJson.GuidId;
                        court.Name      = courtJson.name;
                        court.Latitude  = courtJson.latitude;
                        court.Longitude = courtJson.longitude;

                        this.DB.Court.InsertOnSubmit(court);
                    }
                }
            }

            return(court);
        }
Ejemplo n.º 2
0
        public ActionResult CreateOffer(DateTime date, long[] locations, string courtData, string comments, long?opponentId)
        {
            var fbContext = FacebookWebContext.Current;

            TennisUserModel tennisUser = ModelUtils.GetTennisUsers(this.DB).Where(u => u.FacebookId == fbContext.UserId).FirstOrDefault();

            if (null != tennisUser)
            {
                List <long> pushIds = new List <long>();
                Court       court   = ProcessCourtData(courtData);

                if (!string.IsNullOrEmpty(courtData))
                {
                    CourtJson courtJson = JsonSerializer.Current.DeserializeObject <CourtJson>(courtData);

                    if (null != courtJson && !string.IsNullOrEmpty(courtJson.name))
                    {
                        court = this.DB.Court.Where(c => c.CourtId == courtJson.GuidId).FirstOrDefault();

                        if (null == court)
                        {
                            court           = new Court();
                            court.CourtId   = courtJson.GuidId;
                            court.Name      = courtJson.name;
                            court.Latitude  = courtJson.latitude;
                            court.Longitude = courtJson.longitude;

                            this.DB.Court.InsertOnSubmit(court);
                        }
                    }
                }

                //Add the entry, send out the messages
                Offer offer = new Offer();
                offer.OfferId             = Guid.NewGuid();
                offer.FacebookId          = tennisUser.FacebookId;
                offer.MatchDateUtc        = IndexModel.GetUtcDate(date, tennisUser.TimeZoneOffset);
                offer.PostDateUtc         = DateTime.UtcNow;
                offer.Message             = comments;
                offer.PreferredLocationId = tennisUser.City.LocationId;
                offer.Court = court;
                // BUGBUG: we dont' seem to set the completed flag anywhere currently
                offer.Completed = false;

                User opponent = null;

                if (opponentId.HasValue && opponentId.Value != 0)
                {
                    opponent = this.DB.User.Where(u => u.FacebookId == opponentId.Value).FirstOrDefault();
                }

                if (null != opponent)
                {
                    offer.SpecificOpponentId = opponent.FacebookId;
                    pushIds.Add(opponent.FacebookId);
                }
                else
                {
                    var tennisUsers = ModelUtils.GetTennisUsers(this.DB);
                    var matchUsers  = tennisUsers.Where
                                          (u =>
                                          this.DB.CoordinateDistanceMiles(u.City.Latitude, u.City.Longitude, tennisUser.City.Latitude, tennisUser.City.Longitude) < 15 &&
                                          Math.Abs(tennisUser.Rating - u.Rating) <= 0.25 &&
                                          u.Gender == tennisUser.Gender &&
                                          u.FacebookId != tennisUser.FacebookId &&
                                          u.EmailOffers
                                          );

                    foreach (var tu in matchUsers)
                    {
                        pushIds.Add(tu.FacebookId);
                    }
                }

                this.DB.Offer.InsertOnSubmit(offer);
                this.DB.SubmitChanges();

                string template = Server.MapPath("/content/matchrequest.htm");
                string subject  = string.Format("TennisLoop: Match Requested from <fb:name uid='{0}' capitalize='true'></fb:name>", tennisUser.FacebookId);
                SendMessage(pushIds, subject, template, offer, tennisUser);

                // Send out messages to all matching users

                return(Json
                       (
                           new
                {
                    UserOffers = RenderPartialViewToString("UserOffers", ModelUtils.GetModel <UserOffersModel>(FacebookWebContext.Current.UserId, this.DB)),
                    UserChallenges = RenderPartialViewToString("UserChallenges", ModelUtils.GetModel <UserOffersModel>(FacebookWebContext.Current.UserId, this.DB))
                }
                       ));
            }

            return(Json(""));
        }