public void CommitMatch() // MatchSubsystem.java / commitMatch
        {
            MatchPlayedThisEvent m = this;

            m.AddCommitHistory(new Commit(m.NewCommitInstant, TCommitType.COMMIT));
            if (m.StartTime <= DateTimeAsInteger.NegativeOne)
            {
                m.StartTime = DateTimeOffset.UtcNow;
            }

            if (m.MatchType == TMatchType.TEST)
            {
                m.MatchState = TMatchState.Committed;
            }
            else if (m.MatchType == TMatchType.QUALS)
            {
                m.CommitMatchCore();
                m.MatchState = TMatchState.Committed;

                Database.ThisEvent.CalculateAndSetRankings();
                Database.LeagueSubsystem.CalculateAndSetAllLeagueRankings(); // more a formality/mirroring ScoreKeeper, as this makes no persistent updates
                Database.LeagueSubsystem.CreateExportTemp();
            }
            else // if (m.MatchType == TMatchType.ELIMS)
            {
                throw new NotImplementedException($"{GetType().Name}.{nameof(CommitMatch)}: {m.MatchType}");
            }
        }
        //----------------------------------------------------------------------------------------
        // Saving
        //----------------------------------------------------------------------------------------

        public void SaveNonCommitMatchHistory(TCommitType commitType) // see SQLiteMatchDAO/saveNonCommitMatchHistory
        {
            MatchPlayedThisEvent m = this;

            m.AddCommitHistory(new Commit(m.NewCommitInstant, commitType));
            DateTimeOffset commitTime = m.CommitHistoryLatest.Ts;

            // BLOCK
            {
                var psHistory = Database.Tables.QualsCommitHistory.NewRow();
                psHistory.MatchNumber.Value   = m.MatchNumber;
                psHistory.Ts.Value            = commitTime;
                psHistory.Start.Value         = m.StartTime;
                psHistory.Randomization.Value = (int)m.Randomization;
                psHistory.CommitType.Value    = (int?)commitType;
                psHistory.Insert();
            }

            foreach (var s in new [] { m.RedScores, m.BlueScores })
            {
                int alliance = s == m.RedScores ? 0 : 1;

                // BLOCK
                {
                    var psScoresHistory = Database.Tables.QualsScoresHistory.NewRow();
                    psScoresHistory.MatchNumber.Value = MatchNumber;
                    psScoresHistory.Ts.Value          = commitTime;
                    psScoresHistory.Alliance.Value    = alliance;
                    s.Save(psScoresHistory);
                    psScoresHistory.Insert();
                }

                // BLOCK
                {
                    var psGameHistory = Database.Tables.QualsGameSpecificHistory.NewRow();
                    psGameHistory.MatchNumber.Value = MatchNumber;
                    psGameHistory.Ts.Value          = commitTime;
                    psGameHistory.Alliance.Value    = alliance;
                    s.Save(psGameHistory);
                    psGameHistory.Insert();
                }
            }
        }
Example #3
0
        //----------------------------------------------------------------------------------------
        // Playing
        //----------------------------------------------------------------------------------------

        // Play this match with the required Win for Blue
        protected MatchPlayedThisEvent PlayMatch()
        {
            MatchPlayedThisEvent m = new MatchPlayedThisEvent(Database, FMSScheduleDetailId);

            // match.MatchNumber = MatchNumber; // not needed: match number comes via FMSScheduleDetailId
            m.FmsMatchId.Value = FMSMatchIdGuid;
            m.PlayNumber       = 1; // odd, but that's what SQLiteMachDAO.commitMatch() does
            m.FieldType        = 1; // ditto
            m.RedScores.SetRedEqualizationMatch();
            m.BlueScores.SetBlueEqualizationMatch();
            m.ScoreDetails  = m.EncodeScoreDetails();
            m.RedScore      = m.RedScores.ScoredPoints;
            m.RedPenalty    = m.RedScores.PenaltyPoints;
            m.BlueScore     = m.BlueScores.ScoredPoints;
            m.BluePenalty   = m.BlueScores.PenaltyPoints;
            m.RedAutoScore  = m.RedScores.AutonomousPoints;
            m.BlueAutoScore = m.BlueScores.AutonomousPoints;

            m.CreatedBy = CreatedBy;
            m.StartTime = DateTimeOffset.UtcNow;

            return(m);
        }
        protected void CommitMatchCore() // MatchDAO.java / commitMatch
        {
            MatchPlayedThisEvent m = this;

            DateTimeOffset commitTime = m.CommitHistoryLatest.Ts;

            // BLOCK
            {
                var psData = Database.Tables.QualsData.NewRow();
                psData.MatchNumber.Value   = m.MatchNumber;
                psData.Status.Value        = (int)TMatchState.Committed;
                psData.Randomization.Value = (int)m.Randomization;
                psData.Start.Value         = m.StartTime;
                psData.Update(psData.Columns(new[] { nameof(psData.Status), nameof(psData.Randomization), nameof(psData.Start) }), psData.Where(nameof(psData.MatchNumber), MatchNumber));
            }

            // BLOCK
            {
                // (MatchNumber, Ts) must be unique
                var psHistory = Database.Tables.QualsCommitHistory.NewRow();
                psHistory.MatchNumber.Value   = m.MatchNumber;
                psHistory.Ts.Value            = commitTime;
                psHistory.Start.Value         = m.StartTime;
                psHistory.Randomization.Value = (int)m.Randomization;
                psHistory.CommitType.Value    = (int?)m.CommitHistoryLatest.CommitType;
                psHistory.InsertOrReplace();
            }

            // BLOCK
            {
                var psResult = Database.Tables.QualsResults.NewRow();
                psResult.MatchNumber.Value          = m.MatchNumber;
                psResult.RedScore.Value             = m.RedScore;
                psResult.BlueScore.Value            = m.BlueScore;
                psResult.RedPenaltyCommitted.Value  = m.RedPenalty;
                psResult.BluePenaltyCommitted.Value = m.BluePenalty;
                psResult.InsertOrReplace();
            }

            // BLOCK
            {
                Commit matchEnd         = null;
                Commit referee          = null;
                Commit scoreKeeper      = null;
                Commit firstScoreKeeper = null;
                foreach (var commit in CommitHistoryAscending)
                {
                    switch (commit.CommitType)
                    {
                    case TCommitType.COMMIT:
                        scoreKeeper = commit;
                        if (firstScoreKeeper == null)
                        {
                            firstScoreKeeper = commit;
                        }
                        break;

                    case TCommitType.MATCH_END:
                        matchEnd = commit;
                        break;

                    case TCommitType.BLUE_REF_SUBMIT:
                    case TCommitType.RED_REF_SUBMIT:
                        referee = commit;
                        break;
                    }
                }

                var fmsMatch = Database.Tables.Match.NewRow();
                fmsMatch.FMSMatchId.Value            = m.FmsMatchId.Value;          // 1
                fmsMatch.FMSScheduleDetailId.Value   = m.FMSScheduleDetailId.Value; // 2
                fmsMatch.PlayNumber.Value            = m.PlayNumber;                // 3
                fmsMatch.FieldType.Value             = m.FieldType;                 // 4
                fmsMatch.InitialPrestartTime.Value   = m.InitialPreStartTime;       // 5
                fmsMatch.FinalPreStartTime.Value     = m.FinalPreStartTime;         // 6
                fmsMatch.PreStartCount.Value         = m.PreStartCount;             // 7
                fmsMatch.AutoStartTime.Value         = m.AutoStartTime;             // 8
                fmsMatch.AutoEndTime.Value           = m.AutoEndTime;               // 9
                fmsMatch.TeleopStartTime.Value       = m.TeleopStartTime;           // 10
                fmsMatch.TeleopEndTime.Value         = matchEnd?.Ts;                // 11
                fmsMatch.RefCommitTime.Value         = referee?.Ts;                 // 12
                fmsMatch.ScoreKeeperCommitTime.Value = scoreKeeper?.Ts;             // 13
                fmsMatch.PostMatchTime.Value         = m.PostMatchTime;
                fmsMatch.CancelMatchTime.Value       = m.CancelMatchTime;
                fmsMatch.CycleTime.Value             = m.CycleTime;         // 16

                fmsMatch.RedScore.Value      = m.RedScore;
                fmsMatch.BlueScore.Value     = m.BlueScore;
                fmsMatch.RedPenalty.Value    = m.RedPenalty;
                fmsMatch.BluePenalty.Value   = m.BluePenalty;
                fmsMatch.RedAutoScore.Value  = m.RedAutoScore;
                fmsMatch.BlueAutoScore.Value = m.BlueAutoScore;                                    // 22

                fmsMatch.ScoreDetails.Value  = m.ScoreDetails;                                     // 23
                fmsMatch.HeadRefReview.Value = m.HeadRefReview;                                    // 24
                fmsMatch.VideoUrl.Value      = m.VideoUrl;                                         // 25

                fmsMatch.CreatedOn.Value  = firstScoreKeeper?.Ts ?? DateTimeAsInteger.NegativeOne; // 26
                fmsMatch.CreatedBy.Value  = m.CreatedBy;                                           // 27
                fmsMatch.ModifiedOn.Value = scoreKeeper?.Ts ?? DateTimeAsInteger.NegativeOne;      // 28
                fmsMatch.ModifiedBy.Value = m.ModifiedBy;                                          // 29
                fmsMatch.FMSEventId.Value = m.FMSEventId.Value;                                    // 30
                fmsMatch.RowVersion.Value = m.RowVersion.Value;                                    // 31

                fmsMatch.InsertOrReplace();

                Database.AddOrReplacePlayedMatch(this);
            }

            foreach (var s in new [] { m.RedScores, m.BlueScores })
            {
                int alliance = s == m.RedScores ? 0 : 1;

                // BLOCK
                {
                    var psScores = Database.Tables.QualsScores.NewRow();
                    psScores.MatchNumber.Value = MatchNumber;
                    psScores.Alliance.Value    = alliance;
                    s.Save(psScores);
                    psScores.InsertOrReplace();
                }

                // BLOCK
                {
                    var psScoresHistory = Database.Tables.QualsScoresHistory.NewRow();
                    psScoresHistory.MatchNumber.Value = MatchNumber;
                    psScoresHistory.Ts.Value          = commitTime;
                    psScoresHistory.Alliance.Value    = alliance;
                    s.Save(psScoresHistory);
                    psScoresHistory.InsertOrReplace();
                }

                // BLOCK
                {
                    var psGame = Database.Tables.QualsGameSpecific.NewRow();
                    psGame.MatchNumber.Value = MatchNumber;
                    psGame.Alliance.Value    = alliance;
                    s.Save(psGame);
                    psGame.InsertOrReplace();
                }

                // BLOCK
                {
                    var psGameHistory = Database.Tables.QualsGameSpecificHistory.NewRow();
                    psGameHistory.MatchNumber.Value = MatchNumber;
                    psGameHistory.Ts.Value          = commitTime;
                    psGameHistory.Alliance.Value    = alliance;
                    s.Save(psGameHistory);
                    psGameHistory.InsertOrReplace();
                }
            }
        }