Fix testimony problems, simplify statement hops, add tests for testimony

This commit is contained in:
Cerapter 2021-05-09 18:02:50 +02:00
parent 4ed0c1e238
commit de085461d9
4 changed files with 74 additions and 26 deletions

View File

@ -682,31 +682,20 @@ class AreaData : public QObject {
void removeStatement(int f_position); 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, * @details When advancing forward, if the playback would go past the last statement,
* it instead returns the first statement. * it instead returns the first statement.
* When advancing backward, if the playback would go before the first statement, it * When advancing backward, if the playback would go before the first statement, it
* instead returns the first statement. * instead returns the first statement.
* *
* @param f_forward If true, the testimony playback advances to the next statement in the * @param f_position The index to jump to.
* testimony. If false, it instead advances to the previous statement.
* *
* @return A pair of values: * @return A pair of values:
* * First, a `QStringList` that is the packet of the statement that was advanced to. * * First, a `QStringList` that is the packet of the statement that was advanced to.
* * Then, a `TestimonyProgress` value that describes how the advancement happened. * * Then, a `TestimonyProgress` value that describes how the advancement happened.
*/ */
std::pair<QStringList, TestimonyProgress> advanceTestimony(bool f_forward = true); std::pair<QStringList, AreaData::TestimonyProgress> jumpToStatement(int f_position);
/**
* @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);
/** /**
* @brief Returns a copy of the judgelog in the area. * @brief Returns a copy of the judgelog in the area.

View File

@ -28,6 +28,7 @@ AreaData::AreaData(QString p_name, int p_index) :
m_document("No document."), m_document("No document."),
m_defHP(10), m_defHP(10),
m_proHP(10), m_proHP(10),
m_statement(0),
m_judgelog(), m_judgelog(),
m_lastICMessage() m_lastICMessage()
{ {
@ -397,14 +398,16 @@ void AreaData::removeStatement(int f_position)
--m_statement; --m_statement;
} }
std::pair<QStringList, AreaData::TestimonyProgress> AreaData::advanceTestimony(bool f_forward) std::pair<QStringList, AreaData::TestimonyProgress> AreaData::jumpToStatement(int f_position)
{ {
f_forward ? m_statement++: m_statement--; m_statement = f_position;
if (m_statement > m_testimony.size() - 1) { if (m_statement > m_testimony.size() - 1) {
m_statement = 0;
return {m_testimony.at(m_statement), TestimonyProgress::LOOPED}; return {m_testimony.at(m_statement), TestimonyProgress::LOOPED};
} }
if (m_statement <= 0) { if (m_statement <= 0) {
m_statement = 0;
return {m_testimony.at(m_statement), TestimonyProgress::STAYED_AT_FIRST}; return {m_testimony.at(m_statement), TestimonyProgress::STAYED_AT_FIRST};
} }
else { else {
@ -412,12 +415,6 @@ std::pair<QStringList, AreaData::TestimonyProgress> AreaData::advanceTestimony(b
} }
} }
QStringList AreaData::jumpToStatement(int f_position)
{
m_statement = f_position;
return m_testimony.at(m_statement);
}
const QVector<QStringList>& AreaData::testimony() const const QVector<QStringList>& AreaData::testimony() const
{ {
return m_testimony; return m_testimony;

View File

@ -783,7 +783,7 @@ AOPacket AOClient::validateIcPacket(AOPacket packet)
if (args[4] == ">") { if (args[4] == ">") {
pos = "wit"; 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) { if (l_progress == AreaData::TestimonyProgress::LOOPED) {
sendServerMessageArea("Last statement reached. Looping to first statement."); sendServerMessageArea("Last statement reached. Looping to first statement.");
@ -791,9 +791,9 @@ AOPacket AOClient::validateIcPacket(AOPacket packet)
} }
if (args[4] == "<") { if (args[4] == "<") {
pos = "wit"; 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."); sendServerMessage("First statement reached.");
} }
} }
@ -803,7 +803,22 @@ AOPacket AOClient::validateIcPacket(AOPacket packet)
QRegularExpressionMatch match = jump.match(decoded_message); QRegularExpressionMatch match = jump.match(decoded_message);
if (match.hasMatch()) { if (match.hasMatch()) {
pos = "wit"; 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;
}
} }
} }

View File

@ -60,6 +60,8 @@ private slots:
* @test Tests changing character in the area. * @test Tests changing character in the area.
*/ */
void changeCharacter(); void changeCharacter();
void testimony();
}; };
void Area::init() void Area::init()
@ -186,6 +188,51 @@ void Area::changeCharacter()
} }
} }
void Area::testimony()
{
QVector<QStringList> 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);
}
}
} }
} }