From 44519c0db38145f1eb22dc17d0698642314a5448 Mon Sep 17 00:00:00 2001 From: MangosArentLiterature <58055358+MangosArentLiterature@users.noreply.github.com> Date: Wed, 2 Jun 2021 16:17:07 -0500 Subject: [PATCH] Fix character selection - Fix an issue allowing you to select taken characters - Fix a regression of #64 Co-Authored-By: Cerapter <43446478+Cerapter@users.noreply.github.com> --- core/src/aoclient.cpp | 25 ++++++++++++----------- core/src/area_data.cpp | 11 ++++++---- tests/unittest_area/tst_unittest_area.cpp | 11 ++++++++++ 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/core/src/aoclient.cpp b/core/src/aoclient.cpp index 5470355..5ef7800 100644 --- a/core/src/aoclient.cpp +++ b/core/src/aoclient.cpp @@ -113,7 +113,7 @@ void AOClient::changeArea(int new_area) } if (current_char != "") { - server->areas[current_area]->charactersTaken().removeAll(server->getCharID(current_char)); + server->areas[current_area]->changeCharacter(server->getCharID(current_char), -1); server->updateCharsTaken(server->areas[current_area]); } server->areas[new_area]->clientJoinedArea(char_id); @@ -130,7 +130,7 @@ void AOClient::changeArea(int new_area) sendPacket("DONE"); } else { - server->areas[current_area]->charactersTaken().append(server->getCharID(current_char)); + server->areas[current_area]->changeCharacter(-1, server->getCharID(current_char)); server->updateCharsTaken(server->areas[current_area]); } for (QTimer* timer : server->areas[current_area]->timers()) { @@ -161,18 +161,19 @@ bool AOClient::changeCharacter(int char_id) bool l_successfulChange = area->changeCharacter(server->getCharID(current_char), char_id); - current_char = ""; - - if (l_successfulChange) { - QString char_selected = server->characters[char_id]; - current_char = char_selected; + if (char_id < 0) { + current_char = ""; } - pos = ""; - - server->updateCharsTaken(area); - sendPacket("PV", {QString::number(id), "CID", QString::number(char_id)}); - return true; + if (l_successfulChange == true) { + QString char_selected = server->characters[char_id]; + current_char = char_selected; + pos = ""; + server->updateCharsTaken(area); + sendPacket("PV", {QString::number(id), "CID", QString::number(char_id)}); + return true; + } + return false; } void AOClient::changePosition(QString new_pos) diff --git a/core/src/area_data.cpp b/core/src/area_data.cpp index 968b729..78d3aa0 100644 --- a/core/src/area_data.cpp +++ b/core/src/area_data.cpp @@ -210,19 +210,22 @@ QList AreaData::charactersTaken() const bool AreaData::changeCharacter(int f_from, int f_to) { - if (f_from != -1) { - m_charactersTaken.removeAll(f_from); - } - if (m_charactersTaken.contains(f_to)) { return false; } if (f_to != -1) { + if (f_from != -1) { + m_charactersTaken.removeAll(f_from); + } m_charactersTaken.append(f_to); return true; } + if (f_to == -1 && f_from != -1) { + m_charactersTaken.removeAll(f_from); + } + return false; } diff --git a/tests/unittest_area/tst_unittest_area.cpp b/tests/unittest_area/tst_unittest_area.cpp index b9b8afd..6c2f316 100644 --- a/tests/unittest_area/tst_unittest_area.cpp +++ b/tests/unittest_area/tst_unittest_area.cpp @@ -177,6 +177,17 @@ void Area::changeCharacter() QCOMPARE(m_area->charactersTaken().at(0), 7); QCOMPARE(m_area->charactersTaken().at(1), 8); } + { + // Client attempts to switch to 7. + // Nothing changes, as it is already taken. + + m_area->changeCharacter(8, 7); + + QCOMPARE(m_area->playerCount(), 1); + QCOMPARE(m_area->charactersTaken().size(), 2); + QCOMPARE(m_area->charactersTaken().at(0), 7); + QCOMPARE(m_area->charactersTaken().at(1), 8); + } { // Charid 7 is unlocked for use. // Charid 8 is taken.