YouDunIt/modules/you_done_it/server_node.cpp

98 lines
2.9 KiB
C++

#include "server_node.h"
#include "core/config/engine.h"
#include "you_done_it/ydi_server.h"
ServerNode *ServerNode::singleton_instance{ nullptr };
String const ServerNode::sig_clue_revealed{ "clue_revealed" };
String const ServerNode::sig_connection_established{ "connection_established" };
String const ServerNode::sig_connection_lost{ "connection_lost" };
String const ServerNode::sig_conclusion_sent{ "conclusion_sent" };
void ServerNode::_bind_methods() {
ADD_SIGNAL(MethodInfo(sig_clue_revealed, PropertyInfo(Variant::INT, "id")));
ADD_SIGNAL(MethodInfo(sig_connection_established));
ADD_SIGNAL(MethodInfo(sig_connection_lost));
ADD_SIGNAL(MethodInfo(sig_conclusion_sent,
PropertyInfo(Variant::INT, "method", PROPERTY_HINT_ENUM, NetworkData::ClueID_hint()),
PropertyInfo(Variant::INT, "motive", PROPERTY_HINT_ENUM, NetworkData::ClueID_hint()),
PropertyInfo(Variant::INT, "murderer", PROPERTY_HINT_ENUM, NetworkData::ClueID_hint())));
ClassDB::bind_method(D_METHOD("open"), &self_type::open);
ClassDB::bind_method(D_METHOD("close"), &self_type::close);
ClassDB::bind_method(D_METHOD("is_open"), &self_type::is_open);
ClassDB::bind_method(D_METHOD("send_conclusion", "method", "motive", "murderer"), &self_type::send_conclusion);
ClassDB::bind_static_method(self_type::get_class_static(), "get_singleton", &self_type::get_singleton);
}
void ServerNode::enter_tree() {
if (singleton_instance) {
print_error("Attempt to create duplicate ServerNode, aborting");
abort();
} else {
singleton_instance = this;
}
}
void ServerNode::process(double delta) {
bool new_is_connected{ ydi::server::has_client() };
if (this->is_connected != new_is_connected) {
this->is_connected = new_is_connected;
emit_signal(this->is_connected ? sig_connection_established : sig_connection_lost);
}
if (!ydi::server::has_client()) {
return;
}
Vector<NetworkData::ClueID> new_clues{};
if (ydi::server::receive::new_clues(new_clues)) {
for (NetworkData::ClueID clue : new_clues) {
emit_signal(sig_clue_revealed, clue);
}
}
}
void ServerNode::exit_tree() {
ydi::server::close();
if (singleton_instance == this) {
singleton_instance = nullptr;
}
}
void ServerNode::_notification(int what) {
if (Engine::get_singleton()->is_editor_hint()) {
return;
}
switch (what) {
case NOTIFICATION_ENTER_TREE:
set_process(true);
enter_tree();
return;
case NOTIFICATION_PROCESS:
process(get_process_delta_time());
return;
case NOTIFICATION_EXIT_TREE:
exit_tree();
return;
default:
return;
}
}
ServerNode *ServerNode::get_singleton() {
return singleton_instance;
}
void ServerNode::send_conclusion(NetworkData::ClueID method, NetworkData::ClueID motive, NetworkData::ClueID murderer) {
ydi::server::send::conclusion(method, motive, murderer);
}
bool ServerNode::open() {
return ydi::server::open();
}
void ServerNode::close() {
ydi::server::close();
}
bool ServerNode::is_open() const {
return ydi::server::is_running();
}