public ActionResult Change(MatchesTeam mt, FormCollection form) { MatchesTeam teamLocal = new MatchesTeam() { matchId = int.Parse(form["matchId"].ToString()), teamId = int.Parse(form["dropHome"].ToString()), teamIsLocal = true }; MatchesTeam teamAway = new MatchesTeam() { matchId = int.Parse(form["matchId"].ToString()), teamId = int.Parse(form["dropAway"].ToString()), teamIsLocal = false }; Context ctx = new Context(); int matchId = int.Parse(form["matchId"].ToString()); var exists = (from ma in ctx.MatchesTeam where ma.matchId == matchId select ma).ToList(); if (exists.Count == 0) { ctx.MatchesTeam.Add(teamLocal); ctx.MatchesTeam.Add(teamAway); } else { if (exists[0].teamIsLocal == true) { exists[0].teamId = int.Parse(form["dropHome"].ToString()); exists[1].teamId = int.Parse(form["dropAway"].ToString()); } else { exists[1].teamId = int.Parse(form["dropHome"].ToString()); exists[0].teamId = int.Parse(form["dropAway"].ToString()); } } ctx.SaveChanges(); return RedirectToAction("List", "Match"); }
public ActionResult Edit(Matches matches, FormCollection form) { Context ctx = new Context(); try { int matchId = int.Parse(form["matchId"].ToString()); var match = (from m in ctx.Matches where m.matchId == matchId select m).SingleOrDefault(); match.homeResult = matches.homeResult; match.awayResult = matches.awayResult; match.Played = true; string[] result = null; string[] resultPenalty = null; if (!match.IsFormula) { var teamLocal = (from mt in ctx.MatchesTeam from t in ctx.Teams where mt.matchId == matchId && t.teamId == mt.teamId && mt.teamIsLocal == true select t).SingleOrDefault(); var teamAway = (from mt in ctx.MatchesTeam from t in ctx.Teams where mt.matchId == matchId && t.teamId == mt.teamId && mt.teamIsLocal == false select t).SingleOrDefault(); teamLocal.matchesPlayed += 1; teamAway.matchesPlayed += 1; //result = matches.matchResult.Split(new char[] { '-' }); if (match.homeResult > match.awayResult) { teamLocal.matchesWin += 1; teamAway.matchesLoses += 1; teamLocal.goalsFavor += match.homeResult; teamAway.goalsFavor += match.awayResult; teamLocal.goalsAgainst += match.awayResult; teamAway.goalsAgainst += match.homeResult; teamLocal.points += 3; teamAway.points += 0; teamLocal.goalsDifference = teamLocal.goalsFavor - teamLocal.goalsAgainst; teamAway.goalsDifference = teamAway.goalsFavor - teamAway.goalsAgainst; } else if (match.awayResult > match.homeResult) { teamAway.matchesWin += 1; teamLocal.matchesLoses += 1; teamLocal.goalsFavor += match.homeResult; teamAway.goalsFavor += match.awayResult; teamLocal.goalsAgainst += match.awayResult; teamAway.goalsAgainst += match.homeResult; teamAway.points += 3; teamLocal.points += 0; teamLocal.goalsDifference = teamLocal.goalsFavor - teamLocal.goalsAgainst; teamAway.goalsDifference = teamAway.goalsFavor - teamAway.goalsAgainst; } else if (match.awayResult == match.homeResult) { teamAway.matchesDraw += 1; teamLocal.matchesDraw += 1; teamLocal.goalsFavor += match.homeResult; teamAway.goalsFavor += match.awayResult; teamLocal.goalsAgainst += match.awayResult; teamAway.goalsAgainst += match.homeResult; teamAway.points += 1; teamLocal.points += 1; teamLocal.goalsDifference = teamLocal.goalsFavor - teamLocal.goalsAgainst; teamAway.goalsDifference = teamAway.goalsFavor - teamAway.goalsAgainst; } } else { match.homePenaltyResult = matches.homePenaltyResult; match.awayPenaltyResult = matches.awayPenaltyResult; } var matchProcessed = (from mp in ctx.MatchProcessed where mp.matchId == matchId select mp).FirstOrDefault(); if (matchProcessed == null) { ctx.MatchProcessed.Add(new MatchProcessed() { matchId = matchId, matchProcessed = false }); } else { if (!matchProcessed.matchProcessed) { matchProcessed.matchProcessed = false; } } } catch (Exception ex) { var changedEntities = ctx.ChangeTracker.Entries().Where(x => x.State != System.Data.EntityState.Unchanged); foreach (var entry in changedEntities.Where(x => x.State == System.Data.EntityState.Modified)) { entry.CurrentValues.SetValues(entry.OriginalValues); entry.State = System.Data.EntityState.Unchanged; } foreach (var entry in changedEntities.Where(x => x.State == System.Data.EntityState.Added)) { entry.State = System.Data.EntityState.Detached; } foreach (var entry in changedEntities.Where(x => x.State == System.Data.EntityState.Deleted)) { entry.State = System.Data.EntityState.Unchanged; } } ctx.SaveChanges(); return RedirectToAction("List", "Match"); }
public ActionResult Modify(Predictions pred) { Context ctx = new Context(); var userId = (from u in ctx.UserProfiles where u.UserName == User.Identity.Name select u.UserId).SingleOrDefault(); var prediction = (from p in ctx.Predictions where p.predictionId == pred.predictionId select p).SingleOrDefault(); var match = (from m in ctx.Matches where m.matchId == prediction.matchId select m).SingleOrDefault(); if (DateTime.Compare(Util.LocalDate.Now, match.matchDate)>= 0) { ModelState.AddModelError("", "No puede modificar las predicciones el mismo día del juego."); return RedirectToAction("Modify", "Prediction", new {@id = pred.predictionId}); } if (!userId.Equals(prediction.UserId)) { return RedirectToAction("List", "Prediction"); } //look for a register //prediction.matchPenaltyResult = pred.matchPenaltyResult; prediction.homeResult = pred.homeResult; prediction.homePenaltyResult = pred.homePenaltyResult == null ? 0 : pred.homePenaltyResult; prediction.awayResult = pred.awayResult; prediction.awayPenaltyResult = pred.awayPenaltyResult == null ? 0 : pred.awayPenaltyResult; prediction.matchPredictionModify = Util.LocalDate.Now; //prediction.matchResult = pred.matchResult; ctx.SaveChanges(); return RedirectToAction("List", "Prediction"); }
public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // Attempt to register the user try { Context ctx = new Context(); WebSecurity.CreateUserAndAccount(model.UserName, model.Password); var userProfile = (from u in ctx.UserProfiles where u.UserName == model.UserName select u).SingleOrDefault(); userProfile.isActive = model.UserName.Equals("admin") ? true : false; userProfile.pointsEarned = 0; userProfile.userCompleteName = model.userCompleteName; userProfile.emailAddress = model.emailAddress; if (model.UserName != "admin") { var matches = (from m in ctx.Matches select m).OrderBy(o => o.matchNumber).ToList(); foreach (Matches m in matches) { var prediction = new Predictions(); prediction.matchId = m.matchId; prediction.awayPenaltyResult = 0; prediction.homePenaltyResult = 0; prediction.homeResult = 0; prediction.awayResult = 0; prediction.matchPredictionCreateDate = Util.LocalDate.Now; prediction.UserId = userProfile.UserId; ctx.Predictions.Add(prediction); } } ctx.SaveChanges(); //WebSecurity.Login(model.UserName, model.Password); return RedirectToAction("Login", "Account"); } catch (MembershipCreateUserException e) { ModelState.AddModelError("", ErrorCodeToString(e.StatusCode)); } } // If we got this far, something failed, redisplay form return View(model); }
public ActionResult ResetAll() { Context ctx = new Context(); var matchesProcessed = (from mp in ctx.MatchProcessed select mp).ToList(); foreach (var mp in matchesProcessed) { ctx.MatchProcessed.Remove(mp); } var users = (from u in ctx.UserProfiles where u.UserName != "admin" select u).ToList(); foreach (var user in users) { user.pointsEarned = 0; } //Remove teams stadistics var teams = (from t in ctx.Teams select t).ToList(); foreach (var team in teams) { team.goalsAgainst = 0; team.goalsDifference = 0; team.goalsFavor = 0; team.matchesDraw = 0; team.matchesLoses = 0; team.matchesPlayed = 0; team.matchesWin = 0; team.points = 0; } ctx.SaveChanges(); return RedirectToAction("Index", "Home"); }
public ActionResult ExternalLoginConfirmation(RegisterExternalLoginModel model, string returnUrl) { string provider = null; string providerUserId = null; if (User.Identity.IsAuthenticated || !OAuthWebSecurity.TryDeserializeProviderUserId(model.ExternalLoginData, out provider, out providerUserId)) { return RedirectToAction("Manage"); } if (ModelState.IsValid) { // Insert a new user into the database using (Context db = new Context()) { UserProfile user = db.UserProfiles.FirstOrDefault(u => u.UserName.ToLower() == model.UserName.ToLower()); //Check if user already exists if (user == null) { // Insert name into the profile table db.UserProfiles.Add(new UserProfile { UserName = model.UserName }); db.SaveChanges(); OAuthWebSecurity.CreateOrUpdateAccount(provider, providerUserId, model.UserName); OAuthWebSecurity.Login(provider, providerUserId, createPersistentCookie: false); return RedirectToLocal(returnUrl); } else { ModelState.AddModelError("UserName", "User name already exists. Please enter a different user name."); } } } ViewBag.ProviderDisplayName = OAuthWebSecurity.GetOAuthClientData(provider).DisplayName; ViewBag.ReturnUrl = returnUrl; return View(model); }
public ActionResult ApplyFormulaMatch(int id) { Context ctx = new Context(); var match = (from m in ctx.Matches where m.matchId == id select m).FirstOrDefault(); string formula = string.Empty; if (match.IsFormula) { if (match.formulaByGroups) { formula = match.formula; string[] groups = formula.Split(new char[] { '-' }); var firstLeg = (from g in ctx.Groups from t in ctx.Teams where g.groupName.Contains(" " + groups[0].Substring(0, 1)) && t.groupId == g.groupId select t).OrderByDescending(o => o.points).ThenByDescending(o => o.goalsDifference).ThenByDescending(o => o.goalsFavor).ThenByDescending(o => o.goalsAgainst).ToList(); var secondLeg = (from g in ctx.Groups from t in ctx.Teams where g.groupName.Contains(" " + groups[1].Substring(0, 1)) && t.groupId == g.groupId select t).OrderByDescending(o => o.points).ThenByDescending(o => o.goalsDifference).ThenByDescending(o => o.goalsFavor).ThenByDescending(o => o.goalsAgainst).ToList(); Teams teamId1 = null; Teams teamId2 = null; if (groups[0].Substring(1, 1).ToString().Equals("1")) teamId1 = firstLeg.FirstOrDefault(); else teamId1 = firstLeg.Skip(1).Take(1).SingleOrDefault(); if(groups[1].Substring(1, 1).ToString().Equals("1")) teamId2 = secondLeg.FirstOrDefault(); else teamId2 = secondLeg.Skip(1).Take(1).SingleOrDefault(); MatchesTeam mt = new MatchesTeam(); mt.matchId = match.matchId; mt.teamId = teamId1.teamId; mt.teamIsLocal = true; ctx.MatchesTeam.Add(mt); mt = new MatchesTeam(); mt.matchId = match.matchId; mt.teamId = teamId2.teamId; mt.teamIsLocal = false; ctx.MatchesTeam.Add(mt); } else { formula = match.formula; string[] games = formula.Split(new char[] { '-' }); int matchFormula = 0; if (games[0].Substring(0, 1).ToString().Equals("W")) { matchFormula = int.Parse(games[0].Substring(1, 2).ToString()); var matchResult = (from m in ctx.Matches where m.matchId == matchFormula select m).FirstOrDefault(); } else { } } ctx.SaveChanges(); return View(); } else { return RedirectToAction("Index", "Home"); } }
public ActionResult Champions(FormCollection form) { Context ctx = new Context(); var users = (from u in ctx.UserProfiles where u.UserName == User.Identity.Name select u).SingleOrDefault(); int team1 = int.Parse(form[form.Keys.Get(1)].ToString()); int team2 = int.Parse(form[form.Keys.Get(2)].ToString()); int team3 = int.Parse(form[form.Keys.Get(3)].ToString()); int team4 = int.Parse(form[form.Keys.Get(4)].ToString()); List<Int32> teams = new List<int>(); teams.Add(team1); if (teams.Contains(team2)) { ModelState.AddModelError("", "Revise los finalistas no puede haber repetidos."); return RedirectToAction("Champions", "Account"); } if (teams.Contains(team3)) { ModelState.AddModelError("", "Revise los finalistas no puede haber repetidos."); return RedirectToAction("Champions", "Account"); } if (teams.Contains(team4)) { ModelState.AddModelError("", "Revise los finalistas no puede haber repetidos."); return RedirectToAction("Champions", "Account"); } ctx.UserFinalist.Add(new UserFinalist() { position = 1, teamId = team1, UserId = users.UserId }); ctx.UserFinalist.Add(new UserFinalist() { position = 2, teamId = team2, UserId = users.UserId }); ctx.UserFinalist.Add(new UserFinalist() { position = 3, teamId = team3, UserId = users.UserId }); ctx.UserFinalist.Add(new UserFinalist() { position = 4, teamId = team4, UserId = users.UserId }); ctx.SaveChanges(); return RedirectToAction("Index", "Home"); }
public ActionResult Calculate() { Context ctx = new Context(); int matchResult = 0; //int matchPenalty = 0; int matchPredictionResult = 0; //int matchPredictionPenalty = 0; bool isDraw = false; //get users var users = (from u in ctx.UserProfiles where u.UserName != "admin" && u.isActive == true select u).ToList(); ////get list of matches processed //var matchesProcessed = (from mp in ctx.MatchProcessed // where mp.matchProcessed == false // select mp.matchId).ToList(); //running users foreach (UserProfile up in users) { //get predictions form users var predictions = (from p in ctx.Predictions from m in ctx.Matches from mp in ctx.MatchProcessed where p.UserId == up.UserId && m.matchId == p.matchId && p.matchPredictionModify != null && mp.matchId == p.matchId && mp.matchProcessed == false select p).OrderBy(o => o.matchId).ToList(); foreach (Predictions p in predictions) { var match = (from m in ctx.Matches where m.matchId == p.matchId select m).FirstOrDefault(); matchResult = match.homeResult - match.awayResult; matchPredictionResult = p.homeResult - p.awayResult; //Draw if (matchResult == 0 && matchPredictionResult == 0) { up.pointsEarned += 5; if (match.homeResult == p.homeResult && match.awayResult == p.awayResult) { up.pointsEarned += 5; } isDraw = true; } //local win if (matchResult > 0 && matchPredictionResult > 0) { up.pointsEarned += 5; if (match.homeResult == p.homeResult && match.awayResult == p.awayResult) { up.pointsEarned += 5; } isDraw = false; } //away win if (matchResult < 0 && matchPredictionResult < 0) { up.pointsEarned += 5; if (match.homeResult == p.homeResult && match.awayResult == p.awayResult) { up.pointsEarned += 5; } isDraw = false; } if (match.IsFormula && isDraw) { //validate penalties if (match.homePenaltyResult == p.homePenaltyResult && match.awayPenaltyResult == p.awayPenaltyResult) { up.pointsEarned += 5; } } if (match.matchId >= 63) { //validate the finalist var finalistThird = (from f in ctx.UserFinalist where f.UserId == up.UserId && f.position == (match.matchId == 63 ? 3 : 1) select f).FirstOrDefault(); var finalistFourth = (from f in ctx.UserFinalist where f.UserId == up.UserId && f.position == (match.matchId == 63 ? 4 : 2) select f).FirstOrDefault(); if (!isDraw) { matchResult = match.homeResult - match.awayResult; if (matchResult < 0) { var teamWinner = (from mt in ctx.MatchesTeam where mt.matchId == match.matchId && !mt.teamIsLocal select mt).FirstOrDefault(); var teamLoser = (from mt in ctx.MatchesTeam where mt.matchId == match.matchId && mt.teamIsLocal select mt).FirstOrDefault(); if (finalistThird.teamId == teamWinner.teamId) { if (match.matchId == 63) up.pointsEarned += 50; else up.pointsEarned += 100; } else if (finalistFourth.teamId == teamLoser.teamId) { if (match.matchId == 63) up.pointsEarned += 25; else up.pointsEarned += 75; } } else { var teamWinner = (from mt in ctx.MatchesTeam where mt.matchId == match.matchId && mt.teamIsLocal select mt).FirstOrDefault(); var teamLoser = (from mt in ctx.MatchesTeam where mt.matchId == match.matchId && !mt.teamIsLocal select mt).FirstOrDefault(); if (finalistThird.teamId == teamWinner.teamId) { if (match.matchId == 63) up.pointsEarned += 50; else up.pointsEarned += 100; } else if (finalistFourth.teamId == teamLoser.teamId) { if (match.matchId == 63) up.pointsEarned += 25; else up.pointsEarned += 75; } } } else { matchResult = match.homePenaltyResult - match.awayPenaltyResult; if (matchResult < 0) { var teamWinner = (from mt in ctx.MatchesTeam where mt.matchId == match.matchId && !mt.teamIsLocal select mt).FirstOrDefault(); var teamLoser = (from mt in ctx.MatchesTeam where mt.matchId == match.matchId && mt.teamIsLocal select mt).FirstOrDefault(); if (finalistThird.teamId == teamWinner.teamId) { if (match.matchId == 63) up.pointsEarned += 50; else up.pointsEarned += 100; } else if (finalistFourth.teamId == teamLoser.teamId) { if (match.matchId == 63) up.pointsEarned += 25; else up.pointsEarned += 75; } } else { var teamWinner = (from mt in ctx.MatchesTeam where mt.matchId == match.matchId && mt.teamIsLocal select mt).FirstOrDefault(); var teamLoser = (from mt in ctx.MatchesTeam where mt.matchId == match.matchId && !mt.teamIsLocal select mt).FirstOrDefault(); if (finalistThird.teamId == teamWinner.teamId) { if (match.matchId == 63) up.pointsEarned += 50; else up.pointsEarned += 100; } else if (finalistFourth.teamId == teamLoser.teamId) { if (match.matchId == 63) up.pointsEarned += 25; else up.pointsEarned += 75; } } } } var matchProcess = (from mp in ctx.MatchProcessed where mp.matchId == match.matchId && mp.matchProcessed == false select mp).FirstOrDefault(); matchProcess.matchProcessed = true; } } ctx.SaveChanges(); return RedirectToAction("List", "Account"); }
public ActionResult AuthorizeUser(int id) { Context ctx = new Context(); var users = (from u in ctx.UserProfiles where u.UserId == id select u).SingleOrDefault(); users.isActive = true; ctx.SaveChanges(); return RedirectToAction("Authorize", "Account"); }