From 033d4f93e51580a3496910c7b5571d9a1b536765 Mon Sep 17 00:00:00 2001 From: in1tiate Date: Sat, 27 Feb 2021 17:36:21 -0600 Subject: [PATCH] add CMing and document commands --- include/aoclient.h | 39 +++++++++++++++++-------- include/area_data.h | 10 +++++-- include/server.h | 1 + src/aoclient.cpp | 16 ++++++++-- src/area_data.cpp | 4 +-- src/commands.cpp | 71 +++++++++++++++++++++++++++++++++++++++++++-- src/packets.cpp | 1 + src/server.cpp | 22 +++++++++++++- 8 files changed, 141 insertions(+), 23 deletions(-) diff --git a/include/aoclient.h b/include/aoclient.h index bcd63f0..4a35313 100644 --- a/include/aoclient.h +++ b/include/aoclient.h @@ -38,7 +38,7 @@ class Server; class AOClient : public QObject { Q_OBJECT public: - AOClient(Server* p_server, QTcpSocket* p_socket, QObject* parent = nullptr); + AOClient(Server* p_server, QTcpSocket* p_socket, QObject* parent = nullptr, int user_id = 0); ~AOClient(); QString getHwid(); @@ -46,6 +46,8 @@ class AOClient : public QObject { Server* getServer(); void setHwid(QString p_hwid); + int id; + QHostAddress remote_ip; QString password; bool joined; @@ -152,28 +154,37 @@ class AOClient : public QObject { {"EE", {ACLFlags.value("NONE"), 4, &AOClient::pktEditEvidence}} }; - // Commands + //// Commands void cmdDefault(int argc, QStringList argv); + // Authentication void cmdLogin(int argc, QStringList argv); - void cmdGetAreas(int argc, QStringList argv); - void cmdGetArea(int argc, QStringList argv); - void cmdBan(int argc, QStringList argv); - void cmdKick(int argc, QStringList argv); void cmdChangeAuth(int argc, QStringList argv); void cmdSetRootPass(int argc, QStringList argv); - void cmdSetBackground(int argc, QStringList argv); - void cmdBgLock(int argc, QStringList argv); - void cmdBgUnlock(int argc, QStringList argv); void cmdAddUser(int argc, QStringList argv); void cmdListPerms(int argc, QStringList argv); void cmdAddPerms(int argc, QStringList argv); void cmdRemovePerms(int argc, QStringList argv); void cmdListUsers(int argc, QStringList argv); void cmdLogout(int argc, QStringList argv); - void cmdPos(int argc, QStringList argv); - void cmdG(int argc, QStringList argv); + // Areas + void cmdCM(int argc, QStringList argv); + void cmdUnCM(int argc, QStringList argv); + void cmdGetAreas(int argc, QStringList argv); + void cmdGetArea(int argc, QStringList argv); + void cmdSetBackground(int argc, QStringList argv); + void cmdBgLock(int argc, QStringList argv); + void cmdBgUnlock(int argc, QStringList argv); + // Moderation + void cmdBan(int argc, QStringList argv); + void cmdKick(int argc, QStringList argv); + // Casing/RP void cmdNeed(int argc, QStringList argv); void cmdFlip(int argc, QStringList argv); + void cmdDoc(int argc, QStringList argv); + void cmdClearDoc(int argc, QStringList argv); + // Messaging/Client + void cmdPos(int argc, QStringList argv); + void cmdG(int argc, QStringList argv); // Command helper functions QStringList buildAreaList(int area_idx); @@ -209,7 +220,11 @@ class AOClient : public QObject { {"pos", {ACLFlags.value("NONE"), 1, &AOClient::cmdPos}}, {"g", {ACLFlags.value("NONE"), 1, &AOClient::cmdG}}, {"need", {ACLFlags.value("NONE"), 1, &AOClient::cmdNeed}}, - {"flip", {ACLFlags.value("NONE"), 0, &AOClient::cmdFlip}} + {"flip", {ACLFlags.value("NONE"), 0, &AOClient::cmdFlip}}, + {"doc", {ACLFlags.value("NONE"), 0, &AOClient::cmdDoc}}, + {"cleardoc", {ACLFlags.value("NONE"), 0, &AOClient::cmdClearDoc}}, + {"cm", {ACLFlags.value("NONE"), 0, &AOClient::cmdCM}}, + {"uncm", {ACLFlags.value("NONE"), 0, &AOClient::cmdUnCM}} }; QString partial_packet; diff --git a/include/area_data.h b/include/area_data.h index 175b595..cf0994f 100644 --- a/include/area_data.h +++ b/include/area_data.h @@ -42,13 +42,19 @@ class AreaData { QList evidence; int player_count; QString status; - QString current_cm; - bool locked; + QList owners; + enum LockStatus { + FREE, + LOCKED, + SPECTATABLE + }; + LockStatus locked; QString background; bool showname_allowed; bool locking_allowed; bool iniswap_allowed; bool bg_locked; + QString document; int def_hp; int pro_hp; Logger* logger; diff --git a/include/server.h b/include/server.h index 4e7d823..38a97e1 100644 --- a/include/server.h +++ b/include/server.h @@ -46,6 +46,7 @@ class Server : public QObject { void start(); AOClient* getClient(QString ipid); + AOClient* getClientByID(int id); void updateCharsTaken(AreaData* area); void broadcast(AOPacket packet, int area_index); void broadcast(AOPacket packet); diff --git a/src/aoclient.cpp b/src/aoclient.cpp index 1e10adc..7ce234d 100644 --- a/src/aoclient.cpp +++ b/src/aoclient.cpp @@ -17,11 +17,12 @@ ////////////////////////////////////////////////////////////////////////////////////// #include "include/aoclient.h" -AOClient::AOClient(Server* p_server, QTcpSocket* p_socket, QObject* parent) +AOClient::AOClient(Server* p_server, QTcpSocket* p_socket, QObject* parent, int user_id) : QObject(parent) { socket = p_socket; server = p_server; + id = user_id; joined = false; password = ""; current_area = 0; @@ -155,8 +156,17 @@ void AOClient::arup(ARUPType type, bool broadcast) arup_data.append(area->status); } else if (type == ARUPType::CM) { - arup_data.append(area->current_cm); - } + if (area->owners.isEmpty()) + arup_data.append("FREE"); + else { + QStringList area_owners; + for (int owner_id : area->owners) { + AOClient* owner = server->getClientByID(owner_id); + area_owners.append("[" + QString::number(owner->id) + "] " + owner->current_char); + } + arup_data.append(area_owners.join(", ")); + } + } else if (type == ARUPType::LOCKED) { arup_data.append(area->locked ? "LOCKED" : "FREE"); } diff --git a/src/area_data.cpp b/src/area_data.cpp index 63b57aa..b6916e9 100644 --- a/src/area_data.cpp +++ b/src/area_data.cpp @@ -29,12 +29,12 @@ AreaData::AreaData(QStringList characters, QString p_name, int p_index) background = areas_ini.value("background", "gs4").toString(); areas_ini.endGroup(); player_count = 0; - current_cm = "FREE"; - locked = false; + locked = FREE; status = "FREE"; def_hp = 10; pro_hp = 10; bg_locked = false; + document = "No document."; QSettings config_ini("config/config.ini", QSettings::IniFormat); config_ini.beginGroup("Options"); int log_size = config_ini.value("logbuffer", 50).toInt(); diff --git a/src/commands.cpp b/src/commands.cpp index aa84f9f..0232d13 100644 --- a/src/commands.cpp +++ b/src/commands.cpp @@ -394,6 +394,71 @@ void AOClient::cmdFlip(int argc, QStringList argv) sendServerMessage(sender_name + " flipped a coin and got " + face + "."); } +void AOClient::cmdDoc(int argc, QStringList argv) +{ + QString sender_name = ooc_name; + AreaData* area = server->areas[current_area]; + if (argc == 0) { + sendServerMessage("Document: " + area->document); + } + else { + area->document = argv.join(" "); + sendServerMessage(sender_name + " changed the document."); // broadcast this! + } +} + +void AOClient::cmdClearDoc(int argc, QStringList argv) +{ + QString sender_name = ooc_name; + AreaData* area = server->areas[current_area]; + area->document = "No document."; + sendServerMessage(sender_name + " cleared the document."); // broadcast this! +} + +void AOClient::cmdCM(int argc, QStringList argv) +{ + QString sender_name = ooc_name; + AreaData* area = server->areas[current_area]; + if (area->owners.isEmpty()) { + area->owners.append(id); + sendServerMessage(sender_name + " is now CM in this area."); // broadcast this! + arup(ARUPType::CM, true); + } + else if (!area->owners.contains(id)) { + sendServerMessage("You cannot become a CM in this area."); + } + else if (argc == 1) { + bool ok; + AOClient* owner_candidate = server->getClientByID(argv[0].toInt(&ok)); + if (!ok) { + sendServerMessage("That doesn't look like a valid ID."); + return; + } + if (owner_candidate == nullptr) { + sendServerMessage("Unable to find client with ID " + argv[0] + "."); + return; + } + area->owners.append(owner_candidate->id); + sendServerMessage(owner_candidate->ooc_name + " is now CM in this area."); // broadcast this! + arup(ARUPType::CM, true); + } + else { + sendServerMessage("You are already a CM in this area."); + } +} +void AOClient::cmdUnCM(int argc, QStringList argv) +{ + AreaData* area = server->areas[current_area]; + int removed = area->owners.removeAll(id); + if (removed == 0) + sendServerMessage("You are not a CM in this area."); + else { + sendServerMessage("You are no longer CM in this area."); + arup(ARUPType::CM, true); + } +} + + QStringList AOClient::buildAreaList(int area_idx) { QStringList entries; @@ -403,9 +468,9 @@ QStringList AOClient::buildAreaList(int area_idx) entries.append("[" + QString::number(area->player_count) + " users][" + area->status + "]"); for (AOClient* client : server->clients) { if (client->current_area == area_idx && client->joined) { - QString char_entry = client->current_char; - if (char_entry == "") - char_entry = "Spectator"; + QString char_entry = "[" + QString::number(client->id) + "] " + client->current_char; + if (client->current_char == "") + char_entry += "Spectator"; if (authenticated) char_entry += " (" + client->getIpid() + "): " + client->ooc_name; entries.append(char_entry); diff --git a/src/packets.cpp b/src/packets.cpp index 901952c..8036462 100644 --- a/src/packets.cpp +++ b/src/packets.cpp @@ -142,6 +142,7 @@ void AOClient::pktSelectChar(AreaData* area, int argc, QStringList argv, AOPacke server->updateCharsTaken(area); sendPacket("PV", {"271828", "CID", argv[1]}); + fullArup(); } void AOClient::pktIcChat(AreaData* area, int argc, QStringList argv, AOPacket packet) diff --git a/src/server.cpp b/src/server.cpp index 690a4f2..e89e884 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -92,7 +92,18 @@ void Server::start() void Server::clientConnected() { QTcpSocket* socket = server->nextPendingConnection(); - AOClient* client = new AOClient(this, socket, this); + int user_id; + QList user_ids; + for (AOClient* client : clients) { + user_ids.append(client->id); + } + for (user_id = 0; user_id <= player_count; user_id++) { + if (user_ids.contains(user_id)) + continue; + else + break; + } + AOClient* client = new AOClient(this, socket, this, user_id); if (db_manager->isIPBanned(socket->peerAddress())) { AOPacket ban_reason("BD", {db_manager->getBanReason(socket->peerAddress())}); socket->write(ban_reason.toUtf8()); @@ -156,6 +167,15 @@ AOClient* Server::getClient(QString ipid) return nullptr; } +AOClient* Server::getClientByID(int id) +{ + for (AOClient* client : clients) { + if (client->id == id) + return client; + } + return nullptr; +} + Server::~Server() { for (AOClient* client : clients) {