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

Dev #52

Merged
merged 5 commits into from
Jul 9, 2024
Merged

Dev #52

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
12 changes: 6 additions & 6 deletions src/lua/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ end

function Start()
Env.start_time = os.time()
Env.app_id = Squick:AppID()
Env.app_type= Squick:AppType()
Env.app_name= Squick:AppName()
Env.area = Squick:Area()
Load()
end

function AfterStart()
Expand All @@ -42,11 +47,6 @@ end

-- 在 ReadyUpdate 才进行初始化 Lua 模块,目的是让C++层的所有模块已链接完毕
function ReadyUpdate()
Env.app_id = Squick:AppID()
Env.app_type= Squick:AppType()
Env.app_name= Squick:AppName()
Env.area = Squick:Area()
Load()
ModuleMgr:Start()
end

Expand Down Expand Up @@ -82,6 +82,6 @@ function Load()
if(node_init[Env.app_type]) then
node_init[Env.app_type]()
else
Print("Not surppot this app type: " .. Env.app_type)
Print("Not surppot this app type: " .. tostring(Env.app_type))
end
end
8 changes: 7 additions & 1 deletion src/lua/node/player/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@ local modules = {

ModuleMgr:Register("node.player", modules)

MONGO_PLAYERS_DB = "players_" .. Env.area
MONGO_PLAYERS_DB = "players_" .. Env.area

CC = nil

function CCLogicBind(cc_context)
CC = cc_context
end
3 changes: 3 additions & 0 deletions src/lua/node/player/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ local Test = Module
function Test:Start()
Net:Register(MsgId.IdTReqConfig, self, self.OnTReqConfig)
Net:Register(MsgId.IdTReqDb, self, self.OnTReqDb)

-- test to call cpp
CC:Test(123, "str from lua", 1)
end

function Test:Update()
Expand Down
23 changes: 23 additions & 0 deletions src/node/backstage/logic/logic_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
#include "struct.h"
#include <third_party/common/base64.hpp>
#include <third_party/common/sha256.h>

#include <google/protobuf/message.h>
#include <google/protobuf/util/json_util.h>
#include <google/protobuf/text_format.h>

#define SQUICK_HASH_SALT "7e82e88dfd98952b713c0d20170ce12b"
#define WEB_BASE_PATH "/api"
namespace backstage::logic {
Expand Down Expand Up @@ -250,6 +255,24 @@ Coroutine<bool> LogicModule::OnGetAllNodes(std::shared_ptr<HttpRequest> request)
co_return;
}

// https://protobuf.dev/reference/cpp/api-docs/google.protobuf.util.json_util/

/*
// json-string 转 protobuf message
util::Status google::protobuf::util::JsonStringToMessage(const std::string content, google::protobuf::Message* message);
// protobuf 转 json-string
util::Status google::protobuf::util::MessageToJsonString(const google::protobuf::Message& message, std::string* str );
*/

google::protobuf::util::JsonPrintOptions options;
options.preserve_proto_field_names = true;
options.always_print_primitive_fields = true;
options.always_print_enums_as_ints = true;
options.add_whitespace = true;
std::string json_out;
google::protobuf::util::Status status = google::protobuf::util::MessageToJsonString(pback, &json_out, options);
LOG_DEBUG("Protobuf to json: %v", json_out);

nlohmann::json node_list = nlohmann::json::array();
for (auto &sd : pback.node_list()) {
json n;
Expand Down
24 changes: 10 additions & 14 deletions src/node/player/logic/logic_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,34 @@ namespace player::logic {
bool LogicModule::Start() {
m_lua_script_ = pm_->FindModule<ILuaScriptModule>();
m_node_ = pm_->FindModule<INodeModule>();
m_log_ = pm_->FindModule<ILogModule>();
vector<int> node_types = {rpc::ST_WORLD, rpc::ST_DB_PROXY, rpc::ST_PLAYER};
m_node_->AddSubscribeNode(node_types);

LuaBind();
return true;
}

bool LogicModule::Destroy() { return true; }

bool LogicModule::AfterStart() { return true; }
bool LogicModule::AfterStart() {
LuaBind();
return true;
}

bool LogicModule::Update() { return true; }

bool LogicModule::LuaBind() {
LuaScriptModule *luaModule = dynamic_cast<LuaScriptModule *>(m_lua_script_);
LuaIntf::LuaContext &luaEnv = luaModule->GetLuaEnv();
// SendMsgToGate(const uint16_t msg_id, const std::string& msg, const Guid& self)
LuaIntf::LuaBinding(luaEnv)
.beginClass<LogicModule>("GameServer")
.addFunction("send_to_player", &LogicModule::SendToPlayer)
.addFunction("test", &LogicModule::Test)
.beginClass<LogicModule>("CC")
.addFunction("Test", &LogicModule::Test)
.endClass();
// 将该模块传递到lua层
try {
LuaIntf::LuaRef func(luaEnv, "init_game_server");
LuaIntf::LuaRef func(luaEnv, "CCLogicBind");
func.call<LuaIntf::LuaRef>(this);
} catch (LuaIntf::LuaException &e) {
cout << e.what() << endl;
LOG_ERROR("Call CCLogicBind is error %v", e.what());
}
return true;
}
Expand All @@ -44,11 +44,7 @@ bool LogicModule::LuaBind() {
// 所以在该动态链接库的函数参数 类型,顺序需要符合 core/lua 中已定义的函数参数顺序
// const Guid player, const uint16_t msg_id, const std::string& data
// 由于是跨dll进行解析,无法对已在core/lua上的Guid进行解析,所以只能传普通类型的数据。
void LogicModule::SendToPlayer(string &player_guid_str, uint16_t msg_id, std::string &data) {
Guid guid = Guid(player_guid_str);
// m_node_->SendToPlayer(msg_id, data, guid);
}

void LogicModule::Test(const uint16_t msg_id, string &msg, int a) { std::cout << "LuaBindModule::Test\n" << msg_id << " " << msg << a << std::endl; }
void LogicModule::Test(const uint16_t msg_id, string &msg, int a) { std::cout << "LogicModule::Test\n" << msg_id << " " << msg << a << std::endl; }

} // namespace player::logic
4 changes: 2 additions & 2 deletions src/node/player/logic/logic_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <squick/plugin/lua/export.h>
#include <squick/plugin/net/export.h>
#include <squick/plugin/node/export.h>
#include <squick/plugin/log/export.h>
#include <struct/struct.h>

namespace player::logic {
Expand All @@ -28,13 +29,12 @@ class LogicModule : public ILogicModule {

protected:
bool LuaBind();

void SendToPlayer(string &player_guid_str, uint16_t msg_id, std::string &data);
void Test(const uint16_t msg_id, string &msg, int a);

private:
ILuaScriptModule *m_lua_script_;
INodeModule *m_node_;
ILogModule *m_log_;
};

} // namespace player::logic
2 changes: 2 additions & 0 deletions src/proto/n_backstage.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
syntax = "proto3";
package rpc;
20 changes: 20 additions & 0 deletions src/proto/n_db_table.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Desc: MongoDb table
syntax = "proto3";
package rpc;
import "base.proto";

message OnlineTable {
bool is_online = 1;
int32 node_player = 2;
int32 node_proxy = 3;
int32 node_world = 4;
int32 node_room = 5;
int32 node_game = 6;
}

message PlayerTable {
PlayerData data = 1;
}

message FriendTable {
}
2 changes: 2 additions & 0 deletions src/proto/web.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
syntax = "proto3";
package rpc;
48 changes: 33 additions & 15 deletions src/pycli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@
import _thread
import time
import rel
from protocol import *

# pip install websocket-client websocket rel
# pip install requests
# pip install http
# pip install protobuf

print("pycli:")

session = requests.session()
session.cookies = cookiejar.LWPCookieJar(filename='./login.cookie')
BaseUrl = 'http://127.0.0.1:8088'
BaseUrl = 'http://127.0.0.1:8888'
header = {
'Referer': BaseUrl + "login",
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) Chrome/89.0.4389.82'
Expand All @@ -39,18 +42,32 @@ def login(account, password):
print(ex)
return True, None

def on_message(ws, message):
print(message)

def on_error(ws, error):
print(error)
def AuthConnection(ws):
print("Begin auth")
req = ReqConnectProxy()
req.account_id = '123456'
req.key = 'key'
req.login_node = 1
req.signatrue = 0
sdata = req.SerializeToString()
data = Encode(IdReqConnectProxy, sdata)
print(data)
ws.send(data, 2)

def OnWsRecv(ws, message):
print("Message:", message)

def OnWsError(ws, error):
print("Error:", error)

def on_close(ws, close_status_code, close_msg):
def OnWsClose(ws, close_status_code, close_msg):
print("### closed ###")

def on_open(ws):
def OnWsOpen(ws):
print("Opened connection")

AuthConnection(ws)


if __name__ == '__main__':
# login
Expand All @@ -63,13 +80,14 @@ def on_open(ws):
wsUrl = 'ws://' + str(jrsp['ip']) + ':' + str(jrsp['ws_port']) + '/'
print("connect to proxy: " + wsUrl)
ws = websocket.WebSocketApp(wsUrl,
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close)
on_open=OnWsOpen,
on_message=OnWsRecv,
on_error=OnWsError,
on_close=OnWsClose)

# Set dispatcher to automatic reconnection, 5 second reconnect delay if connection closed unexpectedly
ws.run_forever(dispatcher=rel, reconnect=5)

rel.signal(2, rel.abort) # Keyboard Interrupt
rel.dispatch()

# Set dispatcher to automatic reconnection, 5 second reconnect delay if connection closed unexpectedly
ws.run_forever(dispatcher=rel, reconnect=5)
print("run")
25 changes: 25 additions & 0 deletions src/pycli/protocol.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import struct
import sys
sys.path.append('./proto')

from proxy_pb2 import *
from web_pb2 import *
from player_pb2 import *
from test_pb2 import *
from game_pb2 import *
from base_pb2 import *
from msg_id_pb2 import *


print("sys", sys.path)

def Encode(msg_id, data):
send_data = b''
send_data += (msg_id).to_bytes(2, byteorder="big", signed=False)
send_data += (len(data)).to_bytes(4, byteorder="big", signed=False)
send_data += data
return send_data

def Decode(recv_data):
return msg_id, data

8 changes: 4 additions & 4 deletions src/squick/plugin/node/node_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ NodeModule::NodeModule(IPluginManager *p) {
is_update_ = true;
}

bool NodeModule::Awake() { return true; }

bool NodeModule::Start() {
bool NodeModule::Awake() {
m_net_ = pm_->FindModule<INetModule>();
m_log_ = pm_->FindModule<ILogModule>();
m_net_client_ = pm_->FindModule<INetClientModule>();
is_update_ = true;

pm_->SetAppType(StringNodeTypeToEnum(pm_->GetArg("type=", "proxy")));
pm_->SetArea(pm_->GetArg("area=", 0));
return true;
}

bool NodeModule::Start() {
Listen();
return true;
}
Expand Down
Loading