Skip to content

Commit

Permalink
Courtroom% - Part 1
Browse files Browse the repository at this point in the history
We are almost there folks!
  • Loading branch information
Salanto committed Jan 16, 2024
1 parent 9964be6 commit ef744f8
Show file tree
Hide file tree
Showing 24 changed files with 241 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,8 @@ CMakeLists.txt.user*
*.dll
*.exe

# Project Files
# --------
bin/config
bin/storage

2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ add_executable(HVAC-Server
src/areas/location.h src/areas/location.cpp
src/areas/area.h src/areas/area.cpp
src/helper.h
src/banmanager.h src/banmanager.cpp
src/packet/packet_selectarea.h src/packet/packet_selectarea.cpp
)

target_include_directories(HVAC-Server
Expand Down
1 change: 1 addition & 0 deletions bin/config_sample/config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "An unnamed Server"
description "No description provided."
max_players = 100
ws_port = 50001

[coordinator]
hostname = ""
Expand Down
13 changes: 13 additions & 0 deletions bin/storage_sample/areas/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"backgrounds" : "default",
"musiclist" : "default",
"characters": "default",
"locations": [
{
"name": "Area1",
"background": "default",
"color": 123123123,
"description": ""
}
]
}
12 changes: 12 additions & 0 deletions bin/storage_sample/backgrounds/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"name": "default",
"sides": [
{
"name":"witness",
"image":"witnessempty.png",
"overlay": ""
}
]
}
]
Empty file.
9 changes: 9 additions & 0 deletions bin/storage_sample/music/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"category": "Sample Category",
"songs": [
"song.opus",
"song2.opus"
]
}
]
10 changes: 10 additions & 0 deletions src/areas/area.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,13 @@ bool Area::loadLocations(QJsonValue f_locations)

return true;
}

Location *Area::getLocationByID(const int id) const
{
if (locations.size() < id) {
qDebug() << "Unable to access Location at" << id;
qDebug() << "Location does not exist.";
return nullptr;
}
return locations.at(0);
}
2 changes: 2 additions & 0 deletions src/areas/area.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class Area : public QObject

bool loadLocations(QJsonValue f_locations);

Location *getLocationByID(const int id) const;

private:
QVector<Location *> locations; // aka areas in AO2 slang.
QStringList characters;
Expand Down
11 changes: 11 additions & 0 deletions src/areas/areamanager.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "areamanager.h"
#include "area.h"
#include "client.h"
#include "constants.h"
#include "helper.h"
#include "options.h"
#include "packet_selectarea.h"
#include "packetrelay.h"

#include <QDebug>
Expand All @@ -15,6 +17,8 @@ AreaManager::AreaManager(QObject *parent, PacketRelay *f_relay) :
QObject{parent},
relay{f_relay}
{
connect(f_relay, &PacketRelay::clientSelectArea, this, &AreaManager::onSelectAreaReceived);

QStringList l_areas = Options::areas();
for (const QString &l_area : l_areas) {
qDebug() << "Loading area" << l_area;
Expand Down Expand Up @@ -59,6 +63,13 @@ bool AreaManager::loadAreaFromFile(const QString &f_file)
return true;
}

void AreaManager::onSelectAreaReceived(Packet *f_packet, Client *f_client)
{
qDebug() << "Received SelectArea by client" << f_client->id();
PacketSelectArea *l_packet = static_cast<PacketSelectArea *>(f_packet);
Area *l_area = areas.at(l_packet->getArea());
}

QStringList AreaManager::readCharacters(const QString &f_file)
{
std::unique_ptr<QFile> l_file = Helper::openFile(Storage::character_storage + f_file + ".txt", QIODevice::ReadOnly);
Expand Down
5 changes: 5 additions & 0 deletions src/areas/areamanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include "datatypes.h"

class PacketRelay;
class Packet;
class Client;

class AreaManager : public QObject
{
Expand All @@ -17,6 +19,9 @@ class AreaManager : public QObject

bool loadAreaFromFile(const QString &f_file);

private slots:
void onSelectAreaReceived(Packet *f_packet, Client *f_client);

private:
QStringList readCharacters(const QString &f_file);
DataTypes::BackgroundList readBackgrounds(const QString &f_file);
Expand Down
11 changes: 11 additions & 0 deletions src/areas/location.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,14 @@ void Location::setBackground(const DataTypes::BackgroundInformation &f_backgroun
{
background = f_background;
}

void Location::removePlayer(int f_player)
{
players.removeAll(f_player);
}

bool Location::joinPlayer(int f_player)
{
players.append(f_player);
return true;
}
4 changes: 4 additions & 0 deletions src/areas/location.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ class Location : public QObject
DataTypes::BackgroundInformation getBackground() const;
void setBackground(const DataTypes::BackgroundInformation &f_background);

void removePlayer(int f_player);
bool joinPlayer(int f_player);

private:
int id;
QString name;
QString description;
DataTypes::BackgroundInformation background;
QString song;
QList<int> players;
};

#endif // LOCATION_H
30 changes: 30 additions & 0 deletions src/banmanager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "banmanager.h"
#include "client.h"
#include "packet_hello.h"
#include "packet_selectarea.h"
#include "packetrelay.h"

#include <QDebug>

BanManager::BanManager(QObject *parent, PacketRelay *f_relay) :
QObject{parent},
relay{f_relay}
{
qDebug() << "Creating BanManager";
connect(relay, &PacketRelay::clientHello, this, &BanManager::onHELLOReceived);
}

void BanManager::onHELLOReceived(Packet *f_packet, Client *f_client)
{
PacketHello *packet = static_cast<PacketHello *>(f_packet);
qDebug() << "Received HELLO from ClientID" << f_client->id() << "at" << f_client->getIP().toString();
qDebug() << "App-Name:" << packet->appName();
qDebug() << "App-Version:" << packet->appVersion().toString();
qDebug() << "Identifier:" << packet->hwid();
delete packet;

PacketSelectArea *l_packet = new PacketSelectArea();
l_packet->setArea(0);
l_packet->setLocation(0);
relay->routeInternalPacket(l_packet, f_client);
}
24 changes: 24 additions & 0 deletions src/banmanager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef BANMANAGER_H
#define BANMANAGER_H

#include <QObject>

class PacketRelay;
class Packet;
class Client;

class BanManager : public QObject
{
Q_OBJECT

public:
explicit BanManager(QObject *parent = nullptr, PacketRelay *f_relay = nullptr);

private slots:
void onHELLOReceived(Packet *f_packet, Client *f_client);

public:
PacketRelay *relay;
};

#endif // BANMANAGER_H
18 changes: 18 additions & 0 deletions src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,19 @@ Client::Client(QObject *parent, QWebSocket *f_socket, int f_id) :
// Clientmanager will handle cleanup.
emit socketDisconnected(this);
});

bool const l_is_local = (f_socket->peerAddress() == QHostAddress::LocalHost) ||
(f_socket->peerAddress() == QHostAddress::LocalHostIPv6) ||
(f_socket->peerAddress() == QHostAddress("::ffff:127.0.0.1"));
// TLDR : We check if the header comes trough a proxy/tunnel running locally.
// This is to ensure nobody can send those headers from the web.
QNetworkRequest l_request = f_socket->request();
if (l_request.hasRawHeader("x-forwarded-for") && l_is_local) {
socket_ip = QHostAddress(QString::fromUtf8(l_request.rawHeader("x-forwarded-for")));
}
else {
socket_ip = f_socket->peerAddress();
}
}

Client::~Client()
Expand All @@ -27,3 +40,8 @@ void Client::write(const QByteArray f_data)
{
m_socket->sendBinaryMessage(f_data);
}

QHostAddress Client::getIP() const
{
return socket_ip;
}
2 changes: 2 additions & 0 deletions src/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class Client : public QObject

void write(const QByteArray f_data);
int id() { return m_id; }
QHostAddress getIP() const;

signals:
void networkDataReceived(const QByteArray &f_data, Client *f_client);
Expand All @@ -24,6 +25,7 @@ class Client : public QObject
private:
int m_id;
QWebSocket *m_socket;
QHostAddress socket_ip;
};

#endif // CLIENT_H
39 changes: 39 additions & 0 deletions src/packet/packet_selectarea.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "packet_selectarea.h"

#include "QJsonObject"

bool PacketSelectArea::fromJsonValue(const QJsonValue &f_in)
{
if (!f_in.isObject()) {
return false;
}
QJsonObject l_data = f_in.toObject();
area = l_data["area"].toInt(0);
location = l_data["location"].toInt(0);
return true;
}

QString PacketSelectArea::header() const
{
return "SELECT_AREA";
}

int PacketSelectArea::getArea() const
{
return area;
}

int PacketSelectArea::getLocation() const
{
return location;
}

void PacketSelectArea::setArea(const int f_area)
{
area = f_area;
}

void PacketSelectArea::setLocation(const int f_location)
{
location = f_location;
}
22 changes: 22 additions & 0 deletions src/packet/packet_selectarea.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef PACKETSELECTAREA_H
#define PACKETSELECTAREA_H

#include "packet.h"

class PacketSelectArea : public Packet
{
public:
PacketSelectArea() = default;
bool fromJsonValue(const QJsonValue &f_in) override;
QString header() const override;
int getArea() const;
int getLocation() const;
void setArea(const int f_area);
void setLocation(const int f_location);

private:
int area;
int location;
};

#endif // PACKETSELECTAREA_H
2 changes: 2 additions & 0 deletions src/packet/packetfactory.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "packetfactory.h"
#include "packet_generic.h"
#include "packet_hello.h"
#include "packet_selectarea.h"
#include "packet_selectcharacter.h"

#include <QJsonDocument>
Expand Down Expand Up @@ -49,4 +50,5 @@ void PacketFactory::registerPackets()
{
registerPacket<PacketHello>("HELLO");
registerPacket<PacketSelectCharacter>("SELECT_CHARACTER");
registerPacket<PacketSelectArea>("SELECT_AREA");
}
14 changes: 12 additions & 2 deletions src/packetrelay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@ PacketRelay::PacketRelay(QObject *parent) :
QObject{parent}
{
PacketFactory::registerPackets();
routes["HELLO"] = &PacketRelay::softwareInformation;
routes["HELLO"] = &PacketRelay::clientHello;
routes["SELECT_AREA"] = &PacketRelay::clientSelectArea;
}

void PacketRelay::routeInternalPacket(Packet *f_packet, Client *f_client)
{
const QString l_header = f_packet->header();
qDebug() << "Routing internal packet for" << f_client->id() << "at" << f_client->getIP();
if (canRoutePacket(l_header)) {
emit(this->*routes[l_header])(f_packet, f_client);
return;
}
}

void PacketRelay::packetReceived(QByteArray f_data, Client *f_client)
Expand All @@ -16,7 +27,6 @@ void PacketRelay::packetReceived(QByteArray f_data, Client *f_client)
QString l_header = f_packet->header();

if (canRoutePacket(l_header)) {
qDebug() << "Routing packet on route" << l_header;
emit(this->*routes[l_header])(f_packet, f_client);
return;
}
Expand Down
4 changes: 3 additions & 1 deletion src/packetrelay.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class PacketRelay : public QObject

public:
explicit PacketRelay(QObject *parent = nullptr);
void routeInternalPacket(Packet *f_packet, Client *f_client);

public slots:
void packetReceived(QByteArray f_data, Client *f_client);
Expand All @@ -26,7 +27,8 @@ class PacketRelay : public QObject
// Sends the same packet to all clients.
void broadcastSend(const QByteArray f_data);

void softwareInformation(Packet *f_data, Client *f_client);
void clientHello(Packet *f_data, Client *f_client);
void clientSelectArea(Packet *f_data, Client *f_client);

private:
bool canRoutePacket(QString f_header);
Expand Down
Loading

0 comments on commit ef744f8

Please sign in to comment.