2021-10-14 15:51:38 -04:00
|
|
|
#include "lpxclient.hpp"
|
|
|
|
|
|
|
|
|
|
#include "drivenengine.hpp"
|
|
|
|
|
#include "world.hpp"
|
|
|
|
|
#include "luaconsole.hpp"
|
|
|
|
|
#include "invocation.hpp"
|
2021-11-03 11:11:31 -04:00
|
|
|
#include "util.hpp"
|
2021-10-14 15:51:38 -04:00
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
|
|
class LpxClient : public DrivenEngine {
|
|
|
|
|
public:
|
|
|
|
|
using StringVec = LuaConsole::StringVec;
|
2021-10-15 13:51:32 -04:00
|
|
|
UniqueWorld world_;
|
2021-10-14 15:51:38 -04:00
|
|
|
int64_t actor_id_;
|
|
|
|
|
InvocationQueue unack_;
|
2021-10-15 13:51:32 -04:00
|
|
|
UniqueChannel channel_;
|
2021-10-14 15:51:38 -04:00
|
|
|
LuaConsole console_;
|
|
|
|
|
Gui gui_;
|
|
|
|
|
int64_t gui_place_;
|
|
|
|
|
|
|
|
|
|
public:
|
2021-10-14 16:41:24 -04:00
|
|
|
virtual void event_init(int argc, char *argv[]) {
|
2021-10-15 13:51:32 -04:00
|
|
|
// Create the world model.
|
|
|
|
|
world_.reset(new World(util::WORLD_TYPE_C_SYNC));
|
|
|
|
|
|
|
|
|
|
// This is a temporary actor that will be used only until the server sends
|
|
|
|
|
// us the first difference transmission. We do this only to establish
|
|
|
|
|
// the invariant that there's always an actor. When the first difference
|
|
|
|
|
// transmission arrives, this actor may be deleted, or it may just be
|
|
|
|
|
// ignored, at the server's discretion.
|
|
|
|
|
actor_id_ = world_->create_login_actor();
|
|
|
|
|
|
|
|
|
|
// Establish a connection to the server.
|
|
|
|
|
channel_ = new_outgoing_channel("localhost:8085");
|
2021-10-14 15:51:38 -04:00
|
|
|
}
|
|
|
|
|
|
2021-11-03 11:11:31 -04:00
|
|
|
void do_lua(const std::string &lua) {
|
2021-11-03 12:31:13 -04:00
|
|
|
stdostream() << "Lua: " << lua << std::endl;
|
2021-11-03 11:11:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void do_command(const util::StringVec &words) {
|
2021-11-03 12:31:13 -04:00
|
|
|
stdostream() << "Command: ";
|
2021-11-03 11:11:31 -04:00
|
|
|
for (const std::string &word : words) {
|
2021-11-03 12:31:13 -04:00
|
|
|
stdostream() << word << " ";
|
2021-11-03 11:11:31 -04:00
|
|
|
}
|
2021-11-03 12:31:13 -04:00
|
|
|
stdostream() << std::endl;
|
2021-11-03 11:11:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void console_process(const std::string &line) {
|
|
|
|
|
console_.add(line);
|
|
|
|
|
int action = console_.action();
|
|
|
|
|
if (action == LuaConsole::DO_LUA) {
|
|
|
|
|
do_lua(console_.lua_expression());
|
|
|
|
|
console_.clear();
|
|
|
|
|
} else if (action == LuaConsole::DO_COMMAND) {
|
|
|
|
|
do_command(console_.words());
|
|
|
|
|
console_.clear();
|
|
|
|
|
} else if (action == LuaConsole::DO_SYNTAX) {
|
2021-11-03 12:31:13 -04:00
|
|
|
stdostream() << console_.syntax() << std::endl;
|
2021-11-03 11:11:31 -04:00
|
|
|
console_.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-14 15:51:38 -04:00
|
|
|
virtual void event_update() {
|
2021-11-03 11:11:31 -04:00
|
|
|
// Check for keyboard input on stdin.
|
|
|
|
|
while (true) {
|
|
|
|
|
std::string line = get_stdio_channel()->in()->readline();
|
|
|
|
|
if (line == "") break;
|
|
|
|
|
console_process(line);
|
|
|
|
|
get_stdio_channel()->out()->write_bytes(console_.get_prompt());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for communication from server..
|
|
|
|
|
if (channel_ != nullptr) {
|
|
|
|
|
if (channel_->closed()) {
|
2021-11-03 12:31:13 -04:00
|
|
|
stdostream() << "Server closed connection " << channel_->error() << std::endl;
|
2021-11-03 11:11:31 -04:00
|
|
|
channel_.reset();
|
|
|
|
|
// stop_driver();
|
|
|
|
|
} else {
|
|
|
|
|
// Implement reception of messages.
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-10-14 15:51:38 -04:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
UniqueDrivenEngine make_LpxClient() {
|
|
|
|
|
return UniqueDrivenEngine(new LpxClient);
|
|
|
|
|
}
|
|
|
|
|
|