2022-01-24 02:23:08 +00:00
|
|
|
#include "client.h"
|
|
|
|
#include "client_socket.h"
|
|
|
|
#include "clientplayer.h"
|
2022-03-27 06:49:41 +00:00
|
|
|
#include "util.h"
|
2022-01-24 02:23:08 +00:00
|
|
|
|
|
|
|
Client *ClientInstance;
|
|
|
|
ClientPlayer *Self;
|
|
|
|
|
|
|
|
Client::Client(QObject* parent)
|
2022-04-30 07:27:56 +00:00
|
|
|
: QObject(parent), callback(0)
|
2022-01-24 02:23:08 +00:00
|
|
|
{
|
2022-04-30 07:27:56 +00:00
|
|
|
ClientInstance = this;
|
|
|
|
Self = new ClientPlayer(0, this);
|
|
|
|
QQmlApplicationEngine *engine = Backend->getEngine();
|
|
|
|
engine->rootContext()->setContextProperty("ClientInstance", ClientInstance);
|
|
|
|
engine->rootContext()->setContextProperty("Self", Self);
|
2022-01-24 02:23:08 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
ClientSocket *socket = new ClientSocket;
|
|
|
|
connect(socket, &ClientSocket::error_message, this, &Client::error_message);
|
|
|
|
router = new Router(this, socket, Router::TYPE_CLIENT);
|
2022-01-24 02:23:08 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
L = CreateLuaState();
|
|
|
|
DoLuaScript(L, "lua/freekill.lua");
|
|
|
|
DoLuaScript(L, "lua/client/client.lua");
|
2022-01-24 02:23:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Client::~Client()
|
|
|
|
{
|
2022-04-30 07:27:56 +00:00
|
|
|
ClientInstance = nullptr;
|
|
|
|
lua_close(L);
|
|
|
|
router->getSocket()->disconnectFromHost();
|
|
|
|
router->getSocket()->deleteLater();
|
2022-01-24 02:23:08 +00:00
|
|
|
}
|
|
|
|
|
2022-12-18 04:52:52 +00:00
|
|
|
void Client::connectToHost(const QString &server, ushort port)
|
2022-01-24 02:23:08 +00:00
|
|
|
{
|
2022-04-30 07:27:56 +00:00
|
|
|
router->getSocket()->connectToHost(server, port);
|
2022-01-24 02:23:08 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 12:56:37 +00:00
|
|
|
void Client::replyToServer(const QString& command, const QString& jsonData)
|
2022-01-24 02:23:08 +00:00
|
|
|
{
|
2022-04-30 07:27:56 +00:00
|
|
|
int type = Router::TYPE_REPLY | Router::SRC_CLIENT | Router::DEST_SERVER;
|
|
|
|
router->reply(type, command, jsonData);
|
2022-01-24 02:23:08 +00:00
|
|
|
}
|
|
|
|
|
2022-03-02 12:56:37 +00:00
|
|
|
void Client::notifyServer(const QString& command, const QString& jsonData)
|
2022-01-24 02:23:08 +00:00
|
|
|
{
|
2022-04-30 07:27:56 +00:00
|
|
|
int type = Router::TYPE_NOTIFICATION | Router::SRC_CLIENT | Router::DEST_SERVER;
|
|
|
|
router->notify(type, command, jsonData);
|
2022-01-24 02:23:08 +00:00
|
|
|
}
|
2022-03-28 14:24:30 +00:00
|
|
|
|
|
|
|
ClientPlayer *Client::addPlayer(int id, const QString &name, const QString &avatar) {
|
2022-04-30 07:27:56 +00:00
|
|
|
ClientPlayer *player = new ClientPlayer(id);
|
|
|
|
player->setScreenName(name);
|
|
|
|
player->setAvatar(avatar);
|
2022-03-28 14:24:30 +00:00
|
|
|
|
2022-04-30 07:27:56 +00:00
|
|
|
players[id] = player;
|
|
|
|
return player;
|
2022-03-28 14:24:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Client::removePlayer(int id) {
|
2022-04-30 07:27:56 +00:00
|
|
|
ClientPlayer *p = players[id];
|
|
|
|
p->deleteLater();
|
|
|
|
players[id] = nullptr;
|
2022-03-28 14:24:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Client::clearPlayers() {
|
2022-04-30 07:27:56 +00:00
|
|
|
players.clear();
|
2022-03-28 14:24:30 +00:00
|
|
|
}
|
2022-04-02 07:55:01 +00:00
|
|
|
|
|
|
|
lua_State *Client::getLuaState() {
|
2022-04-30 07:27:56 +00:00
|
|
|
return L;
|
2022-04-02 07:55:01 +00:00
|
|
|
}
|