diff --git a/core/include/area_data.h b/core/include/area_data.h index 30ba989..f2d777d 100644 --- a/core/include/area_data.h +++ b/core/include/area_data.h @@ -682,31 +682,20 @@ class AreaData : public QObject { void removeStatement(int f_position); /** - * @brief Advances the testimony playback. + * @brief Jumps the testimony playback to the given index. * * @details When advancing forward, if the playback would go past the last statement, * it instead returns the first statement. * When advancing backward, if the playback would go before the first statement, it * instead returns the first statement. * - * @param f_forward If true, the testimony playback advances to the next statement in the - * testimony. If false, it instead advances to the previous statement. + * @param f_position The index to jump to. * * @return A pair of values: * * First, a `QStringList` that is the packet of the statement that was advanced to. * * Then, a `TestimonyProgress` value that describes how the advancement happened. */ - std::pair advanceTestimony(bool f_forward = true); - - /** - * @brief Jumps the testimony playback to the given index, and returns the statement in that - * index position. - * - * @param f_position The index to jump to. - * - * @return See short description. - */ - QStringList jumpToStatement(int f_position); + std::pair jumpToStatement(int f_position); /** * @brief Returns a copy of the judgelog in the area. diff --git a/core/src/area_data.cpp b/core/src/area_data.cpp index 64b2dc6..968b729 100644 --- a/core/src/area_data.cpp +++ b/core/src/area_data.cpp @@ -28,6 +28,7 @@ AreaData::AreaData(QString p_name, int p_index) : m_document("No document."), m_defHP(10), m_proHP(10), + m_statement(0), m_judgelog(), m_lastICMessage() { @@ -397,14 +398,16 @@ void AreaData::removeStatement(int f_position) --m_statement; } -std::pair AreaData::advanceTestimony(bool f_forward) +std::pair AreaData::jumpToStatement(int f_position) { - f_forward ? m_statement++: m_statement--; + m_statement = f_position; if (m_statement > m_testimony.size() - 1) { + m_statement = 0; return {m_testimony.at(m_statement), TestimonyProgress::LOOPED}; } if (m_statement <= 0) { + m_statement = 0; return {m_testimony.at(m_statement), TestimonyProgress::STAYED_AT_FIRST}; } else { @@ -412,12 +415,6 @@ std::pair AreaData::advanceTestimony(b } } -QStringList AreaData::jumpToStatement(int f_position) -{ - m_statement = f_position; - return m_testimony.at(m_statement); -} - const QVector& AreaData::testimony() const { return m_testimony; diff --git a/core/src/packets.cpp b/core/src/packets.cpp index bd82d3d..4cdabdd 100644 --- a/core/src/packets.cpp +++ b/core/src/packets.cpp @@ -783,7 +783,7 @@ AOPacket AOClient::validateIcPacket(AOPacket packet) if (args[4] == ">") { pos = "wit"; - std::make_pair(args, l_progress) = area->advanceTestimony(); + std::make_pair(args, l_progress) = area->jumpToStatement(area->statement() + 1); if (l_progress == AreaData::TestimonyProgress::LOOPED) { sendServerMessageArea("Last statement reached. Looping to first statement."); @@ -791,9 +791,9 @@ AOPacket AOClient::validateIcPacket(AOPacket packet) } if (args[4] == "<") { pos = "wit"; - std::make_pair(args, l_progress) = area->advanceTestimony(false); + std::make_pair(args, l_progress) = area->jumpToStatement(area->statement() - 1); - if (l_progress == AreaData::TestimonyProgress::LOOPED) { + if (l_progress == AreaData::TestimonyProgress::STAYED_AT_FIRST) { sendServerMessage("First statement reached."); } } @@ -803,7 +803,22 @@ AOPacket AOClient::validateIcPacket(AOPacket packet) QRegularExpressionMatch match = jump.match(decoded_message); if (match.hasMatch()) { pos = "wit"; - args = area->jumpToStatement(match.captured("int").toInt()); + std::make_pair(args, l_progress) = area->jumpToStatement(match.captured("int").toInt()); + + switch (l_progress){ + case AreaData::TestimonyProgress::LOOPED: + { + sendServerMessageArea("Last statement reached. Looping to first statement."); + } + case AreaData::TestimonyProgress::STAYED_AT_FIRST: + { + sendServerMessage("First statement reached."); + } + case AreaData::TestimonyProgress::OK: + default: + // No need to handle. + break; + } } } diff --git a/tests/unittest_area/tst_unittest_area.cpp b/tests/unittest_area/tst_unittest_area.cpp index d8dca7b..b9b8afd 100644 --- a/tests/unittest_area/tst_unittest_area.cpp +++ b/tests/unittest_area/tst_unittest_area.cpp @@ -60,6 +60,8 @@ private slots: * @test Tests changing character in the area. */ void changeCharacter(); + + void testimony(); }; void Area::init() @@ -186,6 +188,51 @@ void Area::changeCharacter() } } +void Area::testimony() +{ + QVector l_testimony = { + {"A"}, + {"B"}, + {"C"}, + {"D"}, + }; + + { + // Add all statements, and check that they're added. + for (const auto& l_statement : l_testimony) + { + m_area->recordStatement(l_statement); + + QCOMPARE(l_statement, m_area->testimony().at(m_area->statement() - 1)); + } + } + { + // Restart testimony, advance three times. + m_area->restartTestimony(); + + for (int i = 0; i < l_testimony.size() - 1; i++) { + const auto& l_results = m_area->jumpToStatement(m_area->statement() + 1); + + QCOMPARE(l_results.first, l_testimony.at(i + 1)); + QCOMPARE(l_results.second, AreaData::TestimonyProgress::OK); + } + } + { + // Next advancement loops the testimony. + const auto& l_results = m_area->jumpToStatement(m_area->statement() + 1); + + QCOMPARE(l_results.first, l_testimony.at(0)); + QCOMPARE(l_results.second, AreaData::TestimonyProgress::LOOPED); + } + { + // Going back makes the testimony stay at the first statement. + const auto& l_results = m_area->jumpToStatement(m_area->statement() - 1); + + QCOMPARE(l_results.first, l_testimony.at(0)); + QCOMPARE(l_results.second, AreaData::TestimonyProgress::STAYED_AT_FIRST); + } +} + } }