update room list

This commit is contained in:
Notify-ctrl 2022-03-01 17:40:02 +08:00
parent 5f175090c0
commit 9ed3f1bf3f
8 changed files with 84 additions and 31 deletions

View File

@ -12,18 +12,10 @@ function Client:initialize()
if (type(cb) == "function") then if (type(cb) == "function") then
cb(json_data) cb(json_data)
else else
self:notifyUI("error_msg", "Unknown command " .. command); self:notifyUI(command, json_data);
end end
end end
end end
freekill.client_callback["enter_lobby"] = function(json_data)
ClientInstance:notifyUI("enter_lobby", json_data)
end
freekill.client_callback["enter_room"] = function(json_data)
ClientInstance:notifyUI("enter_room", json_data)
end
-- Create ClientInstance (used by Lua) -- Create ClientInstance (used by Lua)
ClientInstance = Client:new() ClientInstance = Client:new()

View File

@ -5,7 +5,7 @@ import QtQuick.Layouts 1.15
Item { Item {
id: root id: root
width: 640; height: 480 property alias roomModel: roomModel
Component { Component {
id: roomDelegate id: roomDelegate

View File

@ -22,6 +22,13 @@ Window {
"enter_room": function(json_data) { "enter_room": function(json_data) {
mainStack.push(room); mainStack.push(room);
mainWindow.busy = false; mainWindow.busy = false;
},
"update_room_list": function(json_data) {
let current = mainStack.currentItem; // should be lobby
current.roomModel.clear();
JSON.parse(json_data).forEach(function(room) {
current.roomModel.append(room);
});
} }
}) })
@ -108,7 +115,7 @@ Window {
if (typeof(cb) === "function") { if (typeof(cb) === "function") {
cb(json_data); cb(json_data);
} else { } else {
callbacks["error_msg"]("Unknown UI command " + command + "!"); callbacks["error_msg"]("Unknown command " + command + "!");
} }
} }
} }

View File

@ -77,7 +77,7 @@ void Room::setOwner(ServerPlayer *owner)
void Room::addPlayer(ServerPlayer *player) void Room::addPlayer(ServerPlayer *player)
{ {
if (!player) return; if (!player) return;
players.insert(player->getUid(), player); players.append(player);
player->setRoom(this); player->setRoom(this);
if (isLobby()) { if (isLobby()) {
player->doNotify("enter_lobby", "{}"); player->doNotify("enter_lobby", "{}");
@ -90,18 +90,31 @@ void Room::addPlayer(ServerPlayer *player)
void Room::removePlayer(ServerPlayer *player) void Room::removePlayer(ServerPlayer *player)
{ {
players.remove(player->getId()); players.removeOne(player);
emit playerRemoved(player); emit playerRemoved(player);
if (isLobby()) return;
if (isAbandoned()) {
emit abandoned();
} else if (player == owner) {
setOwner(players.first());
owner->doNotify("room_owner", "{}");
}
} }
QHash<uint, ServerPlayer *> Room::getPlayers() const QList<ServerPlayer *> Room::getPlayers() const
{ {
return players; return players;
} }
ServerPlayer *Room::findPlayer(uint id) const ServerPlayer *Room::findPlayer(uint id) const
{ {
return players.value(id); foreach (ServerPlayer *p, players) {
if (p->getId() == id)
return p;
}
return nullptr;
} }
void Room::setGameLogic(GameLogic *logic) void Room::setGameLogic(GameLogic *logic)
@ -129,6 +142,15 @@ void Room::doNotify(const QList<ServerPlayer *> targets, int timeout)
// TODO // TODO
} }
void Room::doBroadcastNotify(const QList<ServerPlayer *> targets,
const QString& command, const QString& json_data)
{
foreach (ServerPlayer *p, targets) {
p->doNotify(command, json_data);
}
}
void Room::run() void Room::run()
{ {
// TODO // TODO

View File

@ -2,7 +2,7 @@
#define _ROOM_H #define _ROOM_H
#include <QThread> #include <QThread>
#include <QHash> #include <QList>
class Server; class Server;
class ServerPlayer; class ServerPlayer;
class GameLogic; class GameLogic;
@ -30,7 +30,7 @@ public:
void addPlayer(ServerPlayer *player); void addPlayer(ServerPlayer *player);
void removePlayer(ServerPlayer *player); void removePlayer(ServerPlayer *player);
QHash<uint, ServerPlayer*> getPlayers() const; QList<ServerPlayer*> getPlayers() const;
ServerPlayer *findPlayer(uint id) const; ServerPlayer *findPlayer(uint id) const;
void setGameLogic(GameLogic *logic); void setGameLogic(GameLogic *logic);
@ -41,6 +41,12 @@ public:
void doRequest(const QList<ServerPlayer *> targets, int timeout); void doRequest(const QList<ServerPlayer *> targets, int timeout);
void doNotify(const QList<ServerPlayer *> targets, int timeout); void doNotify(const QList<ServerPlayer *> targets, int timeout);
void doBroadcastNotify(
const QList<ServerPlayer *> targets,
const QString &command,
const QString &json_data
);
signals: signals:
void abandoned(); void abandoned();
@ -62,7 +68,7 @@ private:
bool m_abandoned; // If room is empty, delete it bool m_abandoned; // If room is empty, delete it
ServerPlayer *owner; // who created this room? ServerPlayer *owner; // who created this room?
QHash<uint, ServerPlayer *> players; QList<ServerPlayer *> players;
GameLogic *logic; GameLogic *logic;
}; };

View File

@ -4,6 +4,9 @@
#include "room.h" #include "room.h"
#include "serverplayer.h" #include "serverplayer.h"
#include "global.h" #include "global.h"
#include <QJsonArray>
#include <QJsonDocument>
#include <QJsonObject>
Server *ServerInstance; Server *ServerInstance;
@ -43,7 +46,10 @@ void Server::createRoom(ServerPlayer* owner, const QString &name, uint capacity)
room->setCapacity(capacity); room->setCapacity(capacity);
room->setOwner(owner); room->setOwner(owner);
room->addPlayer(owner); room->addPlayer(owner);
rooms.insert(room->getId(), room); if (room->isLobby())
m_lobby = room;
else
rooms.insert(room->getId(), room);
#ifdef QT_DEBUG #ifdef QT_DEBUG
qDebug() << "Room #" << room->getId() << " created."; qDebug() << "Room #" << room->getId() << " created.";
#endif #endif
@ -56,7 +62,7 @@ Room *Server::findRoom(uint id) const
Room *Server::lobby() const Room *Server::lobby() const
{ {
return findRoom(0); return m_lobby;
} }
ServerPlayer *Server::findPlayer(uint id) const ServerPlayer *Server::findPlayer(uint id) const
@ -64,9 +70,23 @@ ServerPlayer *Server::findPlayer(uint id) const
return players.value(id); return players.value(id);
} }
void Server::updateRoomList(ServerPlayer* user) void Server::updateRoomList()
{ {
// TODO QJsonArray arr;
foreach (Room *room, rooms) {
QJsonObject obj;
obj["roomId"] = (int)room->getId();
obj["roomName"] = room->getName();
obj["gameMode"] = "Role";
obj["playerNum"] = room->getPlayers().count();
obj["capacity"] = (int)room->getCapacity();
arr << obj;
}
lobby()->doBroadcastNotify(
lobby()->getPlayers(),
"update_room_list",
QJsonDocument(arr).toJson()
);
} }
void Server::processNewConnection(ClientSocket* client) void Server::processNewConnection(ClientSocket* client)
@ -86,7 +106,10 @@ void Server::processNewConnection(ClientSocket* client)
void Server::onRoomAbandoned() void Server::onRoomAbandoned()
{ {
// TODO Room *room = qobject_cast<Room *>(sender());
rooms.remove(room->getId());
updateRoomList();
room->deleteLater();
} }
void Server::onUserDisconnected() void Server::onUserDisconnected()

View File

@ -3,6 +3,7 @@
#include <QObject> #include <QObject>
#include <QHash> #include <QHash>
#include <QMap>
#include <QHostAddress> #include <QHostAddress>
#include <lua.hpp> #include <lua.hpp>
@ -28,7 +29,7 @@ public:
ServerPlayer *findPlayer(uint id) const; ServerPlayer *findPlayer(uint id) const;
void updateRoomList(ServerPlayer *user); void updateRoomList();
void callLua(const QString &command, const QString &json_data); void callLua(const QString &command, const QString &json_data);
LuaFunction callback; LuaFunction callback;
@ -47,7 +48,8 @@ public slots:
private: private:
ServerSocket *server; ServerSocket *server;
QHash<uint, Room *> rooms; Room *m_lobby;
QMap<uint, Room *> rooms;
QHash<uint, ServerPlayer *> players; QHash<uint, ServerPlayer *> players;
lua_State *L; lua_State *L;

View File

@ -41,12 +41,13 @@ SWIG_arg ++;
%{ lua_pushstring(L, $1.toUtf8()); SWIG_arg++; %} %{ lua_pushstring(L, $1.toUtf8()); SWIG_arg++; %}
// const QString & // const QString &
%typemap(arginit) QString const &
"QString $1_str;"
%typemap(in, checkfn = "lua_isstring") QString const & %typemap(in, checkfn = "lua_isstring") QString const &
%{ %{
if (1) { // to avoid 'Jump bypasses variable initialization' error $1_str = QString::fromUtf8(lua_tostring(L, $input));
QString $1_str = QString::fromUtf8(lua_tostring(L, $input)); $1 = &$1_str;
$1 = &$1_str;
}
%} %}
%typemap(out) QString const & %typemap(out) QString const &
@ -114,7 +115,7 @@ public:
ServerPlayer *findPlayer(unsigned int id) const; ServerPlayer *findPlayer(unsigned int id) const;
void updateRoomList(ServerPlayer *user); void updateRoomList();
LuaFunction callback; LuaFunction callback;
}; };
@ -219,7 +220,7 @@ public:
void addPlayer(ServerPlayer *player); void addPlayer(ServerPlayer *player);
void removePlayer(ServerPlayer *player); void removePlayer(ServerPlayer *player);
QHash<unsigned int, ServerPlayer*> getPlayers() const; QList<ServerPlayer *> getPlayers() const;
ServerPlayer *findPlayer(unsigned int id) const; ServerPlayer *findPlayer(unsigned int id) const;
void setGameLogic(GameLogic *logic); void setGameLogic(GameLogic *logic);