public void AddCourtToScoring(OpusContext db1, OpusContext db3, string date, CourtAssignment item) { //Load a record into Scores table for each player (4 per item found) //The data being used comes from the CourtAssignment data which keeps player namses as First, Last (STC Rank) //The code below just grabs First and Last for use in the Scoring table. int id = Convert.ToInt32(item.Player1ID); OpusPlayer player = db3.OpusPlayers.Single(q => q.PlayerID == id); Scoring sRow = new Scoring { Date = date, First = player.First, Last = player.Last, Group = player.Group, STCPlayerID = player.PlayerID, Name = item.Player1 }; db1.Scores.Add(sRow); id = Convert.ToInt32(item.Player2ID); player = db3.OpusPlayers.Single(q => q.PlayerID == id); sRow = new Scoring { Date = date, First = player.First, Last = player.Last, Group = player.Group, STCPlayerID = player.PlayerID, Name = item.Player2 }; db1.Scores.Add(sRow); id = Convert.ToInt32(item.Player3ID); player = db3.OpusPlayers.Single(q => q.PlayerID == id); sRow = new Scoring { Date = date, First = player.First, Last = player.Last, Group = player.Group, STCPlayerID = player.PlayerID, Name = item.Player3 }; db1.Scores.Add(sRow); id = Convert.ToInt32(item.Player4ID); player = db3.OpusPlayers.Single(q => q.PlayerID == id); sRow = new Scoring { Date = date, First = player.First, Last = player.Last, Group = player.Group, STCPlayerID = player.PlayerID, Name = item.Player4 }; db1.Scores.Add(sRow); }
public ActionResult Edit([Bind(Include = "ID,Group,PlayCode,Date,Time,Court,Player1ID,Player2ID,Player3ID,Player4ID")] CourtAssignment courtAssignment) { List<PlayerList> players = util.GetPlayers(Session["Group"].ToString(), Session["PlayCode"].ToString()); courtAssignment.Player1 = players.Where(x => x.ID == courtAssignment.Player1ID).First().Name; courtAssignment.Player2 = players.Where(x => x.ID == courtAssignment.Player2ID).First().Name; courtAssignment.Player3 = players.Where(x => x.ID == courtAssignment.Player3ID).First().Name; courtAssignment.Player4 = players.Where(x => x.ID == courtAssignment.Player4ID).First().Name; //Not sure why this must be done but for some reason a Group of "" is returning as null so need to rest to "" if (courtAssignment.Group == null) courtAssignment.Group = ""; if (ModelState.IsValid) { //Check if Scoring has been started for this date and if so update players int[] toBeDeletedID = new int[4] { 0, 0, 0, 0 }; string[] toBeAddedFirst = new string[4]; string[] toBeAddedLast = new string[4]; int[] toBeAddedID = new int[4] { 0, 0, 0, 0 }; string[] toBeAddedName = new string[4]; string[] currentNames = new string[4]; if (db2.Scores.Where(u => u.Date == courtAssignment.Date).Any()) { var current = (from c in db2.Assignments where (c.Date.Equals(courtAssignment.Date) && c.Court == courtAssignment.Court) select c); int iDelete = 0; int iAdd = 0; bool bSaveChanges = false; foreach(var item in current) { //Check for deletions if(ShouldDelete(item.Player1, courtAssignment)) { toBeDeletedID[iDelete++] = item.Player1ID; } currentNames[0] = item.Player1; if (ShouldDelete(item.Player2, courtAssignment)) { toBeDeletedID[iDelete++] = item.Player2ID; } currentNames[1] = item.Player2; if (ShouldDelete(item.Player3, courtAssignment)) { toBeDeletedID[iDelete++] = item.Player3ID; } currentNames[2] = item.Player3; if (ShouldDelete(item.Player4, courtAssignment)) { toBeDeletedID[iDelete++] = item.Player4ID; } currentNames[3] = item.Player4; //Check for additions // Will need to get player info first int id = 0; OpusPlayer player = null; if (ShouldAdd(currentNames, courtAssignment.Player1)) { id = Convert.ToInt32(courtAssignment.Player1ID); player = db1.OpusPlayers.Single(q => q.PlayerID == id); toBeAddedFirst[iAdd] = player.First; toBeAddedLast[iAdd] = player.Last; toBeAddedID[iAdd] = player.PlayerID; toBeAddedName[iAdd++] = courtAssignment.Player1; } if (ShouldAdd(currentNames, courtAssignment.Player2)) { id = Convert.ToInt32(courtAssignment.Player2ID); player = db1.OpusPlayers.Single(q => q.PlayerID == id); toBeAddedFirst[iAdd] = player.First; toBeAddedLast[iAdd] = player.Last; toBeAddedID[iAdd] = player.PlayerID; toBeAddedName[iAdd++] = courtAssignment.Player2; } if (ShouldAdd(currentNames, courtAssignment.Player3)) { id = Convert.ToInt32(courtAssignment.Player3ID); player = db1.OpusPlayers.Single(q => q.PlayerID == id); toBeAddedFirst[iAdd] = player.First; toBeAddedLast[iAdd] = player.Last; toBeAddedID[iAdd] = player.PlayerID; toBeAddedName[iAdd++] = courtAssignment.Player3; } if (ShouldAdd(currentNames, courtAssignment.Player4)) { id = Convert.ToInt32(courtAssignment.Player4ID); player = db1.OpusPlayers.Single(q => q.PlayerID == id); toBeAddedFirst[iAdd] = player.First; toBeAddedLast[iAdd] = player.Last; toBeAddedID[iAdd] = player.PlayerID; toBeAddedName[iAdd++] = courtAssignment.Player4; } } for(int i = 0; i <= 3; i++) { //Remove Player i if (toBeDeletedID[i] != 0) { var deleteID = toBeDeletedID[i]; var player = db2.Scores.First(u => u.STCPlayerID == deleteID && u.Date == courtAssignment.Date); if (player != null) { db2.Scores.Remove(player); bSaveChanges = true; } } //Add Player if(toBeAddedID[i] != 0) { //Load a record into Scores table Scoring sRow = new Scoring { Date = courtAssignment.Date, First = toBeAddedFirst[i], Last = toBeAddedLast[i], STCPlayerID = toBeAddedID[i], Group = Session["Group"].ToString(), Name = toBeAddedName[i] }; db2.Scores.Add(sRow); bSaveChanges = true; } } if(bSaveChanges) db2.SaveChanges(); } db.Entry(courtAssignment).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(courtAssignment); }
private void RankIndividual(Scoring scores) { OpusPlayer player = db3.OpusPlayers.Single(q => q.PlayerID == scores.STCPlayerID); double percent = 0; if (scores.Played > 0) { percent = (double)scores.Won / (double)scores.Played; } Scoring per = db2.Scores.Single(q => q.ID == scores.ID); per.PercentWon = Convert.ToInt32(percent * 100.0); per.Rank = per.PercentWon * player.Factor; per.OverallPercentWon = OverallPercentWon(scores.ID); player.OverallPercentWon = per.OverallPercentWon; player.Rank = per.Rank; db2.SaveChanges(); db3.SaveChanges(); RankOverall(); }