LCOV - code coverage report
Current view: top level - source/simulation2/system - ReplayTurnManager.cpp (source / functions) Hit Total Coverage
Test: 0 A.D. test coverage report Lines: 0 56 0.0 %
Date: 2022-06-14 00:41:00 Functions: 0 7 0.0 %

          Line data    Source code
       1             : /* Copyright (C) 2021 Wildfire Games.
       2             :  * This file is part of 0 A.D.
       3             :  *
       4             :  * 0 A.D. is free software: you can redistribute it and/or modify
       5             :  * it under the terms of the GNU General Public License as published by
       6             :  * the Free Software Foundation, either version 2 of the License, or
       7             :  * (at your option) any later version.
       8             :  *
       9             :  * 0 A.D. is distributed in the hope that it will be useful,
      10             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      11             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      12             :  * GNU General Public License for more details.
      13             :  *
      14             :  * You should have received a copy of the GNU General Public License
      15             :  * along with 0 A.D.  If not, see <http://www.gnu.org/licenses/>.
      16             :  */
      17             : 
      18             : #include "precompiled.h"
      19             : 
      20             : #include "ReplayTurnManager.h"
      21             : 
      22             : #include "gui/GUIManager.h"
      23             : #include "ps/CLogger.h"
      24             : #include "ps/Util.h"
      25             : #include "scriptinterface/ScriptConversions.h"
      26             : #include "scriptinterface/ScriptRequest.h"
      27             : #include "scriptinterface/JSON.h"
      28             : #include "simulation2/Simulation2.h"
      29             : 
      30             : const CStr CReplayTurnManager::EventNameReplayFinished = "ReplayFinished";
      31             : const CStr CReplayTurnManager::EventNameReplayOutOfSync = "ReplayOutOfSync";
      32             : 
      33           0 : CReplayTurnManager::CReplayTurnManager(CSimulation2& simulation, IReplayLogger& replay)
      34           0 :     : CLocalTurnManager(simulation, replay)
      35             : {
      36           0 : }
      37             : 
      38           0 : void CReplayTurnManager::StoreReplayCommand(u32 turn, int player, const std::string& command)
      39             : {
      40             :     // Using the pair we make sure that commands per turn will be processed in the correct order
      41           0 :     m_ReplayCommands[turn].emplace_back(player, command);
      42           0 : }
      43             : 
      44           0 : void CReplayTurnManager::StoreReplayHash(u32 turn, const std::string& hash, bool quick)
      45             : {
      46           0 :     m_ReplayHash[turn] = std::make_pair(hash, quick);
      47           0 : }
      48             : 
      49           0 : void CReplayTurnManager::StoreReplayTurnLength(u32 turn, u32 turnLength)
      50             : {
      51           0 :     m_ReplayTurnLengths[turn] = turnLength;
      52             : 
      53             :     // Initialize turn length
      54           0 :     if (turn == 0)
      55           0 :         m_TurnLength = m_ReplayTurnLengths[0];
      56           0 : }
      57             : 
      58           0 : void CReplayTurnManager::StoreFinalReplayTurn(u32 turn)
      59             : {
      60           0 :     m_FinalTurn = turn;
      61           0 : }
      62             : 
      63           0 : void CReplayTurnManager::NotifyFinishedUpdate(u32 turn)
      64             : {
      65           0 :     if (turn == 1 && m_FinalTurn == 0)
      66           0 :         g_GUI->SendEventToAll(EventNameReplayFinished);
      67             : 
      68           0 :     if (turn > m_FinalTurn)
      69           0 :         return;
      70             : 
      71           0 :     DoTurn(turn);
      72             : 
      73             :     // Compare hash if it exists in the replay and if we didn't have an OOS already
      74           0 :     std::map<u32, std::pair<std::string, bool>>::iterator turnHashIt = m_ReplayHash.find(turn);
      75           0 :     if (m_HasSyncError || turnHashIt == m_ReplayHash.end())
      76             :         return;
      77             : 
      78           0 :     std::string expectedHash = turnHashIt->second.first;
      79           0 :     bool quickHash = turnHashIt->second.second;
      80             : 
      81             :     // Compute hash
      82           0 :     std::string hash;
      83           0 :     ENSURE(m_Simulation2.ComputeStateHash(hash, quickHash));
      84           0 :     hash = Hexify(hash);
      85             : 
      86           0 :     if (hash == expectedHash)
      87           0 :         return;
      88             : 
      89           0 :     m_HasSyncError = true;
      90           0 :     LOGERROR("Replay out of sync on turn %d", turn);
      91             : 
      92           0 :     const ScriptInterface& scriptInterface = m_Simulation2.GetScriptInterface();
      93           0 :     ScriptRequest rq(scriptInterface);
      94             : 
      95           0 :     JS::RootedValueVector paramData(rq.cx);
      96             : 
      97           0 :     ignore_result(paramData.append(JS::NumberValue(turn)));
      98             : 
      99           0 :     JS::RootedValue hashVal(rq.cx);
     100           0 :     Script::ToJSVal(rq, &hashVal, hash);
     101           0 :     ignore_result(paramData.append(hashVal));
     102             : 
     103           0 :     JS::RootedValue expectedHashVal(rq.cx);
     104           0 :     Script::ToJSVal(rq, &expectedHashVal, expectedHash);
     105           0 :     ignore_result(paramData.append(expectedHashVal));
     106             : 
     107           0 :     g_GUI->SendEventToAll(EventNameReplayOutOfSync, paramData);
     108             : }
     109             : 
     110           0 : void CReplayTurnManager::DoTurn(u32 turn)
     111             : {
     112           0 :     debug_printf("Executing turn %u of %u\n", turn, m_FinalTurn);
     113             : 
     114           0 :     m_TurnLength = m_ReplayTurnLengths[turn];
     115             : 
     116           0 :     ScriptRequest rq(m_Simulation2.GetScriptInterface());
     117             : 
     118             :     // Simulate commands for that turn
     119           0 :     for (const std::pair<player_id_t, std::string>& p : m_ReplayCommands[turn])
     120             :     {
     121           0 :         JS::RootedValue command(rq.cx);
     122           0 :         Script::ParseJSON(rq, p.second, &command);
     123           0 :         AddCommand(m_ClientId, p.first, command, m_CurrentTurn + 1);
     124             :     }
     125             : 
     126           0 :     if (turn == m_FinalTurn)
     127           0 :         g_GUI->SendEventToAll(EventNameReplayFinished);
     128           0 : }

Generated by: LCOV version 1.13