From caeda501c3b597a8135b80550cf7c75554285cf3 Mon Sep 17 00:00:00 2001 From: scatterflower Date: Tue, 1 Sep 2020 01:08:21 -0500 Subject: [PATCH] implement area updates --- include/aoclient.h | 9 ++++++ include/area_data.h | 3 ++ include/server.h | 1 + src/aoclient.cpp | 71 ++++++++++++++++++++++++++++++++++++--------- src/area_data.cpp | 4 +++ src/server.cpp | 8 ++++- 6 files changed, 82 insertions(+), 14 deletions(-) diff --git a/include/aoclient.h b/include/aoclient.h index a3cbc38..e312b9c 100644 --- a/include/aoclient.h +++ b/include/aoclient.h @@ -55,8 +55,17 @@ class AOClient : public QObject { Server* server; QTcpSocket* socket; + enum ARUPType { + PLAYER_COUNT, + STATUS, + CM, + LOCKED + }; + void handlePacket(AOPacket packet); void changeArea(int new_area); + void arup(ARUPType type, bool broadcast); + void fullArup(); QString partial_packet; bool is_partial; diff --git a/include/area_data.h b/include/area_data.h index 63f7ce2..81a58dc 100644 --- a/include/area_data.h +++ b/include/area_data.h @@ -31,6 +31,9 @@ class AreaData { int index; QMap characters_taken; int player_count; + QString status; + QString current_cm; + bool locked; QString background; bool showname_allowed; diff --git a/include/server.h b/include/server.h index 80b6364..f1b8bbf 100644 --- a/include/server.h +++ b/include/server.h @@ -43,6 +43,7 @@ class Server : public QObject { AOClient* getClient(QString ipid); void updateCharsTaken(AreaData* area); void broadcast(AOPacket packet, int area_index); + void broadcast(AOPacket packet); int player_count; QStringList characters; diff --git a/src/aoclient.cpp b/src/aoclient.cpp index 83cd113..d2a58a8 100644 --- a/src/aoclient.cpp +++ b/src/aoclient.cpp @@ -53,12 +53,15 @@ void AOClient::clientData() void AOClient::clientDisconnected() { qDebug() << remote_ip << "disconnected"; - if (joined) + if (joined) { server->player_count--; + server->areas[current_area]->player_count--; + arup(ARUPType::PLAYER_COUNT, true); + } if (current_char != "") { - server->areas.value(current_area)->characters_taken[current_char] = + server->areas[current_area]->characters_taken[current_char] = false; - server->updateCharsTaken(server->areas.value(current_area)); + server->updateCharsTaken(server->areas[current_area]); } } @@ -66,7 +69,7 @@ void AOClient::handlePacket(AOPacket packet) { // TODO: like everything here should send a signal //qDebug() << "Received packet:" << packet.header << ":" << packet.contents; - AreaData* area = server->areas.value(current_area); + AreaData* area = server->areas[current_area]; // Lord forgive me if (packet.header == "HI") { setHwid(packet.contents[0]); @@ -105,8 +108,11 @@ void AOClient::handlePacket(AOPacket packet) } else if (packet.header == "RD") { server->player_count++; + area->player_count++; joined = true; server->updateCharsTaken(area); + fullArup(); // Give client all the area data + arup(ARUPType::PLAYER_COUNT, true); // Tell everyone there is a new player QSettings areas_ini("areas.ini", QSettings::IniFormat); QStringList areas = areas_ini.childGroups(); @@ -144,7 +150,7 @@ void AOClient::handlePacket(AOPacket packet) current_char = ""; } - server->updateCharsTaken(server->areas.value(current_area)); + server->updateCharsTaken(area); sendPacket("PV", {"271828", "CID", packet.contents[1]}); } else if (packet.header == "MS") { @@ -200,25 +206,64 @@ void AOClient::changeArea(int new_area) sendPacket("CT", {"Server", "You are already in area " + server->area_names[current_area], "1"}); return; } - if (current_char != "") { - server->areas.value(current_area)->characters_taken[current_char] = - false; - server->updateCharsTaken(server->areas.value(current_area)); + if (server->areas[new_area]->locked) { + sendPacket("CT", {"Server", "Area " + server->area_names[new_area] + " is locked.", "1"}); + return; } + if (current_char != "") { + server->areas[current_area]->characters_taken[current_char] = + false; + server->updateCharsTaken(server->areas[current_area]); + } + server->areas[new_area]->player_count++; + server->areas[current_area]->player_count--; current_area = new_area; + arup(ARUPType::PLAYER_COUNT, true); // send hp, bn, le, arup - if (server->areas.value(current_area)->characters_taken[current_char]) { - server->updateCharsTaken(server->areas.value(current_area)); + if (server->areas[current_area]->characters_taken[current_char]) { + server->updateCharsTaken(server->areas[current_area]); current_char = ""; sendPacket("DONE"); } else { - server->areas.value(current_area)->characters_taken[current_char] = true; - server->updateCharsTaken(server->areas.value(current_area)); + server->areas[current_area]->characters_taken[current_char] = true; + server->updateCharsTaken(server->areas[current_area]); } sendPacket("CT", {"Server", "You have been moved to area " + server->area_names[current_area], "1"}); } +void AOClient::arup(ARUPType type, bool broadcast) +{ + QStringList arup_data; + arup_data.append(QString::number(type)); + for (AreaData* area : server->areas) { + if (type == ARUPType::PLAYER_COUNT) { + arup_data.append(QString::number(area->player_count)); + } + else if (type == ARUPType::STATUS) { + arup_data.append(area->status); + } + else if (type == ARUPType::CM) { + arup_data.append(area->current_cm); + } + else if (type == ARUPType::LOCKED) { + arup_data.append(area->locked ? "LOCKED" : "FREE"); + } + else return; + } + if (broadcast) + server->broadcast(AOPacket("ARUP", arup_data)); + else + sendPacket("ARUP", arup_data); +} + +void AOClient::fullArup() { + arup(ARUPType::PLAYER_COUNT, false); + arup(ARUPType::STATUS, false); + arup(ARUPType::CM, false); + arup(ARUPType::LOCKED, false); +} + void AOClient::sendPacket(AOPacket packet) { qDebug() << "Sent packet:" << packet.header << ":" << packet.contents; diff --git a/src/area_data.cpp b/src/area_data.cpp index adf4a8a..545a866 100644 --- a/src/area_data.cpp +++ b/src/area_data.cpp @@ -28,4 +28,8 @@ AreaData::AreaData(QStringList characters, QString p_name, int p_index) areas_ini.beginGroup(p_name); background = areas_ini.value("background", "gs4").toString(); areas_ini.endGroup(); + player_count = 0; + current_cm = "FREE"; + locked = false; + status = "FREE"; } diff --git a/src/server.cpp b/src/server.cpp index 9367d41..c21c89e 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -107,13 +107,19 @@ void Server::updateCharsTaken(AreaData* area) void Server::broadcast(AOPacket packet, int area_index) { - // TODO: make this selective to the current area only for (AOClient* client : clients) { if (client->current_area == area_index) client->sendPacket(packet); } } +void Server::broadcast(AOPacket packet) +{ + for (AOClient* client : clients) { + client->sendPacket(packet); + } +} + AOClient* Server::getClient(QString ipid) { for (AOClient* client : clients) {