Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scripting: Split Human and Player objects #99

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions code/server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ set(MAFIAMP_SERVER_FILES
src/main.cpp
src/core/server.cpp

src/core/builtins/human.cpp
src/core/builtins/player.cpp
src/core/builtins/vehicle.cpp

Expand Down
5 changes: 2 additions & 3 deletions code/server/src/core/builtins/builtins.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
#pragma once

#include "scripting/engines/node/engine.h"
#include "scripting/engines/node/sdk.h"

#include "chat.h"
#include "human.h"
#include "player.h"
#include "vehicle.h"
#include "world.h"
Expand All @@ -14,6 +12,7 @@ namespace MafiaMP::Scripting {
static void Register(v8::Isolate *isolate, v8pp::module *rootModule) {
Scripting::Chat::Register(isolate, rootModule);
Scripting::Human::Register(isolate, rootModule);
Scripting::Player::Register(isolate, rootModule);
Scripting::Vehicle::Register(isolate, rootModule);
Scripting::World::Register(isolate, rootModule);
}
Expand Down
43 changes: 21 additions & 22 deletions code/server/src/core/builtins/chat.h
Original file line number Diff line number Diff line change
@@ -1,21 +1,34 @@
#pragma once

#include "scripting/engines/node/engine.h"
#include "scripting/engines/node/sdk.h"
#include "core/server.h"

#include "shared/rpc/chat_message.h"

#include "player.h"

#include "core_modules.h"

namespace MafiaMP::Scripting {
class Chat final {
public:
static void EventChatCommand(flecs::entity e, std::string message, std::string command, std::vector<std::string> args) {
const auto engine = MafiaMP::Server::GetNodeEngine();
V8_RESOURCE_LOCK(engine);
engine->InvokeEvent("chatCommand", Player::WrapPlayer(engine->GetIsolate(), e), message, command, args);
}

static void EventChatMessage(flecs::entity e, std::string message) {
const auto engine = MafiaMP::Server::GetNodeEngine();
V8_RESOURCE_LOCK(engine);
engine->InvokeEvent("chatMessage", Player::WrapPlayer(engine->GetIsolate(), e), message);
}

static void SendToAll(std::string message) {
FW_SEND_COMPONENT_RPC(Shared::RPC::ChatMessage, message);
}

static void SendToPlayer(Human *human, std::string message) {
if (human) {
const auto ent = human->GetHandle();
const auto str = ent.get<Framework::World::Modules::Base::Streamer>();
const auto str = ent.get<Framework::World::Modules::Base::Streamer>();

if (!str)
return;
Expand All @@ -24,26 +37,12 @@ namespace MafiaMP::Scripting {
}
}

static void SendToAll(std::string message) {
FW_SEND_COMPONENT_RPC(Shared::RPC::ChatMessage, message);
}

static void EventChatMessage(flecs::entity e, std::string message) {
const auto engine = MafiaMP::Server::GetNodeEngine();
V8_RESOURCE_LOCK(engine);
engine->InvokeEvent("chatMessage", Human::WrapHuman(engine, e), message);
}

static void EventChatCommand(flecs::entity e, std::string message, std::string command, std::vector<std::string> args) {
const auto engine = MafiaMP::Server::GetNodeEngine();
V8_RESOURCE_LOCK(engine);
engine->InvokeEvent("chatCommand", Human::WrapHuman(engine, e), message, command, args);
}

static void Register(v8::Isolate *isolate, v8pp::module *rootModule) {
v8pp::module chat(isolate);
chat.function("sendToPlayer", &Chat::SendToPlayer);

chat.function("sendToAll", &Chat::SendToAll);
chat.function("sendToPlayer", &Chat::SendToPlayer);

rootModule->submodule("Chat", chat);
}
};
Expand Down
87 changes: 87 additions & 0 deletions code/server/src/core/builtins/human.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include "human.h"

#include "shared/game_rpc/add_weapon.h"
#include "shared/game_rpc/human/human_setprops.h"
#include "shared/modules/human_sync.hpp"

#include "vehicle.h"

namespace MafiaMP::Scripting {
std::string Human::ToString() const {
std::ostringstream ss;
ss << "Human{ id: " << _ent.id() << " }";
return ss.str();
}

void Human::Destroy(v8::Isolate *isolate) {
if (_isPlayer) {
isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8(isolate, "Player object can not be destroyed!").ToLocalChecked()));
return;
}

Entity::Destroy();
}

void Human::AddWeapon(int weaponId, int ammo) {
FW_SEND_SERVER_COMPONENT_GAME_RPC(MafiaMP::Shared::RPC::AddWeapon, _ent, weaponId, ammo);
}

v8::Local<v8::Value> Human::GetVehicle(v8::Isolate *isolate) const {
const auto updateData = _ent.get<MafiaMP::Shared::Modules::HumanSync::UpdateData>();
const auto carEnt = flecs::entity(_ent.world(), updateData->carPassenger.carId);

if (carEnt.is_valid() && carEnt.is_alive()) {
return Vehicle::WrapVehicle(isolate, carEnt);
}

return v8::Undefined(isolate);
}

int Human::GetVehicleSeat() const {
const auto updateData = _ent.get<MafiaMP::Shared::Modules::HumanSync::UpdateData>();
const auto carEnt = flecs::entity(_ent.world(), updateData->carPassenger.carId);

if (carEnt.is_valid() && carEnt.is_alive()) {
return updateData->carPassenger.seatId;
}

return -1;
}

float Human::GetHealth() const {
auto h = _ent.get<MafiaMP::Shared::Modules::HumanSync::UpdateData>();
return h->_healthPercent;
}

void Human::SetHealth(float health) {
auto h = _ent.get_mut<MafiaMP::Shared::Modules::HumanSync::UpdateData>();
h->_healthPercent = health;

MafiaMP::Shared::RPC::HumanSetProps msg {};
msg.health = health;
FW_SEND_SERVER_COMPONENT_GAME_RPC(MafiaMP::Shared::RPC::HumanSetProps, _ent, msg);
}

void Human::Register(v8::Isolate *isolate, v8pp::module *rootModule) {
if (!rootModule) {
return;
}

v8pp::class_<Human> cls(isolate);
cls.inherit<Framework::Integrations::Scripting::Entity>();
cls.function("addWeapon", &Human::AddWeapon);
cls.function("destroy", &Human::Destroy);

cls.function("getHealth", &Human::GetHealth);
cls.function("getVehicle", &Human::GetVehicle);
cls.function("getVehicleSeat", &Human::GetVehicleSeat);

cls.function("setHealth", &Human::SetHealth);

rootModule->class_("Human", cls);
}

v8::Local<v8::Object> Human::WrapHuman(v8::Isolate *isolate, flecs::entity e) {
return v8pp::class_<Scripting::Human>::create_object(isolate, e.id());
}
} // namespace MafiaMP::Scripting
33 changes: 33 additions & 0 deletions code/server/src/core/builtins/human.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#pragma once

#include "integrations/server/scripting/builtins/node/entity.h"

namespace MafiaMP::Scripting {
class Human: public Framework::Integrations::Scripting::Entity {
private:
bool _isPlayer = false;

public:
Human(flecs::entity_t ent, bool isPlayer = false): Entity(ent) {
_isPlayer = isPlayer;
}

std::string ToString() const override;

void Destroy(v8::Isolate *isolate);

void AddWeapon(int weaponId, int ammo);

v8::Local<v8::Value> GetVehicle(v8::Isolate *isolate) const;

int GetVehicleSeat() const;

float GetHealth() const;

void SetHealth(float health);

static void Register(v8::Isolate *isolate, v8pp::module *rootModule);

static v8::Local<v8::Object> WrapHuman(v8::Isolate *isolate, flecs::entity e);
};
} // namespace MafiaMP::Scripting
122 changes: 49 additions & 73 deletions code/server/src/core/builtins/player.cpp
Original file line number Diff line number Diff line change
@@ -1,107 +1,83 @@
#include "player.h"

#include "core/server.h"

#include "vehicle.h"

#include "shared/modules/human_sync.hpp"
#include "shared/game_rpc/add_weapon.h"
#include "shared/game_rpc/human/human_setprops.h"
#include "shared/rpc/chat_message.h"

namespace MafiaMP::Scripting {
v8::Local<v8::Object> Human::WrapHuman(Framework::Scripting::Engines::Node::Engine *engine, flecs::entity e) {
return v8pp::class_<Scripting::Human>::create_object(engine->GetIsolate(), e.id());
}

std::string Human::ToString() const {
std::string Player::ToString() const {
std::ostringstream ss;
ss << "Human{ id: " << _ent.id() << " }";
ss << "Player{ id: " << _ent.id() << " }";
return ss.str();
}

void Human::Destroy(v8::Isolate *isolate) {
isolate->ThrowException(v8::Exception::Error(v8::String::NewFromUtf8(isolate, "Human object can not be destroyed!").ToLocalChecked()));
void Player::EventPlayerConnected(flecs::entity e) {
const auto engine = MafiaMP::Server::GetNodeEngine();
V8_RESOURCE_LOCK(engine);
auto playerObj = WrapPlayer(engine->GetIsolate(), e);
engine->InvokeEvent("playerConnected", playerObj);
}

void Human::AddWeapon(int weaponId, int ammo) {
FW_SEND_SERVER_COMPONENT_GAME_RPC(MafiaMP::Shared::RPC::AddWeapon, _ent, weaponId, ammo);
void Player::EventPlayerDisconnected(flecs::entity e) {
const auto engine = MafiaMP::Server::GetNodeEngine();
V8_RESOURCE_LOCK(engine);
auto playerObj = WrapPlayer(engine->GetIsolate(), e);
engine->InvokeEvent("playerDisconnected", playerObj);
}

void Human::SendChat(std::string message) {
const auto str = _ent.get<Framework::World::Modules::Base::Streamer>();
FW_SEND_COMPONENT_RPC_TO(Shared::RPC::ChatMessage, SLNet::RakNetGUID(str->guid), message);
void Player::EventPlayerDied(flecs::entity e) {
const auto engine = MafiaMP::Server::GetNodeEngine();
V8_RESOURCE_LOCK(engine);
auto playerObj = WrapPlayer(engine->GetIsolate(), e);
engine->InvokeEvent("playerDied", playerObj);
}

v8::Local<v8::Value> Human::GetVehicle() const {
const auto updateData = _ent.get<MafiaMP::Shared::Modules::HumanSync::UpdateData>();
const auto carEnt = flecs::entity(_ent.world(), updateData->carPassenger.carId);
if (carEnt.is_valid() && carEnt.is_alive()) {
return v8pp::class_<Vehicle>::create_object(v8::Isolate::GetCurrent(), carEnt.id());
}
return v8::Undefined(v8::Isolate::GetCurrent());
}
void Player::EventPlayerVehicleEnter(flecs::entity player, flecs::entity vehicle, int seatIndex) {
const auto engine = MafiaMP::Server::GetNodeEngine();
V8_RESOURCE_LOCK(engine);

int Human::GetVehicleSeat() const {
const auto updateData = _ent.get<MafiaMP::Shared::Modules::HumanSync::UpdateData>();
const auto carEnt = flecs::entity(_ent.world(), updateData->carPassenger.carId);
if (carEnt.is_valid() && carEnt.is_alive()) {
return updateData->carPassenger.seatId;
}
return -1;
}
auto vehicleObj = Vehicle::WrapVehicle(engine->GetIsolate(), vehicle);
auto playerObj = WrapPlayer(engine->GetIsolate(), player);

void Human::SendChatToAll(std::string message) {
FW_SEND_COMPONENT_RPC(Shared::RPC::ChatMessage, message);
engine->InvokeEvent("playerVehicleEnter", playerObj, vehicleObj, seatIndex);
}

void Human::SetHealth(float health) {
auto h = _ent.get_mut<MafiaMP::Shared::Modules::HumanSync::UpdateData>();
h->_healthPercent = health;
MafiaMP::Shared::RPC::HumanSetProps msg {};
msg.health = health;
FW_SEND_SERVER_COMPONENT_GAME_RPC(MafiaMP::Shared::RPC::HumanSetProps, _ent, msg);
}
void Player::EventPlayerVehicleLeave(flecs::entity player, flecs::entity vehicle) {
const auto engine = MafiaMP::Server::GetNodeEngine();
V8_RESOURCE_LOCK(engine)

float Human::GetHealth() const {
auto h = _ent.get<MafiaMP::Shared::Modules::HumanSync::UpdateData>();
return h->_healthPercent;
}
auto vehicleObj = Vehicle::WrapVehicle(engine->GetIsolate(), vehicle);
auto playerObj = WrapPlayer(engine->GetIsolate(), player);

void Human::EventPlayerDied(flecs::entity e) {
const auto engine = MafiaMP::Server::GetNodeEngine();
V8_RESOURCE_LOCK(engine);
auto playerObj = WrapHuman(engine, e);
engine->InvokeEvent("playerDied", playerObj);
engine->InvokeEvent("playerVehicleLeave", playerObj, vehicleObj);
}

void Human::EventPlayerConnected(flecs::entity e) {
const auto engine = MafiaMP::Server::GetNodeEngine();
V8_RESOURCE_LOCK(engine);
auto playerObj = WrapHuman(engine, e);
engine->InvokeEvent("playerConnected", playerObj);
void Player::SendChat(std::string message) {
const auto str = _ent.get<Framework::World::Modules::Base::Streamer>();
FW_SEND_COMPONENT_RPC_TO(Shared::RPC::ChatMessage, SLNet::RakNetGUID(str->guid), message);
}

void Human::EventPlayerDisconnected(flecs::entity e) {
const auto engine = MafiaMP::Server::GetNodeEngine();
V8_RESOURCE_LOCK(engine);
auto playerObj = WrapHuman(engine, e);
engine->InvokeEvent("playerDisconnected", playerObj);
void Player::SendChatToAll(std::string message) {
FW_SEND_COMPONENT_RPC(Shared::RPC::ChatMessage, message);
}

void Human::Register(v8::Isolate *isolate, v8pp::module *rootModule) {
void Player::Register(v8::Isolate *isolate, v8pp::module *rootModule) {
if (!rootModule) {
return;
}

v8pp::class_<Human> cls(isolate);
cls.inherit<Framework::Integrations::Scripting::Entity>();
cls.function("destroy", &Human::Destroy);
cls.function("addWeapon", &Human::AddWeapon);
cls.function("setHealth", &Human::SetHealth);
cls.function("getHealth", &Human::GetHealth);
cls.function("getVehicle", &Human::GetVehicle);
cls.function("getVehicleSeat", &Human::GetVehicleSeat);
cls.function("sendChat", &Human::SendChat);
cls.function("sendChatToAll", &Human::SendChatToAll);
rootModule->class_("Human", cls);
v8pp::class_<Player> cls(isolate);
cls.inherit<MafiaMP::Scripting::Human>();

cls.function("sendChat", &Player::SendChat);
cls.function("sendChatToAll", &Player::SendChatToAll);

rootModule->class_("Player", cls);
}

v8::Local<v8::Object> Player::WrapPlayer(v8::Isolate *isolate, flecs::entity e) {
return v8pp::class_<Scripting::Player>::create_object(isolate, e.id());
}
}
} // namespace MafiaMP::Scripting
Loading