Skip to content

Commit

Permalink
Pretty-print leaving players
Browse files Browse the repository at this point in the history
  • Loading branch information
vimpostor committed Jan 13, 2024
1 parent cb24bd0 commit fefa7fb
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/network/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ void Server::newConnection() {
connect(s, &QTcpSocket::errorOccurred, this, &Server::socketError);
connect(s, &QTcpSocket::disconnected, this, &Server::socketDisconnect);
connect(s, &QTcpSocket::readyRead, this, &Server::socketReadyRead);

// the username is not sent yet, so we cannot pretty print the name yet
broadcastChatMessage(s->peerAddress().toString() + " joined");
broadcastSettings();
}
Expand Down Expand Up @@ -218,7 +220,7 @@ void Server::removePlayer(const QTcpSocket *s) {
}
// remove from player model
PlayerModel::get()->removeCurver(c);
broadcastChatMessage(s->peerAddress().toString() + " left the game");
broadcastChatMessage(curverNetworkName(s, c) + " left the game");
}
}

Expand Down Expand Up @@ -323,6 +325,16 @@ int Server::getCurverIndex(const FullNetworkAddress peer) {
return -1;
}

/**
* @brief Returns a pretty-printed name for a network user
* @param s The socket corresponding to the user
* @param curver The curver corresponding to the user
* @return A pretty-printed name
*/
QString Server::curverNetworkName(const QTcpSocket *s, const Curver *curver) {
return QString("%1 (%2)").arg(s->peerAddress().toString()).arg(curver->userName);
}

/**
* @brief Returns the Curver connected to a given socket
* @param s The socket to return the Curver of
Expand Down
1 change: 1 addition & 0 deletions src/network/server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ private slots:
void handlePacket(std::unique_ptr<Packet::AbstractPacket> &p, const QTcpSocket *s = nullptr, FullNetworkAddress sender = {});
void broadcastPacket(Packet::AbstractPacket &p, bool udp = false);
int getCurverIndex(const FullNetworkAddress peer);
QString curverNetworkName(const QTcpSocket *s, const Curver *curver);
/**
* @brief The server instance that handles every incoming connection
*/
Expand Down

0 comments on commit fefa7fb

Please sign in to comment.