Reimplement local server and add a temporary function to start it from the main menu

master
Nicole Collings 2020-01-14 16:46:34 -08:00
parent 92a2dc3c0f
commit 8065520c71
9 changed files with 62 additions and 55 deletions

View File

@ -108,11 +108,6 @@ int StartGame(int argc, char* argv[]) {
Client c(path, addr, {1366, 768});
break;
}
// case Mode::LOCAL_SERVER: {
// auto localServer = std::make_unique<LocalServerInstance>(path, addr.port);
// Client c(std::move(localServer), {1366, 768});
// break;
// }
case Mode::SERVER: {
Server s(path, addr.port, subgame);
break;

View File

@ -7,7 +7,8 @@
Client::Client(const std::string& path, const Address &addr, glm::ivec2 dims) :
state(path.substr(0, path.find_last_of('/') + 1), renderer),
renderer(dims),
addr(addr) {
addr(addr),
executablePath(path) {
std::cout << Log::info << "Starting Zepha Client." << Log::endl;
@ -21,6 +22,12 @@ Client::Client(const std::string& path, const Address &addr, glm::ivec2 dims) :
void Client::loop() {
Timer t("Client Loop");
if (state.desiredState == "local") {
state.desiredState = "connect";
localServer = std::make_unique<LocalServerInstance>(executablePath, addr.port, state.subgame);
localServer->start();
}
if (state.desiredState == "connect") {
state.desiredState = "this";
std::unique_ptr<Scene> scene = std::make_unique<ConnectScene>(state, addr);
@ -47,4 +54,7 @@ void Client::loop() {
Client::~Client() {
sceneManager.cleanupScene();
if (localServer) localServer->stop();
//TODO: Kill local server
}

View File

@ -5,27 +5,30 @@
#pragma once
#include "ClientState.h"
#include "../util/Timer.h"
#include "graph/Renderer.h"
#include "graph/scene/SceneManager.h"
#include "scene/MainMenuScene.h"
#include "scene/ConnectScene.h"
#include "scene/GameScene.h"
#include "scene/LuaErrorScene.h"
#include "../util/Timer.h"
#include "../server/LocalServerInstance.h"
class Client {
public:
Client(const std::string& path, const Address& addr, glm::ivec2 dims);
~Client();
private:
void loop();
std::string executablePath;
Address addr {};
Renderer renderer;
ClientState state;
SceneManager sceneManager;
std::unique_ptr<LocalServerInstance> localServer = nullptr;
double timeElapsed = 0.0f;
};

View File

@ -14,6 +14,7 @@ public:
ClientState(const std::string& path, Renderer& renderer);
std::string path;
std::string subgame;
Renderer& renderer;
ServerConnection connection {};

View File

@ -36,15 +36,6 @@ void AnimationState::update(double delta) {
}
}
//void AnimationState::update(double delta) {
// uint start = startFrame;
// uint end = endFrame;
//
// //TODO: Implement loop toggle
//
// if (playing) currentFrame = fmod((currentFrame - start) + (delta * ticksPerSecond), end - start) + start;
//}
void AnimationState::setAnimNamed(const std::string &animationName, double interpolateTime, bool loop) {
auto& anim = animations[animationName];
setAnimRange(anim.startFrame, anim.endFrame, 0, loop);

View File

@ -10,8 +10,16 @@
namespace MenuApi {
void start_game(ClientState& state, sol::table& core) {
//TODO: Don't hardcode the subgame
core.set_function("start_game", [&]() {
state.subgame = "zeus";
state.desiredState = "connect";
});
core.set_function("start_game_local", [&]() {
state.subgame = "zeus";
state.desiredState = "local";
});
}
}
}

View File

@ -4,45 +4,43 @@
#include "LocalServerInstance.h"
#include "../util/Log.h"
#include "Server.h"
#include <iostream>
LocalServerInstance::LocalServerInstance(const std::string &path, unsigned short port) :
LocalServerInstance::LocalServerInstance(const std::string &path, unsigned short port, const std::string& subgame) :
port(port),
path(path) {}
path(path),
subgame(subgame) {}
bool LocalServerInstance::start() {
#ifdef _WIN32
std::cout << Log::err << "Local Server not implemented on Windows! [1]" << Log::endl;
return false;
#else
#ifndef _WIN32
pid = fork();
if (pid == 0) initServer();
if (pid == 0) {
char *arr[] = {
const_cast <char*>(""),
// const_cast <char*>("-iconic"),
const_cast <char*>("-e"),
const_cast <char*>(path.data()),
const_cast <char*>("--mode=server"),
const_cast <char*>("--subgame=zeus"), //TODO: Don't hardcode this
static_cast<char*>(nullptr)
};
execvp("xterm", arr);
}
return true;
#else
std::thread([&]() {
Server s(path.data(), port, subgame);
}).detach();
return true;
#endif
}
void LocalServerInstance::initServer() {
//This method is called on the Server Process.
//TODO: Implement version for Win32.
char *arr[] = {
(char*) "",
// (char*) "-iconic",
(char*) "-e",
const_cast<char *>(path.data()),
(char*) "--mode=server",
(char*) nullptr
};
#ifndef _WIN32
execvp("xterm", arr);
#endif
}
void LocalServerInstance::stop() {
#ifdef _WIN32
std::cout << Log::err << "Local Server not implemented on Windows! [2]" << Log::endl;
#else
if (pid != 0) kill(pid, SIGKILL);
#endif
#ifndef _WIN32
if (pid != 0) kill(pid, SIGKILL);
#else
std::cout << Log::err << "Local Server destructor not implemented on Windows!" << Log::endl;
#endif
}

View File

@ -9,19 +9,20 @@
#ifndef _WIN32
#include <zconf.h>
#include <signal.h>
#include <thread>
#endif
class LocalServerInstance {
public:
LocalServerInstance(const std::string& path, unsigned short port);
LocalServerInstance(const std::string& path, unsigned short port, const std::string& subgame);
bool start();
void stop();
unsigned short port = 0;
private:
void initServer();
std::string path;
std::string subgame;
unsigned short port = 0;
int pid = 0;
};

View File

@ -34,12 +34,12 @@ zepha.set_gui([[
]], {
buttonPlay = {
left = function()
zepha.start_game()
zepha.start_game_local()
end
},
buttonServers = {
left = function()
zepha.server_select()
zepha.start_game()
end
}
})