Skip to content

Commit

Permalink
增加支持客户端rpc的支持
Browse files Browse the repository at this point in the history
  • Loading branch information
huahua132 committed Dec 16, 2024
1 parent 0b6ba81 commit 4122d8f
Show file tree
Hide file tree
Showing 64 changed files with 2,688 additions and 110 deletions.
4 changes: 2 additions & 2 deletions examples/digitalbomb/hall/hall_plug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ local ws_spnet_util = require "skynet-fly.utils.net.ws_spnet_util"

local g_interface_mgr = nil

local test_proto = 'sp'
local test_proto = skynet.getenv("test_proto")

local M = {}

Expand Down Expand Up @@ -58,7 +58,7 @@ end
local function match_req(player_id,packname,pack_body)
local ok,errorcode,errormsg = g_interface_mgr:match_join_table(player_id,pack_body.table_name)
if ok then
login_msg:match_res(player_id,{table_id = errorcode})
login_msg:match_res(player_id,{table_id = ok})
end
return ok,errorcode,errormsg
end
Expand Down
2 changes: 1 addition & 1 deletion examples/digitalbomb/login/login_plug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ local ws_spnet_util = require "skynet-fly.utils.net.ws_spnet_util"

local assert = assert

local test_proto = 'sp'
local test_proto = skynet.getenv("test_proto")

local g_interface_mgr = nil

Expand Down
1 change: 1 addition & 0 deletions examples/digitalbomb/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local contriner_launcher = require "skynet-fly.contriner.contriner_launcher"

skynet.start(function()
skynet.error("start digitalbomb!!!>>>>>>>>>>>>>>>>>")
skynet.setenv("test_proto", "pb") --设置测试协议
local delay_run = contriner_launcher.run()

skynet.uniqueservice("room_game_login")
Expand Down
9 changes: 1 addition & 8 deletions examples/digitalbomb/module/client_m.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local table_util = require "skynet-fly.utils.table_util"
local contriner_client = require "skynet-fly.client.contriner_client"
contriner_client:register("share_config_m")

local test_proto = 'sp'
local test_proto = skynet.getenv("test_proto")

local CMD = {}

Expand Down Expand Up @@ -46,13 +46,6 @@ local function connnect(handle)

net_util.recv(fd,handle or dispatch)

local msg = "sdfuishfsdfgf"
local send_buffer = string.pack(">I2",msg:len()) .. msg
if g_config.protocol == 'websocket' then
websocket.write(fd, send_buffer)
else
socket.write(fd, send_buffer)
end
if test_proto == 'pb' then
net_util.send(nil,fd,'.login.LoginReq',login_req)
else
Expand Down
4 changes: 2 additions & 2 deletions examples/digitalbomb/msg/errors_msg.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local errorcode = require "enum.errorcode"

local test_proto = 'sp'
local skynet = require "skynet"
local test_proto = skynet.getenv("test_proto")

local setmetatable = setmetatable

Expand Down
4 changes: 2 additions & 2 deletions examples/digitalbomb/msg/game_msg.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local setmetatable = setmetatable

local test_proto = 'sp'
local skynet = require "skynet"
local test_proto = skynet.getenv("test_proto")

local M = {}
local meta = {__index = M}
Expand Down
4 changes: 2 additions & 2 deletions examples/digitalbomb/msg/login_msg.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local setmetatable = setmetatable

local test_proto = 'sp'
local skynet = require "skynet"
local test_proto = skynet.getenv("test_proto")

local M = {}
local meta = {__index = M}
Expand Down
2 changes: 1 addition & 1 deletion examples/digitalbomb/proto/login.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ message matchReq {
}

message matchRes {
optional int32 table_id = 1;
optional string table_id = 1;
}

message serverInfoReq {
Expand Down
2 changes: 1 addition & 1 deletion examples/digitalbomb/table/table_plug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ local assert = assert
local g_table_conf = module_cfg.table_conf
local g_interface_mgr = nil

local test_proto = 'sp'
local test_proto = skynet.getenv("test_proto")

--======================enum=================================
local MINE_MIN = 1
Expand Down
4 changes: 2 additions & 2 deletions examples/digitalbomb_byid/hall/hall_plug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ local ws_spnet_byid = require "skynet-fly.utils.net.ws_spnet_byid"

local g_interface_mgr = nil

local test_proto = 'sp'
local test_proto = skynet.getenv("test_proto")

local M = {}

Expand Down Expand Up @@ -60,7 +60,7 @@ end
local function match_req(player_id,packname,pack_body)
local ok,errorcode,errormsg = g_interface_mgr:match_join_table(player_id,pack_body.table_name)
if ok then
login_msg:match_res(player_id,{table_id = errorcode})
login_msg:match_res(player_id,{table_id = ok})
end
return ok,errorcode,errormsg
end
Expand Down
2 changes: 1 addition & 1 deletion examples/digitalbomb_byid/login/login_plug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ local ws_spnet_byid = require "skynet-fly.utils.net.ws_spnet_byid"

local assert = assert

local test_proto = 'sp'
local test_proto = skynet.getenv("test_proto")

local g_interface_mgr = nil

Expand Down
1 change: 1 addition & 0 deletions examples/digitalbomb_byid/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local contriner_launcher = require "skynet-fly.contriner.contriner_launcher"

skynet.start(function()
skynet.error("start digitalbomb!!!>>>>>>>>>>>>>>>>>")
skynet.setenv("test_proto", "pb") --设置测试协议
local delay_run = contriner_launcher.run()

skynet.uniqueservice("room_game_login")
Expand Down
3 changes: 2 additions & 1 deletion examples/digitalbomb_byid/module/client_m.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ contriner_client:register("share_config_m")

local net_util = nil

local test_proto = 'sp'
local skynet = require "skynet"
local test_proto = skynet.getenv("test_proto")

local CMD = {}

Expand Down
2 changes: 1 addition & 1 deletion examples/digitalbomb_byid/proto/login.proto
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ message matchReq {
}

message matchRes {
optional int32 table_id = 1;
optional string table_id = 1;
}

message serverInfoReq {
Expand Down
2 changes: 1 addition & 1 deletion examples/digitalbomb_byid/table/table_plug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ local assert = assert
local g_table_conf = module_cfg.table_conf
local g_interface_mgr = nil

local test_proto = 'sp'
local test_proto = skynet.getenv("test_proto")

--======================enum=================================
local MINE_MIN = 1
Expand Down
188 changes: 188 additions & 0 deletions examples/digitalbomb_byrpc/alloc/alloc_plug.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
local log = require "skynet-fly.log"
local errorcode = require "enum.errorcode"
local GAME_STATE = require "enum.GAME_STATE"
local skynet = require "skynet"
local timer = require "skynet-fly.timer"

local pairs = pairs
local table = table
local ipairs = ipairs
local assert = assert
local next = next

local g_table_map = {}
local g_cant_enter_map = {}

local M = {}

local CMD = {}

function CMD.update_state(table_id,state)
log.info("update_state:",table_id)

local t_info = g_table_map[table_id]
if not t_info then
log.warn("update state not exists table_id = ",table_id)
return
end

log.fatal("update_state:", table_id, state)
if t_info.state == GAME_STATE.stop then
return
end
t_info.state = state
end

M.register_cmd = CMD

function M.init(alloc_mgr) --初始化
--初始化的时候不能访问其他服务,用fock让它启动完成再去
skynet.fork(function()
log.info("创建桌子》》》》》》》》",alloc_mgr.create_table("room_3", 10001000))
end)
end

local function check_can_join(t_info,player_id)
local max_player_num = t_info.config.table_conf.player_num
if t_info.state ~= GAME_STATE.waiting then
return false
end

if #t_info.player_list + 1 > max_player_num then
return false
end

return true
end

function M.match(player_id) --匹配
local table_num_map = {}

local max_player_num = 0
for table_id,t_info in pairs(g_table_map) do
local player_num = #t_info.player_list
if not table_num_map[player_num] then
table_num_map[player_num] = {}
end
if not g_cant_enter_map[table_id] then
table.insert(table_num_map[player_num],t_info)
end

if t_info.config.table_conf.player_num > max_player_num then
max_player_num = t_info.config.table_conf.player_num
end
end

--log.info("matching_table",g_table_map,table_num_map)

for i = max_player_num - 1,0,-1 do
local t_list = table_num_map[i]
if t_list then
for _,t_info in ipairs(t_list) do
if check_can_join(t_info,player_id) then
return t_info.table_id
end
end
end
end

return nil
end

function M.createtable(table_name, table_id, config, create_player_id) --创建桌子
log.info("createtable:",table_id)
assert(not g_table_map[table_id],"repeat table_id")
g_table_map[table_id] = {
table_id = table_id,
table_name = table_name,
config = config,
state = GAME_STATE.waiting,
player_list = {}
}
end

function M.entertable(table_id,player_id) --进入桌子
log.info("entertable:",table_id,player_id)
assert(g_table_map[table_id],"table not exists")

local t_info = g_table_map[table_id]
local player_list = t_info.player_list

for i = 1,#player_list do
local pid = player_list[i]
if pid == player_id then
log.error("entertable player exists ",table_id,player_id)
return
end
end

table.insert(t_info.player_list,player_id)
if #t_info.player_list == t_info.config.table_conf.player_num then
g_cant_enter_map[table_id] = true
end
end

function M.leavetable(table_id,player_id) --离开桌子
log.info("leavetable:",table_id,player_id)
assert(g_table_map[table_id],"table not exists")

local t_info = g_table_map[table_id]
local player_list = t_info.player_list

for i = #player_list,1,-1 do
local pid = player_list[i]
if pid == player_id then
table.remove(player_list,i)
return
end
end

log.error("leavetable player not exists ",table_id,player_id)
end

function M.dismisstable(table_id) --解散桌子
log.info("dismisstable:",table_id)
assert(g_table_map[table_id],"table not exists")

local t_info = g_table_map[table_id]
local player_list = t_info.player_list

assert(not next(player_list),"dismisstable exists player " .. #player_list)

g_cant_enter_map[table_id] = nil
g_table_map[table_id] = nil
end

function M.tablefull()
return nil,errorcode.TABLE_FULL,"table full"
end

function M.table_not_exists()
return nil,errorcode.TABLE_NOT_EXISTS,"not table"
end

------------------------------------服务退出回调-------------------------------------
function M.herald_exit()
log.error("预告退出")
end

function M.exit()
log.error("退出")
return true
end

function M.fix_exit()
log.error("确认要退出")
end

function M.cancel_exit()
log.error("取消退出")
end

function M.check_exit()
log.error("检查退出")
return true
end


return M
23 changes: 23 additions & 0 deletions examples/digitalbomb_byrpc/common/pack_helper.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
local pb_netpack = require "skynet-fly.netpack.pb_netpack"
local sp_netpack = require "skynet-fly.netpack.sp_netpack"
local string_util = require "skynet-fly.utils.string_util"
local msg_id = require "enum.msg_id"

local pairs = pairs

local M = {}

function M.set_packname_id()
for packname, pack_id in pairs(msg_id) do
pb_netpack.set_packname_id(pack_id, '.' .. packname:gsub("_","."))
end
end

function M.set_sp_packname_id()
for packname, pack_id in pairs(msg_id) do
local sp_str = string_util.split(packname, '_')
sp_netpack.set_packname_id(pack_id, sp_str[2])
end
end

return M
6 changes: 6 additions & 0 deletions examples/digitalbomb_byrpc/enum/GAME_STATE.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
return {
waiting = 0,
playing = 1,
over = 2,
stop = 3, --停服
}
Loading

0 comments on commit 4122d8f

Please sign in to comment.