Skip to content

Commit

Permalink
[ChatCommandHandler] '/stop' command added.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jun 19, 2020
1 parent b1ed51f commit c1176a4
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 13 deletions.
5 changes: 2 additions & 3 deletions source/client/network/ClientCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,8 @@ void ClientCommandHandler::setupCallbacks() {
std::string message;
packet >> message;

gk::ApplicationStateStack::getInstance().pop();
gk::ApplicationStateStack::getInstance().pop();
gk::ApplicationStateStack::getInstance().push<ConnectionErrorState>(message, m_client.serverAddress().toString(), m_client.serverPort(), m_client.texturePack(), &gk::ApplicationStateStack::getInstance().top());
gk::ApplicationStateStack::getInstance().clear();
gk::ApplicationStateStack::getInstance().push<ConnectionErrorState>(message, m_client.serverAddress().toString(), m_client.serverPort(), m_client.texturePack(), nullptr);
});

m_client.setCommandCallback(Network::Command::RegistryData, [this](Network::Packet &packet) {
Expand Down
18 changes: 14 additions & 4 deletions source/client/states/ConnectionErrorState.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@
* =====================================================================================
*/
#include <gk/core/ApplicationStateStack.hpp>
#include <gk/core/Mouse.hpp>

#include "Config.hpp"
#include "ConnectionErrorState.hpp"
#include "GameState.hpp"
#include "ServerLoadingState.hpp"
#include "TitleScreenState.hpp"

ConnectionErrorState::ConnectionErrorState(const std::string &error, const std::string &host, u16 port, const std::string &texturePack, gk::ApplicationState *parent) : InterfaceState(parent) {
gk::Mouse::setCursorGrabbed(false);
gk::Mouse::setCursorVisible(true);

m_host = host;
m_port = port;

Expand All @@ -55,7 +60,13 @@ ConnectionErrorState::ConnectionErrorState(const std::string &error, const std::
m_cancelButton.setText("Cancel");
m_cancelButton.setScale(Config::guiScale, Config::guiScale);
m_cancelButton.setCallback([this](TextButton &) {
m_stateStack->pop();
if (m_stateStack->size() > 1) {
m_stateStack->pop();
}
else {
m_stateStack->pop();
m_stateStack->push<TitleScreenState>(m_port).setTexturePack(m_texturePack);
}
});

updateWidgetPosition();
Expand Down Expand Up @@ -83,12 +94,11 @@ void ConnectionErrorState::updateWidgetPosition() {
}

void ConnectionErrorState::draw(gk::RenderTarget &target, gk::RenderStates states) const {
if (m_parent)
target.draw(*m_parent, states);

if (&m_stateStack->top() == this) {
prepareDraw(target, states);

target.draw(m_background, states);

target.draw(m_text, states);

target.draw(m_reconnectButton, states);
Expand Down
2 changes: 2 additions & 0 deletions source/client/states/ConnectionErrorState.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class ConnectionErrorState : public InterfaceState {

TextButton m_reconnectButton;
TextButton m_cancelButton;

gk::Image m_background{"texture-title_screen"};
};

#endif // CONNECTIONERRORSTATE_HPP_
2 changes: 2 additions & 0 deletions source/server/core/ServerApplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,8 @@ int ServerApplication::run(bool isProtected) {
mainLoop();
}

gkInfo() << "Stopping server...";

m_serverCommandHandler.sendServerClosed("Server closed.");

m_registry.clear();
Expand Down
6 changes: 6 additions & 0 deletions source/server/network/ChatCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void ChatCommandHandler::parseCommand(const std::string &str, ClientInfo &client
{"tp", &ChatCommandHandler::teleportationCommand},
{"save", &ChatCommandHandler::saveCommand},
{"load", &ChatCommandHandler::loadCommand},
{"stop", &ChatCommandHandler::stopCommand},
};

if (!command.empty()) {
Expand Down Expand Up @@ -111,3 +112,8 @@ void ChatCommandHandler::loadCommand(const std::vector<std::string> &command, Cl
}
}

void ChatCommandHandler::stopCommand(const std::vector<std::string> &, ClientInfo &client) const {
m_server.sendChatMessage(0, "Stopping server...", &client);
m_server.stopServer();
}

1 change: 1 addition & 0 deletions source/server/network/ChatCommandHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class ChatCommandHandler {
void teleportationCommand(const std::vector<std::string> &command, ClientInfo &client) const;
void saveCommand(const std::vector<std::string> &command, ClientInfo &client) const;
void loadCommand(const std::vector<std::string> &command, ClientInfo &client) const;
void stopCommand(const std::vector<std::string> &command, ClientInfo &client) const;

ServerCommandHandler &m_server;
WorldController &m_worldController;
Expand Down
4 changes: 4 additions & 0 deletions source/server/network/ServerCommandHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ inline ServerWorld &ServerCommandHandler::getWorldForClient(u16 clientID) {
return m_worldController.getWorld(player->dimension());
}

void ServerCommandHandler::stopServer() const {
m_server.setRunning(false);
}

// Please update 'docs/lua-api-cpp.md' if you change this
void ServerCommandHandler::initUsertype(sol::state &lua) {
lua.new_usertype<ServerCommandHandler>("ServerCommandHandler",
Expand Down
2 changes: 2 additions & 0 deletions source/server/network/ServerCommandHandler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ class ServerCommandHandler {

void setPlayerPosition(u16 clientID, s32 x, s32 y, s32 z);

void stopServer() const;

const Server &server() const { return m_server; }

static void initUsertype(sol::state &lua);
Expand Down
12 changes: 6 additions & 6 deletions source/server/world/WorldController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void WorldController::update() {
}

void WorldController::load(const std::string &name) {
gkDebug() << ("Loading '" + name + "'...").c_str();
gkInfo() << ("Loading '" + name + "'...").c_str();

std::ifstream file("saves/" + name + ".dat", std::ofstream::binary);

Expand All @@ -69,7 +69,7 @@ void WorldController::load(const std::string &name) {
unsigned int chunkCount;
save >> chunkCount;

gkDebug() << "Loading dimension" << world.dimension().id() << "| Chunk count:" << chunkCount;
gkInfo() << "Loading dimension" << world.dimension().id() << "| Chunk count:" << chunkCount;

for (unsigned int i = 0 ; i < chunkCount ; ++i) {
int cx, cy, cz;
Expand All @@ -96,11 +96,11 @@ void WorldController::load(const std::string &name) {
}
}

gkDebug() << "Loading done.";
gkInfo() << "Loading done.";
}

void WorldController::save(const std::string &name) {
gkDebug() << ("Saving '" + name + "'...").c_str();
gkInfo() << ("Saving '" + name + "'...").c_str();

std::filesystem::create_directory("saves");

Expand Down Expand Up @@ -129,13 +129,13 @@ void WorldController::save(const std::string &name) {
++chunkCount;
}

gkDebug() << "Saving dimension" << world.dimension().id() << "| Chunk count:" << chunkCount;
gkInfo() << "Saving dimension" << world.dimension().id() << "| Chunk count:" << chunkCount;

save << chunkCount;
save.append(chunks.getData(), chunks.getDataSize());
}

file.write((const char *)save.getData(), save.getDataSize());

gkDebug() << "Saving done.";
gkInfo() << "Saving done.";
}

0 comments on commit c1176a4

Please sign in to comment.