private void tempsavetodb() { using (var db = new FF()) { var game = db.FFGameDB.Find(gameID); db.Entry(game).State = System.Data.Entity.EntityState.Unchanged; decimal total = new decimal(); foreach (var h in _homePlayers) total += h.Value.currentPts; game.HScore = total; total = 0; foreach (var a in _awayPlayers) total += a.Value.currentPts; game.VScore = total; foreach (var n in _playersStats) db.NFLPlayerStats.Add(n.Value); db.SaveChanges(); } }
public void EndWeek() { using (var db = new FF()) { var game = db.FFGameDB.Find(gameID); FFLeague League = db.FFLeagueDB.Find(game.FFLeagueID); //RunLive updates score in ffgamedb var allGamesInCurrWeek = db.FFGameDB.Where(x => x.Week == currentWeekServ).ToList(); foreach (FFGame g in allGamesInCurrWeek) { int homeID; int awayID; FFTeam HomeTeam; FFTeam VisTeam; //pull teams if null if (g.HomeTeam != null) { homeID = (int)g.HomeTeamID; HomeTeam = db.FFTeamDB.Find(g.HomeTeamID); if (g.HScore == null) { g.HScore = 0; } } else throw new Exception("FFGame Hometeam null"); if (g.VisTeam != null) { awayID = (int)g.VisTeamID; VisTeam = db.FFTeamDB.Find(g.VisTeamID); if (g.VScore == null) { g.VScore = 0; } } else throw new Exception("FFGame Visteam null"); //these shouldn't work foreach (NFLPlayer p in g.HomeTeam.Players) { g.HScore += p.currentPts; } foreach (NFLPlayer p in g.VisTeam.Players) { g.VScore += p.currentPts; } db.Entry(HomeTeam).State = System.Data.Entity.EntityState.Unchanged; db.Entry(VisTeam).State = System.Data.Entity.EntityState.Unchanged; //win/loss if (g.HScore > g.VScore) { //HomeTeam Won g.HomeTeam.Win += 1; g.VisTeam.Lose += 1; } else if (g.VScore > g.HScore) { //VisTeam Won g.HomeTeam.Lose += 1; g.VisTeam.Win += 1; } else { //Tie g.HomeTeam.Tie += 1; g.VisTeam.Tie += 1; } g.HomeTeam.FPTotal += (decimal)g.HScore; g.VisTeam.FPTotal += (decimal)g.VScore; //delete temp proj, add currentWeek to db and update db.SaveChanges(); } var sett = db.Settings.Find(1); db.Entry(sett).State = System.Data.Entity.EntityState.Unchanged; sett.CurrentWeek++; currentWeekServ = sett.CurrentWeek; db.SaveChanges(); //Check this dont think need it //Clients.All.OnDisconnected(true); } }
public void DeserializePlayerStats(JObject homeStats, string homeTeam, JObject awayStats, string awayTeam) { //This is the names of the two different properties in the JSON JObject homePassing = (JObject)homeStats["passing"]; JObject awayPassing = (JObject)awayStats["passing"]; //Need something of all stats subcategories strings //List<string> StatsChildren = new List<string>(new string[] { "passing", "rushing", "receiving", "fumbles", "kicking" }); Dictionary<string, string> StatsChildren = new Dictionary<string, string> { {"passing", "PassingStats"}, {"rushing", "RushingStats"}, {"receiving", "ReceivingStats"}, {"fumbles", "FumbleStats"}, {"kicking", "KickingStats"} }; //playerIDKeys.AddRange(awayPassing.Properties().Select(p => p.Name).ToList()); Dictionary<string, JObject> JObjectHomeAway = new Dictionary<string, JObject>(); JObjectHomeAway.Add("homeStats", homeStats); JObjectHomeAway.Add("awayStats", awayStats); List<string> playerIDStringKeys = new List<string>(); List<NFLPlayer> PlayerList = new List<NFLPlayer>(); //List of made NFL Players using (var db = new FF()) { //goes by passing, rushing, rec, etc. goes by home/away player foreach (KeyValuePair<string, string> child in StatsChildren) { foreach (KeyValuePair<string, JObject> objName in JObjectHomeAway) { JObject statsJObj = objName.Value; //passed in from dict, is either the homeStats or awayStats jObject string objPropertyName = objName.Key; JObject getIDs = (JObject)statsJObj[child.Key]; playerIDStringKeys.AddRange(getIDs.Properties().Select(p => p.Name).ToList()); foreach (string playerID in playerIDStringKeys) { /*PsC - Create list of players Get PlayerID of player about to be added Compare to List of players If Found*/ NFLPlayer NFLFoundPlayer = null; string s = RemoveSpecialCharacters(playerID); //converts string to int, need int for the key, need string to search JOBject int playerIDInt = Convert.ToInt32(s); //going through the list of already made players and pulling the player if the id's match //if found add stats according to child (pass,rec, rush, etc) //if not found, create player and copy material if (PlayerList.Count() != 0) { NFLFoundPlayer = PlayerList.Find(x => x.id == playerIDInt); } //Player not found if null, so create and fill in player info if (NFLFoundPlayer == null) { NFLFoundPlayer = new NFLPlayer(); NFLFoundPlayer.id = playerIDInt; //PlayerID in int format NFLFoundPlayer.id_nflformat = playerID; //PlayerID in NFL string format //Using homeStats and awayStats as property names., jObj is home or awayStats jObject NFLFoundPlayer.name = statsJObj[child.Key][playerID]["name"].ToString(); if (objPropertyName == "homeStats") { NFLFoundPlayer.team = homeTeam; } else if (objPropertyName == "awayStats") { NFLFoundPlayer.team = awayTeam; } else { NFLFoundPlayer.team = "XXX"; } } else { } //Finds the correct type of stats with the playerId and puts it into a JOnject var statsPullJSON = statsJObj[child.Key][playerID]; //takes pulled stats and adds them to the FoundPlayer //if (child.Key == "passing") { // NFLFoundPlayer.PassingStats = null;//(PassingGameStats)statsPullJSON.ToObject(typeof(PassingGameStats)); //} //else if (child.Key == "rushing") { // NFLFoundPlayer.RushingStats = null;// (RushingGameStats)statsPullJSON.ToObject(typeof(RushingGameStats)); //} //else if (child.Key == "receiving") { // NFLFoundPlayer.ReceivingStats = null;// (ReceivingGameStats)statsPullJSON.ToObject(typeof(ReceivingGameStats)); //} //else if (child.Key == "fumbles") { // NFLFoundPlayer.FumbleStats = null;// (FumbleGameStats)statsPullJSON.ToObject(typeof(FumbleGameStats)); //} //else if (child.Key == "kicking") { // NFLFoundPlayer.KickingStats = null;// (KickingGameStats)statsPullJSON.ToObject(typeof(KickingGameStats)); //} //else { //throw exception //} //add in NFLPlayer to Playerlist and DB, sep function? var dbaddorupdate = PlayerList.Find(x => x.id == NFLFoundPlayer.id); if (dbaddorupdate == null) { PlayerList.Add(NFLFoundPlayer); } //checking to see if found in db if not add //Fix this sometime not sure of better way var dbcheck = db.NFLPlayer.Find(NFLFoundPlayer.id); if (dbcheck == null) db.NFLPlayer.Add(NFLFoundPlayer); else db.Entry(NFLFoundPlayer).State = EntityState.Modified; db.SaveChanges(); //empty keys for next iteration } playerIDStringKeys.Clear(); } } } }