public static int updateJamToDb(GameViewModel game, ManagementContext db, JamViewModel jam, DataModels.Game.Game gDb)
        {
            int c = 0;
            try
            {


                var findJam = gDb.GameJams.Where(x => x.JamId == jam.JamId).FirstOrDefault();

                if (findJam == null)
                    GameJamClass.insertNewJamIntoDb(game.GameId, game.Team1.TeamId, game.Team2.TeamId, jam, db, gDb);
                else
                {
                    findJam.Game = findJam.Game;
                    findJam.CurrentPeriod = jam.CurrentPeriod;
                    findJam.DidJamEndWithInjury = jam.DidJamEndWithInjury;
                    findJam.DidJamGetCalledByJammerT1 = jam.DidJamGetCalledByJammerT1;
                    findJam.DidJamGetCalledByJammerT2 = jam.DidJamGetCalledByJammerT2;
                    findJam.TotalPointsForJamT1 = jam.TotalPointsForJamT1;
                    findJam.TotalPointsForJamT2 = jam.TotalPointsForJamT2;
                    findJam.GameTimeElapsedMillisecondsEnd = jam.GameTimeElapsedMillisecondsEnd;
                    findJam.GameTimeElapsedMillisecondsStart = jam.GameTimeElapsedMillisecondsStart;
                    c += db.SaveChanges();
                    try
                    {
                        for (int i = 0; i < jam.LeadJammers.Count; i++)
                        {
                            try
                            {
                                var j = findJam.LeadJammers.Where(x => x.GameJamLeadId == jam.LeadJammers[i].GameLeadJamId).FirstOrDefault();
                                if (j == null)
                                {
                                    GameLeadJammer l = new GameLeadJammer();
                                    l.GameJam = findJam;
                                    l.GameMemberId = jam.LeadJammers[i].Jammer.SkaterId;
                                    l.GameTimeInMilliseconds = jam.LeadJammers[i].GameTimeInMilliseconds;
                                    l.JamTimeInMilliseconds = jam.LeadJammers[i].JamTimeInMilliseconds;
                                    l.GameJamLeadId = jam.LeadJammers[i].GameLeadJamId;
                                    findJam.LeadJammers.Add(l);
                                }
                            }
                            catch (Exception exception)
                            {
                                ErrorDatabaseManager.AddException(exception, exception.GetType());
                            }
                        }
                        c += db.SaveChanges();
                    }
                    catch (Exception exception)
                    {
                        ErrorDatabaseManager.AddException(exception, exception.GetType());
                    }
                    try
                    {
                        for (int i = 0; i < jam.JamPasses.Count; i++)
                        {
                            try
                            {
                                var pass = findJam.JamPasses.Where(x => x.GamePassId == jam.JamPasses[i].PassId).FirstOrDefault();
                                if (pass == null)
                                {
                                    GameJamPasses p = new GameJamPasses();
                                    p.GameJam = findJam;
                                    p.GamePassId = jam.JamPasses[i].PassId;
                                    p.JamTimeMilliseconds = jam.JamPasses[i].JamTimeMilliseconds;
                                    p.PassNumber = jam.JamPasses[i].PassNumber;
                                    p.PointsScoredForPass = jam.JamPasses[i].PointsScoredForPass;
                                    p.SkaterWhoPassed = GameMemberClass.getMemberOfTeamInGame(jam.JamPasses[i].SkaterWhoPassed.SkaterId, gDb);
                                    p.TeamNumberEnum = (byte)jam.JamPasses[i].Team;
                                    findJam.JamPasses.Add(p);
                                }
                                else
                                {
                                    pass.PointsScoredForPass = jam.JamPasses[i].PointsScoredForPass;
                                }
                            }
                            catch (Exception exception)
                            {
                                ErrorDatabaseManager.AddException(exception, exception.GetType());
                            }
                        }
                        c += db.SaveChanges();
                    }
                    catch (Exception exception)
                    {
                        ErrorDatabaseManager.AddException(exception, exception.GetType());
                    }
                    if ((jam.Blocker1T1 != null && findJam.Blocker1Team1 == null) || (jam.Blocker1T1 != null && findJam.Blocker1Team1.GameMemberId != jam.Blocker1T1.SkaterId))
                    {
                        var m = GameMemberClass.getMemberOfTeamInGame(game.Team1.TeamId, jam.Blocker1T1.SkaterId, gDb);
                        if (m != null)
                            findJam.Blocker1Team1 = m;
                    }
                    if ((jam.Blocker1T2 != null && findJam.Blocker1Team2 == null) || (jam.Blocker1T2 != null && findJam.Blocker1Team2.GameMemberId != jam.Blocker1T2.SkaterId))
                    {
                        var m = GameMemberClass.getMemberOfTeamInGame(game.Team2.TeamId, jam.Blocker1T2.SkaterId, gDb);
                        if (m != null)
                            findJam.Blocker1Team2 = m;
                    }
                    if ((jam.Blocker2T1 != null && findJam.Blocker2Team1 == null) || (jam.Blocker2T1 != null && findJam.Blocker2Team1.GameMemberId != jam.Blocker2T1.SkaterId))
                    {
                        var m = GameMemberClass.getMemberOfTeamInGame(game.Team1.TeamId, jam.Blocker2T1.SkaterId, gDb);
                        if (m != null)
                            findJam.Blocker2Team1 = m;
                    }
                    if ((jam.Blocker2T2 != null && findJam.Blocker2Team2 == null) || (jam.Blocker2T2 != null && findJam.Blocker2Team2.GameMemberId != jam.Blocker2T2.SkaterId))
                    {
                        var m = GameMemberClass.getMemberOfTeamInGame(game.Team2.TeamId, jam.Blocker2T2.SkaterId, gDb);
                        if (m != null)
                            findJam.Blocker2Team2 = m;
                    }
                    if ((jam.Blocker3T1 != null && findJam.Blocker3Team1 == null) || (jam.Blocker3T1 != null && findJam.Blocker3Team1.GameMemberId != jam.Blocker3T1.SkaterId))
                    {
                        var m = GameMemberClass.getMemberOfTeamInGame(game.Team1.TeamId, jam.Blocker3T1.SkaterId, gDb);
                        if (m != null)
                            findJam.Blocker3Team1 = m;
                    }
                    if ((jam.Blocker3T2 != null && findJam.Blocker3Team2 == null) || (jam.Blocker3T2 != null && findJam.Blocker3Team2.GameMemberId != jam.Blocker3T2.SkaterId))
                    {
                        var m = GameMemberClass.getMemberOfTeamInGame(game.Team2.TeamId, jam.Blocker3T2.SkaterId, gDb);
                        if (m != null)
                            findJam.Blocker3Team2 = m;
                    }
                    if ((jam.Blocker4T1 != null && findJam.Blocker4Team1 == null) || (jam.Blocker4T1 != null && findJam.Blocker4Team1.GameMemberId != jam.Blocker4T1.SkaterId))
                    {
                        var m = GameMemberClass.getMemberOfTeamInGame(game.Team1.TeamId, jam.Blocker4T1.SkaterId, gDb);
                        if (m != null)
                            findJam.Blocker4Team1 = m;
                    }
                    if ((jam.Blocker4T2 != null && findJam.Blocker4Team2 == null) || (jam.Blocker4T2 != null && findJam.Blocker4Team2.GameMemberId != jam.Blocker4T2.SkaterId))
                    {
                        var m = GameMemberClass.getMemberOfTeamInGame(game.Team2.TeamId, jam.Blocker4T2.SkaterId, gDb);
                        if (m != null)
                            findJam.Blocker4Team2 = m;
                    }
                    if ((jam.PivotT1 != null && findJam.PivotTeam1 == null) || (jam.PivotT1 != null && findJam.PivotTeam1.GameMemberId != jam.PivotT1.SkaterId))
                    {
                        var m = GameMemberClass.getMemberOfTeamInGame(game.Team1.TeamId, jam.PivotT1.SkaterId, gDb);
                        if (m != null)
                            findJam.PivotTeam1 = m;
                    }
                    if ((jam.PivotT2 != null && findJam.PivotTeam2 == null) || (jam.PivotT2 != null && findJam.PivotTeam2.GameMemberId != jam.PivotT2.SkaterId))
                    {
                        var m = GameMemberClass.getMemberOfTeamInGame(game.Team2.TeamId, jam.PivotT2.SkaterId, gDb);
                        if (m != null)
                            findJam.PivotTeam2 = m;
                    }
                    if ((jam.JammerT1 != null && findJam.JammerTeam1 == null) || (jam.JammerT1 != null && findJam.JammerTeam1.GameMemberId != jam.JammerT1.SkaterId))
                    {
                        var m = GameMemberClass.getMemberOfTeamInGame(game.Team1.TeamId, jam.JammerT1.SkaterId, gDb);
                        if (m != null)
                        {
                                                        findJam.JammerTeam1 = m;
                            findJam.DidTeam1JammerLoseLeadEligibility = jam.JammerT1.LostLeadJammerEligibility;
                        }
                    }
                    if ((jam.JammerT2 != null && findJam.JammerTeam2 == null) || (jam.JammerT2 != null && findJam.JammerTeam2.GameMemberId != jam.JammerT2.SkaterId))
                    {
                        var m = GameMemberClass.getMemberOfTeamInGame(game.Team2.TeamId, jam.JammerT2.SkaterId, gDb);
                        if (m != null)
                        {
                            findJam.JammerTeam2 = m;
                            findJam.DidTeam2JammerLoseLeadEligibility = jam.JammerT2.LostLeadJammerEligibility;
                        }
                    }

                    c += db.SaveChanges();
                }
                var findClock = (from xx in db.GameStopWatch
                                 where xx.StopwatchForId == game.GameId
                                 where xx.JamId == jam.JamId
                                 select xx).FirstOrDefault();
                if (findClock != null && jam.JamClock != null)
                {
                    findClock.IsClockAtZero = jam.JamClock.IsClockAtZero == true ? 1 : 0;
                    findClock.IsRunning = jam.JamClock.IsRunning == true ? 1 : 0;
                    findClock.Length = jam.JamClock.TimerLength;
                    if (jam.JamClock.StartTime != new DateTime())
                        findClock.StartDateTime = jam.JamClock.StartTime;
                    findClock.TimeElapsed = jam.JamClock.TimeElapsed;
                    findClock.TimeRemaining = jam.JamClock.TimeRemaining;
                    findClock.LastModified = DateTime.UtcNow;
                    findClock.Game = gDb;
                    db.SaveChanges();
                }
                else if (jam.JamClock != null && jam.JamClock.StartTime != new DateTime())
                {
                    GameStopwatch stop = new GameStopwatch();
                    stop.Created = DateTime.UtcNow;
                    stop.IsClockAtZero = jam.JamClock.IsClockAtZero == true ? 1 : 0;
                    stop.IsRunning = jam.JamClock.IsRunning == true ? 1 : 0;
                    stop.Length = jam.JamClock.TimerLength;
                    if (jam.JamClock.StartTime != new DateTime())
                        stop.StartDateTime = jam.JamClock.StartTime;
                    stop.StopwatchForId = game.GameId;
                    stop.TimeElapsed = jam.JamClock.TimeElapsed;
                    stop.TimeRemaining = jam.JamClock.TimeRemaining;
                    stop.JamNumber = jam.JamNumber;
                    stop.JamId = jam.JamId;
                    stop.Type = (int)StopWatchTypeEnum.JamClock;
                    stop.Game = gDb;
                    db.GameStopWatch.Add(stop);
                    db.SaveChanges();
                }
                c += db.SaveChanges();
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            return c;
        }
        private static DataModels.Game.Game insertNewGameToDb(GameViewModel game, ManagementContext db)
        {
            DataModels.Game.Game gameNew = new DataModels.Game.Game();
            gameNew.IdForOnlineManagementUse = game.IdForOnlineManagementUse;
            gameNew.GameDate = game.GameDate;
            gameNew.LastModified = DateTime.UtcNow;

            if (game.GameEndDate != DateTime.MinValue)
                gameNew.GameEndDate = game.GameEndDate;
            else
                gameNew.GameEndDate = game.GameDate;

            //debug or live mode.  Debug is for developing it, so users never see this mode and only open to developers.
            gameNew.ScoreboardType = Convert.ToInt32(game.ScoreboardMode);
            if (!String.IsNullOrEmpty(game.GameName))
                gameNew.GameName = game.GameName;
            else
                gameNew.GameName = ScoreboardConfig.DEFAULT_GAME_NAME;
            if (game.Policy != null)
                gameNew.GameType = game.Policy.GameSelectionType.ToString();
            gameNew.IsGameOver = game.HasGameEnded;
            gameNew.HasGameStarted = game.HasGameStarted;
            gameNew.IsGameLive = game.PublishGameOnline;
            gameNew.IsGameScrimmage = game.SaveGameOnline;
            gameNew.GameLocation = game.GameLocation;
            gameNew.GameCity = game.GameCity;
            gameNew.GameState = game.GameState;
            gameNew.GameId = game.GameId;
            gameNew.VersionNumber = game.VersionNumber;


            gameNew.IsGamePublishedOnline = game.PublishGameOnline;
            gameNew.Created = DateTime.UtcNow;
            if (game.FederationId != new Guid())
            {
                GameFederationOwnership fedOwner = new GameFederationOwnership();
                fedOwner.Federation = db.Federations.Where(x => x.FederationId == game.FederationId).FirstOrDefault();
                fedOwner.Game = gameNew;
                fedOwner.OwnerType = Convert.ToInt32(GameOwnerEnum.Owner);
                gameNew.FederationOwners.Add(fedOwner);
            }
            db.Games.Add(gameNew);
            int c = db.SaveChanges();

            InsertGameLinks(game, db, gameNew);

            //inserts the current jam into the DB.
            if (game.CurrentJam != null)
            {
                GameJamClass.insertNewJamIntoDb(game.GameId, game.Team1.TeamId, game.Team2.TeamId, game.CurrentJam, db, gameNew);
            }
            c += db.SaveChanges();

            if (game.CurrentLineUpClock != null && game.CurrentLineUpClock.StartTime != new DateTime())
            {
                GameStopwatch stop = new GameStopwatch();
                stop.Created = DateTime.UtcNow;
                stop.IsClockAtZero = game.CurrentLineUpClock.IsClockAtZero == true ? 1 : 0;
                stop.IsRunning = game.CurrentLineUpClock.IsRunning == true ? 1 : 0;
                stop.Length = game.CurrentLineUpClock.TimerLength;
                stop.StartDateTime = game.CurrentLineUpClock.StartTime;
                stop.StopwatchForId = game.GameId;
                stop.TimeElapsed = game.CurrentLineUpClock.TimeElapsed;
                stop.TimeRemaining = game.CurrentLineUpClock.TimeRemaining;
                stop.Type = (int)StopWatchTypeEnum.LineUpClock;
                stop.Game = gameNew;
                db.GameStopWatch.Add(stop);
                c += db.SaveChanges();
            }
            if (game.CurrentTimeOutClock != null && game.CurrentTimeOutClock.StartTime != new DateTime())
            {
                GameStopwatch stop = new GameStopwatch();
                stop.Created = DateTime.UtcNow;
                stop.IsClockAtZero = game.CurrentTimeOutClock.IsClockAtZero == true ? 1 : 0;
                stop.IsRunning = game.CurrentTimeOutClock.IsRunning == true ? 1 : 0;
                stop.Length = game.CurrentTimeOutClock.TimerLength;
                stop.StartDateTime = game.CurrentTimeOutClock.StartTime;
                stop.StopwatchForId = game.GameId;
                stop.TimeElapsed = game.CurrentTimeOutClock.TimeElapsed;
                stop.TimeRemaining = game.CurrentTimeOutClock.TimeRemaining;
                stop.Type = (int)StopWatchTypeEnum.TimeOutClock;
                stop.Game = gameNew;
                db.GameStopWatch.Add(stop);
                c += db.SaveChanges();
            }
            if (game.IntermissionClock != null && game.IntermissionClock.StartTime != new DateTime())
            {
                GameStopwatch stop = new GameStopwatch();
                stop.Created = DateTime.UtcNow;
                stop.IsClockAtZero = game.IntermissionClock.IsClockAtZero == true ? 1 : 0;
                stop.IsRunning = game.IntermissionClock.IsRunning == true ? 1 : 0;
                stop.Length = game.IntermissionClock.TimerLength;
                stop.StartDateTime = game.IntermissionClock.StartTime;
                stop.StopwatchForId = game.GameId;
                stop.TimeElapsed = game.IntermissionClock.TimeElapsed;
                stop.TimeRemaining = game.IntermissionClock.TimeRemaining;
                stop.Type = (int)StopWatchTypeEnum.IntermissionClock;
                stop.Game = gameNew;
                db.GameStopWatch.Add(stop);
                c += db.SaveChanges();
            }
            try
            {
                if (game.PeriodClock != null && game.PeriodClock.StartTime != new DateTime())
                {
                    GameStopwatch stop = new GameStopwatch();
                    stop.Created = DateTime.UtcNow;
                    stop.IsClockAtZero = game.PeriodClock.IsClockAtZero == true ? 1 : 0;
                    stop.IsRunning = game.PeriodClock.IsRunning == true ? 1 : 0;
                    stop.Length = game.PeriodClock.TimerLength;
                    stop.StartDateTime = game.PeriodClock.StartTime;
                    stop.StopwatchForId = game.GameId;
                    stop.TimeElapsed = game.PeriodClock.TimeElapsed;
                    stop.TimeRemaining = game.PeriodClock.TimeRemaining;
                    stop.Type = (int)StopWatchTypeEnum.PeriodClock;
                    stop.Game = gameNew;
                    db.GameStopWatch.Add(stop);
                    c += db.SaveChanges();
                }
            }
            catch (Exception exception)
            {
                ErrorDatabaseManager.AddException(exception, exception.GetType());
            }
            //teams need to be inserted first before the jams...
            GameTeamClass.insertTeamIntoDb(game, game.Team1, db, gameNew);

            GameTeamClass.insertTeamIntoDb(game, game.Team2, db, gameNew);

            foreach (var jam in game.Jams)
            {
                GameJamClass.insertNewJamIntoDb(game.GameId, game.Team1.TeamId, game.Team2.TeamId, jam, db, gameNew);
            }
            foreach (var advert in game.Advertisements)
            {
                //TODO: save adverts, but we need to upload these from the scoreboard.
            }
            foreach (var timeout in game.TimeOuts)
            {
                insertTimeoutsIntoDb(game, timeout, db, gameNew);
            }
            foreach (var score in game.ScoresTeam1)
            {
                GameScoreClass.insertScoreIntoDb(game.Team1.TeamId, game, score, db, gameNew);
            }
            foreach (var score in game.ScoresTeam2)
            {
                GameScoreClass.insertScoreIntoDb(game.Team2.TeamId, game, score, db, gameNew);
            }
            for (int i = 0; i < game.AssistsForTeam1.Count; i++)
            {
                GameAssistsClass.insertAssistIntoDb(game.Team1.TeamId, game, game.AssistsForTeam1[i], db, gameNew);
            }
            for (int i = 0; i < game.AssistsForTeam2.Count; i++)
            {
                GameAssistsClass.insertAssistIntoDb(game.Team2.TeamId, game, game.AssistsForTeam2[i], db, gameNew);
            }
            for (int i = 0; i < game.BlocksForTeam1.Count; i++)
            {
                GameBlocksClass.insertBlockIntoDb(game.Team1.TeamId, game, game.BlocksForTeam1[i], db, gameNew);
            }
            for (int i = 0; i < game.BlocksForTeam2.Count; i++)
            {
                GameBlocksClass.insertBlockIntoDb(game.Team2.TeamId, game, game.BlocksForTeam2[i], db, gameNew);
            }
            for (int i = 0; i < game.OfficialReviews.Count; i++)
            {
                OfficialReviewsClass.AddOfficialReview(game, gameNew, game.OfficialReviews[i]);
            }
            c += db.SaveChanges();
            //penalties added for the team.
            for (int i = 0; i < game.PenaltiesForTeam1.Count; i++)
            {
                GamePenaltiesClass.insertPenaltyIntoDb(game.Team1.TeamId, game, game.PenaltiesForTeam1[i], db, gameNew);
            }
            for (int i = 0; i < game.PenaltiesForTeam2.Count; i++)
            {
                GamePenaltiesClass.insertPenaltyIntoDb(game.Team2.TeamId, game, game.PenaltiesForTeam2[i], db, gameNew);
            }
            //times the skater went to the box.
            foreach (var pen in game.PenaltyBox)
            {
                GamePenaltiesClass.insertNewPenaltyIntoDb(game, db, pen, gameNew);
            }
            if (game.Policy != null)
            {
                insertNewPoliciesIntoDb(game, db);
            }
            if (game.Officials != null)
            {
                GameOfficialsClass.AddOfficialsToDb(game, db, gameNew);
            }

            c += db.SaveChanges();
            return gameNew;
        }
        public static int insertNewJamIntoDb(Guid gameId, Guid team1Id, Guid team2Id, JamViewModel jam, ManagementContext db, DataModels.Game.Game g)
        {
            int c = 0;
            GameJam jamNew = new GameJam();
            if (jam.Blocker1T1 != null)
            {
                var m = GameMemberClass.getMemberOfTeamInGame(team1Id, jam.Blocker1T1.SkaterId, g);
                if (m != null)
                    jamNew.Blocker1Team1 = m;
            }
            if (jam.Blocker1T2 != null)
            {
                var m = GameMemberClass.getMemberOfTeamInGame(team2Id, jam.Blocker1T2.SkaterId, g);
                if (m != null)
                    jamNew.Blocker1Team2 = m;
            }
            if (jam.Blocker2T1 != null)
            {
                var m = GameMemberClass.getMemberOfTeamInGame(team1Id, jam.Blocker2T1.SkaterId, g);
                if (m != null)
                    jamNew.Blocker2Team1 = m;
            }
            if (jam.Blocker2T2 != null)
            {
                var m = GameMemberClass.getMemberOfTeamInGame(team2Id, jam.Blocker2T2.SkaterId, g);
                if (m != null)
                    jamNew.Blocker2Team2 = m;
            }
            if (jam.Blocker3T1 != null)
            {
                var m = GameMemberClass.getMemberOfTeamInGame(team1Id, jam.Blocker3T1.SkaterId, g);
                if (m != null)
                    jamNew.Blocker3Team1 = m;
            }
            if (jam.Blocker3T2 != null)
            {
                var m = GameMemberClass.getMemberOfTeamInGame(team2Id, jam.Blocker3T2.SkaterId, g);
                if (m != null)
                    jamNew.Blocker3Team2 = m;
            }
            if (jam.Blocker4T1 != null)
            {
                var m = GameMemberClass.getMemberOfTeamInGame(team1Id, jam.Blocker4T1.SkaterId, g);
                if (m != null)
                    jamNew.Blocker4Team1 = m;
            }
            if (jam.Blocker4T2 != null)
            {
                var m = GameMemberClass.getMemberOfTeamInGame(team2Id, jam.Blocker4T2.SkaterId, g);
                if (m != null)
                    jamNew.Blocker4Team2 = m;
            }
            if (jam.PivotT1 != null)
            {
                var m = GameMemberClass.getMemberOfTeamInGame(team1Id, jam.PivotT1.SkaterId, g);
                if (m != null)
                    jamNew.PivotTeam1 = m;
            }
            if (jam.PivotT2 != null)
            {
                var m = GameMemberClass.getMemberOfTeamInGame(team2Id, jam.PivotT2.SkaterId, g);
                if (m != null)
                    jamNew.PivotTeam2 = m;
            }
            if (jam.JammerT1 != null)
            {
                var m = GameMemberClass.getMemberOfTeamInGame(team1Id, jam.JammerT1.SkaterId, g);
                if (m != null)
                {
                    jamNew.JammerTeam1 = m;
                    jamNew.DidTeam1JammerLoseLeadEligibility = jam.JammerT1.LostLeadJammerEligibility;
                }
            }
            if (jam.JammerT2 != null)
            {
                var m = GameMemberClass.getMemberOfTeamInGame(team2Id, jam.JammerT2.SkaterId, g);
                if (m != null)
                {
                    jamNew.JammerTeam2 = m;
                    jamNew.DidTeam2JammerLoseLeadEligibility = jam.JammerT2.LostLeadJammerEligibility;
                }
            }

            jamNew.JamId = jam.JamId;
            jamNew.CurrentPeriod = jam.CurrentPeriod;
            jamNew.Created = DateTime.UtcNow;
            jamNew.GameTimeElapsedMillisecondsStart = jam.GameTimeElapsedMillisecondsStart;
            jamNew.GameTimeElapsedMillisecondsEnd = jam.GameTimeElapsedMillisecondsEnd;
            jamNew.JamNumber = jam.JamNumber;
            jamNew.TeamLeadingJam = (byte)jam.TeamLeadingJam;
            jamNew.TotalPointsForJamT1 = jam.TotalPointsForJamT1;
            jamNew.TotalPointsForJamT2 = jam.TotalPointsForJamT2;
            jamNew.DidJamEndWithInjury = jam.DidJamEndWithInjury;
            jamNew.DidJamGetCalledByJammerT1 = jam.DidJamGetCalledByJammerT1;
            jamNew.DidJamGetCalledByJammerT2 = jam.DidJamGetCalledByJammerT2;
            g.GameJams.Add(jamNew);
            c += db.SaveChanges();



            foreach (var leadJammer in jam.LeadJammers)
            {
                try
                {
                    GameLeadJammer lead = new GameLeadJammer();
                    lead.Created = DateTime.UtcNow;
                    lead.GameMemberId = leadJammer.Jammer.SkaterId;
                    lead.GameTimeInMilliseconds = leadJammer.GameTimeInMilliseconds;
                    lead.GameJam = jamNew;
                    lead.JamTimeInMilliseconds = lead.JamTimeInMilliseconds;
                    jamNew.LeadJammers.Add(lead);
                }
                catch (Exception exception)
                {
                    ErrorDatabaseManager.AddException(exception, exception.GetType());
                }
            }
            db.SaveChanges();
            if (jam.JamClock != null && jam.JamClock.StartTime != new DateTime())
            {
                try
                {
                    GameStopwatch stop = new GameStopwatch();
                    stop.Game = g;
                    stop.Created = DateTime.UtcNow;
                    stop.IsClockAtZero = jam.JamClock.IsClockAtZero == true ? 1 : 0; ;
                    stop.IsRunning = jam.JamClock.IsRunning == true ? 1 : 0;
                    stop.Length = jam.JamClock.TimerLength;
                    stop.JamNumber = jam.JamNumber;
                    stop.JamId = jam.JamId;
                    stop.StartDateTime = jam.JamClock.StartTime;
                    stop.StopwatchForId = gameId;
                    stop.TimeElapsed = jam.JamClock.TimeElapsed;
                    stop.TimeRemaining = jam.JamClock.TimeRemaining;
                    stop.Type = (int)StopWatchTypeEnum.JamClock;
                    db.GameStopWatch.Add(stop);
                }
                catch (Exception exception)
                {
                    ErrorDatabaseManager.AddException(exception, exception.GetType());
                }
            }
            g.GameJams.Add(jamNew);
            c += db.SaveChanges();


            AddJamPasses(jam, db, g, jamNew);
            return c;
        }
        /// <summary>
        /// updates the stop watch clock for a chosen clock.
        /// </summary>
        /// <param name="gameId"></param>
        /// <param name="db"></param>
        /// <param name="watch"></param>
        /// <param name="type"></param>
        private static bool updateClock(Guid gameId, ManagementContext db, StopwatchWrapper watch, StopWatchTypeEnum type, DataModels.Game.Game g)
        {
            try
            {
                if (watch.StartTime != new DateTime())
                {
                    var findPeriod = (from xx in db.GameStopWatch
                                      where xx.StopwatchForId == gameId
                                      where xx.Type == (int)type
                                      select xx).FirstOrDefault();
                    if (findPeriod == null)
                    {
                        GameStopwatch stop = new GameStopwatch();
                        stop.Created = DateTime.UtcNow;
                        stop.IsClockAtZero = watch.IsClockAtZero == true ? 1 : 0;
                        stop.IsRunning = watch.IsRunning == true ? 1 : 0;
                        stop.Length = watch.TimerLength;
                        stop.StartDateTime = watch.StartTime;
                        stop.StopwatchForId = gameId;
                        stop.TimeElapsed = watch.TimeElapsed;
                        stop.TimeRemaining = watch.TimeRemaining;
                        stop.Type = (int)type;
                        stop.Game = g;
                        db.GameStopWatch.Add(stop);
                        int c = db.SaveChanges();
                        return c > 0;
                    }
                    else
                    {
                        findPeriod.Game = findPeriod.Game;
                        findPeriod.IsClockAtZero = watch.IsClockAtZero == true ? 1 : 0;
                        findPeriod.IsRunning = watch.IsRunning == true ? 1 : 0;
                        findPeriod.Length = watch.TimerLength;
                        findPeriod.StartDateTime = watch.StartTime;
                        findPeriod.TimeElapsed = watch.TimeElapsed;
                        findPeriod.TimeRemaining = watch.TimeRemaining;
                        int c = db.SaveChanges();
                        return c > 0;
                    }
                }
            }
            catch (Exception e)
            {
                ErrorDatabaseManager.AddException(e, e.GetType(), ErrorGroupEnum.Database);
            }
            return false;

        }