Skip to content

Commit

Permalink
net 提供以协议号的方式收发消息
Browse files Browse the repository at this point in the history
  • Loading branch information
huahua132 committed Mar 28, 2024
1 parent 9899764 commit d3eeaab
Show file tree
Hide file tree
Showing 32 changed files with 2,152 additions and 91 deletions.
205 changes: 205 additions & 0 deletions examples/digitalbomb_byid/alloc/alloc_plug.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
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("default"))
end)

-- 监听有没有空桌子
timer:new(timer.minute,0,function()
local empty_map = alloc_mgr.get_empty_map()
for table_id,empty_time in pairs(empty_map) do
local t_info = g_table_map[table_id]
if not t_info then
log.error("桌子不存在", table_id)
else
if t_info.state == GAME_STATE.stop then
log.error("销毁已经停止的空桌子》》》》》》》", table_id, t_info.state, alloc_mgr.dismisstable(table_id),empty_time)
else
log.error("桌子还不能销毁>>>>>>>>>>>>>>>>>", table_id, t_info.state)
end
end
end
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
14 changes: 14 additions & 0 deletions examples/digitalbomb_byid/common/pack_helper.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
local pb_netpack = require "skynet-fly.netpack.pb_netpack"
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

return M
6 changes: 6 additions & 0 deletions examples/digitalbomb_byid/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, --停服
}
5 changes: 5 additions & 0 deletions examples/digitalbomb_byid/enum/SEAT_STATE.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
return {
empty = 0, --空座位
waitting = 1, --等待中
playing = 2, --在玩中
}
12 changes: 12 additions & 0 deletions examples/digitalbomb_byid/enum/errorcode.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
return {
UNKNOWN_ERR = 0, --未知错误
LOGIN_PASS_ERR = 1, --登录密码错误
NOT_LOGIN = 2, --没有登录
REQ_PARAM_ERR = 3, --请求参数错误
REPAET_LOGIN = 4, --重复请求登录
TABLE_FULL = 5, --房间爆满
TABLE_ENTER_ERR = 6,--坐下失败
LOGINING = 7, --登录中
playing = 8, --游戏进行中
TABLE_NOT_EXISTS = 9, --桌子不存在
}
26 changes: 26 additions & 0 deletions examples/digitalbomb_byid/enum/msg_id.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
local ENUM = {
--common
errors_Error = 1, --通用消息通知
--login
login_LoginReq = 101, --登录请求
login_LoginRes = 102, --登录回复
login_LoginOutReq = 103, --登出请求
login_LoginOutRes = 104, --登出回复
login_matchReq = 105, --匹配请求
login_matchRes = 106, --匹配回复
login_serverInfoReq = 107, --请求服务器信息
login_serverInfoRes = 108, --回复服务器信息

--game
game_DoingReq = 201, --请求操作
game_GameStatusReq = 203, --游戏状态请求
game_GameStatusRes = 204, --游戏状态回复
game_EnterCast = 281, --坐下广播
game_GameStartCast = 282, --游戏开始广播
game_NextDoingCast = 283, --下一个操作人广播
game_GameOverCast = 284, --游戏结束广播
game_LeaveCast = 285, --离开广播
game_DoingCast = 286, --操作广播
}

return ENUM
Loading

0 comments on commit d3eeaab

Please sign in to comment.