├── config ├── README.md ├── config_test ├── config_test1 ├── config_db ├── config_locator ├── config_chat ├── config_login ├── config_hall ├── config_auth ├── config_xpnn ├── config_gate ├── config_xpnn21 ├── config_xpnn22 ├── config_xpnn23 └── config_public ├── proto ├── README.md ├── xproto │ ├── xpb │ │ └── package.xpb │ └── xproto │ │ └── package.xproto └── sproto │ ├── spb │ ├── ddz.spb │ ├── zjh.spb │ ├── bank.spb │ ├── cash.spb │ ├── lrnn.spb │ ├── lrsh.spb │ ├── player.spb │ ├── push.spb │ ├── message.spb │ ├── auth.spb │ ├── chat.spb │ ├── desk.spb │ ├── gate.spb │ ├── login.spb │ ├── room.spb │ ├── xpnn.spb │ ├── package.spb │ ├── hall.spb │ └── area.spb │ ├── sproto │ ├── ddz.sproto │ ├── zjh.sproto │ ├── bank.sproto │ ├── cash.sproto │ ├── lrnn.sproto │ ├── player.sproto │ ├── push.sproto │ ├── message.sproto │ ├── lrsh.sproto │ ├── room.sproto │ ├── gate.sproto │ ├── chat.sproto │ ├── desk.sproto │ ├── hall.sproto │ ├── package.sproto │ ├── area.sproto │ ├── login.sproto │ ├── auth.sproto │ └── xpnn.sproto │ └── sproto_gen │ ├── lualib │ ├── print_r.lua │ └── sprotoloader.lua │ ├── sproto_gen_win.lua │ └── sproto_gen_linux.lua ├── logic ├── bank │ ├── bank_db.lua │ ├── bank_ctrl.lua │ └── bank_impl.lua ├── cash │ ├── cash_db.lua │ ├── cash_ctrl.lua │ └── cash_impl.lua ├── push │ ├── push_db.lua │ ├── push_ctrl.lua │ └── push_impl.lua ├── xpnn │ ├── xpnn_db.lua │ ├── xpnn_const.lua │ └── xpnn_impl.lua ├── offline │ ├── README.md │ ├── charge_offline_ctrl.lua │ └── player_offline_ctrl.lua ├── agent │ ├── agent_impl.lua │ ├── agent_db.lua │ └── agent_ctrl.lua ├── config │ ├── global_config.lua │ ├── xpnn_module.lua │ ├── hall_config.lua │ ├── xpnn_config.lua │ ├── locator_server_config.lua │ ├── create_role_config.lua │ ├── create_player_config.lua │ ├── game_type_config.lua │ ├── mysql_config.lua │ ├── game_config.lua │ ├── redis_config.lua │ ├── xpnn_desk_config.lua │ ├── db_config.lua │ ├── nickname_config.lua │ └── cluster_config.lua ├── desk │ ├── desk_impl.lua │ ├── desk_const.lua │ ├── desk_db.lua │ └── desk_ctrl.lua ├── role │ ├── role_impl.lua │ ├── role_ctrl.lua │ ├── role_logic.lua │ └── role_db.lua ├── locator │ ├── locator_impl.lua │ └── locator_ctrl.lua ├── chat │ ├── chat_impl.lua │ ├── chat_ctrl.lua │ └── chat_logic.lua ├── common │ ├── db_module.lua │ ├── db_mgr.lua │ └── db_helper.lua ├── heartbeat │ ├── heartbeat_impl.lua │ └── heartbeat_ctrl.lua ├── room │ ├── room_impl.lua │ └── room_ctrl.lua ├── hall │ ├── hall_impl.lua │ ├── hall_db.lua │ └── hall_ctrl.lua ├── login │ ├── login_impl.lua │ ├── login_logic.lua │ └── login_ctrl.lua ├── auth │ └── auth_impl.lua ├── area │ ├── area_impl.lua │ └── area_logic.lua ├── README.md ├── gate │ ├── gate_mgr.lua │ └── socket_msg.lua ├── cache │ ├── cache_db.lua │ └── cache_logic.lua ├── player │ ├── player_impl.lua │ ├── player_db.lua │ └── player_ctrl.lua ├── fd │ └── fd_db.lua └── game │ └── cd_ctrl.lua ├── lualib ├── xproto_helper.lua ├── secret.lua ├── config_helper.lua ├── guid.lua ├── protobuf_helper.lua ├── README.md ├── config_db.lua ├── sky_crypt.lua ├── service_base.lua ├── context.lua ├── command_base.lua ├── share_memory.lua ├── random.lua ├── redis_mq.lua ├── timer.lua ├── requester.lua ├── cluster_monitor.lua ├── connector.lua └── utils.lua ├── tool ├── run_client.sh ├── restart.sh ├── stop_game.sh ├── run_redis.sh ├── run_test.sh ├── run_test1.sh ├── client.sh ├── killnode.sh ├── stop.sh ├── run.sh ├── run_db.sh ├── run_hall.sh ├── run_auth.sh ├── run_chat.sh ├── run_locator.sh ├── run_login.sh ├── run_gate.sh ├── run_xpnn.sh └── run_all.sh ├── 详细设计文档.pdf ├── .gitattributes ├── preload ├── server_define.lua ├── game_define.lua ├── preload.lua ├── db_define.lua ├── service_define.lua ├── rpc_error.lua ├── system_define.lua └── module_define.lua ├── test ├── test_table_func.lua ├── test_ext.lua ├── test_binary.lua ├── test_xpnn_logic.lua └── client_json_msg.lua ├── .gitmodules ├── service ├── chat_logic_svc.lua ├── login_logic_svc.lua ├── chat.lua ├── test │ ├── test_share_memory1.lua │ ├── test_share_memory.lua │ ├── main.lua │ └── main1.lua ├── area_logic_svc.lua ├── auth.lua ├── locator.lua ├── heartbeat.lua ├── login.lua ├── README.md ├── chatserver │ └── main.lua ├── hallserver │ └── main.lua ├── locatorserver │ └── main.lua ├── hall.lua ├── loginserver │ └── main.lua ├── httpServer.lua ├── gameserver │ └── xpnn │ │ └── main.lua ├── mysql_svc.lua ├── watchdog.lua ├── wswatchdog.lua ├── hall_logic_svc.lua ├── room.lua ├── master_db_svc.lua ├── auth_logic_svc.lua ├── share_memoryd.lua ├── authserver │ └── main.lua ├── area.lua ├── mysql_mgr.lua ├── dbserver │ └── main.lua ├── gateserver │ └── main.lua ├── cluster_monitord.lua ├── master_db.lua ├── desk.lua ├── loggerd.lua ├── httpwatchdog.lua ├── agent.lua └── httpServerAgent.lua ├── Makefile ├── sql └── create.sql ├── client ├── simplesocket.lua └── simplemessage.lua └── README.md /config/README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /proto/README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /logic/bank/bank_db.lua: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /logic/cash/cash_db.lua: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /logic/push/push_db.lua: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /logic/xpnn/xpnn_db.lua: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /logic/bank/bank_ctrl.lua: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /logic/push/push_ctrl.lua: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /lualib/xproto_helper.lua: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /proto/xproto/xpb/package.xpb: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /logic/offline/README.md: -------------------------------------------------------------------------------- 1 | # 离线数据处理流程 -------------------------------------------------------------------------------- /logic/offline/charge_offline_ctrl.lua: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /logic/offline/player_offline_ctrl.lua: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /proto/sproto/spb/ddz.spb: -------------------------------------------------------------------------------- 1 |  ddz -------------------------------------------------------------------------------- /proto/sproto/spb/zjh.spb: -------------------------------------------------------------------------------- 1 |  zjh -------------------------------------------------------------------------------- /proto/sproto/sproto/ddz.sproto: -------------------------------------------------------------------------------- 1 | .ddz{ 2 | 3 | } -------------------------------------------------------------------------------- /proto/sproto/sproto/zjh.sproto: -------------------------------------------------------------------------------- 1 | .zjh{ 2 | 3 | } -------------------------------------------------------------------------------- /proto/sproto/spb/bank.spb: -------------------------------------------------------------------------------- 1 |  bank -------------------------------------------------------------------------------- /proto/sproto/spb/cash.spb: -------------------------------------------------------------------------------- 1 |  cash -------------------------------------------------------------------------------- /proto/sproto/spb/lrnn.spb: -------------------------------------------------------------------------------- 1 |  lrnn -------------------------------------------------------------------------------- /proto/sproto/spb/lrsh.spb: -------------------------------------------------------------------------------- 1 |  lrsh -------------------------------------------------------------------------------- /proto/sproto/spb/player.spb: -------------------------------------------------------------------------------- 1 | player -------------------------------------------------------------------------------- /proto/sproto/spb/push.spb: -------------------------------------------------------------------------------- 1 |  push -------------------------------------------------------------------------------- /proto/sproto/sproto/bank.sproto: -------------------------------------------------------------------------------- 1 | .bank{ 2 | 3 | } -------------------------------------------------------------------------------- /proto/sproto/sproto/cash.sproto: -------------------------------------------------------------------------------- 1 | .cash{ 2 | 3 | } -------------------------------------------------------------------------------- /proto/sproto/sproto/lrnn.sproto: -------------------------------------------------------------------------------- 1 | .lrnn{ 2 | 3 | } -------------------------------------------------------------------------------- /proto/sproto/sproto/player.sproto: -------------------------------------------------------------------------------- 1 | .player{ 2 | 3 | } -------------------------------------------------------------------------------- /proto/sproto/sproto/push.sproto: -------------------------------------------------------------------------------- 1 | .push{ 2 | 3 | } -------------------------------------------------------------------------------- /proto/sproto/spb/message.spb: -------------------------------------------------------------------------------- 1 | message -------------------------------------------------------------------------------- /proto/sproto/sproto/message.sproto: -------------------------------------------------------------------------------- 1 | .message{ 2 | 3 | } -------------------------------------------------------------------------------- /proto/sproto/sproto/lrsh.sproto: -------------------------------------------------------------------------------- 1 | #两人梭哈 2 | 3 | .lrsh{ 4 | 5 | } -------------------------------------------------------------------------------- /tool/run_client.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | ../skynet/3rd/lua/lua test/client.lua 3 | -------------------------------------------------------------------------------- /详细设计文档.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/najsword/serverOne/HEAD/详细设计文档.pdf -------------------------------------------------------------------------------- /logic/agent/agent_impl.lua: -------------------------------------------------------------------------------- 1 | local agent_impl = {} 2 | 3 | 4 | return agent_impl -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /logic/config/global_config.lua: -------------------------------------------------------------------------------- 1 | local global_config = {} 2 | 3 | 4 | 5 | return global_config -------------------------------------------------------------------------------- /proto/sproto/spb/auth.spb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/najsword/serverOne/HEAD/proto/sproto/spb/auth.spb -------------------------------------------------------------------------------- /proto/sproto/spb/chat.spb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/najsword/serverOne/HEAD/proto/sproto/spb/chat.spb -------------------------------------------------------------------------------- /proto/sproto/spb/desk.spb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/najsword/serverOne/HEAD/proto/sproto/spb/desk.spb -------------------------------------------------------------------------------- /proto/sproto/spb/gate.spb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/najsword/serverOne/HEAD/proto/sproto/spb/gate.spb -------------------------------------------------------------------------------- /proto/sproto/spb/login.spb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/najsword/serverOne/HEAD/proto/sproto/spb/login.spb -------------------------------------------------------------------------------- /proto/sproto/spb/room.spb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/najsword/serverOne/HEAD/proto/sproto/spb/room.spb -------------------------------------------------------------------------------- /proto/sproto/spb/xpnn.spb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/najsword/serverOne/HEAD/proto/sproto/spb/xpnn.spb -------------------------------------------------------------------------------- /proto/sproto/spb/package.spb: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/najsword/serverOne/HEAD/proto/sproto/spb/package.spb -------------------------------------------------------------------------------- /logic/agent/agent_db.lua: -------------------------------------------------------------------------------- 1 | local db_mgr = require "common.db_mgr" 2 | 3 | local agent_db = {} 4 | 5 | 6 | return agent_db -------------------------------------------------------------------------------- /tool/restart.sh: -------------------------------------------------------------------------------- 1 | pkill skynet;rm -f console.log;touch console.log;./run_all.sh 1;cat console.log;tail -f console.log 2 | -------------------------------------------------------------------------------- /logic/desk/desk_impl.lua: -------------------------------------------------------------------------------- 1 | local desk_ctrl = require "desk.desk_ctrl" 2 | 3 | local desk_impl = {} 4 | 5 | 6 | 7 | return desk_impl -------------------------------------------------------------------------------- /tool/stop_game.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | index=$1 3 | pkill -u `whoami` -xf "../skynet/skynet ../config/config_xpnn$index __default__" 4 | -------------------------------------------------------------------------------- /logic/config/xpnn_module.lua: -------------------------------------------------------------------------------- 1 | local modules = {} 2 | 3 | modules.desk = "desk.desk_impl" 4 | modules.xpnn = "xpnn.xpnn_impl" 5 | 6 | return modules -------------------------------------------------------------------------------- /tool/run_redis.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | dir=.. 3 | branch=__default__ 4 | redisdir=$dir/redis 5 | 6 | #start redis 7 | ($redisdir/redis-server $redisdir/redis.conf &) 8 | 9 | -------------------------------------------------------------------------------- /proto/sproto/sproto/room.sproto: -------------------------------------------------------------------------------- 1 | .room { 2 | .EnterRoomReply{ 3 | room_id 0 : integer 4 | roomproxy 1 : string 5 | } 6 | 7 | .ExitRoomReply { 8 | 9 | } 10 | } -------------------------------------------------------------------------------- /tool/run_test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | dir=.. 3 | branch=__default__ 4 | redisdir=$dir/redis 5 | skynetdir=$dir/skynet 6 | 7 | ($skynetdir/skynet $dir/config/config_test $branch ) 8 | -------------------------------------------------------------------------------- /tool/run_test1.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | dir=.. 3 | branch=__default__ 4 | redisdir=$dir/redis 5 | skynetdir=$dir/skynet 6 | 7 | ($skynetdir/skynet $dir/config/config_test1 $branch ) 8 | -------------------------------------------------------------------------------- /preload/server_define.lua: -------------------------------------------------------------------------------- 1 | 2 | SERVER = { 3 | GATE = 1, 4 | DB = 2, 5 | LOGIN = 3, 6 | HALL = 4, 7 | LOCATOR = 6, 8 | CHAT = 7, 9 | AUTH = 8, 10 | GAME = 21, 11 | } 12 | -------------------------------------------------------------------------------- /tool/client.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | #export ROOT=$(cd `dirname $0`; pwd) 3 | export ROOT=$(dirname $(pwd)) 4 | 5 | $ROOT/skynet/3rd/lua/lua $ROOT/client/simpleclient.lua $ROOT "127.0.0.1" $1 6 | 7 | -------------------------------------------------------------------------------- /logic/push/push_impl.lua: -------------------------------------------------------------------------------- 1 | local push_impl = {} 2 | 3 | function push_impl.get_push_info() 4 | 5 | end 6 | 7 | function push_impl.get_push_details() 8 | 9 | end 10 | 11 | return push_impl -------------------------------------------------------------------------------- /logic/role/role_impl.lua: -------------------------------------------------------------------------------- 1 | local role_ctrl = require "role.role_ctrl" 2 | local role_impl = {} 3 | 4 | 5 | -- function role_impl.visitor_login(ctx, req) 6 | 7 | -- end 8 | 9 | return role_impl -------------------------------------------------------------------------------- /tool/killnode.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | if [ $# == 1 ]; then 4 | progress=$1 5 | ps -ef | grep skynet | grep $progress | awk '{print $2}' | xargs kill -9 6 | echo "kill $progress ok" 7 | fi 8 | -------------------------------------------------------------------------------- /logic/cash/cash_ctrl.lua: -------------------------------------------------------------------------------- 1 | local charge_ctrl = {} 2 | 3 | 4 | function charge_ctrl.on_login() 5 | 6 | end 7 | 8 | function charge_ctrl.on_logout() 9 | 10 | end 11 | 12 | return charge_ctrl -------------------------------------------------------------------------------- /proto/sproto/sproto/gate.sproto: -------------------------------------------------------------------------------- 1 | .gate { 2 | .NetworkEvent { 3 | type 0 : integer #网络事件类型 4 | } 5 | 6 | .HeartbeatReq{ 7 | 8 | } 9 | 10 | .HeartbeatReply{ 11 | 12 | } 13 | 14 | .Ping{ 15 | 16 | } 17 | } -------------------------------------------------------------------------------- /preload/game_define.lua: -------------------------------------------------------------------------------- 1 | 2 | ERR_SEAT = 255 3 | 4 | SEAT_STATE = { 5 | null = 0x00, 6 | unready = 0x01, 7 | ready = 0x02, 8 | gaming = 0x04, 9 | offline = 0x08, 10 | exit = 0x10, 11 | game_end = 0x12, 12 | } 13 | -------------------------------------------------------------------------------- /proto/sproto/sproto/chat.sproto: -------------------------------------------------------------------------------- 1 | .chat { 2 | .ChatReq { 3 | type 0 : integer 4 | context 1 : string 5 | } 6 | 7 | .ChatEvent{ 8 | type 0 : integer 9 | from 1 : integer 10 | context 2 : string 11 | } 12 | } -------------------------------------------------------------------------------- /proto/sproto/sproto/desk.sproto: -------------------------------------------------------------------------------- 1 | .desk{ 2 | 3 | 4 | .AddCdEvent{ 5 | id 0 : integer 6 | cd_type 1 : integer 7 | cd_time 2 : integer 8 | } 9 | 10 | .DelCdEvent{ 11 | id 0 : integer 12 | cd_type 1 : integer 13 | } 14 | } -------------------------------------------------------------------------------- /tool/stop.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | branch=__default__ 4 | 5 | #stop skynet 6 | ps -ef | grep skynet | grep $branch | awk '{print $2}' | xargs kill -9 7 | 8 | #stop redis 9 | ps -ef | grep redis | grep -v 'grep' | awk '{print $2}' | xargs kill -9 -------------------------------------------------------------------------------- /logic/desk/desk_const.lua: -------------------------------------------------------------------------------- 1 | local desk_const = {} 2 | 3 | 4 | desk_const.DESK_CALLBACK = { 5 | login_desk = 1, 6 | logout_desk = 2, 7 | player_disconnect = 3, 8 | player_reconnect = 4, 9 | can_group = 5, 10 | } 11 | 12 | return desk_const -------------------------------------------------------------------------------- /test/test_table_func.lua: -------------------------------------------------------------------------------- 1 | local tb = { 2 | [1] = "hello", 3 | [2] = "world", 4 | [3] = "i am worker", 5 | [4] = "hao are you", 6 | } 7 | 8 | 9 | table.insert(tb, 1, "zhangsan") 10 | 11 | print(table.unpack(tb)) 12 | 13 | print(1.00 == 1) -------------------------------------------------------------------------------- /lualib/secret.lua: -------------------------------------------------------------------------------- 1 | local crypt = require "skynet_crypt" 2 | 3 | local secret ={} 4 | 5 | function secret.make_key() 6 | local random_key = crypt.randomkey() 7 | return crypt.base64encode(crypt.dhexchange(random_key)) 8 | end 9 | 10 | return secret -------------------------------------------------------------------------------- /proto/xproto/xproto/package.xproto: -------------------------------------------------------------------------------- 1 | 2 | #包头信息 3 | .Package { 4 | protoid 0 : integer #协议id 5 | session 1 : integer #消息id 6 | errorcode 2 : integer #错误id 7 | datasize 3 : integer #包体长 8 | gameproxy 4 : string #游戏代理 9 | } 10 | 11 | -------------------------------------------------------------------------------- /preload/preload.lua: -------------------------------------------------------------------------------- 1 | require "luaext" 2 | require "rpc_error" 3 | require "error_code" 4 | require "system_define" 5 | require "service_define" 6 | require "server_define" 7 | require "module_define" 8 | require "proto_map" 9 | require "db_define" 10 | require "game_define" -------------------------------------------------------------------------------- /config/config_test: -------------------------------------------------------------------------------- 1 | ---------------------------public----------------------- 2 | include("config_public") 3 | --------------------------------------------------------- 4 | 5 | start = "main" 6 | logfile = "test.log" 7 | debugPort = 7001 8 | luaservice = luaservice .. "../service/test/?.lua;" -------------------------------------------------------------------------------- /config/config_test1: -------------------------------------------------------------------------------- 1 | ---------------------------public----------------------- 2 | include("config_public") 3 | --------------------------------------------------------- 4 | 5 | start = "main1" 6 | logfile = "test.log" 7 | debugPort = 7001 8 | luaservice = luaservice .. "../service/test/?.lua;" -------------------------------------------------------------------------------- /logic/desk/desk_db.lua: -------------------------------------------------------------------------------- 1 | local db_mgr = require "common.db_mgr" 2 | 3 | local desk_db = {} 4 | 5 | local IncrDealIdKey = "incr_deal_id" 6 | 7 | function desk_db.incr_deal_id() 8 | local redisdb = db_mgr.get_redis_db() 9 | return tonumber(redisdb:incr(IncrDealIdKey)) 10 | end 11 | 12 | return desk_db -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "skynet"] 2 | path = skynet 3 | url = https://github.com/najsword/skynet.git 4 | [submodule "redis"] 5 | path = redis 6 | url = https://github.com/najsword/redis.git 7 | branch = 3.2 8 | [submodule "lsocket"] 9 | path = lsocket 10 | url = https://github.com/najsword/lsocket.git 11 | -------------------------------------------------------------------------------- /logic/config/hall_config.lua: -------------------------------------------------------------------------------- 1 | local config_helper = require "config_helper" 2 | 3 | local config = {} 4 | 5 | config.game_type_config = config_helper.new("game_type_config") 6 | config.game_config = config_helper.new("game_config") 7 | config.game_room_config = config_helper.new("game_room_config") 8 | 9 | return config -------------------------------------------------------------------------------- /logic/config/xpnn_config.lua: -------------------------------------------------------------------------------- 1 | local config_helper = require "config_helper" 2 | 3 | local config = {} 4 | 5 | config.game_type_config = config_helper.new("game_type_config") 6 | config.game_config = config_helper.new("game_config") 7 | config.game_room_config = config_helper.new("game_room_config") 8 | 9 | return config -------------------------------------------------------------------------------- /test/test_ext.lua: -------------------------------------------------------------------------------- 1 | function math.round(num) 2 | return math.floor(num + 0.5) 3 | end 4 | 5 | function tonum(v, base) 6 | return tonumber(v, base) or 0 7 | end 8 | 9 | function toint(v) 10 | return math.round(tonum(v)) 11 | end 12 | 13 | print(toint(1/2)) 14 | print(toint(2/2)) 15 | print(toint(3/2)) -------------------------------------------------------------------------------- /config/config_db: -------------------------------------------------------------------------------- 1 | ---------------------------public----------------------- 2 | include("config_public") 3 | --------------------------------------------------------- 4 | 5 | start = "main" 6 | logfile = "db.log" 7 | debugPort = 7001 8 | luaservice = luaservice .. "../service/dbserver/?.lua;" 9 | cluster_server_id = 3 -------------------------------------------------------------------------------- /proto/sproto/spb/hall.spb: -------------------------------------------------------------------------------- 1 | ! hallXhall.PlayerOnlineState4room_id roomproxyp hall.RoomInstU roomproxy 2 | player_num player_limit=hall.RoomInstList 3 | room_insts -------------------------------------------------------------------------------- /test/test_binary.lua: -------------------------------------------------------------------------------- 1 | local binary = string.char( 39, 22, 18, 17, 23, 0, 18, 0, 57 ) 2 | local str = "aaa" 3 | 4 | local result = binary .. str 5 | print(result) 6 | 7 | local req = "10101" 8 | print("req=", req, "sz=", #req) 9 | 10 | local boolean_bin = string.pack(">B", 3) 11 | print(string.unpack(">B", boolean_bin)) -------------------------------------------------------------------------------- /tool/run.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | dir=.. 3 | branch=__default__ 4 | redisdir=$dir/redis 5 | skynetdir=$dir/skynet 6 | 7 | cd $dir 8 | sh stop.sh > /dev/null 2>&1 9 | 10 | #start redis 11 | ($redisdir/redis-server $redisdir/redis.conf &) 12 | 13 | #start skynet 14 | ($skynetdir/skynet $dir/config/config_test $branch &) 15 | -------------------------------------------------------------------------------- /logic/locator/locator_impl.lua: -------------------------------------------------------------------------------- 1 | local locator_ctrl = require "locator.locator_ctrl" 2 | 3 | local locator_impl = {} 4 | 5 | 6 | function locator_impl.init() 7 | locator_ctrl.init() 8 | end 9 | 10 | function locator_impl.route_sid(req) 11 | locator_ctrl.route_sid(req.game_id) 12 | end 13 | 14 | return locator_impl -------------------------------------------------------------------------------- /tool/run_db.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | dir=.. 3 | branch=__default__ 4 | redisdir=$dir/redis 5 | skynetdir=$dir/skynet 6 | 7 | 8 | #start skynet 9 | if [ -n "$1" ] ;then 10 | ($skynetdir/skynet $dir/config/config_db $branch) 11 | else 12 | nohup $skynetdir/skynet $dir/config/config_db $branch >> console.log 2>&1 & 13 | fi -------------------------------------------------------------------------------- /tool/run_hall.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | dir=.. 3 | branch=__default__ 4 | redisdir=$dir/redis 5 | skynetdir=$dir/skynet 6 | 7 | #start skynet 8 | if [ -n "$1" ] ;then 9 | ($skynetdir/skynet $dir/config/config_hall $branch) 10 | else 11 | nohup $skynetdir/skynet $dir/config/config_hall $branch >> console.log 2>&1 & 12 | fi -------------------------------------------------------------------------------- /tool/run_auth.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | dir=.. 3 | branch=__default__ 4 | redisdir=$dir/redis 5 | skynetdir=$dir/skynet 6 | 7 | 8 | #start skynet 9 | if [ -n "$1" ] ;then 10 | ($skynetdir/skynet $dir/config/config_auth $branch) 11 | else 12 | nohup $skynetdir/skynet $dir/config/config_auth $branch >> console.log 2>&1 & 13 | fi -------------------------------------------------------------------------------- /tool/run_chat.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | dir=.. 3 | branch=__default__ 4 | redisdir=$dir/redis 5 | skynetdir=$dir/skynet 6 | 7 | 8 | #start skynet 9 | if [ -n "$1" ] ;then 10 | ($skynetdir/skynet $dir/config/config_chat $branch) 11 | else 12 | nohup $skynetdir/skynet $dir/config/config_chat $branch >> console.log 2>&1 & 13 | fi -------------------------------------------------------------------------------- /tool/run_locator.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | dir=.. 3 | branch=__default__ 4 | redisdir=$dir/redis 5 | skynetdir=$dir/skynet 6 | 7 | #start skynet 8 | if [ -n "$1" ] ;then 9 | ($skynetdir/skynet $dir/config/config_locator $branch) 10 | else 11 | nohup $skynetdir/skynet $dir/config/config_locator $branch >> console.log 2>&1 & 12 | fi -------------------------------------------------------------------------------- /tool/run_login.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | dir=.. 3 | branch=__default__ 4 | redisdir=$dir/redis 5 | skynetdir=$dir/skynet 6 | 7 | 8 | #start skynet 9 | if [ -n "$1" ] ;then 10 | ($skynetdir/skynet $dir/config/config_login $branch) 11 | else 12 | nohup $skynetdir/skynet $dir/config/config_login $branch >> console.log 2>&1 & 13 | fi -------------------------------------------------------------------------------- /logic/config/locator_server_config.lua: -------------------------------------------------------------------------------- 1 | 2 | local locator_server_config = { 3 | [21] = { 4 | game_id = 101, 5 | player_limit = 10000, 6 | }, 7 | [22] = { 8 | game_id = 101, 9 | player_limit = 10000, 10 | }, 11 | [23] = { 12 | game_id = 101, 13 | player_limit = 10000, 14 | }, 15 | } 16 | return locator_server_config -------------------------------------------------------------------------------- /tool/run_gate.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | dir=.. 3 | branch=__default__ 4 | redisdir=$dir/redis 5 | skynetdir=$dir/skynet 6 | 7 | 8 | #start skynet 9 | if [ -n "$1" ] ;then 10 | ($skynetdir/skynet $dir/config/config_gate $branch) 11 | else 12 | nohup $skynetdir/skynet $dir/config/config_gate $branch >> console.log 2>&1 & 13 | fi 14 | -------------------------------------------------------------------------------- /config/config_locator: -------------------------------------------------------------------------------- 1 | ---------------------------public----------------------- 2 | include("config_public") 3 | --------------------------------------------------------- 4 | 5 | start = "main" 6 | logfile = "locator.log" 7 | debugPort = 7005 8 | cluster_server_id = 6 9 | luaservice = luaservice .. "../service/locatorserver/?.lua;" 10 | -------------------------------------------------------------------------------- /logic/bank/bank_impl.lua: -------------------------------------------------------------------------------- 1 | 2 | local bank_impl = {} 3 | 4 | function bank_impl.get_bank_info(ctx, req) 5 | 6 | end 7 | 8 | function bank_impl.gold_from_bank(ctx, req) 9 | 10 | end 11 | 12 | function bank_impl.gold_to_bank(ctx, req) 13 | 14 | end 15 | 16 | function bank_impl.bank_trade_details(ctx, req) 17 | 18 | end 19 | 20 | return bank_impl -------------------------------------------------------------------------------- /logic/chat/chat_impl.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local chat_ctrl = require "chat.chat_ctrl" 3 | local chat_impl = {} 4 | 5 | 6 | function chat_impl.chat_req(ctx, req) 7 | return chat_ctrl.chat_req(ctx, req) 8 | end 9 | 10 | function chat_impl.set_ctx(ctx) 11 | return chat_ctrl.set_ctx(ctx) 12 | end 13 | 14 | 15 | return chat_impl -------------------------------------------------------------------------------- /config/config_chat: -------------------------------------------------------------------------------- 1 | ---------------------------public----------------------- 2 | include("config_public") 3 | --------------------------------------------------------- 4 | 5 | start = "main" 6 | logfile = "chat.log" 7 | debugPort = 7001 8 | cluster_server_id = 7 9 | luaservice = luaservice .. "../service/chatserver/?.lua;" 10 | 11 | chat_logic_count = 3 -------------------------------------------------------------------------------- /logic/cash/cash_impl.lua: -------------------------------------------------------------------------------- 1 | 2 | local cash_impl = {} 3 | 4 | function cash_impl.get_deposit_agent_list(ctx, req) 5 | 6 | end 7 | 8 | function cash_impl.alipay_deposit(ctx, req) 9 | 10 | end 11 | 12 | function cash_impl.weixinpay_deposit(ctx, req) 13 | 14 | end 15 | 16 | function cash_impl.bank_card_deposit(ctx, req) 17 | 18 | end 19 | 20 | return cash_impl -------------------------------------------------------------------------------- /logic/config/create_role_config.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | 配置id, 头像id, 姓别, 金币 3 | id, head_id, sex, gold 4 | ]] 5 | local create_role_config = { 6 | [1] = { 7 | id = 1, 8 | head_id = 1, 9 | sex = 1, 10 | gold = 100, 11 | }, 12 | [2] = { 13 | id = 2, 14 | head_id = 2, 15 | sex = 2, 16 | gold = 100, 17 | }, 18 | } 19 | return create_role_config -------------------------------------------------------------------------------- /tool/run_xpnn.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | dir=.. 3 | branch=__default__ 4 | redisdir=$dir/redis 5 | skynetdir=$dir/skynet 6 | 7 | index=$1 8 | 9 | #start skynet 10 | if [ -n "$2" ] ;then 11 | ($skynetdir/skynet $dir/config/config_xpnn$index $branch) 12 | else 13 | nohup $skynetdir/skynet $dir/config/config_xpnn$index $branch > xpnn_$index.log 2>&1 & 14 | fi -------------------------------------------------------------------------------- /logic/config/create_player_config.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | 配置id, 头像id, 姓别, 金币 3 | id, head_id, sex, gold 4 | ]] 5 | local create_player_config = { 6 | [1] = { 7 | id = 1, 8 | head_id = 1, 9 | sex = 1, 10 | gold = 100, 11 | }, 12 | [2] = { 13 | id = 2, 14 | head_id = 2, 15 | sex = 2, 16 | gold = 100, 17 | }, 18 | } 19 | return create_player_config -------------------------------------------------------------------------------- /proto/sproto/sproto/hall.sproto: -------------------------------------------------------------------------------- 1 | .hall { 2 | .RoomInst{ 3 | roomproxy 0 : string #房间实例代理 4 | player_num 1 : integer #当前玩家数量 5 | player_limit 2 : integer #房间玩家上限 6 | } 7 | 8 | .RoomInstList{ 9 | room_insts 0 : *RoomInst #游戏房间实例 10 | } 11 | 12 | .PlayerOnlineState{ 13 | room_id 0 : integer #玩家所在房间id 14 | roomproxy 1 : string #玩家所在房间代理 15 | } 16 | } -------------------------------------------------------------------------------- /proto/sproto/sproto/package.sproto: -------------------------------------------------------------------------------- 1 | .Package { 2 | protoid 0 : integer #协议id 3 | session 1 : integer #消息id 4 | errorcode 2 : integer #错误id 5 | roomproxy 3 : integer #gameid 6 | datasize 4 : integer #包体长 7 | response 5 : integer #默认是请求包(nil) 8 | roomtype 6 : integer #房间类型 9 | packid 7 : integer #包id 10 | playerid 8 : integer 11 | } -------------------------------------------------------------------------------- /config/config_login: -------------------------------------------------------------------------------- 1 | ---------------------------public----------------------- 2 | include("config_public") 3 | --------------------------------------------------------- 4 | 5 | start = "main" 6 | logfile = "login.log" 7 | debugPort = 7001 8 | cluster_server_id = 4 9 | luaservice = luaservice .. "../service/loginserver/?.lua;" 10 | 11 | 12 | 13 | login_logic_count = 2 -------------------------------------------------------------------------------- /preload/db_define.lua: -------------------------------------------------------------------------------- 1 | GET_SVC_TYPE = { 2 | unique = 1, 3 | robin = 2, 4 | player_id = 3, 5 | } 6 | 7 | DB_SERVICE = { 8 | unique = "unique_db", 9 | account = "account_db", 10 | hall = "hall_db", 11 | game = "game_db", 12 | agent = "agent_db", 13 | fd = "fd_db", 14 | auth = "auth_db", 15 | } 16 | 17 | CACHE_TYPE = { 18 | memory = 0, 19 | redis = 1, 20 | mysql = 2, 21 | } -------------------------------------------------------------------------------- /config/config_hall: -------------------------------------------------------------------------------- 1 | ---------------------------public----------------------- 2 | include("config_public") 3 | --------------------------------------------------------- 4 | 5 | start = "main" 6 | logfile = "hall.log" 7 | debugPort = 7005 8 | cluster_server_id = 5 9 | luaservice = luaservice .. "../service/hallserver/?.lua;" 10 | 11 | 12 | 13 | hall_logic_count = 4 14 | agent_count = 4 -------------------------------------------------------------------------------- /proto/sproto/spb/area.spb: -------------------------------------------------------------------------------- 1 | u areaXarea.CreateRoleReq8 create_indexnickname8area.EnterAreaReqgame_idUarea.EnterRoomReply4room_id roomproxy9area.EnterRoomReqroomtypearea.ExitAreaReqarea.GetRoleReq -------------------------------------------------------------------------------- /logic/xpnn/xpnn_const.lua: -------------------------------------------------------------------------------- 1 | local xpnn_const = {} 2 | 3 | xpnn_const.CD_TYPE = { 4 | 5 | } 6 | 7 | xpnn_const.GAME_STATE = { 8 | ready_begin = 0, --用到 9 | gaming = 1, --用到 10 | qiang_banker = 2, 11 | bet = 3, 12 | open_card = 4, 13 | game_end = 5, --用到 14 | exit = 6, --用到 15 | } 16 | 17 | xpnn_const.MAX_QIANG_BANKER_TIMES = 4 18 | xpnn_const.MAX_BET_TIMES = 5 19 | 20 | return xpnn_const -------------------------------------------------------------------------------- /service/chat_logic_svc.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local command = require "command_base" 3 | local chat_logic = require "chat.chat_logic" 4 | 5 | function command.chat_req(ctx, req) 6 | return chat_logic.chat_req(ctx, req) 7 | end 8 | 9 | function command.set_ctx(ctx, req) 10 | return chat_logic.set_ctx(ctx, req) 11 | end 12 | 13 | skynet.start(function() 14 | 15 | end) 16 | -------------------------------------------------------------------------------- /config/config_auth: -------------------------------------------------------------------------------- 1 | ---------------------------public----------------------- 2 | include("config_public") 3 | --------------------------------------------------------- 4 | 5 | start = "main" 6 | logfile = "auth.log" 7 | debugPort = 7008 8 | luaservice = luaservice .. "../service/authserver/?.lua;" 9 | cluster_server_id = 8 10 | 11 | websocket_test = true 12 | watchdog_port = 5003 13 | auth_logic_count = 2 14 | -------------------------------------------------------------------------------- /logic/role/role_ctrl.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local role_logic = require "role.role_logic" 3 | local role_ctrl = {} 4 | 5 | 6 | function role_ctrl.init() 7 | 8 | end 9 | 10 | function role_ctrl.get_role(ctx, req) 11 | return role_logic.get_role_id(ctx, req) 12 | end 13 | 14 | function role_ctrl.create_role(ctx, req) 15 | return role_logic.create_role(ctx, req) 16 | end 17 | 18 | return role_ctrl -------------------------------------------------------------------------------- /config/config_xpnn: -------------------------------------------------------------------------------- 1 | ---------------------------public----------------------- 2 | include("config_public") 3 | --------------------------------------------------------- 4 | 5 | start = "main" 6 | logfile = "xunpinnn.log" 7 | debugPort = 7006 8 | cluster_server_id = 21 9 | luaservice = luaservice .. "../service/gameserver/xpnn/?.lua;" 10 | 11 | init_desk_count = 10 12 | init_agent_count = 0 13 | room_id = 10101 14 | -------------------------------------------------------------------------------- /logic/common/db_module.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | 3 | local db_modules = {} 4 | local modules = {} 5 | 6 | setmetatable(db_modules, { 7 | __index = function(t, k) 8 | local mod = modules[k] 9 | if mod ~= nil then 10 | return mod 11 | end 12 | mod = require(k.."."..k.."_db") 13 | if mod then 14 | modules[k] = mod 15 | end 16 | return mod 17 | end 18 | }) 19 | 20 | return db_modules 21 | 22 | -------------------------------------------------------------------------------- /config/config_gate: -------------------------------------------------------------------------------- 1 | ---------------------------public----------------------- 2 | include("config_public") 3 | --------------------------------------------------------- 4 | 5 | start = "main" 6 | logfile = "gate.log" 7 | debugPort = 7001 8 | luaservice = luaservice .. "../service/gateserver/?.lua;" 9 | cluster_server_id = 1 10 | 11 | websocket_test = true 12 | 13 | http_port = 6001 14 | watchdog_port = 5001 15 | wswatchdog_port = 5002 -------------------------------------------------------------------------------- /logic/config/game_type_config.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | 游戏大类id, 游戏大类名称 3 | game_type, name 4 | ]] 5 | local game_type_config = { 6 | [100] = { 7 | game_type = 100, 8 | name = [[对战场]], 9 | }, 10 | [200] = { 11 | game_type = 200, 12 | name = [[百人场]], 13 | }, 14 | [300] = { 15 | game_type = 300, 16 | name = [[比赛场]], 17 | }, 18 | [400] = { 19 | game_type = 400, 20 | name = [[包厢场]], 21 | }, 22 | } 23 | return game_type_config -------------------------------------------------------------------------------- /preload/service_define.lua: -------------------------------------------------------------------------------- 1 | 2 | SERVICE = { 3 | CLUSTER_MONITOR = ".cluster_monitor", 4 | MASTER_DB = ".master_db", 5 | AGENT_MGR = ".agent_mgr", 6 | WATCHDOG = ".watchdog", 7 | AUTH = ".auth", 8 | LOGIN = ".login", 9 | HALL = ".hall", 10 | AREA = ".area", 11 | ROOM = ".room", 12 | LOCATOR = ".locator", 13 | CHAT = ".chat", 14 | HEARTBEAT = ".heartbeat", 15 | } -------------------------------------------------------------------------------- /config/config_xpnn21: -------------------------------------------------------------------------------- 1 | ---------------------------public----------------------- 2 | include("config_public") 3 | --------------------------------------------------------- 4 | 5 | start = "main" 6 | logfile = "xunpinnn1.log" 7 | debugPort = 7006 8 | cluster_server_id = 21 9 | luaservice = luaservice .. "../service/gameserver/xpnn/?.lua;" 10 | 11 | init_desk_count = 3 12 | init_agent_count = 0 13 | area_logic_count = 3 14 | game_id = 101 15 | -------------------------------------------------------------------------------- /config/config_xpnn22: -------------------------------------------------------------------------------- 1 | ---------------------------public----------------------- 2 | include("config_public") 3 | --------------------------------------------------------- 4 | 5 | start = "main" 6 | logfile = "xunpinnn2.log" 7 | debugPort = 7006 8 | cluster_server_id = 22 9 | luaservice = luaservice .. "../service/gameserver/xpnn/?.lua;" 10 | 11 | init_desk_count = 3 12 | init_agent_count = 0 13 | area_logic_count = 3 14 | game_id = 101 15 | -------------------------------------------------------------------------------- /config/config_xpnn23: -------------------------------------------------------------------------------- 1 | ---------------------------public----------------------- 2 | include("config_public") 3 | --------------------------------------------------------- 4 | 5 | start = "main" 6 | logfile = "xunpinnn3.log" 7 | debugPort = 7006 8 | cluster_server_id = 23 9 | luaservice = luaservice .. "../service/gameserver/xpnn/?.lua;" 10 | 11 | init_desk_count = 3 12 | init_agent_count = 0 13 | area_logic_count = 3 14 | game_id = 101 15 | -------------------------------------------------------------------------------- /proto/sproto/sproto/area.sproto: -------------------------------------------------------------------------------- 1 | .area { 2 | .EnterAreaReq { 3 | game_id 0 : integer 4 | } 5 | 6 | .ExitAreaReq { 7 | 8 | } 9 | 10 | .CreateRoleReq { 11 | create_index 0 : integer #创建索引 12 | nickname 1 : string #昵称 13 | } 14 | 15 | .GetRoleReq { 16 | } 17 | 18 | .EnterRoomReq { 19 | roomtype 0 : integer 20 | } 21 | 22 | .EnterRoomReply{ 23 | room_id 0 : integer 24 | roomproxy 1 : string 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /logic/heartbeat/heartbeat_impl.lua: -------------------------------------------------------------------------------- 1 | local heartbeat_ctrl = require "heartbeat.heartbeat_ctrl" 2 | 3 | local heartbeat_impl = {} 4 | 5 | 6 | function heartbeat_impl.init() 7 | heartbeat_ctrl.init() 8 | end 9 | 10 | function heartbeat_impl.reset_updatetime(player_id) 11 | heartbeat_ctrl.reset_updatetime(player_id) 12 | end 13 | 14 | function heartbeat_impl.del_playerId(player_id) 15 | heartbeat_ctrl.del_playerId(player_id) 16 | end 17 | 18 | return heartbeat_impl -------------------------------------------------------------------------------- /logic/xpnn/xpnn_impl.lua: -------------------------------------------------------------------------------- 1 | local xpnn_ctrl = require "xpnn.xpnn_ctrl" 2 | local xpnn_impl = {} 3 | 4 | function xpnn_impl.qry_desk(ctx, req) 5 | return xpnn_ctrl.qry_desk(ctx, req) 6 | end 7 | 8 | function xpnn_impl.qiang_banker(ctx, req) 9 | return xpnn_ctrl.qiang_banker(ctx, req) 10 | end 11 | 12 | function xpnn_impl.bet(ctx, req) 13 | return xpnn_ctrl.bet(ctx, req) 14 | end 15 | 16 | function xpnn_impl.open_card(ctx, req) 17 | return xpnn_ctrl.open_card(ctx, req) 18 | end 19 | 20 | return xpnn_impl -------------------------------------------------------------------------------- /logic/room/room_impl.lua: -------------------------------------------------------------------------------- 1 | local room_ctrl = require "room.room_ctrl" 2 | 3 | local room_impl = {} 4 | 5 | function room_impl.enter_room(ctx, req) 6 | return room_ctrl.enter_room(ctx, req) 7 | end 8 | 9 | function room_impl.exit_room(ctx, req) 10 | return room_ctrl.exit_room(ctx, req) 11 | end 12 | 13 | function room_impl.group_request(ctx, req) 14 | return room_ctrl.group_request(ctx, req) 15 | end 16 | 17 | function room_impl.logout_desk(ctx, req) 18 | return room_ctrl.logout_desk(ctx, req) 19 | end 20 | 21 | return room_impl -------------------------------------------------------------------------------- /logic/hall/hall_impl.lua: -------------------------------------------------------------------------------- 1 | local hall_ctrl = require "hall.hall_ctrl" 2 | local hall_impl = {} 3 | 4 | function hall_impl.cast_login(ctx, req) 5 | return hall_ctrl.cast_login(ctx, req) 6 | end 7 | 8 | function hall_impl.cast_logout(ctx, req) 9 | return hall_ctrl.cast_logout(ctx, req) 10 | end 11 | 12 | function hall_impl.get_room_inst_list(ctx, req) 13 | return hall_ctrl.get_room_inst_list(ctx, req) 14 | end 15 | 16 | function hall_impl.get_player_online_state(ctx, req) 17 | return hall_ctrl.get_player_online_state(ctx, req) 18 | end 19 | 20 | return hall_impl -------------------------------------------------------------------------------- /logic/login/login_impl.lua: -------------------------------------------------------------------------------- 1 | local login_ctrl = require "login.login_ctrl" 2 | 3 | local login_impl = {} 4 | 5 | function login_impl.signin_account(ctx, req) 6 | return login_ctrl.signin_account(ctx, req) 7 | end 8 | 9 | function login_impl.weixin_account(ctx, req) 10 | return login_ctrl.weixin_account(ctx, req) 11 | end 12 | 13 | function login_impl.vistor_account(ctx, req) 14 | return login_ctrl.vistor_account(ctx, req) 15 | end 16 | 17 | function login_impl.logout_account(ctx, req) 18 | return login_ctrl.logout_account(ctx, req) 19 | end 20 | 21 | return login_impl -------------------------------------------------------------------------------- /service/login_logic_svc.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local json = require "json" 3 | local command = require "command_base" 4 | local login_logic = require "login.login_logic" 5 | 6 | function command.signin_account(ctx, req) 7 | return login_logic.signin_account(ctx, req) 8 | end 9 | 10 | function command.weixin_account(ctx, req) 11 | return login_logic.weixin_account(ctx, req) 12 | end 13 | 14 | function command.vistor_account(ctx, req) 15 | return login_logic.vistor_account(ctx, req) 16 | end 17 | 18 | skynet.start(function() 19 | login_logic.init() 20 | end) 21 | -------------------------------------------------------------------------------- /service/chat.lua: -------------------------------------------------------------------------------- 1 | require "skynet.manager" 2 | local skynet = require "skynet" 3 | local service = require "service_base" 4 | local chat_ctrl = require "chat.chat_ctrl" 5 | local chat_impl = require "chat.chat_impl" 6 | local command = service.command 7 | 8 | function command.chat_req(ctx, req) 9 | return chat_ctrl.chat_req(ctx, req) 10 | end 11 | 12 | function command.set_ctx(ctx) 13 | return chat_ctrl.set_ctx(ctx) 14 | end 15 | 16 | function service.on_start() 17 | skynet.register(SERVICE.CHAT) 18 | chat_ctrl.init() 19 | end 20 | 21 | service.modules.chat = chat_impl 22 | service.start() -------------------------------------------------------------------------------- /service/test/test_share_memory1.lua: -------------------------------------------------------------------------------- 1 | require "skynet.manager" 2 | local skynet = require "skynet" 3 | local service = require "service_base" 4 | local requester = require "requester" 5 | local logger = require "logger" 6 | local command = service.command 7 | 8 | function command.test(str) 9 | skynet.error("recv from node="..str) 10 | logger.debug("recv from node="..str) 11 | 12 | requester.rpc_send("node", ".test", "test_send", "i'm node1, now test send.") 13 | 14 | return "node1 recv ok" 15 | end 16 | 17 | function service.on_start() 18 | skynet.register(".test1") 19 | end 20 | 21 | 22 | service.start() -------------------------------------------------------------------------------- /service/area_logic_svc.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local json = require "json" 3 | local command = require "command_base" 4 | local area_logic = require "area.area_logic" 5 | 6 | function command.enter_area(ctx, req) 7 | return area_logic.enter_area(ctx, req) 8 | end 9 | 10 | function command.exit_area(ctx, req) 11 | return area_logic.exit_area(ctx, req) 12 | end 13 | 14 | function command.get_role(ctx, req) 15 | return area_logic.get_role(ctx, req) 16 | end 17 | 18 | function command.create_role(ctx, req) 19 | return area_logic.create_role(ctx, req) 20 | end 21 | 22 | skynet.start(function() 23 | 24 | end) 25 | -------------------------------------------------------------------------------- /lualib/config_helper.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : config_helper.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | local skynet = require "skynet" 9 | local sharedata_core = require "skynet.sharedata.corelib" 10 | local pathprefix = skynet.getenv("pathprefix") or "config." 11 | 12 | local config_helper = {} 13 | 14 | function config_helper.new(path) 15 | local data = require(pathprefix .. path) 16 | local cobj = sharedata_core.host.new(data) 17 | package.loaded[pathprefix .. path] = nil 18 | return cobj 19 | end 20 | 21 | 22 | return config_helper 23 | -------------------------------------------------------------------------------- /proto/sproto/sproto/login.sproto: -------------------------------------------------------------------------------- 1 | .login { 2 | 3 | .LoginReq { 4 | account 0 : string #帐号 5 | password 1 : string #密码 6 | login_addr 2 : integer 7 | subid 3 : integer 8 | player_id 4 : integer 9 | } 10 | 11 | .LoginReply { 12 | 13 | } 14 | 15 | .LogoutReq { 16 | 17 | } 18 | 19 | .LogoutReply { 20 | 21 | } 22 | 23 | .WeixinLoginReq { 24 | login_addr 0 : integer 25 | subid 1 : integer 26 | } 27 | 28 | .WeixinLoginReply { 29 | 30 | } 31 | 32 | .VistorLoginReq { 33 | login_addr 0 : integer 34 | subid 1 : integer 35 | } 36 | 37 | .VistorLoginReply { 38 | 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /logic/auth/auth_impl.lua: -------------------------------------------------------------------------------- 1 | local auth_ctrl = require "auth.auth_ctrl" 2 | 3 | local auth_impl = {} 4 | 5 | function auth_impl.register_account(ctx, req) 6 | return auth_ctrl.register_account(ctx, req) 7 | end 8 | 9 | function auth_impl.login_account(ctx, req) 10 | return auth_ctrl.login_account(ctx, req) 11 | end 12 | 13 | function auth_impl.weixin_login(ctx, req) 14 | return auth_ctrl.weixin_login(ctx, req) 15 | end 16 | 17 | function auth_impl.visitor_login(ctx, req) 18 | return auth_ctrl.visitor_login(ctx, req) 19 | end 20 | 21 | function auth_impl.auth_secret(ctx, req) 22 | return auth_ctrl.auth_secret(ctx, req) 23 | end 24 | 25 | return auth_impl -------------------------------------------------------------------------------- /logic/area/area_impl.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local area_ctrl = require "area.area_ctrl" 3 | local area_impl = {} 4 | 5 | 6 | function area_impl.enter_area(ctx, req) 7 | return area_ctrl.enter_area(ctx, req) 8 | end 9 | 10 | function area_impl.exit_area(ctx, req) 11 | return area_ctrl.exit_area(ctx, req) 12 | end 13 | 14 | function area_impl.get_role(ctx, req) 15 | return area_ctrl.get_role(ctx, req) 16 | end 17 | 18 | function area_impl.create_role(ctx, req) 19 | return area_ctrl.create_role(ctx, req) 20 | end 21 | 22 | function area_impl.enter_room(ctx, req) 23 | return area_ctrl.enter_room(ctx, req) 24 | end 25 | 26 | return area_impl -------------------------------------------------------------------------------- /service/auth.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : auth.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | require "skynet.manager" 9 | local skynet = require "skynet" 10 | local service = require "service_base" 11 | local auth_ctrl = require "auth.auth_ctrl" 12 | local auth_impl = require "auth.auth_impl" 13 | local command = service.command 14 | 15 | function command.cast_logout(fd) 16 | return auth_ctrl.cast_logout(fd) 17 | end 18 | 19 | function service.on_start() 20 | skynet.register(SERVICE.AUTH) 21 | auth_ctrl.init() 22 | end 23 | 24 | service.modules.auth = auth_impl 25 | service.start() -------------------------------------------------------------------------------- /service/locator.lua: -------------------------------------------------------------------------------- 1 | require "skynet.manager" 2 | local skynet = require "skynet" 3 | local service = require "service_base" 4 | local locator_ctrl = require "locator.locator_ctrl" 5 | local locator_impl = require "locator.locator_impl" 6 | local command = service.command 7 | 8 | function command.route_sid(req) 9 | return locator_ctrl.route_sid(req.game_id) 10 | end 11 | 12 | function command.register_start_info(server_id) 13 | return locator_ctrl.register_start_info(server_id) 14 | end 15 | 16 | function service.on_start() 17 | skynet.register(SERVICE.LOCATOR) 18 | locator_ctrl.init() 19 | end 20 | 21 | service.modules.locator = locator_impl 22 | service.start() -------------------------------------------------------------------------------- /service/test/test_share_memory.lua: -------------------------------------------------------------------------------- 1 | require "skynet.manager" 2 | local skynet = require "skynet" 3 | local service = require "service_base" 4 | local requester = require "requester" 5 | local logger = require "logger" 6 | local command = service.command 7 | 8 | function command.test_send(str) 9 | skynet.error(str) 10 | logger.debug(str) 11 | end 12 | 13 | function service.on_start() 14 | skynet.register(".test") 15 | skynet.fork(function() 16 | skynet.sleep(2 * 100) 17 | for i = 1, 2 do 18 | print(requester.rpc_call("node1", ".test1", "test", "this is test, I'm node")) 19 | skynet.sleep(2 * 100) 20 | end 21 | end) 22 | end 23 | 24 | 25 | service.start() -------------------------------------------------------------------------------- /lualib/guid.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : guid.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | 9 | local template ="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" 10 | return function() 11 | local data = io.open("/dev/urandom", "r"):read(4) 12 | math.randomseed(os.time() + data:byte(1) + (data:byte(2) * 256) + (data:byte(3) * 65536) + (data:byte(4) * 4294967296)) 13 | return string.gsub(template, "x", function (c) 14 | local v = (c == "x") and math.random(0, 0xf) or math.random(8, 0xb) 15 | return string.format("%x", v) 16 | end) 17 | end 18 | 19 | -------------------------------------------------------------------------------- /lualib/protobuf_helper.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : protobuf_helper.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | local protobuf = require "protobuf" 9 | local proto_map = require "proto_map" 10 | local protobuf_helper = {} 11 | 12 | 13 | function protobuf_helper.pack(header, data) 14 | 15 | end 16 | 17 | function protobuf_helper.unpack(buffer) 18 | 19 | end 20 | 21 | function protobuf_helper.register(path) 22 | path = path or "proto/protobuf/pb/" 23 | for _, file in pairs(PROTO_FILES) do 24 | protobuf.register_file(path .. file..".pb") 25 | end 26 | end 27 | 28 | return protobuf_helper -------------------------------------------------------------------------------- /test/test_xpnn_logic.lua: -------------------------------------------------------------------------------- 1 | local root = "../" 2 | local skynet_root = root .. "skynet/" 3 | package.path = skynet_root .. "lualib/?.lua;"..root.."preload/?.lua;"..root.."logic/?.lua;" 4 | 5 | require "luaext" 6 | 7 | local xpnn_logic = require "xpnn.xpnn_logic" 8 | 9 | print(xpnn_logic.get_card_type({ 0x01, 0x02, 0x03, 0x04, 0x05 })) 10 | print(xpnn_logic.get_card_type({ 0x08, 0x18, 0x28, 0x03, 0x02 })) 11 | print(xpnn_logic.get_card_type({ 0x0a, 0x02, 0x08, 0x12, 0x0b })) 12 | print(xpnn_logic.get_card_type({ 0x0d, 0x03, 0x07, 0x0a, 0x0b })) 13 | print(xpnn_logic.get_card_type({ 0x0d, 0x0c, 0x1c, 0x2b, 0x0a })) 14 | print(xpnn_logic.get_card_type({ 0x0d, 0x0c, 0x1c, 0x2b, 0x0b })) 15 | -------------------------------------------------------------------------------- /service/heartbeat.lua: -------------------------------------------------------------------------------- 1 | require "skynet.manager" 2 | local skynet = require "skynet" 3 | local service = require "service_base" 4 | local heartbeat_ctrl = require "heartbeat.heartbeat_ctrl" 5 | local heartbeat_impl = require "heartbeat.heartbeat_impl" 6 | local command = service.command 7 | 8 | function command.reset_updatetime(player_id) 9 | return heartbeat_ctrl.reset_updatetime(player_id) 10 | end 11 | 12 | function command.del_playerId(player_id) 13 | return heartbeat_ctrl.del_playerId(player_id) 14 | end 15 | 16 | function service.on_start() 17 | skynet.register(SERVICE.HEARTBEAT) 18 | heartbeat_ctrl.init() 19 | end 20 | 21 | service.modules.heartbeat = heartbeat_impl 22 | service.start() -------------------------------------------------------------------------------- /service/login.lua: -------------------------------------------------------------------------------- 1 | require "skynet.manager" 2 | local skynet = require "skynet" 3 | local service = require "service_base" 4 | local login_ctrl = require "login.login_ctrl" 5 | local login_impl = require "login.login_impl" 6 | local command = service.command 7 | 8 | function command.cast_logout(fd) 9 | return login_ctrl.cast_logout(fd) 10 | end 11 | 12 | function command.logout_account(ctx, req) 13 | skynet.error("cy============================ login logout_account") 14 | return login_ctrl.logout_account(ctx, req) 15 | end 16 | 17 | function service.on_start() 18 | skynet.register(SERVICE.LOGIN) 19 | login_ctrl.init() 20 | end 21 | 22 | service.modules.login = login_impl 23 | service.start() -------------------------------------------------------------------------------- /logic/config/mysql_config.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | mysql连接id, mysql地址, mysql端口, mysql库名, mysql用户名, mysql密码, mysql包最大长度, 注释 3 | id, host, port, database, user, password, max_packet_size, desc 4 | ]] 5 | local mysql_config = { 6 | [1] = { 7 | id = 1, 8 | host = "127.0.0.1", 9 | port = 3306, 10 | database = "game", 11 | user = "mmouser", 12 | password = "Mm!Ou@S2e1R", 13 | max_packet_size = 1048576, 14 | desc = [[game库]], 15 | }, 16 | [2] = { 17 | id = 2, 18 | host = "127.0.0.1", 19 | port = 3306, 20 | database = "gamelog", 21 | user = "gamelog", 22 | password = "gamelog!Zsq1214", 23 | max_packet_size = 1048576, 24 | desc = [[gamelog库]], 25 | }, 26 | } 27 | return mysql_config -------------------------------------------------------------------------------- /logic/config/game_config.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | 游戏id, 游戏大类, 游戏名称, 是否开放, 展示序号, 游戏模块 3 | game_id, game_type, game_name, is_open, show_index, module_name 4 | ]] 5 | local game_config = { 6 | [101] = { 7 | game_id = 101, 8 | game_type = 100, 9 | game_name = [[血拼牛牛]], 10 | is_open = 1, 11 | show_index = 2, 12 | module_name = "xpnn", 13 | }, 14 | [102] = { 15 | game_id = 102, 16 | game_type = 100, 17 | game_name = [[龙虎斗]], 18 | is_open = 1, 19 | show_index = 3, 20 | module_name = "lhd", 21 | }, 22 | [103] = { 23 | game_id = 103, 24 | game_type = 100, 25 | game_name = [[斗地主]], 26 | is_open = 1, 27 | show_index = 1, 28 | module_name = "ddz", 29 | }, 30 | } 31 | return game_config -------------------------------------------------------------------------------- /preload/rpc_error.lua: -------------------------------------------------------------------------------- 1 | local errors = {} 2 | 3 | local function add(err) 4 | assert(errors[err.code] == nil, string.format("have the same error code[%x], msg[%s]", err.code, err.desc)) 5 | errors[err.code] = { desc = err.desc } 6 | return err.code 7 | end 8 | 9 | function rpc_errmsg(ec) 10 | if not ec then 11 | return "nil" 12 | end 13 | return errors[ec].desc 14 | end 15 | 16 | RPC_ERROR = { 17 | success = add{ code = 0x01, desc = "请求成功"}, 18 | node_nil = add{ code = 0x02, desc = "请求节点为nil"}, 19 | service_nil = add{ code = 0x03, desc = "请求服务为nil"}, 20 | node_offline = add{ code = 0x04, desc = "节点不在线"}, 21 | service_stoped = add{ code = 0x05, desc = "服务故障"}, 22 | } 23 | 24 | return errors -------------------------------------------------------------------------------- /service/README.md: -------------------------------------------------------------------------------- 1 | # skynet服务相关约定 2 | 3 | ## 1.服务类型, 基于service_base.lua有两种服务,是否agent服务. 4 | 非agent服务,客户端请求是基于并发式的,如果一个coroutine也就是一个请求yeild 5 | 后,其它的请求可以继续执行,这样的并发方式类似于多线程,如果一个数据被两个请 6 | 求所修改,那么就不能保证数据的正确性,这是由于每个请求并不是原子性的. 7 | agent服务,对于此服务,所有请求都放进了队列,所以它能保证每个请求执行完毕才执 8 | 行下一下请求.这种方式有缺点,不能处理大量的请求,所以在玩家agent中使用是 9 | 可以的. 10 | 11 | ## 2.基于非agent服务定制的有序请求类 12 | 由于单个agent在游戏中占用比较多的内存,同时创建服务的开销比较大,所以使用非 13 | agent服务搭建若干个role_base类, 每个role_base类里都有一个queue,所以每个都 14 | 是一个agent. 15 | 16 | 17 | ## 3.对于服务节点监听端口布置 18 | gateserver --> tcp:5001 ws:5002 cluster:9001 19 | dbserver --> cluster:9002 20 | loginserver --> cluster:9003 21 | lobbyserver --> cluster:9004 http:6001 22 | gameserver --> cluster:9005 -------------------------------------------------------------------------------- /service/chatserver/main.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local cluster_monitor = require "cluster_monitor" 3 | local sproto_helper = require "sproto_helper" 4 | local redis_config = require "config.redis_config" 5 | local cluster_config = require "config.cluster_config" 6 | 7 | skynet.start(function() 8 | local cluster_reids_id = tonumber(skynet.getenv("cluster_redis_id")) 9 | local cluster_server_id = tonumber(skynet.getenv("cluster_server_id")) 10 | cluster_monitor.start(redis_config[cluster_reids_id], cluster_config[cluster_server_id]) 11 | 12 | sproto_helper.register_protos() 13 | 14 | skynet.uniqueservice("chat") 15 | 16 | cluster_monitor.open() 17 | 18 | skynet.error("******************** chatserver start ok ********************") 19 | end) -------------------------------------------------------------------------------- /service/hallserver/main.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local cluster_monitor = require "cluster_monitor" 3 | local sproto_helper = require "sproto_helper" 4 | local redis_config = require "config.redis_config" 5 | local cluster_config = require "config.cluster_config" 6 | 7 | skynet.start(function() 8 | local cluster_reids_id = tonumber(skynet.getenv("cluster_redis_id")) 9 | local cluster_server_id = tonumber(skynet.getenv("cluster_server_id")) 10 | cluster_monitor.start(redis_config[cluster_reids_id], cluster_config[cluster_server_id]) 11 | 12 | sproto_helper.register_protos() 13 | 14 | skynet.uniqueservice("hall") 15 | 16 | cluster_monitor.open() 17 | 18 | skynet.error("******************** hallserver start ok ********************") 19 | end) -------------------------------------------------------------------------------- /service/locatorserver/main.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local cluster_monitor = require "cluster_monitor" 3 | local sproto_helper = require "sproto_helper" 4 | local redis_config = require "config.redis_config" 5 | local cluster_config = require "config.cluster_config" 6 | 7 | skynet.start(function() 8 | local cluster_reids_id = tonumber(skynet.getenv("cluster_redis_id")) 9 | local cluster_server_id = tonumber(skynet.getenv("cluster_server_id")) 10 | cluster_monitor.start(redis_config[cluster_reids_id], cluster_config[cluster_server_id]) 11 | 12 | sproto_helper.register_protos() 13 | 14 | skynet.uniqueservice("locator") 15 | 16 | cluster_monitor.open() 17 | 18 | skynet.error("******************** locatorserver start ok ********************") 19 | end) -------------------------------------------------------------------------------- /service/hall.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : hall.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | require "skynet.manager" 9 | local skynet = require "skynet" 10 | local service = require "service_base" 11 | local hall_ctrl = require "hall.hall_ctrl" 12 | local hall_impl = require "hall.hall_impl" 13 | local command = service.command 14 | 15 | function command.cast_login(ctx, player_info) 16 | return hall_ctrl.cast_login(ctx, player_info) 17 | end 18 | 19 | function command.cast_logout(ctx, req) 20 | return hall_ctrl.cast_logout(ctx, req) 21 | end 22 | 23 | function service.on_start() 24 | skynet.register(SERVICE.HALL) 25 | hall_ctrl.init() 26 | end 27 | 28 | service.modules.hall = hall_impl 29 | service.start() -------------------------------------------------------------------------------- /service/test/main.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local RedisMQ = require "redis_mq" 3 | local share_memory = require "share_memory" 4 | local cluster_monitor = require "cluster_monitor" 5 | 6 | 7 | skynet.start(function() 8 | local redis_conf = { 9 | host = "127.0.0.1" , 10 | port = 6379 , 11 | db = 0 12 | } 13 | 14 | local current_conf = { 15 | nodename = "node", 16 | nodeport = 9001, 17 | intranetip = "127.0.0.1", 18 | extranetip = "127.0.0.1", 19 | use_intranet = 1, 20 | serverid = 1, 21 | servertype = 1, 22 | ver = 0 23 | } 24 | 25 | cluster_monitor.start(redis_conf, current_conf) 26 | 27 | skynet.newservice("test_share_memory") 28 | 29 | cluster_monitor.open() 30 | 31 | skynet.error("server is start") 32 | 33 | end) -------------------------------------------------------------------------------- /lualib/README.md: -------------------------------------------------------------------------------- 1 | # lua自定义库 2 | 3 | ## class.lua创建类工具库 4 | 5 | 6 | 7 | ## connector.lua 连接器 8 | 9 | ## cluster_mgr.lua 集群节点管理 10 | 11 | ## cluster_monitor.lua 集群工具 12 | ```lua 13 | local redis_conf = { host="127.0.0.1", port=6379, db=0 } 14 | local node_conf = { 15 | nodename="node1", 16 | nodeprot=9001, 17 | intranetip="127.0.0.1", 18 | extranetip="127.1.1.1", 19 | use_intranet=1, 20 | serverid=1, 21 | servertype=1, 22 | ver=0, 23 | } 24 | ``` 25 | 26 | 27 | ## dispatcher.lua rpc分派相关 28 | 29 | ## requester.lua rpc请求相关 30 | 31 | ## logger.lua 日志工具 32 | 33 | ## command_base.lua 接收skynet命令基础类 34 | 35 | ## service_base.lua 服务基础类 36 | 37 | ## session_base.lua 会话相关 38 | 39 | ## role_base.lua 玩家相关 40 | 41 | ## share_memory.lua 共享内存 42 | 43 | ## redis_mq.lua 使用redis实现的pubsub类 -------------------------------------------------------------------------------- /service/loginserver/main.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local cluster_monitor = require "cluster_monitor" 3 | local sproto_helper = require "sproto_helper" 4 | local redis_config = require "config.redis_config" 5 | local cluster_config = require "config.cluster_config" 6 | 7 | skynet.start(function() 8 | local cluster_reids_id = tonumber(skynet.getenv("cluster_redis_id")) 9 | local cluster_server_id = tonumber(skynet.getenv("cluster_server_id")) 10 | cluster_monitor.start(redis_config[cluster_reids_id], cluster_config[cluster_server_id]) 11 | 12 | sproto_helper.register_protos() 13 | 14 | -- skynet.uniqueservice("auth") 15 | skynet.uniqueservice("login") 16 | 17 | cluster_monitor.open() 18 | 19 | skynet.error("******************** loginserver start ok ********************") 20 | end) -------------------------------------------------------------------------------- /service/test/main1.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local RedisMQ = require "redis_mq" 3 | local share_memory = require "share_memory" 4 | local cluster_monitor = require "cluster_monitor" 5 | 6 | 7 | 8 | skynet.start(function() 9 | local redis_conf = { 10 | host = "127.0.0.1" , 11 | port = 6379 , 12 | db = 0 13 | } 14 | 15 | local current_conf = { 16 | nodename = "node1", 17 | nodeport = 9002, 18 | intranetip = "127.0.0.1", 19 | extranetip = "127.0.0.1", 20 | use_intranet = 1, 21 | serverid = 2, 22 | servertype = 2, 23 | ver = 0, 24 | } 25 | 26 | cluster_monitor.start(redis_conf, current_conf) 27 | 28 | skynet.newservice("test_share_memory1") 29 | 30 | cluster_monitor.open() 31 | 32 | skynet.error("server is start") 33 | 34 | end) -------------------------------------------------------------------------------- /service/httpServer.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local socket = require "skynet.socket" 3 | local htpp_port = ... 4 | 5 | skynet.start(function() 6 | local agent = {} 7 | for i= 1, 2 do 8 | -- 启动 20 个代理服务用于处理 http 请求 9 | agent[i] = skynet.newservice("httpServerAgent") 10 | end 11 | local balance = 1 12 | -- 监听一个 web 端口 13 | local id = socket.listen("0.0.0.0", htpp_port) 14 | socket.start(id , function(id, addr) 15 | -- 当一个 http 请求到达的时候, 把 socket id 分发到事先准备好的代理中去处理。 16 | skynet.error(string.format("%s connected, pass it to agent :%08x", addr, agent[balance])) 17 | skynet.send(agent[balance], "lua", id) 18 | balance = balance + 1 19 | if balance > #agent then 20 | balance = 1 21 | end 22 | end) 23 | end) -------------------------------------------------------------------------------- /config/config_public: -------------------------------------------------------------------------------- 1 | root = "../" 2 | skynetroot = "../skynet/" 3 | thread = 4 4 | logger = nil 5 | logpath = "../log/" 6 | harbor = 0 --0-单点 1-master slave 7 | bootstrap = "snlua bootstrap" 8 | lualoader = skynetroot.."lualib/loader.lua" 9 | preload = root.."preload/preload.lua" 10 | 11 | 12 | -- C编写的服务模块路径 13 | cpath = skynetroot.."cservice/?.so;" 14 | 15 | -- lua编写的服务模块路径 16 | luaservice = skynetroot.."service/?.lua;"..root.."service/?.lua;" 17 | 18 | -- lua动态库文件搜索路径,供require调用 19 | lua_cpath = skynetroot.."luaclib/?.so;" 20 | 21 | -- lua文件搜索路径,供require调用 22 | lua_path = skynetroot.."lualib/?.lua;".. 23 | root.."preload/?.lua;".. 24 | root.."common/?.lua;".. 25 | root.."lualib/?.lua;".. 26 | root.."logic/?.lua;" 27 | 28 | random_type = 2 29 | cluster_redis_id = 1 30 | -------------------------------------------------------------------------------- /service/gameserver/xpnn/main.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local cluster_monitor = require "cluster_monitor" 3 | local sproto_helper = require "sproto_helper" 4 | local redis_config = require "config.redis_config" 5 | local cluster_config = require "config.cluster_config" 6 | local game_id = skynet.getenv("game_id") 7 | 8 | skynet.start(function() 9 | local cluster_reids_id = tonumber(skynet.getenv("cluster_redis_id")) 10 | local cluster_server_id = tonumber(skynet.getenv("cluster_server_id")) 11 | cluster_monitor.start(redis_config[cluster_reids_id], cluster_config[cluster_server_id]) 12 | 13 | sproto_helper.register_protos() 14 | 15 | skynet.uniqueservice("area", game_id) 16 | 17 | cluster_monitor.open() 18 | 19 | skynet.error("******************** gameserver-xpnn start ok ********************") 20 | end) -------------------------------------------------------------------------------- /service/mysql_svc.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : mysql_svc.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | local skynet = require "skynet" 9 | local mysql = require "skynet.db.mysql" 10 | 11 | local db = nil 12 | local command = {} 13 | 14 | function command.ping() 15 | db:query("select 1") 16 | end 17 | 18 | function command.execute(sql) 19 | return db:query(sql) 20 | end 21 | 22 | function command.init(conf) 23 | db = mysql.connect(conf) 24 | end 25 | 26 | local function dispatch(session, addr, cmd, ...) 27 | local func = assert(command[cmd, string.format("mysql svc func is nil")]) 28 | if session > 0 then 29 | skynet.retpack(func(...)) 30 | else 31 | func(...) 32 | end 33 | end 34 | 35 | skynet.start(function() 36 | skynet.dispatch("lua", dispatch) 37 | end) -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # Top level makefile, the real shit is at src/Makefile 2 | all: skynet/skynet redis/redis-server lsocket 3 | 4 | skynet/skynet: 5 | #yum install dos2unix; yum install libcurl-dev libcurl-devel 6 | cd skynet && $(MAKE) linux 7 | 8 | JEMALLOC_STATICLIB := redis/deps/jemalloc/lib/libjemalloc_pic.a 9 | REDIS_SERVER := redis/src/redis-server 10 | 11 | redis/redis-server: | $(REDIS_SERVER) 12 | cp $(REDIS_SERVER) redis/ 13 | 14 | $(REDIS_SERVER): | $(JEMALLOC_STATICLIB) 15 | cd redis && $(MAKE) 16 | 17 | $(JEMALLOC_STATICLIB): redis/deps/jemalloc/Makefile 18 | cd redis/deps/jemalloc && $(MAKE) CC=$(CC) 19 | 20 | lsocket: 21 | cd lsocket && $(MAKE) LUA_INCLUDE=../skynet/3rd/lua 22 | 23 | redis/deps/jemalloc/Makefile: 24 | cd redis/deps/jemalloc && find ./ -name "*.sh" | xargs chmod +x && ./autogen.sh --with-jemalloc-prefix=je_ --disable-valgrind -------------------------------------------------------------------------------- /service/watchdog.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : watchdog.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | local skynet = require "skynet" 9 | local proto_map = require "proto_map" 10 | local socket_msg = require "gate.socket_msg" 11 | local gate_msg = require "gate.gate_msg" 12 | local gate_mgr = require "gate.gate_mgr" 13 | 14 | 15 | skynet.start(function() 16 | skynet.dispatch("lua", function(session, source, cmd, subcmd, ...) 17 | if cmd == "socket" then 18 | local f = socket_msg[subcmd] 19 | f(...) 20 | -- socket api don't need return 21 | else 22 | local f = assert(gate_msg[cmd]) 23 | if session > 0 then 24 | skynet.retpack(f(subcmd, ...)) 25 | else 26 | f(subcmd, ...) 27 | end 28 | end 29 | end) 30 | gate_mgr.init("gate", false) 31 | end) 32 | -------------------------------------------------------------------------------- /service/wswatchdog.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : wswatchdog.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | local skynet = require "skynet" 9 | local proto_map = require "proto_map" 10 | local socket_msg = require "gate.socket_msg" 11 | local gate_msg = require "gate.gate_msg" 12 | local gate_mgr = require "gate.gate_mgr" 13 | 14 | 15 | skynet.start(function() 16 | skynet.dispatch("lua", function(session, source, cmd, subcmd, ...) 17 | if cmd == "socket" then 18 | local f = socket_msg[subcmd] 19 | f(...) 20 | -- socket api don't need return 21 | else 22 | local f = assert(gate_msg[cmd]) 23 | if session > 0 then 24 | skynet.retpack(f(subcmd, ...)) 25 | else 26 | f(subcmd, ...) 27 | end 28 | end 29 | end) 30 | gate_mgr.init("wsgate", true) 31 | end) -------------------------------------------------------------------------------- /service/hall_logic_svc.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : hall_logic_svc.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | local skynet = require "skynet" 9 | local json = require "json" 10 | local command = require "command_base" 11 | local hall_logic = require "hall.hall_logic" 12 | 13 | function command.cast_login(ctx, req) 14 | return hall_logic.cast_login(ctx, req) 15 | end 16 | 17 | function command.cast_logout(ctx, req) 18 | return hall_logic.cast_logout(ctx, req) 19 | end 20 | 21 | function command.get_player_online_state(ctx, req) 22 | return hall_logic.get_player_online_state(ctx, req) 23 | end 24 | 25 | function command.get_room_inst_list(ctx, req) 26 | return hall_logic.get_room_inst_list(ctx, req) 27 | end 28 | 29 | skynet.start(function() 30 | hall_logic.init() 31 | end) 32 | -------------------------------------------------------------------------------- /logic/README.md: -------------------------------------------------------------------------------- 1 | # lua代码文件规范 2 | 3 | ## 1.lua文件名规范, 使用小写字母加下划线: 4 | ```lua 5 | connection_mgr.lua 6 | data_center.lua 7 | ``` 8 | 9 | ## 2.变量名命名规范, 使用小写字母加下划线(和数据库名保持一致): 10 | ```lua 11 | local role_info 12 | local account_name 13 | local player_id 14 | ``` 15 | 16 | ## 3.私有变量命名规范, 使用下划线在最前面 17 | ```lua 18 | local _room_map 19 | local _player_list 20 | ``` 21 | 22 | ## 4.常量命名规范, 使用大写单词加下划线: 23 | ```lua 24 | local STATUS_ONLINE 25 | local STATUS_OFFLINE 26 | ``` 27 | 28 | ## 5.类名命名规范, 使用大写字母加小写字母(驼峰式命名): 29 | RedisMQ 30 | SessionMgr 31 | 32 | ## 6.函数命名规范, 使用小写字母加下划线: 33 | ```lua 34 | function get_player_info() end 35 | function set_player_info() end 36 | ``` 37 | 38 | ## 7.私有函数命名规范, 使用带前下划线加小写字母: 39 | ```lua 40 | function _update_role_info() end 41 | function _delete_role_info() end 42 | ``` -------------------------------------------------------------------------------- /service/room.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : room.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | require "skynet.manager" 9 | local skynet = require "skynet" 10 | local service = require "service_base" 11 | local room_ctrl = require "room.room_ctrl" 12 | local room_impl = require "room.room_impl" 13 | local command = service.command 14 | local room_id = tonumber(...) 15 | 16 | function command.enter_room(ctx, req) 17 | return room_ctrl.enter_room(ctx, req) 18 | end 19 | 20 | function command.exit_room(ctx, req) 21 | return room_ctrl.exit_room(ctx, req) 22 | end 23 | 24 | function command.logout_desk(ctx, req) 25 | return room_ctrl.logout_desk(ctx, req) 26 | end 27 | 28 | function service.on_start() 29 | --skynet.register(SERVICE.ROOM) 30 | room_ctrl.init(room_id) 31 | end 32 | 33 | service.modules.room = room_impl 34 | service.start() -------------------------------------------------------------------------------- /preload/system_define.lua: -------------------------------------------------------------------------------- 1 | 2 | RANDOM_TYPE = { 3 | os_time = 0, 4 | mt_19937 = 1, 5 | linux_urandom = 2, 6 | linux_random = 3, 7 | } 8 | 9 | ONLINE_STATE = { 10 | offline = 0, 11 | online = 1, 12 | onarea = 2, 13 | onroom = 3, 14 | ongroup = 4, 15 | } 16 | 17 | OFFLINE_STATE = { 18 | off = 0, 19 | on = 1, 20 | } 21 | 22 | ACCOUNT_TYPE = { 23 | visitor = 0, 24 | normal = 1, 25 | weixin = 2, 26 | } 27 | 28 | GROUP_TYPE = { 29 | auto = 1, 30 | ready = 2, 31 | } 32 | 33 | FD_TYPE = { 34 | hall = 1, 35 | area = 2, 36 | room = 3, 37 | } 38 | 39 | CHAT_TYPE = { 40 | hall = 1, 41 | game = 2, 42 | server = 3, 43 | roomtype = 4, 44 | rommaddr = 5, 45 | } 46 | 47 | HEART_BEAT_TIME = { 48 | intval = 3, 49 | intcnt = 3, 50 | kicktime = 15, 51 | } 52 | 53 | HEART_BEAT_STATE = { 54 | normal = 1, 55 | offline = 2, 56 | kick =3 , 57 | } 58 | 59 | WAIT_ARGS = { 60 | basenum = 1000, 61 | basetime = 10, 62 | } -------------------------------------------------------------------------------- /service/master_db_svc.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : master_db_svc.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | local skynet = require "skynet" 9 | local db_mgr = require "common.db_mgr" 10 | local db_module = require "common.db_module" 11 | local svc_name = ... 12 | local command = {} 13 | 14 | local function dispatch(session, addr, cmd, ...) 15 | skynet.error("masterdb svc dispatcher, cmd=", cmd) 16 | local modname, funcname = string.match(cmd, "([%w_]+)%.([%w_]+)") 17 | local mod = db_module[modname] 18 | if not mod then 19 | return 20 | end 21 | local func = mod[funcname] 22 | if not func then 23 | return 24 | end 25 | 26 | if session > 0 then 27 | skynet.retpack(func(...)) 28 | else 29 | func(...) 30 | end 31 | end 32 | 33 | 34 | skynet.start(function() 35 | db_mgr.init(svc_name) 36 | skynet.dispatch("lua", dispatch) 37 | end) -------------------------------------------------------------------------------- /logic/config/redis_config.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | redis连接id, redis地址, redis端口, redis库名, redis验证, 注释 3 | id, host, port, db, auth, desc 4 | ]] 5 | local redis_config = { 6 | [1] = { 7 | id = 1, 8 | host = "127.0.0.1", 9 | port = 6379, 10 | db = 0, 11 | auth = nil, 12 | desc = [[全局数据缓存]], 13 | }, 14 | [2] = { 15 | id = 2, 16 | host = "127.0.0.1", 17 | port = 6379, 18 | db = 1, 19 | auth = nil, 20 | desc = [[lobby数据缓存]], 21 | }, 22 | [3] = { 23 | id = 3, 24 | host = "127.0.0.1", 25 | port = 6379, 26 | db = 2, 27 | auth = nil, 28 | desc = [[game数据缓存]], 29 | }, 30 | [4] = { 31 | id = 4, 32 | host = "127.0.0.1", 33 | port = 6379, 34 | db = 3, 35 | auth = nil, 36 | desc = [[玩家数据缓存]], 37 | }, 38 | [5] = { 39 | id = 5, 40 | host = "127.0.0.1", 41 | port = 6379, 42 | db = 4, 43 | auth = nil, 44 | desc = [[gamelog数据缓存]], 45 | }, 46 | } 47 | return redis_config -------------------------------------------------------------------------------- /logic/gate/gate_mgr.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | 3 | 4 | local gate_mgr = {} 5 | local gate 6 | local iswebsocket 7 | local connections = {} 8 | local client_count = 0 9 | 10 | 11 | function gate_mgr.get_gate() 12 | return gate 13 | end 14 | 15 | function gate_mgr.is_websocket() 16 | return iswebsocket 17 | end 18 | 19 | function gate_mgr.add_connection(fd, c) 20 | connections[fd] = c 21 | client_count = client_count + 1 22 | skynet.call(gate, "lua", "accept", fd) 23 | end 24 | 25 | function gate_mgr.get_connection(fd) 26 | return connections[fd] 27 | end 28 | 29 | function gate_mgr.close_connection(fd) 30 | local c = connections[fd] 31 | if c then 32 | skynet.error("cy==============gate_mgr close conn fd=", fd) 33 | connections[fd] = nil 34 | end 35 | end 36 | 37 | function gate_mgr.init(gatename, isws) 38 | iswebsocket = isws 39 | gate = skynet.newservice(gatename) 40 | end 41 | 42 | return gate_mgr 43 | -------------------------------------------------------------------------------- /logic/room/room_ctrl.lua: -------------------------------------------------------------------------------- 1 | require "skynet.manager" 2 | local skynet = require "skynet" 3 | local room_logic = require "room.room_logic" 4 | local room_ctrl = {} 5 | 6 | 7 | function room_ctrl.init(room_id) 8 | room_logic.init(room_id) 9 | end 10 | 11 | function room_ctrl.enter_room(ctx, req) 12 | local code, reply_data = room_logic.check_enter_room(ctx, req) 13 | if code ~= SYSTEM_ERROR.success then 14 | return code, reply_data 15 | end 16 | local player_info = reply_data 17 | room_logic.change_enter_room_state(ctx, player_info) 18 | return room_logic.get_enter_room_reply() 19 | end 20 | 21 | function room_ctrl.exit_room(ctx, req) 22 | return room_logic.change_exit_room_state(ctx, req) 23 | end 24 | 25 | function room_ctrl.group_request(ctx, req) 26 | return room_logic.group_request(ctx, req) 27 | end 28 | 29 | function room_ctrl.logout_desk(ctx, req) 30 | return room_logic.logout_desk(ctx, req) 31 | end 32 | 33 | 34 | return room_ctrl -------------------------------------------------------------------------------- /proto/sproto/sproto_gen/lualib/print_r.lua: -------------------------------------------------------------------------------- 1 | local print = print 2 | local tconcat = table.concat 3 | local tinsert = table.insert 4 | local srep = string.rep 5 | local type = type 6 | local pairs = pairs 7 | local tostring = tostring 8 | local next = next 9 | 10 | local function print_r(root) 11 | local cache = { [root] = "." } 12 | local function _dump(t,space,name) 13 | local temp = {} 14 | for k,v in pairs(t) do 15 | local key = tostring(k) 16 | if cache[v] then 17 | tinsert(temp,"+" .. key .. " {" .. cache[v].."}") 18 | elseif type(v) == "table" then 19 | local new_key = name .. "." .. key 20 | cache[v] = new_key 21 | tinsert(temp,"+" .. key .. _dump(v,space .. (next(t,k) and "|" or " " ).. srep(" ",#key),new_key)) 22 | else 23 | tinsert(temp,"+" .. key .. " [" .. tostring(v).."]") 24 | end 25 | end 26 | return tconcat(temp,"\n"..space) 27 | end 28 | print(_dump(root, "","")) 29 | end 30 | 31 | return print_r -------------------------------------------------------------------------------- /proto/sproto/sproto_gen/lualib/sprotoloader.lua: -------------------------------------------------------------------------------- 1 | local parser = require "sprotoparser" 2 | local core = require "sproto.core" 3 | local sproto = require "sproto" 4 | 5 | local loader = {} 6 | 7 | function loader.register(filename, index) 8 | local f = assert(io.open(filename), "Can't open sproto file") 9 | local data = f:read "a" 10 | f:close() 11 | local sp = core.newproto(parser.parse(data)) 12 | core.saveproto(sp, index) 13 | end 14 | 15 | function loader.save(bin, index) 16 | local sp = core.newproto(bin) 17 | core.saveproto(sp, index) 18 | end 19 | 20 | function loader.save_from_file(filename, index) 21 | local f = assert(io.open(filename, "rb"), "Can't open sproto file") 22 | local data = f:read "*all" 23 | f:close() 24 | loader.save(data, index) 25 | end 26 | 27 | function loader.load(index) 28 | local sp = core.loadproto(index) 29 | -- no __gc in metatable 30 | return sproto.sharenew(sp) 31 | end 32 | 33 | return loader 34 | 35 | -------------------------------------------------------------------------------- /service/auth_logic_svc.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : auth_logic_svc.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | local skynet = require "skynet" 9 | local json = require "json" 10 | local command = require "command_base" 11 | local auth_logic = require "auth.auth_logic" 12 | 13 | function command.register_account(ctx, req) 14 | return auth_logic.register_account(ctx, req) 15 | end 16 | 17 | function command.login_account(ctx, req) 18 | return auth_logic.login_account(ctx, req) 19 | end 20 | 21 | function command.weixin_login(ctx, req) 22 | return auth_logic.weixin_login(ctx, req) 23 | end 24 | 25 | function command.visitor_login(ctx, req) 26 | return auth_logic.visitor_login(ctx, req) 27 | end 28 | 29 | function command.auth_secret(ctx, req) 30 | return auth_logic.auth_secret(ctx, req) 31 | end 32 | 33 | skynet.start(function() 34 | auth_logic.init() 35 | end) 36 | -------------------------------------------------------------------------------- /service/share_memoryd.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : share_memoryd.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | local skynet = require "skynet" 9 | local stm = require "skynet.stm" 10 | 11 | local command = {} 12 | 13 | local stmobj_map = {} 14 | 15 | function command.get_stmobj(name) 16 | local stmobj = stmobj_map[name] 17 | if not stmobj then 18 | return 19 | end 20 | return stm.copy(stmobj) 21 | end 22 | 23 | function command.set_stmobj(name, data) 24 | local stmobj = stmobj_map[name] 25 | if not stmobj then 26 | stmobj = stm.new(skynet.pack(data)) 27 | stmobj_map[name] = stmobj 28 | return 29 | end 30 | stmobj(skynet.pack(data)) 31 | return 32 | end 33 | 34 | skynet.start(function() 35 | skynet.dispatch("lua", function(session, address, cmd, ...) 36 | local f = command[cmd] 37 | if session > 0 then 38 | skynet.retpack(f(...)) 39 | else 40 | f(...) 41 | end 42 | end) 43 | end) -------------------------------------------------------------------------------- /service/authserver/main.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local cluster_monitor = require "cluster_monitor" 3 | local sproto_helper = require "sproto_helper" 4 | local redis_config = require "config.redis_config" 5 | local cluster_config = require "config.cluster_config" 6 | local watchdog_port = skynet.getenv("watchdog_port") 7 | 8 | skynet.start(function() 9 | local cluster_reids_id = tonumber(skynet.getenv("cluster_redis_id")) 10 | local cluster_server_id = tonumber(skynet.getenv("cluster_server_id")) 11 | 12 | cluster_monitor.start(redis_config[cluster_reids_id], cluster_config[cluster_server_id]) 13 | 14 | sproto_helper.register_protos() 15 | 16 | local watchdog = skynet.newservice("watchdog") 17 | skynet.call(watchdog, "lua", "start", { 18 | port = watchdog_port, 19 | maxclient = max_client, 20 | nodelay = true, 21 | }) 22 | 23 | skynet.uniqueservice("auth") 24 | 25 | cluster_monitor.open() 26 | skynet.error("******************** authserver start ok ********************") 27 | 28 | end) -------------------------------------------------------------------------------- /service/area.lua: -------------------------------------------------------------------------------- 1 | require "skynet.manager" 2 | local skynet = require "skynet" 3 | local service = require "service_base" 4 | local area_ctrl = require "area.area_ctrl" 5 | local area_impl = require "area.area_impl" 6 | local command = service.command 7 | local game_id = tonumber(...) 8 | 9 | function command.enter_area(ctx, req) 10 | return area_ctrl.enter_area(ctx, req) 11 | end 12 | 13 | function command.exit_area(ctx, req) 14 | return area_ctrl.exit_area(ctx, req) 15 | end 16 | 17 | function command.create_role(ctx, req) 18 | return area_ctrl.create_role(ctx, req) 19 | end 20 | 21 | function command.get_role(ctx, req) 22 | return area_ctrl.get_role(ctx, req) 23 | end 24 | 25 | function command.get_room(room_id) 26 | return area_ctrl.get_room(room_id) 27 | end 28 | 29 | function command.enter_room(ctx, req) 30 | return area_ctrl.enter_room(ctx, req) 31 | end 32 | 33 | function service.on_start() 34 | skynet.register(SERVICE.AREA) 35 | area_ctrl.init(game_id) 36 | end 37 | 38 | service.modules.area = area_impl 39 | service.start() -------------------------------------------------------------------------------- /service/mysql_mgr.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : mysql_mgr.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | require "skynet.manager" 9 | local skynet = require "skynet" 10 | local mysql = require "skynet.db.mysql" 11 | 12 | local command = {} 13 | local db_pool = {} 14 | local index = 1 15 | 16 | local function init_db_pool() 17 | local dbcount = skynet.getenv("dbcount") 18 | for i=1, dbcount do 19 | db_pool[#db_pool+1] = skynet.newservice("mysql_svc") 20 | end 21 | end 22 | 23 | local function dispatch(session, addr, cmd, ...) 24 | local func = command[cmd] 25 | if not func then 26 | return 27 | end 28 | if session > 0 then 29 | skynet.retpack(func(...)) 30 | else 31 | func(...) 32 | end 33 | end 34 | 35 | function command.get_db_svc() 36 | local db = db_pool[index] 37 | index = index + 1 38 | if index > #db_pool then 39 | index = 1 40 | end 41 | return db 42 | end 43 | 44 | skynet.start(function() 45 | skynet.dispatch("lua", dispatch) 46 | skynet.register(SERVICE.MYSQL_MGR) 47 | end) -------------------------------------------------------------------------------- /lualib/config_db.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : config_db.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | local sharedata_core = require "skynet.sharedata.corelib" 9 | 10 | local config_db = {} 11 | local config_keys = {} 12 | 13 | setmetatable(config_db, { 14 | __index = function(t, k) 15 | local value = config_keys[k] 16 | if value == nil then 17 | return require("config."..k) 18 | end 19 | if value[2] then 20 | return value[2] 21 | end 22 | local obj = sharedata_core.box(value[1]) 23 | value[2] = obj 24 | return obj 25 | end 26 | }) 27 | 28 | 29 | function config_db.init(configs) 30 | for k, p in pairs(configs) do 31 | config_keys[k] = {p} 32 | end 33 | end 34 | 35 | function config_db.update(configs) 36 | for k, p in pairs(configs) do 37 | local value = config_keys[k] 38 | if value then 39 | if value[2] then 40 | sharedata_core.update(value[2], p) 41 | end 42 | value[1] = p 43 | else 44 | config_keys[k] = {p} 45 | end 46 | end 47 | end 48 | 49 | return config_db 50 | -------------------------------------------------------------------------------- /tool/run_all.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | dir=.. 3 | branch=__default__ 4 | redisdir=$dir/redis 5 | skynetdir=$dir/skynet 6 | 7 | #start skynet 8 | if [ -n "$1" ] ;then 9 | nohup $skynetdir/skynet $dir/config/config_auth $branch >> console.log 2>&1 & 10 | nohup $skynetdir/skynet $dir/config/config_gate $branch >> console.log 2>&1 & 11 | nohup $skynetdir/skynet $dir/config/config_login $branch >> console.log 2>&1 & 12 | nohup $skynetdir/skynet $dir/config/config_hall $branch >> console.log 2>&1 & 13 | nohup $skynetdir/skynet $dir/config/config_db $branch >> console.log 2>&1 & 14 | nohup $skynetdir/skynet $dir/config/config_chat $branch >> console.log 2>&1 & 15 | sleep 2s 16 | nohup $skynetdir/skynet $dir/config/config_locator $branch >> console.log 2>&1 & 17 | sleep 1s 18 | nohup $skynetdir/skynet $dir/config/config_xpnn21 $branch >> console.log 2>&1 & 19 | else 20 | ./run_db.sh 21 | ./run_auth.sh 22 | ./run_gate.sh 23 | ./run_login.sh 24 | ./run_hall.sh 25 | ./run_chat.sh 26 | sleep 1s 27 | ./run_locator.sh 28 | sleep 1s 29 | ./run_xpnn.sh 21 30 | fi -------------------------------------------------------------------------------- /service/dbserver/main.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local sproto_helper = require "sproto_helper" 3 | local cluster_monitor = require "cluster_monitor" 4 | local redis_config = require "config.redis_config" 5 | local cluster_config = require "config.cluster_config" 6 | 7 | skynet.start(function() 8 | skynet.error("cy===============================================dbtimea=", skynet.now(), skynet.starttime(), skynet.time()) 9 | --nodemonitor 10 | local cluster_reids_id = tonumber(skynet.getenv("cluster_redis_id")) 11 | local cluster_server_id = tonumber(skynet.getenv("cluster_server_id")) 12 | cluster_monitor.start(redis_config[cluster_reids_id], cluster_config[cluster_server_id]) 13 | --spb 14 | sproto_helper.register_protos() 15 | 16 | -- todo 17 | local master_db = skynet.newservice("master_db") 18 | skynet.call(master_db, "lua", "start") 19 | 20 | local svc = skynet.call(master_db, "lua", "get_db_svc", "lobby_db") 21 | 22 | cluster_monitor.open() 23 | skynet.error("cy===============================================dbtimeb=", skynet.now(), skynet.starttime(), skynet.time()) 24 | skynet.error("******************** dbserver is start ********************") 25 | end) -------------------------------------------------------------------------------- /sql/create.sql: -------------------------------------------------------------------------------- 1 | create table tb_normal_account 2 | ( 3 | player_id int(20), 4 | telephone varchar(255), 5 | account varchar(255), 6 | password varchar(255), 7 | create_time int(20), 8 | PRIMARY KEY (`player_id`) 9 | )ENGINE=InnoDB DEFAULT CHARSET=utf8; 10 | 11 | create table tb_player 12 | ( 13 | player_id int(20), 14 | head_id int(20), 15 | head_url varchar(255), 16 | nickname varchar(255), 17 | sex int(2), 18 | gold int(20), 19 | create_time int(20), 20 | PRIMARY KEY (`player_id`) 21 | )ENGINE=InnoDB DEFAULT CHARSET=utf8; 22 | 23 | create table tb_visitor_account 24 | ( 25 | player_id int(20), 26 | visit_token varchar(255), 27 | create_time int(20), 28 | PRIMARY KEY (`player_id`) 29 | )ENGINE=InnoDB DEFAULT CHARSET=utf8; 30 | 31 | create table tb_weixin_account 32 | ( 33 | player_id int(20), 34 | union_id varchar(255), 35 | create_time int(20), 36 | PRIMARY KEY (`player_id`) 37 | )ENGINE=InnoDB DEFAULT CHARSET=utf8; 38 | 39 | create table tb_role 40 | ( 41 | role_id int(20), 42 | player_id int(20), 43 | game_id int(20), 44 | head_id int(20), 45 | head_url varchar(255), 46 | nickname varchar(255), 47 | sex int(2), 48 | gold int(20), 49 | create_time int(20), 50 | PRIMARY KEY (`role_id`) 51 | )ENGINE=InnoDB DEFAULT CHARSET=utf8; -------------------------------------------------------------------------------- /logic/chat/chat_ctrl.lua: -------------------------------------------------------------------------------- 1 | require "skynet.manager" 2 | local skynet = require "skynet" 3 | local context = require "context" 4 | local db_helper = require "common.db_helper" 5 | local chat_logic = require "chat.chat_logic" 6 | local chat_ctrl = {} 7 | 8 | local fd_ctx = {} 9 | local logic_svc_pool = {} 10 | local logic_svc_index = 1 11 | 12 | local function init_logic_pool() 13 | local logic_count = skynet.getenv("chat_logic_count") 14 | for i=1, logic_count do 15 | local svc = skynet.newservice("chat_logic_svc") 16 | logic_svc_pool[#logic_svc_pool + 1] = svc 17 | end 18 | end 19 | 20 | local function get_logic_svc() 21 | local svc = logic_svc_pool[logic_svc_index] 22 | logic_svc_index = logic_svc_index + 1 23 | if logic_svc_index > #logic_svc_pool then 24 | logic_svc_index = 1 25 | end 26 | return svc 27 | end 28 | 29 | function chat_ctrl.init() 30 | init_logic_pool() 31 | end 32 | 33 | function chat_ctrl.set_ctx(ctx) 34 | fd_ctx[ctx.fd] = {} 35 | fd_ctx[ctx.fd]= {fd = ctx.fd, watchdog = ctx.watchdog, gate = ctx.gate} 36 | return SYSTEM_ERROR.success 37 | end 38 | 39 | function chat_ctrl.chat_req(ctx, req) 40 | local svc = get_logic_svc() 41 | req.fd_ctx = fd_ctx 42 | return context.call(svc, "chat_req", ctx, req) 43 | end 44 | 45 | 46 | return chat_ctrl -------------------------------------------------------------------------------- /lualib/sky_crypt.lua: -------------------------------------------------------------------------------- 1 | local crypt = require "skynet_crypt" 2 | 3 | local sky_crypt ={} 4 | 5 | function sky_crypt.make_random() 6 | return crypt.randomkey() 7 | end 8 | 9 | function sky_crypt.dhexchange(key) 10 | return crypt.dhexchange(key) 11 | end 12 | 13 | function sky_crypt.base64encode(key) 14 | return crypt.base64encode(key) 15 | end 16 | 17 | function sky_crypt.base64decode(key) 18 | return crypt.base64decode(key) 19 | end 20 | 21 | --用key加密plaintext得到密文,key必须是8字节 22 | function sky_crypt.desencode(key, plaintext) 23 | return crypt.desencode(key, plaintext) 24 | end 25 | 26 | --用key解密ciphertext得到明文,key必须是8字节 27 | function sky_crypt.desdecode(key, ciphertext) 28 | return crypt.desdecode(key, ciphertext) 29 | end 30 | 31 | function sky_crypt.hexencode(key) 32 | return crypt.hexencode(key) 33 | end 34 | 35 | function sky_crypt.hexdecode(key) 36 | return crypt.hexdecode(key) 37 | end 38 | 39 | function sky_crypt.dhsecret(ckey, skey) 40 | return crypt.dhsecret(ckey, skey) 41 | end 42 | 43 | --HMAC64运算利用哈希算法,以一个密钥secret和一个消息challenge为输入,生成一个消息摘要hmac作为输出。 44 | function sky_crypt.hmac64(challenge, secret) 45 | return crypt.hmac64(challenge, secret) 46 | end 47 | 48 | --云风自实现的hash算法,只能哈希小于8字节的数据,返回8字节数据的hash 49 | function sky_crypt.hashkey(str) 50 | return crypt.hashkey(str) 51 | end 52 | 53 | return sky_crypt -------------------------------------------------------------------------------- /lualib/service_base.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : service_base.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | local skynet = require "skynet" 9 | local dispatcher = require "dispatcher" 10 | local cluster_monitor = require "cluster_monitor" 11 | 12 | local service_base = {} 13 | service_base.name = nil 14 | service_base.modules = {} 15 | service_base.command = require "command_base" 16 | service_base.is_agent = false 17 | dispatcher.service_base = service_base 18 | local command = service_base.command 19 | 20 | function command.dispatch_client_msg(ctx, ...) 21 | return dispatcher.dispatch_client_msg(ctx, ...) 22 | end 23 | 24 | function command.dispatch_service_msg(ctx, ...) 25 | return dispatcher.dispatch_service_msg(ctx, ...) 26 | end 27 | 28 | function command.monitor_node_change(conf) 29 | if not conf then return end 30 | if not conf.nodename then return end 31 | local callback = cluster_monitor.get_subcribe_callback(conf.nodename) 32 | if not callback then 33 | return 34 | end 35 | return callback(conf) 36 | end 37 | 38 | 39 | function command.gc() 40 | 41 | end 42 | 43 | function service_base.on_start() 44 | 45 | end 46 | 47 | function service_base.start() 48 | skynet.start(function() 49 | service_base.on_start() 50 | end) 51 | end 52 | 53 | return service_base -------------------------------------------------------------------------------- /proto/sproto/sproto_gen/sproto_gen_win.lua: -------------------------------------------------------------------------------- 1 | local root = "../" 2 | package.path = "lualib/?.lua;" 3 | package.cpath = "luaclib/?.dll;" 4 | local parser = require "sprotoparser" 5 | 6 | local protoDir = root .. "sproto/" 7 | local spbDir = root .. "spb/" 8 | local sprotoGen = {} 9 | 10 | function sprotoGen.create(files) 11 | local dumpFiles = {} 12 | 13 | for k, v in pairs(files) do 14 | local filename = protoDir .. v 15 | local f = assert(io.open(filename), "Can't open sproto filename["..filename.."]") 16 | local data = f:read "a" 17 | dumpFiles[k] = dumpFiles[k] or "" 18 | dumpFiles[k] = dumpFiles[k] .. data 19 | end 20 | 21 | for k, v in pairs(dumpFiles) do 22 | local file = io.open(spbDir .. k ..".spb", "w+b") 23 | file:write(parser.parse(v)) 24 | file:close() 25 | end 26 | end 27 | 28 | local files = { 29 | package = "package.sproto", 30 | gate = "gate.sproto", 31 | auth = "auth.sproto", 32 | hall = "hall.sproto", 33 | player = "player.sproto", 34 | bank = "bank.sproto", 35 | cash = "cash.sproto", 36 | push = "push.sproto", 37 | message = "message.sproto", 38 | room = "room.sproto", 39 | desk = "desk.sproto", 40 | xpnn = "xpnn.sproto", 41 | lrnn = "lrnn.sproto", 42 | ddz = "ddz.sproto", 43 | zjh = "zjh.sproto", 44 | lrsh = "lrsh.sproto", 45 | } 46 | 47 | sprotoGen.create(files) 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /logic/cache/cache_db.lua: -------------------------------------------------------------------------------- 1 | local db_mgr = require "common.db_mgr" 2 | local json = require "json" 3 | local skynet = require "skynet" 4 | local utils = require "utils" 5 | local cache_db = {} 6 | 7 | local CacheInfoKey = "cache_info" 8 | local function get_cache_info_key(pack_info) 9 | local player_id = pack_info.player_id 10 | local pack_id = pack_info.pack_id 11 | return CacheInfoKey .. ":" .. player_id .. ":" .. pack_id 12 | end 13 | 14 | function cache_db.insert_cache_info(pack_info, pack_data) 15 | local redisdb = db_mgr.get_redis_db() 16 | redisdb:hset(get_cache_info_key(pack_info), pack_data.proto_id, json.encode(pack_data)) 17 | end 18 | 19 | function cache_db.get_cache_info(pack_info) 20 | local redisdb = db_mgr.get_redis_db() 21 | return redisdb:hgetall(get_cache_info_key(pack_info)) 22 | end 23 | 24 | function cache_db.get_cache_info_by_protoid(pack_info, proto_id) 25 | local redisdb = db_mgr.get_redis_db() 26 | local data = redisdb:hget(get_cache_info_key(pack_info), proto_id) 27 | utils.print_r(data) 28 | if data == nil then 29 | return nil 30 | end 31 | return json.decode(data) 32 | end 33 | 34 | function cache_db.remove_cache_info(pack_info) 35 | local redisdb = db_mgr.get_redis_db() 36 | pack_info.pack_id = "*" 37 | local keys = redisdb:keys(get_cache_info_key(pack_info)) 38 | redisdb:del(keys) 39 | end 40 | 41 | return cache_db -------------------------------------------------------------------------------- /preload/module_define.lua: -------------------------------------------------------------------------------- 1 | --模块 2 | MODULE = { 3 | GATE = { id = 0x00, name = "gate", server = SERVER.GATE, service = SERVICE.WATCHDOG }, 4 | AUTH = { id = 0x01, name = "auth", server = SERVER.AUTH, service = SERVICE.AUTH }, 5 | HALL = { id = 0x02, name = "hall", server = SERVER.HALL, service = SERVICE.HALL }, 6 | PLAYER = { id = 0x03, name = "player", server = nil, service = nil, is_agent = true }, 7 | BANK = { id = 0x04, name = "bank", server = nil, service = nil, is_agent = true}, 8 | CASH = { id = 0x05, name = "cash", server = nil, service = nil, is_agent = true}, 9 | PUSH = { id = 0x06, name = "push", server = nil, service = nil, is_agent = true}, 10 | MESSAGE = { id = 0x07, name = "message", server = nil, service = nil, is_agent = true}, 11 | CHAT = { id = 0x08, name = "chat", server = SERVER.CHAT, service = SERVICE.CHAT}, 12 | LOGIN = { id = 0x09, name = "login", server = SERVER.LOGIN, service = SERVICE.LOGIN }, 13 | 14 | AREA = { id = 0x0c, name = "area", server = SERVER.GAME, service = SERVICE.AREA }, 15 | ROOM = { id = 0x0d, name = "room", server = SERVER.GAME, service = nil }, 16 | DESK = { id = 0x0e, name = "desk", server = SERVER.GAME, service = nil }, 17 | XPNN = { id = 0x10, name = "xpnn", server = SERVER.GAME, service = nil }, 18 | LHD = { id = 0x11, name = "lhd", server = SERVER.GAME, service = nil }, 19 | DDZ = { id = 0x12, name = "ddz", server = SERVER.GAME, service = nil }, 20 | 21 | } 22 | -------------------------------------------------------------------------------- /client/simplesocket.lua: -------------------------------------------------------------------------------- 1 | local lsocket = require "lsocket" 2 | local utils = require "utils" 3 | local socket = {} 4 | local fd 5 | local message 6 | 7 | socket.error = setmetatable({}, { __tostring = function() return "[socket error]" end } ) 8 | 9 | function socket.connect(addr, port) 10 | assert(fd == nil) 11 | fd = lsocket.connect(addr, port) 12 | if fd == nil then 13 | error(socket.error) 14 | end 15 | 16 | lsocket.select(nil, {fd}) 17 | local ok, errmsg = fd:status() 18 | if not ok then 19 | error(socket.error) 20 | end 21 | 22 | message = "" 23 | end 24 | 25 | function socket.isconnect(ti) 26 | local rd, wt = lsocket.select(nil, { fd }, ti) 27 | return next(wt) ~= nil 28 | end 29 | 30 | function socket.close() 31 | fd:close() 32 | fd = nil 33 | message = nil 34 | end 35 | 36 | function socket.read(ti) 37 | while true do 38 | local ok, msg, n = pcall(string.unpack, ">s2", message) 39 | if not ok then 40 | local rd = lsocket.select({fd}, {fd}, ti) 41 | if next(rd) == nil then 42 | return nil 43 | end 44 | local p = fd:recv() 45 | if not p then 46 | error(socket.error) 47 | end 48 | message = message .. p 49 | else 50 | message = message:sub(n) 51 | return msg 52 | end 53 | end 54 | end 55 | 56 | function socket.write(msg) 57 | local pack = string.pack(">s2", msg) 58 | repeat 59 | local bytes = fd:send(pack) 60 | if not bytes then 61 | error(socket.error) 62 | end 63 | pack = pack:sub(bytes+1) 64 | until pack == "" 65 | end 66 | 67 | return socket 68 | -------------------------------------------------------------------------------- /logic/player/player_impl.lua: -------------------------------------------------------------------------------- 1 | local player_ctrl = require "player.player_ctrl" 2 | 3 | local player_impl = {} 4 | 5 | --TODO 获取玩家信息 for service 6 | function player_impl.get_player_info(ctx) 7 | return player_ctrl.get_player_info(ctx) 8 | end 9 | 10 | --TODO for service 11 | function player_impl.handle_player_payout(desk_svc, winlost_info) 12 | return player_ctrl.handle_player_payout(desk_svc, winlost_info) 13 | end 14 | 15 | --TODO 获取玩家数据for client 16 | function player_impl.qry_player_info(ctx, req) 17 | return player_ctrl.qry_player_info(ctx, req) 18 | end 19 | 20 | --TODO 修改玩家头像数据 21 | function player_impl.modify_head_info(ctx, req) 22 | return player_ctrl.modify_head_info(ctx, req) 23 | end 24 | 25 | function player_impl.modify_nickname_info(ctx, req) 26 | return player_ctrl.modify_nickname_info(ctx, req) 27 | end 28 | 29 | function player_impl.get_alipay_info(ctx, req) 30 | return player_ctrl.get_alipay_info(ctx, req) 31 | end 32 | 33 | function player_impl.get_bank_card_info(ctx, req) 34 | return player_ctrl.get_bank_card_info(ctx, req) 35 | end 36 | 37 | function player_impl.get_weixinpay_info(ctx, req) 38 | return player_ctrl.get_weixinpay_info(ctx, req) 39 | end 40 | 41 | function player_impl.bind_alipay_info(ctx, req) 42 | return player_ctrl.bind_alipay_info(ctx, req) 43 | end 44 | 45 | function player_impl.bind_bank_card_info(ctx, req) 46 | return player_ctrl.bind_bank_card_info(ctx, req) 47 | end 48 | 49 | function player_impl.bind_weixinpay_info(ctx, req) 50 | return player_ctrl.bind_weixinpay_info(ctx, req) 51 | end 52 | 53 | 54 | return player_impl -------------------------------------------------------------------------------- /proto/sproto/sproto/auth.sproto: -------------------------------------------------------------------------------- 1 | .auth { 2 | .PlayerBase { 3 | player_id 0 : integer 4 | nickname 1 : string 5 | head_id 2 : integer 6 | head_url 3 : string 7 | sex 4 : integer #0-未知 1-男 2-女 8 | gold 5 : integer(2) #金币 9 | } 10 | 11 | .AuthInfo { 12 | login_addr 0 : integer 13 | subid 1 : integer 14 | secret 2 : string 15 | waitsecond 3 : integer 16 | waitnum 4 : integer 17 | pid 5 : integer 18 | } 19 | .RegisterReq { 20 | account 0 : string #帐号 21 | password 1 : string #密码 22 | telephone 2 : string #电话号码 23 | agent_id 3 : string #代理id(推荐人id) 24 | create_index 4 : integer #创建索引 25 | nickname 5 : string #昵称 26 | } 27 | 28 | .RegisterReply { 29 | player 0 : PlayerBase 30 | auth_info 1 : AuthInfo 31 | } 32 | 33 | .LoginReq { 34 | account 0 : string #帐号 35 | password 1 : string #密码 36 | } 37 | 38 | .LoginReply { 39 | player 0 : PlayerBase 40 | auth_info 1 : AuthInfo 41 | } 42 | 43 | .WeixinReq { 44 | union_id 0 : string #微信用户惟一id 45 | head_url 1 : string #微信头像 46 | nickname 2 : string #微信昵称 47 | sex 3 : integer #性别 1-男 2-女 48 | } 49 | 50 | .WeiXinReply { 51 | player 0 : PlayerBase 52 | auth_info 1 : AuthInfo 53 | } 54 | 55 | .VisitorReq { 56 | visit_token 0 : string #游客token 57 | } 58 | 59 | .VisitorReply { 60 | player 0 : PlayerBase 61 | visit_token 1 : string #游客登录token 62 | auth_info 2: AuthInfo 63 | } 64 | 65 | .AuthSecretReq { 66 | step 0 : integer 67 | ckey 1 : string 68 | chmac 2 : string 69 | } 70 | 71 | .AuthSecretReply { 72 | challenge 0 : string 73 | skey 1 : string 74 | } 75 | } -------------------------------------------------------------------------------- /logic/chat/chat_logic.lua: -------------------------------------------------------------------------------- 1 | require "skynet.manager" 2 | local skynet = require "skynet" 3 | local context = require "context" 4 | local db_helper = require "common.db_helper" 5 | local utils = require "utils" 6 | local chat_logic = {} 7 | 8 | 9 | local function get_fds(ctx, req) 10 | local player_id = ctx.player_id 11 | local online = db_helper.call(DB_SERVICE.hall, "hall.get_player_online", player_id) 12 | if online ~= nil then 13 | local type = req.type 14 | local fds = db_helper.call(DB_SERVICE.fd, "fd.get_fds", type, online.game_id, online.server_id, online.room_id, online.room_addr) 15 | return fds 16 | end 17 | return nil 18 | end 19 | 20 | local function send_data(ctx, req, fds) 21 | local reply ={type = req.type, from = ctx.player_id, context = req.context} 22 | local fd_ctx = req.fd_ctx 23 | 24 | for k, s_fd in pairs(fds) do 25 | local fd = tonumber(s_fd) 26 | if ctx.fd ~= fd then 27 | local ctx = fd_ctx[fd] 28 | context.send_client_event(ctx, M_CHAT.chat_event, reply) 29 | end 30 | end 31 | end 32 | 33 | function chat_logic.chat_req(ctx, req) 34 | local contextdata = req.context 35 | local type = req.type 36 | if contextdata == nil or type == nil then 37 | return CHAT_ERROR.chat_param_error 38 | end 39 | local fds = get_fds(ctx, req) 40 | if fds == nil then 41 | skynet.error("chat fds is nil") 42 | return SYSTEM_ERROR.success 43 | end 44 | send_data(ctx, req, fds) 45 | return SYSTEM_ERROR.success 46 | end 47 | 48 | return chat_logic -------------------------------------------------------------------------------- /logic/config/xpnn_desk_config.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | 房间id, 游戏台坐位数, 底注, 最小入场, 玩游戏最小人数, 准备开始cd, 抢庄cd, 倍投cd, 开牌cd, 结束cd, 系统抽水 3 | room_id, seat_count, init_bet, min_enter, min_player, ready_begin_time, qiang_banker_time, bet_time, open_card_time, game_end_time, fee 4 | ]] 5 | local xpnn_desk_config = { 6 | [10101] = { 7 | room_id = 10101, 8 | seat_count = 5, 9 | init_bet = 0.10, 10 | min_enter = 30.00, 11 | min_player = 2, 12 | ready_begin_time = 3, 13 | qiang_banker_time = 4, 14 | bet_time = 5, 15 | open_card_time = 5, 16 | game_end_time = 5, 17 | exit_time = 10, 18 | fee = 0.05, 19 | }, 20 | [10102] = { 21 | room_id = 10102, 22 | seat_count = 5, 23 | init_bet = 1.00, 24 | min_enter = 50.00, 25 | min_player = 2, 26 | ready_begin_time = 3, 27 | qiang_banker_time = 4, 28 | bet_time = 5, 29 | open_card_time = 5, 30 | game_end_time = 2, 31 | exit_time = 5, 32 | fee = 0.05, 33 | }, 34 | [10103] = { 35 | room_id = 10103, 36 | seat_count = 5, 37 | init_bet = 5.00, 38 | min_enter = 300.00, 39 | min_player = 2, 40 | ready_begin_time = 3, 41 | qiang_banker_time = 4, 42 | bet_time = 5, 43 | open_card_time = 5, 44 | game_end_time = 2, 45 | exit_time = 5, 46 | fee = 0.05, 47 | }, 48 | [10104] = { 49 | room_id = 10104, 50 | seat_count = 5, 51 | init_bet = 10.00, 52 | min_enter = 600.00, 53 | min_player = 2, 54 | ready_begin_time = 3, 55 | qiang_banker_time = 4, 56 | bet_time = 5, 57 | open_card_time = 5, 58 | game_end_time = 2, 59 | exit_time = 5, 60 | fee = 0.05, 61 | }, 62 | } 63 | return xpnn_desk_config -------------------------------------------------------------------------------- /service/gateserver/main.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local cluster_monitor = require "cluster_monitor" 3 | local sproto_helper = require "sproto_helper" 4 | local redis_config = require "config.redis_config" 5 | local cluster_config = require "config.cluster_config" 6 | local http_port = skynet.getenv("http_port") 7 | local watchdog_port = skynet.getenv("watchdog_port") 8 | local wswatchdog_port = skynet.getenv("wswatchdog_port") 9 | 10 | skynet.start(function() 11 | local cluster_reids_id = tonumber(skynet.getenv("cluster_redis_id")) 12 | local cluster_server_id = tonumber(skynet.getenv("cluster_server_id")) 13 | 14 | cluster_monitor.start(redis_config[cluster_reids_id], cluster_config[cluster_server_id]) 15 | 16 | sproto_helper.register_protos() 17 | 18 | local watchdog = skynet.newservice("watchdog") 19 | skynet.call(watchdog, "lua", "start", { 20 | port = watchdog_port, 21 | maxclient = max_client, 22 | nodelay = true, 23 | }) 24 | 25 | local wswatchdog = skynet.newservice("wswatchdog") 26 | skynet.call(wswatchdog, "lua", "start", { 27 | port = wswatchdog_port, 28 | maxclient = max_client, 29 | nodelay = true, 30 | }) 31 | 32 | skynet.newservice("httpServer", http_port) 33 | local webclient = skynet.uniqueservice("webclient") 34 | local url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code" 35 | print(skynet.call(webclient, "lua", "request", url)) 36 | 37 | skynet.newservice("heartbeat") 38 | 39 | cluster_monitor.open() 40 | skynet.error("******************** gateserver start ok ********************") 41 | 42 | end) -------------------------------------------------------------------------------- /proto/sproto/sproto_gen/sproto_gen_linux.lua: -------------------------------------------------------------------------------- 1 | local root = "../" 2 | local skynetroot = root.."../../skynet/" 3 | package.path = skynetroot.."lualib/?.lua;" 4 | package.cpath = skynetroot.."luaclib/?.so;" 5 | local parser = require "sprotoparser" 6 | 7 | local protoDir = root .. "sproto/" 8 | local spbDir = root .. "spb/" 9 | local sprotoGen = {} 10 | 11 | function sprotoGen.create(files) 12 | local dumpFiles = {} 13 | 14 | for k, v in pairs(files) do 15 | local filename = protoDir .. v 16 | local f = assert(io.open(filename), "Can't open sproto filename["..filename.."]") 17 | local data = f:read "a" 18 | dumpFiles[k] = dumpFiles[k] or "" 19 | dumpFiles[k] = dumpFiles[k] .. data 20 | end 21 | 22 | for k, v in pairs(dumpFiles) do 23 | local file = io.open(spbDir .. k ..".spb", "w+b") 24 | file:write(parser.parse(v)) 25 | file:close() 26 | end 27 | 28 | end 29 | 30 | --[[local files = { 31 | ["package"] = "package.sproto", 32 | } 33 | sprotoGen.create(c2sfiles, "C2S.spb") 34 | ]]-- 35 | 36 | local files = { 37 | package = "package.sproto", 38 | gate = "gate.sproto", 39 | auth = "auth.sproto", 40 | login = "login.sproto", 41 | area = "area.sproto", 42 | hall = "hall.sproto", 43 | player = "player.sproto", 44 | bank = "bank.sproto", 45 | cash = "cash.sproto", 46 | push = "push.sproto", 47 | message = "message.sproto", 48 | room = "room.sproto", 49 | desk = "desk.sproto", 50 | xpnn = "xpnn.sproto", 51 | lrnn = "lrnn.sproto", 52 | ddz = "ddz.sproto", 53 | zjh = "zjh.sproto", 54 | lrsh = "lrsh.sproto", 55 | chat = "chat.sproto", 56 | } 57 | 58 | sprotoGen.create(files) -------------------------------------------------------------------------------- /service/cluster_monitord.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : cluster_monitord.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | require "skynet.manager" 9 | local skynet = require "skynet" 10 | --local cluster = require "skynet.cluster" 11 | local cluster_mgr = require "common.cluster_mgr" 12 | 13 | local command = {} 14 | 15 | 16 | function command.connect(session, conf) 17 | --session 为 0 的是 send > 0 的是 call 18 | local ret1, ret2 19 | if session > 0 then 20 | skynet.retpack(true) 21 | end 22 | end 23 | 24 | function command.check_alive(session, conf) 25 | 26 | end 27 | 28 | function command.subscribe_monitor(session, addr) 29 | local ret1, ret2 = cluster_mgr.subscribe_monitor(addr) 30 | if session > 0 then 31 | skynet.retpack(ret1, ret2) 32 | end 33 | end 34 | 35 | function command.unsubscribe_monitor(session, addr) 36 | local ret1, ret2 = cluster_mgr.unsubscribe_monitor(addr) 37 | if session > 0 then 38 | skynet.retpack(ret1, ret2) 39 | end 40 | end 41 | 42 | function command.start(session, redis_conf, current_conf) 43 | local ret1, ret2 = cluster_mgr.start(redis_conf, current_conf) 44 | if session > 0 then 45 | skynet.retpack(ret1, ret2) 46 | end 47 | end 48 | 49 | function command.open(session) 50 | local ret1, ret2 = cluster_mgr.open() 51 | if session > 0 then 52 | skynet.retpack(ret1, ret2) 53 | end 54 | end 55 | 56 | skynet.start(function() 57 | skynet.dispatch("lua", function(session, address, cmd, ...) 58 | local f = command[cmd] 59 | assert(f, "function["..cmd.."] is nil") 60 | f(session, ...) 61 | end) 62 | skynet.register(SERVICE.CLUSTER_MONITOR) 63 | end) 64 | -------------------------------------------------------------------------------- /lualib/context.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : context.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | 9 | local skynet = require "skynet" 10 | local requester = require "requester" 11 | local context = {} 12 | 13 | 14 | function context.call(addr, cmd, ...) 15 | return requester.call(addr, cmd, ...) 16 | end 17 | 18 | function context.send(addr, cmd, ...) 19 | return requester.send(addr, cmd, ...) 20 | end 21 | 22 | function context.rpc_call(node, addr, cmd, ...) 23 | return requester.rpc_call(node, addr, cmd, ...) 24 | end 25 | 26 | function context.rpc_send(node, addr, cmd, ...) 27 | return requester.rpc_send(node, addr, cmd, ...) 28 | end 29 | 30 | function context.send_service(addr, cmd, ...) 31 | return requester.send(addr, "dispatch_service_msg", cmd, ...) 32 | end 33 | 34 | function context.call_service(addr, cmd, ...) 35 | return requester.call(addr, "dispatch_service_msg", cmd, ...) 36 | end 37 | 38 | function context.rpc_send_service(node, addr, cmd, ...) 39 | return requester.rpc_send(node, addr, "dispatch_service_msg", cmd, ...) 40 | end 41 | 42 | function context.rpc_call_service(node, addr, cmd, ...) 43 | return requester.rpc_call(node, addr, "dispatch_service_msg", cmd, ...) 44 | end 45 | 46 | function context.send_client_event(ctx, proto, data) 47 | --set_cache(ctx, proto, push_data) 48 | return requester.send_client_msg(ctx, proto, nil, data) 49 | end 50 | 51 | function context.response_client(ctx, proto, data) 52 | --set_cache(ctx, proto, push_data) 53 | return requester.send_client_msg(ctx, proto, {session = ctx.request_id, proto_id = proto.id}, data) 54 | end 55 | 56 | return context -------------------------------------------------------------------------------- /logic/hall/hall_db.lua: -------------------------------------------------------------------------------- 1 | local db_mgr = require "common.db_mgr" 2 | 3 | local hall_db = {} 4 | 5 | local PlayerOnlineKey = "player_online" 6 | local FdPlayerIdKey = "fd_playerId" 7 | 8 | local function get_player_online_key(id) 9 | return PlayerOnlineKey .. ":" .. id 10 | end 11 | 12 | function hall_db.get_player_online(id) 13 | local redisdb = db_mgr.get_redis_db() 14 | return array_totable(redisdb:hgetall(get_player_online_key(id))) 15 | end 16 | 17 | function hall_db.set_player_online(id, online) 18 | local redisdb = db_mgr.get_redis_db() 19 | local data = assert(table.toarray(online), "online is not table") 20 | redisdb:hmset(get_player_online_key(id), table.unpack(data)) 21 | return 22 | end 23 | 24 | function hall_db.update_player_online(id, key, value) 25 | local redisdb = db_mgr.get_redis_db() 26 | redisdb:hset(get_player_online_key(id), key, value) 27 | return 28 | end 29 | 30 | function hall_db.del_player_online(id) 31 | local redisdb = db_mgr.get_redis_db() 32 | redisdb:del(get_player_online_key(id)) 33 | return 34 | end 35 | 36 | function hall_db.del_player_online_value(id, key) 37 | local redisdb = db_mgr.get_redis_db() 38 | redisdb:hdel(get_player_online_key(id), key) 39 | return 40 | end 41 | 42 | -- function hall_db.set_player_online_value(id, key, value) 43 | -- local redisdb = db_mgr.get_redis_db() 44 | -- redisdb:hset(get_player_online_key(id), key, value) 45 | -- end 46 | 47 | -- fd - playerid 无用 48 | function hall_db.get_fd_playerid(fd) 49 | local redisdb = db_mgr.get_redis_db() 50 | return redisdb:hget(FdPlayerIdKey, fd) 51 | end 52 | 53 | function hall_db.set_fd_playerid(fd, playerId) 54 | local redisdb = db_mgr.get_redis_db() 55 | redisdb:hmset(FdPlayerIdKey, fd, playerId) 56 | end 57 | 58 | return hall_db -------------------------------------------------------------------------------- /logic/config/db_config.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | 连接名, mysql连接id, redis连接id, 访问svc方式, service服务名, 连接个数, 注释 3 | svc_name, mysql_id, redis_id, get_svc, service_name, svc_count, desc 4 | ]] 5 | local db_config = { 6 | ["unique_db"] = { 7 | svc_name = [[unique_db]], 8 | mysql_id = 1, 9 | redis_id = 1, 10 | get_svc = 1, 11 | service_name = ".unique_db", 12 | svc_count = 1, 13 | desc = [[该db用于惟一id, 玩家在线状态]], 14 | }, 15 | ["account_db"] = { 16 | svc_name = [[account_db]], 17 | mysql_id = 1, 18 | redis_id = 0, 19 | get_svc = 2, 20 | service_name = ".account_db", 21 | svc_count = 4, 22 | desc = [[该db用于登陆(查询tb_account, tb_player)]], 23 | }, 24 | ["hall_db"] = { 25 | svc_name = [[hall_db]], 26 | mysql_id = 1, 27 | redis_id = 2, 28 | get_svc = 1, 29 | service_name = ".hall_db", 30 | svc_count = 1, 31 | desc = [[该db用于缓存玩家在线状态,玩家登录状态]], 32 | }, 33 | ["game_db"] = { 34 | svc_name = [[game_db]], 35 | mysql_id = 1, 36 | redis_id = 3, 37 | get_svc = 1, 38 | service_name = ".game_db", 39 | svc_count = 1, 40 | desc = [[该db用于游戏房间注册等]], 41 | }, 42 | ["agent_db"] = { 43 | svc_name = [[agent_db]], 44 | mysql_id = 1, 45 | redis_id = 4, 46 | get_svc = 3, 47 | service_name = ".agent_db", 48 | svc_count = 4, 49 | desc = [[该db用于缓存玩家数据(每个玩家id对应一个db)]], 50 | }, 51 | ["fd_db"] = { 52 | svc_name = [[fd_db]], 53 | mysql_id = 1, 54 | redis_id = 5, 55 | get_svc = 1, 56 | service_name = ".fd_db", 57 | svc_count = 1, 58 | desc = [[该db用于fd管理]], 59 | }, 60 | ["auth_db"] = { 61 | svc_name = [[auth_db]], 62 | mysql_id = 1, 63 | redis_id = 2, 64 | get_svc = 1, 65 | service_name = ".auth_db", 66 | svc_count = 1, 67 | desc = [[该db用于auth]], 68 | }, 69 | } 70 | return db_config -------------------------------------------------------------------------------- /lualib/command_base.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : command_base.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | require("debug") 9 | local skynet = require "skynet" 10 | local profile = require "skynet.profile" 11 | local ti = {} 12 | local command_base = {} 13 | 14 | 15 | function command_base.run(source, filename, ...) 16 | local output = {} 17 | local function print(...) 18 | local value = {...} 19 | for k, v in ipairs(value) do 20 | value[k] = tostring(v) 21 | end 22 | table.insert(output, table.concat(value, "\t")) 23 | end 24 | 25 | local env = setmetatable({print = print, args = {...}}, {__index = _ENV}) 26 | local func, err = load(source, filename, "bt", env) 27 | if not func then 28 | return {err} 29 | end 30 | local ok, err = xpcall(func, debug.traceback) 31 | if not ok then 32 | table.insert(output, err) 33 | end 34 | end 35 | 36 | local function profile_call(func, cmd, ...) 37 | profile.start() 38 | local ret1, ret2, ret3, ret4 = func(...) 39 | local time = profile.stop() 40 | local p = ti[cmd] 41 | if p == nil then 42 | p = { n = 0, ti = 0 } 43 | ti[cmd] = p 44 | end 45 | p.n = p.n + 1 46 | p.ti = p.ti + time 47 | return ret1, ret2, ret3, ret4 48 | end 49 | 50 | local function dispatch(session, address, cmd, ...) 51 | --skynet.error("cy=======command_base=", cmd) 52 | local func = assert(command_base[cmd], string.format("command[%s] is nil", cmd)) 53 | if session > 0 then 54 | skynet.retpack(func(...)) 55 | else 56 | func(...) 57 | end 58 | end 59 | 60 | skynet.dispatch("lua", dispatch) 61 | 62 | skynet.info_func(function() 63 | return {mem = collectgarbage("count"), ti = ti } 64 | end) 65 | 66 | return command_base 67 | -------------------------------------------------------------------------------- /logic/common/db_mgr.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local redis = require "skynet.db.redis" 3 | local mysql = require "skynet.db.mysql" 4 | local mongo = require "skynet.db.mongo" 5 | local db_config = require "config.db_config" 6 | local mysql_config = require "config.mysql_config" 7 | local redis_config = require "config.redis_config" 8 | local db_mgr = {} 9 | 10 | local _redisdb = nil 11 | local _mongodb = nil 12 | local _mysqldb = nil 13 | local _mysqldb_log = nil 14 | 15 | function db_mgr.get_mysql_db() 16 | return _mysqldb 17 | end 18 | 19 | function db_mgr.get_redis_db() 20 | return _redisdb 21 | end 22 | 23 | function db_mgr.init_mysql_db(mysql_id) 24 | local conf = mysql_config[mysql_id] 25 | if not conf then 26 | return 27 | end 28 | -- local mysql_conf = { 29 | -- host = conf.host, 30 | -- port = conf.port, 31 | -- database = conf.database, 32 | -- user = conf.user, 33 | -- password = conf.password, 34 | -- max_packet_size = conf.max_packet_size 35 | -- } 36 | _mysqldb = mysql.connect(conf) 37 | assert(_mysqldb, string.format("mysql connect %s:%d failed", conf.host, conf.port)) 38 | _mysqldb:query("set charset utf8") 39 | end 40 | 41 | function db_mgr.init_redis_db(redis_id) 42 | local conf = redis_config[redis_id] 43 | if not conf then 44 | return 45 | end 46 | -- local redis_conf = { 47 | -- host = conf.host, 48 | -- port = conf.port, 49 | -- db = conf.db 50 | -- } 51 | _redisdb = redis.connect(conf) 52 | assert(_redisdb, string.format("redis connect %s:%d failed", conf.host, conf.port)) 53 | end 54 | 55 | function db_mgr.init(svc_name) 56 | local conf = assert(db_config[svc_name], "not found svc_name") 57 | db_mgr.init_mysql_db(conf.mysql_id) 58 | db_mgr.init_redis_db(conf.redis_id) 59 | end 60 | 61 | return db_mgr -------------------------------------------------------------------------------- /logic/role/role_logic.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local json = require "json" 3 | local md5 = require "md5" 4 | local random = require "random" 5 | local db_helper = require "common.db_helper" 6 | local create_role_config = require "config.create_role_config" 7 | local nickname_config = require "config.nickname_config" 8 | local role_logic = {} 9 | 10 | function role_logic.init() 11 | 12 | end 13 | 14 | function role_logic.get_role_id(ctx, req) 15 | local role_info = db_helper.call(DB_SERVICE.agent, "role.get_role", ctx.player_id, ctx.game_id) 16 | if role_info then 17 | db_helper.call(DB_SERVICE.agent, "role.cache_role_info", ctx.player_id, ctx.game_id) 18 | return SYSTEM_ERROR.success, role_info.role_id 19 | else 20 | return ROLE_ERROR.role_nil 21 | end 22 | end 23 | 24 | function role_logic.create_role(ctx, req) 25 | local conf = create_role_config[req.create_index] 26 | local role_info = db_helper.call(DB_SERVICE.agent, "role.get_role", ctx.player_id, ctx.game_id) 27 | if role_info then 28 | return ROLE_ERROR.role_exist 29 | end 30 | local role_id = db_helper.call(DB_SERVICE.unique, "role.incr_role_id", nil) 31 | if not role_id then 32 | return ROLE_ERROR.role_id_limit 33 | end 34 | local nickname = req.nickname or random.random_one(nickname_config) 35 | role_info = { 36 | role_id = role_id, 37 | player_id = ctx.player_id, 38 | game_id = ctx.game_id, 39 | head_id = conf.head_id, 40 | head_url = "", 41 | nickname = nickname, 42 | sex = conf.sex, 43 | gold = conf.gold, 44 | create_time = os.time(), 45 | } 46 | db_helper.call(DB_SERVICE.agent, "role.create_role", nil, role_info) 47 | db_helper.call(DB_SERVICE.agent, "role.cache_role_info", ctx.player_id, ctx.game_id) 48 | return SYSTEM_ERROR.success, role_info.role_id 49 | end 50 | 51 | return role_logic -------------------------------------------------------------------------------- /logic/cache/cache_logic.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local context = require "context" 3 | local utils = require "utils" 4 | local db_helper = require "common.db_helper" 5 | local cache_logic = {} 6 | 7 | 8 | function cache_logic.set_cache(packid, player_id, proto_id, ec, data) 9 | if packid == nil or player_id == nil or proto_id == nil then 10 | return 11 | end 12 | local pack_info = {player_id = player_id, pack_id = packid} 13 | local pack_data = {proto_id = proto_id, ec = ec, data = data} 14 | db_helper.call(DB_SERVICE.auth, "cache.insert_cache_info", pack_info, pack_data) 15 | end 16 | 17 | function cache_logic.get_cache(player_id, packid, protoid) 18 | if player_id == nil or packid == nil or protoid == nil then 19 | return nil 20 | end 21 | local pack_info = {player_id = player_id, pack_id = packid} 22 | return db_helper.call(DB_SERVICE.auth, "cache.get_cache_info_by_protoid", pack_info, protoid) 23 | end 24 | 25 | function cache_logic.set_c_context(c, player_id) 26 | local online = db_helper.call(DB_SERVICE.hall, "hall.get_player_online", player_id) 27 | if online and tonumber(online.offline) == OFFLINE_STATE.on then 28 | skynet.error("cy=========================================================================do reconnect work ssssss") 29 | context.rpc_call(online.gate, online.watchdog, "set_c_context", c.fd, online) 30 | context.rpc_call(online.gate, SERVICE.HEARTBEAT, "reset_updatetime", player_id) 31 | local update_online_info = { 32 | offline = OFFLINE_STATE.off, 33 | fd = c.fd, 34 | ip = c.ip, 35 | gate = c.gate, 36 | watchdog = c.watchdog, 37 | } 38 | db_helper.call(DB_SERVICE.hall, "hall.set_player_online", player_id, update_online_info) 39 | end 40 | end 41 | 42 | return cache_logic -------------------------------------------------------------------------------- /lualib/share_memory.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : share_memory.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | local skynet = require "skynet" 9 | local stm = require "skynet.stm" 10 | 11 | local addr 12 | 13 | local share_memory = {} 14 | local share_data_map = {} 15 | 16 | local function init() 17 | addr = skynet.uniqueservice("share_memoryd") 18 | end 19 | 20 | local function get_stmcopy(name) 21 | if share_data_map[name] then 22 | return share_data_map[name].stmcopy 23 | end 24 | 25 | local stmobj = skynet.call(addr, "lua", "get_stmobj", name) 26 | if not stmobj then 27 | return nil 28 | end 29 | share_data_map[name] = {} 30 | share_data_map[name].stmcopy = stm.newcopy(stmobj) 31 | share_data_map[name].data = nil 32 | return share_data_map[name].stmcopy 33 | end 34 | 35 | local function get_share_memory(name) 36 | local stmcopy = get_stmcopy(name) 37 | if not stmcopy then 38 | return 39 | end 40 | 41 | local ok, data = stmcopy(skynet.unpack) 42 | if not ok then --旧数据 43 | return share_data_map[name].data 44 | end 45 | share_data_map[name].data = data 46 | return share_data_map[name].data 47 | end 48 | 49 | local function set_share_memory(name, data) 50 | local stmcopy = get_stmcopy(name) 51 | if not stmcopy then 52 | skynet.call(addr, "lua", "set_stmobj", name, data) 53 | 54 | stmcopy = get_stmcopy(name) 55 | share_data_map[name].data = data 56 | return 57 | end 58 | 59 | skynet.call(addr, "lua", "set_stmobj", name, data) 60 | share_data_map[name].data = data 61 | return 62 | end 63 | 64 | setmetatable(share_memory, { 65 | __index = function(t, k) 66 | return get_share_memory(k) 67 | end, 68 | __newindex = function(t, k, v) 69 | return set_share_memory(k, v) 70 | end, 71 | }) 72 | 73 | 74 | skynet.init(init) 75 | 76 | 77 | return share_memory 78 | 79 | 80 | -------------------------------------------------------------------------------- /lualib/random.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : random.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | 9 | local skynet = require "skynet" 10 | local mt19937 = require "mt19937" 11 | local random = {} 12 | 13 | local random_type = tonumber(skynet.getenv("random_type")) 14 | local __randomseed = os.time() + skynet.self() 15 | 16 | 17 | function random.rand(i, j) 18 | __randomseed = __randomseed + 1 19 | if random_type == RANDOM_TYPE.mt_19937 then --随机性低,快 20 | mt19937.init(__randomseed) 21 | if not j then 22 | j = i + 1 23 | return mt19937.randi(1, i+1) 24 | else 25 | return mt19937.randi(i, j+1) 26 | end 27 | elseif random_type == RANDOM_TYPE.linux_urandom then --随机性中,快 28 | local data = io.open("/dev/urandom", "r"):read(4) 29 | math.randomseed(os.time() + data:byte(1) + (data:byte(2) * 256) + (data:byte(3) * 65536) + (data:byte(4) * 4294967296)) 30 | return math.random(i, j) 31 | elseif random_type == RANDOM_TYPE.linux_random then --随机性高,慢 32 | local data = io.open("/dev/random", "r"):read(4) 33 | math.randomseed(os.time() + data:byte(1) + (data:byte(2) * 256) + (data:byte(3) * 65536) + (data:byte(4) * 4294967296)) 34 | return math.random(i, j) 35 | else --随机性极低 36 | math.randomseed(__randomseed) 37 | return math.random(i, j) 38 | end 39 | end 40 | 41 | function random.random_one(list) 42 | return list[random.rand(1, #list)] 43 | end 44 | 45 | function random.random_shuffle(list) 46 | local array = copy(list) 47 | local length = #array 48 | local function swap(i, j) 49 | local tmp = clone(array[i]) 50 | array[i] = array[j] 51 | array[j] = tmp 52 | end 53 | for i=1, length-1 do 54 | local j = random.rand(i+1, length) 55 | swap(i, j) 56 | end 57 | return array 58 | end 59 | 60 | return random 61 | -------------------------------------------------------------------------------- /test/client_json_msg.lua: -------------------------------------------------------------------------------- 1 | local json = require "json" 2 | 3 | --要登录后才能用 4 | local pingReq = { 5 | header = { 6 | protoid = 0x0001, 7 | session = 1, 8 | response = nil, 9 | }, 10 | data = 1234, 11 | } 12 | print("pingReq="..json.encode(pingReq)) 13 | 14 | 15 | local registerReq = { 16 | header = { 17 | protoid = 0x0101, 18 | session = 1, 19 | response = nil, 20 | }, 21 | data = { 22 | account = "游客1001", 23 | password = "123456", 24 | telephone = "1383838438", 25 | create_index = 1, 26 | nickname = "helloworld", 27 | }, 28 | } 29 | print("registerReq="..json.encode(registerReq)) 30 | 31 | local visitorReq = { 32 | header = { 33 | protoid = 0x0104, 34 | session = 1, 35 | response = nil, 36 | }, 37 | data = { 38 | visit_token = "a3984026-c67b-4889-c827-db693fba8f64", 39 | }, 40 | } 41 | print("visitorReq="..json.encode(visitorReq)) 42 | 43 | local visitorReq = { 44 | header = { 45 | protoid = 0x0104, 46 | session = 1, 47 | response = nil, 48 | }, 49 | data = { 50 | visit_token = "b4a7d645-f3c9-472e-c76c-14c09b8596c8", 51 | }, 52 | } 53 | print("visitorReq="..json.encode(visitorReq)) 54 | 55 | 56 | local roomListReq = { 57 | header = { 58 | protoid = 0x0201, 59 | session = 2, 60 | response = nil, 61 | }, 62 | data = 10101, 63 | } 64 | print("roomListReq="..json.encode(roomListReq)) 65 | 66 | local enterRoomReq = { 67 | header = { 68 | protoid = 0x0d01, 69 | session = 2, 70 | response = nil, 71 | roomproxy = "xpnn", 72 | }, 73 | } 74 | print("enterRoomReq="..json.encode(enterRoomReq)) 75 | 76 | exitRoomReq = { 77 | header = { 78 | protoid = 0x0d02, 79 | session = 2, 80 | response = nil, 81 | roomproxy = "xpnn", 82 | }, 83 | } 84 | print("exitRoomReq="..json.encode(exitRoomReq)) 85 | 86 | groupRequest = { 87 | header = { 88 | protoid = 0x0d03, 89 | session = 2, 90 | response = nil, 91 | roomproxy = "xpnn", 92 | }, 93 | } 94 | print("groupRequest="..json.encode(groupRequest)) -------------------------------------------------------------------------------- /service/master_db.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : master_db.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | require "skynet.manager" 9 | local skynet = require "skynet" 10 | local db_config = require "config.db_config" 11 | local utils = require "utils" 12 | 13 | local command = {} 14 | local db_pool = {} 15 | local db_pool_index = {} 16 | 17 | 18 | local function init_db_pool() 19 | for _, conf in pairs(db_config) do 20 | local svc_count = conf.svc_count 21 | for i=1, svc_count do 22 | local svc = skynet.newservice("master_db_svc", conf.svc_name) 23 | if conf.get_svc == GET_SVC_TYPE.unique then 24 | skynet.error("init_db_pool svcname=", conf.service_name) 25 | skynet.name(conf.service_name, svc) 26 | else 27 | skynet.error("init_db_pool svcname=", conf.service_name..i) 28 | skynet.name(conf.service_name..i, svc) 29 | end 30 | db_pool[conf.svc_name] = db_pool[conf.svc_name] or {} 31 | table.insert(db_pool[conf.svc_name], svc) 32 | end 33 | end 34 | end 35 | 36 | local function dispatch(session, addr, cmd, ...) 37 | local func = command[cmd] 38 | if not func then 39 | return 40 | end 41 | if session > 0 then 42 | skynet.retpack(func(...)) 43 | else 44 | func(...) 45 | end 46 | end 47 | 48 | function command.get_db_svc(svc_name) 49 | if not svc_name or not db_pool[svc_name] then 50 | return nil 51 | end 52 | db_pool_index[svc_name] = db_pool_index[svc_name] or 1 53 | local index = db_pool_index[svc_name] 54 | local svc = db_pool[svc_name][index] 55 | if not svc then 56 | return nil 57 | end 58 | db_pool_index[svc_name] = db_pool_index[svc_name] + 1 59 | if db_pool_index[svc_name] > #db_pool[svc_name] then 60 | db_pool_index[svc_name] = 1 61 | end 62 | 63 | return svc 64 | end 65 | 66 | function command.start() 67 | init_db_pool() 68 | end 69 | 70 | skynet.start(function() 71 | skynet.dispatch("lua", dispatch) 72 | skynet.register(SERVICE.MASTER_DB) 73 | end) 74 | -------------------------------------------------------------------------------- /logic/agent/agent_ctrl.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local context = require "context" 3 | local db_helper = require "common.db_helper" 4 | local agent_ctrl = {} 5 | 6 | local current_conf 7 | 8 | local function get_hall_player_agent(player_id) 9 | local agent_count = skynet.getenv("agent_count") 10 | local index = toint(player_id % agent_count) 11 | if index == 0 then index = agent_count end 12 | return ".agent"..index 13 | end 14 | 15 | function agent_ctrl.is_hall() 16 | return current_conf.servertype == SERVER.HALL 17 | end 18 | 19 | function agent_ctrl.init(conf) 20 | current_conf = conf 21 | end 22 | 23 | function agent_ctrl.on_login(ctx, player_info) 24 | -- print("agent_ctrl.on_login ctx=", table.tostring(ctx), "player_info=", table.tostring(player_info)) 25 | local player_id = player_info.player_id 26 | if agent_ctrl.is_hall() then 27 | db_helper.call(DB_SERVICE.hall, "hall.update_player_online", player_id, "hall_agentnode", current_conf.nodename) 28 | context.rpc_call(ctx.gate, ctx.watchdog, "login_ok", ctx.fd, player_id, current_conf.nodename, get_hall_player_agent(player_id)) 29 | else 30 | local update_online_info = { 31 | -- room_id = room_id, 32 | agentaddr = skynet.self(), 33 | -- agentnode = current_conf.nodename, 34 | -- agentver = current_conf.ver, 35 | } 36 | db_helper.call(DB_SERVICE.hall, "hall.set_player_online", player_info.player_id, update_online_info) 37 | context.rpc_call(ctx.gate, ctx.watchdog, "set_agent", ctx.fd, skynet.self()) 38 | end 39 | end 40 | 41 | function agent_ctrl.on_logout(ctx) 42 | print("recv agent_ctrl.on_logout is_hall()=", agent_ctrl.is_hall()) 43 | if not agent_ctrl.is_hall() then 44 | db_helper.call(DB_SERVICE.hall, "hall.del_player_online_value", ctx.player_id, "room_id") 45 | -- db_helper.call(DB_SERVICE.hall, "hall.del_player_online_value", ctx.player_id, "agentnode") 46 | db_helper.call(DB_SERVICE.hall, "hall.del_player_online_value", ctx.player_id, "agentaddr") 47 | -- db_helper.call(DB_SERVICE.hall, "hall.del_player_online_value", ctx.player_id, "agentver") 48 | end 49 | end 50 | 51 | function agent_ctrl.reconnect(ctx) 52 | 53 | end 54 | 55 | 56 | 57 | return agent_ctrl -------------------------------------------------------------------------------- /service/desk.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : desk.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | local skynet = require "skynet" 9 | local service = require "service_base" 10 | local cluster_monitor = require "cluster_monitor" 11 | local context = require "context" 12 | local config_db = require "config_db" 13 | local cluster_config = require "config.cluster_config" 14 | local desk_ctrl = require "desk.desk_ctrl" 15 | local command = service.command 16 | local room_id 17 | local modulename = ... 18 | service.is_agent = true 19 | 20 | local server_id 21 | local current_conf 22 | local module_ctrl 23 | 24 | -- local modules 25 | 26 | -- local function init_modules() 27 | -- modules = require("config."..modulename.."_module") 28 | -- setmetatable(service.modules, { 29 | -- __index = function(t, k) 30 | -- local mod = modules[k] 31 | -- if not mod then 32 | -- return nil 33 | -- end 34 | -- local v = require(mod) 35 | -- t[k] = v 36 | -- return v 37 | -- end 38 | -- }) 39 | -- end 40 | 41 | function command.update_configs(configs) 42 | config_db.update(configs) 43 | end 44 | 45 | function command.init(configs, room_id) 46 | room_id = room_id 47 | config_db.init(configs) 48 | desk_ctrl.init() 49 | module_ctrl = require(modulename.."."..modulename.."_ctrl") 50 | module_ctrl.init(room_id) 51 | end 52 | 53 | function command.login_desk(ctx, agent) 54 | return desk_ctrl.login_desk(ctx, agent) 55 | end 56 | 57 | function command.logout_desk(ctx) 58 | return desk_ctrl.logout_desk(ctx) 59 | end 60 | 61 | function command.player_reconnect(ctx) 62 | return desk_ctrl.player_reconnect(ctx) 63 | end 64 | 65 | function command.player_disconnect(ctx) 66 | return desk_ctrl.player_disconnect(ctx) 67 | end 68 | 69 | function command.can_group() 70 | return desk_ctrl.can_group() 71 | end 72 | 73 | function service.on_start() 74 | server_id = tonumber(skynet.getenv("cluster_server_id")) 75 | current_conf = cluster_config[server_id] 76 | end 77 | 78 | service.modules.desk = require("desk.desk_impl") 79 | service.modules[modulename] = require(modulename.."."..modulename.."_impl") 80 | service.start() -------------------------------------------------------------------------------- /lualib/redis_mq.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : redis_mq.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | local class = require "class" 9 | local skynet = require "skynet" 10 | local redis = require "skynet.db.redis" 11 | local utils = require "utils" 12 | 13 | local RedisMQ = class() 14 | function RedisMQ:_init(conf, on_message) 15 | self._db_conf = conf 16 | self._db = redis.connect(conf) 17 | self._subscribe_map = {} 18 | -- need subscribe 19 | self._subscribe_list = {} 20 | self._psubscribe_list = {} 21 | 22 | -- watch 23 | self._watch = nil 24 | self._pause = true 25 | self._on_message = on_message 26 | self._watch = redis.watch(self._db_conf) 27 | end 28 | 29 | function RedisMQ:start_watching() 30 | self._pause = false 31 | local function watching() 32 | 33 | self._watch:subscribe("watching.pause") 34 | 35 | while not self._pause do 36 | local data, method = self._watch:message() 37 | if method ~= "watching.pause" and self._on_message then 38 | self._on_message(method, data) 39 | end 40 | end 41 | end 42 | skynet.fork(watching) 43 | end 44 | 45 | function RedisMQ:stop_watching() 46 | self._pause = true 47 | self._db:publish("watching.pause", "pause") 48 | end 49 | 50 | -- publish message 51 | function RedisMQ:publish(method, message) 52 | self._db:publish(method, message) 53 | end 54 | 55 | -- subscribe message 56 | function RedisMQ:subscribe(method) 57 | if self._subscribe_map[method] then return end 58 | 59 | --self:stop_watching() 60 | 61 | self._watch:subscribe(method) 62 | 63 | self._subscribe_map[method] = true 64 | 65 | --self:start_watching() 66 | end 67 | 68 | -- psubscribe message 69 | function RedisMQ:psubscribe(method) 70 | if self._subscribe_map[method] then return end 71 | 72 | --self:stop_watching() 73 | 74 | self._watch:psubscribe(method) 75 | 76 | self._subscribe_map[method] = true 77 | 78 | --self:start_watching() 79 | 80 | end 81 | 82 | -- unsubscribe message 83 | function RedisMQ:unsubscribe(method) 84 | if not self._subscribe_map[method] then return end 85 | end 86 | 87 | function RedisMQ:get_redis_db() 88 | return self._db 89 | end 90 | 91 | return RedisMQ 92 | -------------------------------------------------------------------------------- /service/loggerd.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : loggerd.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | require "skynet.manager" 9 | local skynet = require "skynet" 10 | local logpath = skynet.getenv("logpath") or "./log" 11 | local command = {} 12 | local file_list = {} 13 | 14 | local log_level_desc = { 15 | [0] = "NOLOG", 16 | [10] = "DEBUG", 17 | [20] = "INFO", 18 | [30] = "WARNING", 19 | [40] = "ERROR", 20 | [50] = "CRITICAL", 21 | [60] = "FATAL", 22 | } 23 | 24 | local function init() 25 | local logfile = skynet.getenv("logfile") or "default.log" 26 | os.execute("mkdir -p " .. logpath) 27 | local file = assert(io.open(logpath.."/"..logfile, "a"), "log file open failed") 28 | file_list["default"] = file 29 | end 30 | 31 | local function dumplog(name, text) 32 | local file = file_list[name] 33 | if not file then 34 | return 35 | end 36 | 37 | file:write(text) 38 | file:write("\n") 39 | file:flush() 40 | end 41 | 42 | local function log_format(object) 43 | if object.tags and next(object.tags) then 44 | return string.format("[%s %s] [%s]%s %s", object.timestamp, object.level,table.concat(object.tags, ","), object.src, object.msg) 45 | else 46 | return string.format("[%s %s]%s %s", object.timestamp, object.level, object.src, object.msg) 47 | end 48 | end 49 | 50 | function command.log(name, modname, level, timestamp, msg, src, tags) 51 | dumplog("default", log_format { 52 | name = name, 53 | modname = modname, 54 | level = log_level_desc[level], 55 | timestamp = timestamp, 56 | msg = msg, 57 | src = src or '', 58 | tags = tags, 59 | }) 60 | end 61 | 62 | function command.new_log(name, filename) 63 | local file = assert(io.open(logpath.."/"..filename, "a"), "log file open failed") 64 | file_list[name] = file 65 | end 66 | 67 | local function dispatch(session, address, cmd, ...) 68 | local func = assert(command[cmd], string.format("command[%s] is nil", tostring(cmd))) 69 | if session > 0 then 70 | skynet.retpack(func(...)) 71 | else 72 | func(...) 73 | end 74 | end 75 | 76 | skynet.start(function() 77 | init() 78 | skynet.dispatch("lua", dispatch) 79 | end) -------------------------------------------------------------------------------- /logic/config/nickname_config.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | 玩家昵称 3 | nickname 4 | ]] 5 | local nickname_config = { 6 | [[西门赌神]], 7 | [[东方不败]], 8 | [[南宫必赢]], 9 | [[天胡欧阳]], 10 | [[躺着唱征菔]], 11 | [[半世迷离ら]], 12 | [[说到痛处才拼命反驳]], 13 | [[止痛药也是我的错]], 14 | [[诸多相遇]], 15 | [[不盼共伴]], 16 | [[记忆碎影]], 17 | [[森屿初夏°]], 18 | [[一个人的情侣装╮]], 19 | [[当王只因你要权]], 20 | [[情深似海i]], 21 | [[每天都被自己帅醒]], 22 | [[爱网购的妹子]], 23 | [[哥的温柔百度搜不到]], 24 | [[姐的霸道百度搜不到]], 25 | [[浅蓝铯dē爱]], 26 | [[我胸小随我爸不行啊]], 27 | [[碰碰胡]], 28 | [[珴先脱,祢随意]], 29 | [[朕今晚翻你牌子]], 30 | [[借來的幸福,终究要还]], 31 | [[安之若素丶]], 32 | [[.·安小颜·.]], 33 | [[遇到の你]], 34 | [[何必自欺欺人呢つ]], 35 | [[此女子、禁止暧昧]], 36 | [[lovesickness°]], 37 | [[的虚伪、我早已看透]], 38 | [[╰你有算得了什么。]], 39 | [[你的爱跟我没半毛钱关系*]], 40 | [[閑誮鬦誮絡]], 41 | [[用沉默埋葬過去]], 42 | [[一个比蒙牛还猛的男人]], 43 | [[飛☆鳥々]], 44 | [[其实不想走,其实我想留]], 45 | [[╰╯痴人说梦_____]], 46 | [[浅唱轻吟⊕]], 47 | [[# 夏瑾夕丶]], 48 | [[努力变成他爱的的样子阿]], 49 | [[Temptation 矛盾]], 50 | [[我菂执着、因你而值得]], 51 | [[别用你吻过别人的嘴说爱我]], 52 | [[没有人会把你当全世界﹏]], 53 | [[≠.臭包子]], 54 | [[学校就是毁人不倦的地方]], 55 | [[三无先森灬]], 56 | [[世上最动人的话是你的名字]], 57 | [[弄得我多稀罕你似得]], 58 | [[Sugar Free]], 59 | [[日久贱人心]], 60 | [[对不起ゝ沵不是姐的菜]], 61 | [[吃货总比痴货好]], 62 | [[You Summer丶]], 63 | [[迷戀上祢的微笑い]], 64 | [[free body.]], 65 | [[越长大越孤单越长大越不安]], 66 | [[换掉心脏给我一颗铁石心肠]], 67 | [[倾心之恋]], 68 | [[GoodnIght淡年华]], 69 | [[可恨仓井已成空]], 70 | [[既然是戏,何必太认真]], 71 | [[★靜候誮開☆]], 72 | [[瑾年丶曦]], 73 | [[哭的太狼狽]], 74 | [[Ring My Bells]], 75 | [[男人有道便是王*]], 76 | [[@( ̄- ̄)@]], 77 | [[Please, stop]], 78 | [[为你→封鈊锁爱]], 79 | [[心已打烊]], 80 | [[你是我最明媚的阳光゛]], 81 | [[蓦然回首、那人已挂掉]], 82 | [[不想打扰尓的幸福]], 83 | [[情之所動、淚也傾城]], 84 | [[时光老去,我们也不散去]], 85 | [[嗯.分手么]], 86 | [[世界无童话]], 87 | [[不听不看不说不想 ※]], 88 | [[幸福ヽ已成空]], 89 | [[不美不萌不做作]], 90 | [[爱已归零]], 91 | [[一只特立独行的猪]], 92 | [[笨死的猪zZZ]], 93 | [[握在手中卻流失于指縫]], 94 | [[什么也打倒不了我嘴角的笑]], 95 | [[半身死灵Thenecromancer°]], 96 | [[shineミ苏雨萱]], 97 | [[丶碉暴的小肥羊]], 98 | [[人神共愤弑神]], 99 | [[伪善者]], 100 | [[想你的思绪在蔓延]], 101 | [[爹丶你撸不起]], 102 | [[不堪回首旳曾经]], 103 | [[丿Mua灬丨承诺艹]], 104 | [[℡农村尐爷们゛]], 105 | [[此男子空虚]], 106 | [[ぃ胸毛の飘飘^]], 107 | [[烟灰的寂寞]], 108 | [[唯一的专属]], 109 | [[與吻離別式]], 110 | [[丿皇族紫吟调]], 111 | [[胸罩哥]], 112 | } 113 | return nickname_config -------------------------------------------------------------------------------- /service/httpwatchdog.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | 3 | local CMD = {} 4 | local HTTP = {} 5 | local http 6 | 7 | function HTTP.open(fd, addr) 8 | print("watchdog conn:"..fd) 9 | end 10 | 11 | function HTTP.close(fd) 12 | print("socket close",fd) 13 | end 14 | 15 | local function tablify(tbl) 16 | local res = '' 17 | local k, v 18 | for k, v in pairs(tbl) do 19 | res = res .. '" 26 | end 27 | res = res .. "
' .. k .. "" 20 | if type(v) == "table" then 21 | res = res .. tablify(v) 22 | else 23 | res = res .. tostring(v) 24 | end 25 | res = res .. "
" 28 | return res 29 | end 30 | 31 | function HTTP.error(fd, msg) 32 | print("socket error",fd, msg) 33 | end 34 | 35 | function HTTP.get(rq, header) 36 | print("http recv:", rq.url) 37 | 38 | local res = table.concat { 39 | "", rq.url, "
",
40 | 		'

GET ', rq.url, "

"} 41 | if rq.path == "/status" then 42 | res = res .. "

Status

" .. tablify(server:status()) 43 | else 44 | res = res .. table.concat{ 45 | "

Header

", 46 | tablify(header), 47 | "

Request

", 48 | tablify(rq)} 49 | end 50 | res = res .. "
" 51 | 52 | return res 53 | end 54 | 55 | function HTTP.post(rq, header, data) 56 | print("http recv:", data) 57 | 58 | local res = table.concat{ 59 | "", rq.url, "
",
60 | 		'

POST ', rq.url, "

", 61 | "

Header

", 62 | tablify(header), 63 | "

Request

", 64 | tablify(rq), 65 | "data:
", data, "
", 66 | "
"} 67 | 68 | return res 69 | end 70 | 71 | 72 | function CMD.start(conf) 73 | skynet.call(http, "lua", "open" , conf) 74 | end 75 | 76 | skynet.start(function() 77 | skynet.dispatch("lua", function(session, source, cmd, subcmd, ...) 78 | if cmd == "http" then 79 | local f = HTTP[subcmd] 80 | local res = f(...) 81 | if res then 82 | skynet.ret(skynet.pack(res)) 83 | end 84 | else 85 | local f = assert(CMD[cmd]) 86 | skynet.ret(skynet.pack(f(subcmd, ...))) 87 | end 88 | end) 89 | 90 | http = skynet.newservice("http") 91 | end) 92 | -------------------------------------------------------------------------------- /lualib/timer.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : timer.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | local skynet = require "skynet" 9 | local class = require "class" 10 | 11 | local timer = class() 12 | 13 | function timer:_init(func, class_obj) 14 | self._taskid = 0 15 | self._expire_time = 0 16 | self._repeat_count = 0 17 | self._func = func 18 | self._class_obj = obj 19 | self._args = {} 20 | self._on_expire = function(taskid) 21 | if taskid ~= self._taskid then 22 | return 23 | end 24 | if not self._func then 25 | return 26 | end 27 | if self._repeat_count == 0 then 28 | self._expire_time = 0 29 | self._args = {} 30 | return 31 | end 32 | 33 | if self._repeat_count > 0 then 34 | self._repeat_count = self._repeat_count - 1 35 | end 36 | if self._class_obj then 37 | self._func(self._class_obj, table.unpack(self._args)) 38 | else 39 | self._func(table.unpack(self._args)) 40 | end 41 | 42 | if self._repeat_count == 0 then 43 | self._args = {} 44 | self._expire_time = 0 45 | else 46 | skynet.timeout(self._expire_time * 100, function() self._on_expire(taskid) end) 47 | end 48 | 49 | end 50 | end 51 | 52 | function timer:run_every(second, count, ...) 53 | if not self:is_canceled() then 54 | return nil 55 | end 56 | if not count or count < 0 then 57 | self._repeat_count = -1 58 | else 59 | self._repeat_count = count 60 | end 61 | self._taskid = self._taskid + 1 62 | self._expire_time = second 63 | self._args = table.pack(...) 64 | skynet.timeout(second * 100, function() self._on_expire(self._taskid) end) 65 | return self._taskid 66 | end 67 | 68 | function timer:run_after(second, ...) 69 | if not self:is_canceled() then 70 | return nil 71 | end 72 | self._taskid = self._taskid + 1 73 | self._expire_time = second 74 | self._repeat_count = 1 75 | self._args = table.pack(...) 76 | skynet.timeout(second * 100, function() self._on_expire(self._taskid) end) 77 | return self._taskid 78 | end 79 | 80 | function timer:cancel() 81 | if self._repeat_count == 0 then 82 | return 83 | end 84 | self._repeat_count = 0 85 | self._args = {} 86 | self._expire_time = 0 87 | end 88 | 89 | function timer:is_canceled() 90 | return self._repeat_count == 0 91 | end 92 | 93 | return timer -------------------------------------------------------------------------------- /logic/login/login_logic.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local db_helper = require "common.db_helper" 3 | local utils = require "utils" 4 | local context = require "context" 5 | local crypt = require "sky_crypt" 6 | 7 | local login_logic = {} 8 | 9 | function login_logic.init() 10 | 11 | end 12 | 13 | local function get_auth_info(req) 14 | local user_info = {user = req.playerid} 15 | return db_helper.call(DB_SERVICE.auth, "auth.get_auth_info", user_info) 16 | end 17 | 18 | local function decode_passwd(secret, password) 19 | return crypt.base64encode(crypt.desdecode(secret, password)) 20 | end 21 | 22 | function login_logic.signin_account(ctx, req) 23 | local auth_info = get_auth_info(req) 24 | if auth_info == nil then 25 | return AUTH_ERROR.auth_info_expire 26 | end 27 | local passwd = decode_passwd(auth_info.secret, req.password) 28 | local account_info = db_helper.call(DB_SERVICE.account, "auth.get_normal_account", nil, req.account) 29 | if not account_info then 30 | return AUTH_ERROR.account_not_exist 31 | end 32 | if account_info.password ~= passwd then 33 | return AUTH_ERROR.password_wrong 34 | end 35 | local player_info = db_helper.call(DB_SERVICE.account, "auth.get_player", account_info.player_id) 36 | if not player_info then 37 | return AUTH_ERROR.player_not_exist 38 | end 39 | local reply = {} 40 | reply.player = player_info 41 | return SYSTEM_ERROR.success, reply 42 | end 43 | 44 | function login_logic.weixin_account(ctx, req) 45 | local auth_info = get_auth_info(req) 46 | if auth_info == nil then 47 | return AUTH_ERROR.auth_info_expire 48 | end 49 | local player_info = db_helper.call(DB_SERVICE.account, "auth.get_player", req.playerid) 50 | if not player_info then 51 | return AUTH_ERROR.player_not_exist 52 | end 53 | local reply = {} 54 | reply.player = player_info 55 | return SYSTEM_ERROR.success, reply 56 | end 57 | 58 | function login_logic.vistor_account(ctx, req) 59 | local auth_info = get_auth_info(req) 60 | if auth_info == nil then 61 | return AUTH_ERROR.auth_info_expire 62 | end 63 | local player_info = db_helper.call(DB_SERVICE.account, "auth.get_player", req.playerid) 64 | if not player_info then 65 | return AUTH_ERROR.player_not_exist 66 | end 67 | local reply = {} 68 | reply.player = player_info 69 | return SYSTEM_ERROR.success, reply 70 | end 71 | 72 | return login_logic -------------------------------------------------------------------------------- /service/agent.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : agent.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | local skynet = require "skynet" 9 | local utils = require "utils" 10 | local service = require "service_base" 11 | local agent_ctrl = require "agent.agent_ctrl" 12 | local cluster_monitor = require "cluster_monitor" 13 | local context = require "context" 14 | local config_db = require "config_db" 15 | local command = service.command 16 | service.is_agent = true 17 | 18 | local current_conf 19 | local login_callbacks 20 | local logout_callbacks 21 | 22 | local modules = { 23 | agent = "agent.agent_ctrl", 24 | player = "player.player_ctrl", 25 | } 26 | 27 | local function register_login_and_logout() 28 | login_callbacks = { 29 | require("agent.agent_ctrl").on_login, 30 | require("player.player_ctrl").on_login, 31 | } 32 | logout_callbacks = { 33 | require("player.player_ctrl").on_logout, 34 | require("agent.agent_ctrl").on_logout, 35 | } 36 | end 37 | 38 | local function init_modules() 39 | setmetatable(service.modules, { 40 | __index = function(t, k) 41 | local mod = modules[k] 42 | if not mod then 43 | return nil 44 | end 45 | local v = require(mod) 46 | t[k] = v 47 | return v 48 | end 49 | }) 50 | end 51 | 52 | 53 | function command.login(ctx, player_info) 54 | ctx.player_id = player_info.player_id 55 | if current_conf.server_type == SERVER.HALL then 56 | service.player_id = player_info.player_id 57 | service.fd = ctx.fd 58 | end 59 | for _, callback in ipairs(login_callbacks) do 60 | callback(ctx, player_info) 61 | end 62 | end 63 | 64 | function command.logout(ctx) 65 | for _, callback in ipairs(logout_callbacks) do 66 | callback(ctx) 67 | end 68 | end 69 | 70 | function command.reconnect(ctx) 71 | return agent_ctrl.reconnect(ctx) 72 | end 73 | 74 | function command.update_configs(configs) 75 | config_db.update(configs) 76 | end 77 | 78 | function command.init(configs) 79 | -- local server_id = tonumber(skynet.getenv("cluster_server_id")) 80 | -- current_conf = cluster_monitor.get_current_node() 81 | 82 | config_db.init(configs) 83 | init_modules() 84 | register_login_and_logout() 85 | 86 | agent_ctrl.init(current_conf) 87 | end 88 | 89 | function service.on_start() 90 | current_conf = cluster_monitor.get_current_node() 91 | end 92 | 93 | service.start() -------------------------------------------------------------------------------- /logic/common/db_helper.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local context = require "context" 3 | local cluster_monitor = require "cluster_monitor" 4 | local db_config = require "config.db_config" 5 | 6 | local db_helper = {} 7 | 8 | local db_node = nil 9 | local svc_map = {} 10 | 11 | local function get_db_node() 12 | if not db_node then 13 | local node = cluster_monitor.get_cluster_node_by_server(SERVER.DB) 14 | db_node = node.nodename 15 | end 16 | return db_node 17 | end 18 | 19 | local function get_db_svc(dbname, id) 20 | if db_node and svc_map[dbname] then 21 | return db_node, svc_map[dbname] 22 | end 23 | 24 | local nodename = get_db_node() 25 | if not nodename then 26 | return nil 27 | end 28 | 29 | local conf = db_config[dbname] 30 | if not conf then 31 | return nil 32 | end 33 | 34 | if conf.get_svc == GET_SVC_TYPE.unique then 35 | svc_map[dbname] = conf.service_name 36 | return nodename, svc_map[dbname] 37 | elseif conf.get_svc == GET_SVC_TYPE.robin then 38 | local rpc_err, svc = context.rpc_call(nodename, SERVICE.MASTER_DB, "get_db_svc", dbname) 39 | if rpc_err ~= RPC_ERROR.success then 40 | return nil 41 | end 42 | svc_map[dbname] = svc 43 | return nodename, svc_map[dbname] 44 | elseif conf.get_svc == GET_SVC_TYPE.player_id then 45 | if not id then return nil end 46 | local index = id % conf.svc_count 47 | if index == 0 then index = conf.svc_count end 48 | svc_map[dbname] = conf.service_name..index 49 | return nodename, svc_map[dbname] 50 | end 51 | return nil 52 | end 53 | 54 | function db_helper.call(dbname, method, id, ...) 55 | local nodename, svc = get_db_svc(dbname, id) 56 | if not nodename or not svc then 57 | return nil 58 | end 59 | local rpc_err, ret = context.rpc_call(nodename, svc, method, id, ...) 60 | if rpc_err ~= RPC_ERROR.success then 61 | error(string.format("db_helper.call err rpc_err[%d] dbname[%s] method[%s] id[%s]", rpc_err, dbname, method, tostring(id))) 62 | end 63 | return ret 64 | end 65 | 66 | function db_helper.send(dbname, method, id, ...) 67 | local nodename, svc = get_db_svc(dbname, id) 68 | if not nodename or not svc then 69 | return nil 70 | end 71 | local rpc_err = context.rpc_send(nodename, svc, method, id, ...) 72 | if rpc_err ~= RPC_ERROR.success then 73 | error(string.format("db_helper.send err dbname[%s] method[%s] id[%s]", dbname, method, tostring(id))) 74 | end 75 | end 76 | 77 | return db_helper 78 | -------------------------------------------------------------------------------- /logic/gate/socket_msg.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local gate_mgr = require "gate.gate_mgr" 3 | local client_msg = require "gate.client_msg" 4 | local gate_msg = require "gate.gate_msg" 5 | local sproto_helper = require "sproto_helper" 6 | local utils = require "utils" 7 | local json = require "json" 8 | local uuid = require "uuid" 9 | local socket_msg = {} 10 | 11 | function socket_msg.open(fd, ip) 12 | uuid.seed() 13 | local c = {} 14 | c.fd = fd 15 | c.ip = string.match(ip, "([%d.]+):(%d+)") 16 | c.agentnode = nil 17 | c.agentaddr = nil 18 | c.game_id = nil 19 | c.session = uuid() 20 | gate_mgr.add_connection(fd, c) 21 | end 22 | 23 | --收到socket关闭的通知 24 | function socket_msg.close(fd) 25 | skynet.error("cy=====================socket_msg.close") 26 | gate_msg.close(fd) 27 | end 28 | 29 | function socket_msg.error(fd, msg) 30 | skynet.error("cy=====================socket_msg.error") 31 | --待补充 double check 32 | gate_msg.close(fd) 33 | end 34 | 35 | function socket_msg.warning(fd, size) 36 | skynet.error(string.format("%d bytes havn't send out in fd[%d]", fd, size)) 37 | end 38 | 39 | local function dispatch_data(c, msg) 40 | local header, content 41 | local ok, err = xpcall(function() 42 | header, content = sproto_helper.unpack_header(msg, #msg) 43 | if not header then 44 | gate_mgr.close_connection(c.fd) 45 | return 46 | end 47 | client_msg.dispatch(c, header, msg) 48 | end, debug.traceback) 49 | if not ok then 50 | gate_mgr.close_connection(c.fd) 51 | error("unpack_header:"..err) 52 | return 53 | end 54 | end 55 | 56 | function socket_msg.data(fd, msg) 57 | local c = gate_mgr.get_connection(fd) 58 | if not c then 59 | return 60 | end 61 | 62 | if gate_mgr.is_websocket() then 63 | if msg == "@heart" then 64 | return 65 | end 66 | end 67 | 68 | --todo with websocket 69 | if skynet.getenv("websocket_test") and gate_mgr.is_websocket() then 70 | print("recv socket_msg msg=", msg) 71 | local packet = json.decode(msg) 72 | local j_header = packet.header 73 | local j_data = packet.data 74 | j_header.response = 0 75 | -- print("j_header=", table.tostring(j_header), "j_data=", j_data) 76 | local buffer = sproto_helper.pack(j_header, j_data) 77 | dispatch_data(c, buffer) 78 | return 79 | end 80 | 81 | --todo with socket 82 | dispatch_data(c, msg) 83 | --skynet.send(gate_mgr.get_gate(), "lua", "send_buffer", fd, msg) 84 | end 85 | 86 | 87 | return socket_msg -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ----------------------------------- 2 | # 模块分类 3 | ## 实现模块 4 | 基础服务:集群模块 消息处理模块 数据库模块 5 | gateserver: 网关模块 http模块 websocket模块 6 | authserver: 网关模块 断线重连模块 排队认证模块 7 | loginserver: 登录模块 8 | hallserver: game模块 9 | locatorserver: locator模块 10 | gameserver: game模块 11 | chatserver: chat模块 12 | dbserver: 数据库模块 13 | 14 | ## 待实现模块 15 | AOI模块 16 | 日志染色 17 | 热更新 18 | 宕机重启 redis存放在线用户的rpc请求 19 | 限流降级 20 | redis mysql事务 21 | 22 | ## 垂直分布 23 | auth 认证、注册、排队 24 | login 登录 25 | hall 平台 (认证、第三方、支付、邮件) 26 | area (mmo) 27 | room (副本/moba/rpg) 28 | desk (team) 同服组队,或者跨服组队(desk服务独立于room实现) 29 | locator 辅助启动、负载均衡gameserver 30 | chat 聊天 31 | 32 | ## 注意: 33 | redis无法存放int数据 34 | pkill skynet;rm -f console.log;touch console.log;./start.sh 1;cat console.log;tail -f console.log 35 | 36 | ----------------------------------- 37 | # 使用 38 | ## 源码地址(了解) 39 | https://github.com/zhangshiqian1214/skynet-server 40 | ## 修改(了解) 41 | 1 顶层makefile添加lsocket库支持 42 | 2 skynet makefile添加 skynet_crypt httppack add支持 43 | lualib-src/lua-crypt.c lualib-src/lua-httppack.c lualib-src/add.c 44 | 45 | ## 安装(重要) 46 | 1 步骤就一步: cd serverOne; make all; 47 | 2 步骤解释:serverOne依赖skynet、redis、lsocket,makefile负责编译它们,同时需要安装mysql数据库。 48 | 3 编译遇到问题(提供参考,没有遇到则忽略) 49 | skynet: yum install dos2unix; yum install libcurl-dev libcurl-devel 50 | make socket 安装客户端lsocket 51 | 问题: 安装 Redis 执行 make #error "Newer version of jemalloc required" 52 | => make MALLOC=libc 53 | openssl/crypto.h: No such file or directory 54 | => yum install openssl-devel 55 | “uuid/uuid.h: No such file or directory 56 | => yum install libuuid-devel 57 | 58 | 测试cluster:1 ./run_test1.sh ./run_test.sh 59 | 60 | ## 运行服务端(重要) 61 | 执行步骤:./run_redis.sh; redis启动一次即可 ./restart.sh; 重启服务端命令 62 | 服务端操作相关: 63 | 1 只启动gate login hall db服务,redis没有其他服务 64 | 单服务启动 65 | ./run_redis.sh 66 | ./run_gate.sh 67 | ./run_login.sh 68 | ./run_hall.sh 69 | ./run_db.sh 70 | 2 单服务停止 71 | pkill -u `whoami` -xf "./skynet/skynet ./config/config_xpnn __default__" 72 | 3 停止game服务 73 | pkill -u `whoami` -xf "./skynet/skynet ./config/config_xpnn21 __default__" 74 | 75 | ## 运行客户端(重要) 76 | 执行步骤: ./client.sh uid uid是用户id可以是任何正整数,表示用户唯一标识,例如:./client.sh 1 77 | 客户端相关: 78 | 1 控制台输入0 正常退出客户端 79 | 2 控制台ctrl+c 断线(socket直接断开) 80 | -------------------------------------------------------------------------------- /logic/hall/hall_ctrl.lua: -------------------------------------------------------------------------------- 1 | require "skynet.manager" 2 | local skynet = require "skynet" 3 | local context = require "context" 4 | local configs = require "config.hall_config" 5 | local hall_ctrl = {} 6 | 7 | local logic_svc_pool = {} 8 | local logic_svc_index = 1 9 | 10 | local agent_pool = {} 11 | 12 | local function init_logic_pool() 13 | local logic_count = skynet.getenv("hall_logic_count") 14 | for i=1, logic_count do 15 | local svc = skynet.newservice("hall_logic_svc") 16 | table.insert(logic_svc_pool, svc) 17 | end 18 | end 19 | 20 | local function init_agent_pool() 21 | local agent_count = skynet.getenv("agent_count") 22 | for i=1, agent_count do 23 | local agent = skynet.newservice("agent") 24 | skynet.name(".agent"..i, agent) 25 | context.call(agent, "init", configs) 26 | table.insert(agent_pool, agent) 27 | end 28 | end 29 | 30 | local function get_logic_svc() 31 | local svc = logic_svc_pool[logic_svc_index] 32 | logic_svc_index = logic_svc_index + 1 33 | if logic_svc_index > #logic_svc_pool then 34 | logic_svc_index = 1 35 | end 36 | return svc 37 | end 38 | 39 | --没用到 40 | local function get_agent(player_id) 41 | local agent_count = skynet.getenv("agent_count") 42 | local index = player_id % agent_count 43 | if index == 0 then index = agent_count end 44 | return agent_pool[index] 45 | end 46 | 47 | function hall_ctrl.init() 48 | init_logic_pool() 49 | init_agent_pool() 50 | end 51 | 52 | function hall_ctrl.cast_login(ctx, req) 53 | assert(ctx.player_id == nil, "player repeat login") 54 | -- print("cast_login ctx=", table.tostring(ctx), "req=", table.tostring(req)) 55 | local svc = get_logic_svc() 56 | context.call(svc, "cast_login", ctx, req) 57 | end 58 | 59 | function hall_ctrl.cast_logout(ctx, req) 60 | local svc = get_logic_svc() 61 | context.call(svc, "cast_logout", ctx, req) 62 | end 63 | 64 | function hall_ctrl.get_player_online_state(ctx, req) 65 | local svc = get_logic_svc() 66 | local reply = context.call(svc, "get_player_online_state", ctx, req) 67 | return SYSTEM_ERROR.success, reply 68 | end 69 | 70 | function hall_ctrl.get_room_inst_list(ctx, req) 71 | -- print("hall_ctrl.get_room_inst_list ctx=", table.tostring(ctx), "req=", req) 72 | local svc = get_logic_svc() 73 | local reply = context.call(svc, "get_room_inst_list", ctx, req) 74 | -- print("hall_ctrl.get_room_inst_list reply=", table.tostring(reply)) 75 | return SYSTEM_ERROR.success, reply 76 | end 77 | 78 | return hall_ctrl -------------------------------------------------------------------------------- /logic/fd/fd_db.lua: -------------------------------------------------------------------------------- 1 | local db_mgr = require "common.db_mgr" 2 | local skynet = require "skynet" 3 | local fd_db = {} 4 | 5 | local HallFdKey = "fd_hall" 6 | local GameFdKey = "fd_game" 7 | local ServerFdKey = "fd_server" 8 | local RoomTypeFdKey = "fd_roomtype" 9 | local RoomAddrFdKey = "fd_roomaddr" 10 | 11 | local function get_hallfd_key() 12 | return HallFdKey 13 | end 14 | local function get_gamefd_key(game_id) 15 | return GameFdKey..":"..game_id 16 | end 17 | local function get_serverfd_key(server_id) 18 | return ServerFdKey..":"..server_id 19 | end 20 | local function get_roomtypefd_key(room_id) 21 | return RoomTypeFdKey..":"..room_id 22 | end 23 | local function get_roomaddrfd_key(server_id, room_addr) 24 | return RoomAddrFdKey..":"..server_id.."@"..room_addr 25 | end 26 | 27 | function fd_db.set_fd(type, game_id, server_id, room_id, room_addr, fd) 28 | local redisdb = db_mgr.get_redis_db() 29 | if type == FD_TYPE.hall then 30 | redisdb:lpush(get_hallfd_key(), fd) 31 | elseif type == FD_TYPE.area then 32 | redisdb:lpush(get_gamefd_key(game_id), fd) 33 | redisdb:lpush(get_serverfd_key(server_id), fd) 34 | elseif type == FD_TYPE.room then 35 | redisdb:lpush(get_roomtypefd_key(room_id), fd) 36 | redisdb:lpush(get_roomaddrfd_key(server_id, room_addr), fd) 37 | end 38 | end 39 | 40 | function fd_db.unset_fd(type, game_id, server_id, room_id, room_addr, fd) 41 | local redisdb = db_mgr.get_redis_db() 42 | if type == FD_TYPE.hall then 43 | redisdb:lrem(get_hallfd_key(), 0, fd) 44 | elseif type == FD_TYPE.area then 45 | redisdb:lrem(get_gamefd_key(game_id), 0, fd) 46 | redisdb:lrem(get_serverfd_key(server_id), 0, fd) 47 | elseif type == FD_TYPE.room then 48 | redisdb:lrem(get_roomtypefd_key(room_id), 0, fd) 49 | redisdb:lrem(get_roomaddrfd_key(server_id, room_addr), 0, fd) 50 | end 51 | end 52 | 53 | function fd_db.get_fds(type, game_id, server_id, room_id, room_addr) 54 | local redisdb = db_mgr.get_redis_db() 55 | if type == FD_TYPE.hall then 56 | return redisdb:lrange(get_hallfd_key(), 0, -1) 57 | elseif type == FD_TYPE.game then 58 | return redisdb:lrange(get_gamefd_key(game_id), 0, -1) 59 | elseif type == FD_TYPE.server then 60 | return redisdb:lrange(get_serverfd_key(server_id), 0, -1) 61 | elseif type == FD_TYPE.roomtype then 62 | return redisdb:lrange(get_roomtypefd_key(room_id), 0, -1) 63 | elseif type == FD_TYPE.roomaddr then 64 | return redisdb:lrange(get_roomaddrfd_key(server_id, room_addr), 0, -1) 65 | end 66 | end 67 | 68 | return fd_db -------------------------------------------------------------------------------- /service/httpServerAgent.lua: -------------------------------------------------------------------------------- 1 | -- examples/simpleweb.lua 2 | local skynet = require "skynet" 3 | local socket = require "skynet.socket" 4 | local httpd = require "http.httpd" 5 | local sockethelper = require "http.sockethelper" 6 | local urllib = require "http.url" 7 | local string = string 8 | 9 | local function response(id, ...) 10 | local ok, err = httpd.write_response(sockethelper.writefunc(id), ...) 11 | if not ok then 12 | -- if err == sockethelper.socket_error , that means socket closed. 13 | skynet.error(string.format("fd = %d, %s", id, err)) 14 | end 15 | end 16 | 17 | skynet.start(function() 18 | skynet.dispatch("lua", function (_,_,id) 19 | socket.start(id) -- 开始接收一个 socket 20 | -- limit request body size to 8192 (you can pass nil to unlimit) 21 | -- 一般的业务不需要处理大量上行数据,为了防止攻击,做了一个 8K 限制。这个限制可以去掉。 22 | local code, url, method, header, body = httpd.read_request(sockethelper.readfunc(id), 8192) 23 | if code then 24 | if code ~= 200 then -- 如果协议解析有问题,就回应一个错误码 code 。 25 | response(id, code) 26 | else 27 | -- 这是一个示范的回应过程,你可以根据你的实际需要,解析 url, method 和 header 做出回应。 28 | if header.host then 29 | skynet.error("header host", header.host) 30 | end 31 | 32 | local path, query = urllib.parse(url) 33 | skynet.error(string.format("path: %s", path)) 34 | local color,text = "red", "hello" 35 | if query then 36 | local q = urllib.parse_query(query) --获取请求的参数 37 | for k, v in pairs(q) do 38 | skynet.error(string.format("query: %s= %s", k,v)) 39 | if(k == "color") then 40 | color = v 41 | elseif (k == "text") then 42 | text = v 43 | end 44 | end 45 | end 46 | local resphtml = ""..text.."\n" --返回一张网页 47 | response(id, code, resphtml) --返回状态码200,并且跟上内容 48 | end 49 | else 50 | -- 如果抛出的异常是 sockethelper.socket_error 表示是客户端网络断开了。 51 | if url == sockethelper.socket_error then 52 | skynet.error("socket closed") 53 | else 54 | skynet.error(url) 55 | end 56 | end 57 | socket.close(id) 58 | end) 59 | end) 60 | -------------------------------------------------------------------------------- /logic/player/player_db.lua: -------------------------------------------------------------------------------- 1 | local db_mgr = require "common.db_mgr" 2 | 3 | local player_db = {} 4 | 5 | local PlayerInfoKey = "player_info" 6 | local function get_player_info_key(id) 7 | return PlayerInfoKey..":"..id 8 | end 9 | 10 | function player_db.get_player_info(id) 11 | local mysqldb = db_mgr.get_mysql_db() 12 | local sql = string.format("select * from tb_player where player_id = %d;", id) 13 | local result = mysqldb:query(sql) 14 | if result.err then 15 | error(result.err) 16 | end 17 | return result[1] 18 | end 19 | 20 | function player_db.cache_player_info(id) 21 | local player_info = player_db.get_player_info(id) 22 | if not player_info then 23 | return nil 24 | end 25 | player_db.set_player_info_cache(id, player_info) 26 | return player_info 27 | end 28 | 29 | --载入到redis 30 | function player_db.get_player_info_cache(id) 31 | local redisdb = db_mgr.get_redis_db() 32 | local player_info = array_totable(redisdb:hgetall(get_player_info_key(id))) 33 | player_info.player_id = tonumber(player_info.player_id) 34 | player_info.head_id = tonumber(player_info.head_id) 35 | player_info.sex = tonumber(player_info.sex) 36 | player_info.gold = tonumber(player_info.gold) 37 | player_info.safe_gold = tonumber(player_info.safe_gold) 38 | player_info.last_mod_time = tonumber(player_info.last_mod_time) 39 | return player_info 40 | end 41 | 42 | --写入到redis 43 | function player_db.set_player_info_cache(id, player_info) 44 | local redisdb = db_mgr.get_redis_db() 45 | local data = assert(table.toarray(player_info), "online is not table") 46 | redisdb:hmset(get_player_info_key(id), table.unpack(data)) 47 | return 48 | end 49 | 50 | --写入到mysql 51 | function player_db.uncache_player_info(id) 52 | local player_info = player_db.get_player_info_cache(id) 53 | local mysqldb = db_mgr.get_mysql_db() 54 | local sql = string.format("update tb_player set head_id=%s, head_url='%s', nickname='%s', sex=%s, gold=%s, safe_gold=%s, last_mod_time=%s where player_id = %s;", 55 | player_info.head_id, player_info.head_url, player_info.nickname, player_info.sex, player_info.gold, player_info.safe_gold, player_info.last_mod_time) 56 | local result = mysqldb:query(sql) 57 | if result.err then 58 | error(result.err) 59 | end 60 | local redisdb = db_mgr.get_redis_db() 61 | redisdb:del(get_player_info_key(id)) 62 | return 63 | end 64 | 65 | --更新玩家数据 66 | function player_db.update_player_info_cache(id, key, value) 67 | local redisdb = db_mgr.get_redis_db() 68 | redisdb:hset(get_player_info_key(id), key, value) 69 | return 70 | end 71 | 72 | return player_db -------------------------------------------------------------------------------- /logic/heartbeat/heartbeat_ctrl.lua: -------------------------------------------------------------------------------- 1 | require "skynet.manager" 2 | local skynet = require "skynet" 3 | local db_helper = require "common.db_helper" 4 | local context = require "context" 5 | local utils = require "utils" 6 | local heartbeat_ctrl = {} 7 | 8 | local player_heartbeat_map = {} -- player_id : {update_time, state} 9 | 10 | local function do_kick_work(player_id) 11 | local online = db_helper.call(DB_SERVICE.hall, "hall.get_player_online", player_id) 12 | if tonumber(online.offline) == OFFLINE_STATE.on then 13 | context.call(online.watchdog, "player_leave", tonumber(online.fd)) 14 | end 15 | end 16 | 17 | local function do_offline_work(player_id) 18 | local online = db_helper.call(DB_SERVICE.hall, "hall.get_player_online", player_id) 19 | if tonumber(online.offline) == OFFLINE_STATE.off then 20 | db_helper.call(DB_SERVICE.hall, "hall.update_player_online", player_id, "offline", OFFLINE_STATE.on) 21 | context.call(online.watchdog, "close", tonumber(online.fd)) 22 | end 23 | end 24 | 25 | local function cal_offline_player() 26 | local curtime = skynet.now() 27 | for player_id, v in pairs(player_heartbeat_map) do 28 | local dif_time = (curtime - v.update_time) / 100 29 | if dif_time < HEART_BEAT_TIME.intval * HEART_BEAT_TIME.intcnt then 30 | v.state = HEART_BEAT_STATE.normal 31 | elseif dif_time >= HEART_BEAT_TIME.kicktime then 32 | if v.state ~= HEART_BEAT_STATE.kick then 33 | v.state = HEART_BEAT_STATE.kick 34 | do_kick_work(player_id) 35 | end 36 | elseif dif_time >= HEART_BEAT_TIME.intval * HEART_BEAT_TIME.intcnt then 37 | if v.state ~= HEART_BEAT_STATE.offline then 38 | v.state = HEART_BEAT_STATE.offline 39 | do_offline_work(player_id) 40 | end 41 | end 42 | --skynet.error("cy============================heartbeat state=", dif_time, player_id, v.state) 43 | end 44 | end 45 | 46 | function heartbeat_ctrl.init() 47 | skynet.fork(function() 48 | while true do 49 | skynet.sleep(300) 50 | cal_offline_player() 51 | end 52 | end) 53 | end 54 | 55 | function heartbeat_ctrl.reset_updatetime(player_id) 56 | if player_id then 57 | player_heartbeat_map[player_id] = player_heartbeat_map[player_id] or {} 58 | player_heartbeat_map[player_id].update_time = skynet.now() 59 | end 60 | end 61 | 62 | function heartbeat_ctrl.del_playerId(player_id) 63 | if player_id then 64 | player_heartbeat_map[player_id] = nil 65 | end 66 | end 67 | 68 | return heartbeat_ctrl -------------------------------------------------------------------------------- /logic/role/role_db.lua: -------------------------------------------------------------------------------- 1 | local db_mgr = require "common.db_mgr" 2 | local role_db = {} 3 | 4 | local IncrIdKey = "incr_role_id" 5 | local RoleInfoKey = "role_info" 6 | 7 | local function get_role_info_key(id) 8 | return RoleInfoKey..":"..id 9 | end 10 | 11 | function role_db.incr_role_id(id) 12 | local redisdb = db_mgr.get_redis_db() 13 | local role_id = tonumber(redisdb:incr(IncrIdKey)) 14 | return role_id 15 | end 16 | 17 | function role_db.get_role(player_id, game_id) 18 | local mysqldb = db_mgr.get_mysql_db() 19 | local result = mysqldb:query(string.format("select * from tb_role where player_id='%d' and game_id='%d'", player_id, game_id)) 20 | if result.err then 21 | error(result.err) 22 | end 23 | return result[1] 24 | end 25 | 26 | function role_db.get_role_by_role_id(id) 27 | local mysqldb = db_mgr.get_mysql_db() 28 | local result = mysqldb:query(string.format("select * from tb_role where role_id='%d'", id)) 29 | if result.err then 30 | error(result.err) 31 | end 32 | return result[1] 33 | end 34 | 35 | function role_db.create_role(id, role_info) 36 | local mysqldb = db_mgr.get_mysql_db() 37 | local sql = string.format("insert into tb_role(role_id, player_id, game_id, head_id, head_url, nickname, sex, gold, create_time) values(%d, '%d', '%d', %d, '%s', '%s', %d, %d,'%s');", 38 | role_info.role_id,role_info.player_id,role_info.game_id,role_info.head_id,role_info.head_url,role_info.nickname,role_info.sex,role_info.gold,role_info.create_time) 39 | local result = mysqldb:query(sql) 40 | if result.err then 41 | error(result.err) 42 | end 43 | return result[1] 44 | end 45 | 46 | --载入到redis 47 | function role_db.cache_role_info(player_id, game_id) 48 | local role_info = role_db.get_role(player_id, game_id) 49 | if not role_info then 50 | return nil 51 | end 52 | role_db.set_role_info_cache(tonumber(role_info.role_id), role_info) 53 | return role_info 54 | end 55 | 56 | function role_db.set_role_info_cache(id, role_info) 57 | local redisdb = db_mgr.get_redis_db() 58 | local data = assert(table.toarray(role_info), "online is not table") 59 | redisdb:hmset(get_role_info_key(id), table.unpack(data)) 60 | return 61 | end 62 | 63 | function role_db.get_role_info_cache(id) 64 | local redisdb = db_mgr.get_redis_db() 65 | local role_info = array_totable(redisdb:hgetall(get_role_info_key(id))) 66 | role_info.role_id = tonumber(role_info.role_id) 67 | role_info.player_id = tonumber(role_info.player_id) 68 | role_info.game_id = tonumber(role_info.game_id) 69 | role_info.head_id = tonumber(role_info.head_id) 70 | role_info.sex = tonumber(role_info.sex) 71 | role_info.gold = tonumber(role_info.gold) 72 | return role_info 73 | end 74 | 75 | 76 | return role_db -------------------------------------------------------------------------------- /lualib/requester.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : requester.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | local skynet = require "skynet" 9 | local utils = require "utils" 10 | local cluster = require "skynet.cluster" 11 | local cluster_monitor = require "cluster_monitor" 12 | local logger = require "logger" 13 | local sproto_helper = require "sproto_helper" 14 | local requester = {} 15 | 16 | function requester.call(service, cmd, ...) 17 | return skynet.call(service, "lua", cmd, ...) 18 | end 19 | 20 | function requester.send(service, cmd, ...) 21 | skynet.send(service, "lua", cmd, ...) 22 | end 23 | 24 | function requester.rpc_call(node, service, cmd, ...) 25 | if not node then 26 | return RPC_ERROR.node_nil 27 | end 28 | 29 | if not service then 30 | return RPC_ERROR.service_nil 31 | end 32 | 33 | local nodeconf = cluster_monitor.get_cluster_node(node) 34 | if not nodeconf then 35 | return RPC_ERROR.node_offline 36 | end 37 | 38 | local rets 39 | local args = {...} 40 | local ok, msg = xpcall(function() 41 | rets = table.pack(cluster.call(node, service, cmd, table.unpack(args))) 42 | end, debug.traceback) 43 | if not ok then 44 | logger.fatalf("rpc_call fatal, node[%s] err:%s", tostring(node), msg) 45 | --assert(false, string.format("rpc_call fatal, node[%s] err:%s", tostring(node), msg)) 46 | return RPC_ERROR.service_stoped 47 | end 48 | 49 | if rets then 50 | rets = table.unpack(rets) 51 | end 52 | return RPC_ERROR.success, rets 53 | end 54 | 55 | function requester.rpc_send(node, service, cmd, ...) 56 | if not node then 57 | return RPC_ERROR.node_nil 58 | end 59 | 60 | if not service then 61 | return RPC_ERROR.service_nil 62 | end 63 | 64 | local nodeconf = cluster_monitor.get_cluster_node(node) 65 | if not nodeconf then 66 | return RPC_ERROR.node_offline 67 | end 68 | 69 | local args = {...} 70 | local ok, msg = xpcall(function() 71 | cluster.send(node, service, cmd, table.unpack(args)) 72 | end, debug.traceback) 73 | if not ok then 74 | return RPC_ERROR.service_stoped 75 | end 76 | return RPC_ERROR.success 77 | end 78 | 79 | function requester.send_client_msg(ctx, proto, header, data) 80 | if not ctx or not proto then 81 | return 82 | end 83 | header = header or {} 84 | if table.empty(header) then 85 | -- 添加 seat push无法解析到数据 86 | header.response = 0 87 | end 88 | header.protoid = proto.id 89 | local buffer = sproto_helper.pack(header, data) 90 | 91 | if not buffer then 92 | print("send_client_msg proto=", table.tostring(proto), "data=", table.tostring(data)) 93 | return 94 | end 95 | requester.rpc_call(ctx.gate, ctx.watchdog, "send_client_msg", ctx.fd, buffer) 96 | end 97 | 98 | 99 | return requester -------------------------------------------------------------------------------- /logic/player/player_ctrl.lua: -------------------------------------------------------------------------------- 1 | local db_helper = require "common.db_helper" 2 | local agent_ctrl = require "agent.agent_ctrl" 3 | local skynet = require "skynet" 4 | 5 | local player_ctrl = {} 6 | 7 | local player_info 8 | 9 | 10 | --TODO for service 11 | function player_ctrl.update_player_info(update_info, cache_type) 12 | for k, v in pairs(update_info) do 13 | player_info[k] = v 14 | end 15 | if cache_type == CACHE_TYPE.redis then 16 | 17 | elseif cache_type == CACHE_TYPE.mysql then 18 | 19 | end 20 | end 21 | 22 | --TODO for service 23 | function player_ctrl.incr_player_info(incr_info, cache_type) 24 | for k, v in pairs(incr_info) do 25 | player_info[k] = player_info[k] + v 26 | end 27 | if cache_type == CACHE_TYPE.redis then 28 | 29 | elseif cache_type == CACHE_TYPE.mysql then 30 | 31 | end 32 | end 33 | 34 | --TODO for service 35 | function player_ctrl.get_player_info() 36 | if player_info then 37 | return player_info 38 | end 39 | player_info = db_helper.call(DB_SERVICE.agent, "player.get_player_info_cache", ctx.player_id) 40 | return player_info 41 | end 42 | 43 | --TODO for service 44 | function player_ctrl.handle_player_payout(desk_svc, winlost_info) 45 | 46 | --context.call(desk_svc, "player_payout_end", ,player_info) 47 | end 48 | 49 | 50 | --TODO for impl 51 | function player_ctrl.qry_player_info(ctx) 52 | if not agent_ctrl.is_hall() then 53 | return SYSTEM_ERROR.success,player_info 54 | else 55 | local pl_info = db_helper.call(DB_SERVICE.agent, "player.get_player_info_cache", ctx.player_id) 56 | return SYSTEM_ERROR.success, pl_info 57 | end 58 | end 59 | 60 | function player_ctrl.modify_head_info(ctx, req) 61 | 62 | end 63 | 64 | function player_ctrl.modify_nickname_info(ctx, req) 65 | 66 | end 67 | 68 | function player_ctrl.get_alipay_info(ctx, req) 69 | 70 | end 71 | 72 | function player_ctrl.get_bank_card_info(ctx, req) 73 | 74 | end 75 | 76 | function player_ctrl.get_weixinpay_info(ctx, req) 77 | 78 | end 79 | 80 | function player_ctrl.bind_alipay_info(ctx, req) 81 | 82 | end 83 | 84 | function player_ctrl.bind_bank_card_info(ctx, req) 85 | 86 | end 87 | 88 | function player_ctrl.bind_weixinpay_info(ctx, req) 89 | 90 | end 91 | 92 | function player_ctrl.on_login(ctx, pl_info) 93 | if not agent_ctrl.is_hall() then 94 | player_info = db_helper.call(DB_SERVICE.agent, "player.get_player_info_cache", pl_info.player_id) 95 | else 96 | db_helper.call(DB_SERVICE.agent, "player.cache_player_info", pl_info.player_id) 97 | end 98 | end 99 | 100 | function player_ctrl.on_logout(ctx) 101 | if not agent_ctrl.is_hall() then 102 | db_helper.call(DB_SERVICE.agent, "player.set_player_info_cache", ctx.player_id, player_info) 103 | end 104 | end 105 | 106 | return player_ctrl -------------------------------------------------------------------------------- /logic/config/cluster_config.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | 服务器id, 节点名, 节点端口, 内网地址, 外网地址, 是否使用内网, 服务器类型, 服务器版本号, 是否在线 3 | serverid, nodename, nodeport, intranetip, extranetip, use_intranet, servertype, ver, is_online 4 | ]] 5 | local cluster_config = { 6 | [1] = { 7 | serverid = 1, 8 | nodename = "gate1", 9 | nodeport = 9001, 10 | intranetip = "127.0.0.1", 11 | extranetip = "127.0.0.1", 12 | use_intranet = 1, 13 | servertype = 1, 14 | ver = 0, 15 | is_online = 1, 16 | }, 17 | [2] = { 18 | serverid = 2, 19 | nodename = "gate2", 20 | nodeport = 9002, 21 | intranetip = "127.0.0.1", 22 | extranetip = "127.0.0.1", 23 | use_intranet = 1, 24 | servertype = 1, 25 | ver = 0, 26 | is_online = 1, 27 | }, 28 | [3] = { 29 | serverid = 3, 30 | nodename = "db1", 31 | nodeport = 9003, 32 | intranetip = "127.0.0.1", 33 | extranetip = "127.0.0.1", 34 | use_intranet = 1, 35 | servertype = 2, 36 | ver = 0, 37 | is_online = 1, 38 | }, 39 | [4] = { 40 | serverid = 4, 41 | nodename = "login", 42 | nodeport = 9004, 43 | intranetip = "127.0.0.1", 44 | extranetip = "127.0.0.1", 45 | use_intranet = 1, 46 | servertype = 3, 47 | ver = 0, 48 | is_online = 1, 49 | }, 50 | [5] = { 51 | serverid = 5, 52 | nodename = "hall", 53 | nodeport = 9005, 54 | intranetip = "127.0.0.1", 55 | extranetip = "127.0.0.1", 56 | use_intranet = 1, 57 | servertype = 4, 58 | ver = 0, 59 | is_online = 1, 60 | }, 61 | [6] = { 62 | serverid = 6, 63 | nodename = "locator", 64 | nodeport = 9006, 65 | intranetip = "127.0.0.1", 66 | extranetip = "127.0.0.1", 67 | use_intranet = 1, 68 | servertype = 6, 69 | ver = 0, 70 | is_online = 1, 71 | }, 72 | [7] = { 73 | serverid = 7, 74 | nodename = "chat", 75 | nodeport = 9007, 76 | intranetip = "127.0.0.1", 77 | extranetip = "127.0.0.1", 78 | use_intranet = 1, 79 | servertype = 7, 80 | ver = 0, 81 | is_online = 1, 82 | }, 83 | [8] = { 84 | serverid = 8, 85 | nodename = "auth", 86 | nodeport = 9008, 87 | intranetip = "127.0.0.1", 88 | extranetip = "127.0.0.1", 89 | use_intranet = 1, 90 | servertype = 8, 91 | ver = 0, 92 | is_online = 1, 93 | }, 94 | [21] = { 95 | serverid = 21, 96 | nodename = "xpnn1", 97 | nodeport = 9021, 98 | intranetip = "127.0.0.1", 99 | extranetip = "127.0.0.1", 100 | use_intranet = 1, 101 | servertype = 21, 102 | ver = 0, 103 | is_online = 1, 104 | }, 105 | [22] = { 106 | serverid = 22, 107 | nodename = "xpnn2", 108 | nodeport = 9022, 109 | intranetip = "127.0.0.1", 110 | extranetip = "127.0.0.1", 111 | use_intranet = 1, 112 | servertype = 21, 113 | ver = 0, 114 | is_online = 1, 115 | }, 116 | [23] = { 117 | serverid = 23, 118 | nodename = "xpnn3", 119 | nodeport = 9023, 120 | intranetip = "127.0.0.1", 121 | extranetip = "127.0.0.1", 122 | use_intranet = 1, 123 | servertype = 21, 124 | ver = 0, 125 | is_online = 1, 126 | }, 127 | } 128 | return cluster_config -------------------------------------------------------------------------------- /logic/game/cd_ctrl.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local context = require "context" 3 | local queue = require "skynet.queue" 4 | local cd_ctrl = {} 5 | 6 | local cs = queue() 7 | local incr_id = 0 8 | local cache_cds = {} -- incr_id : {incr_id, cd_type, end_time, args} 9 | local cd_callbacks = {} -- cd_type :func 10 | local cd_listeners = {} -- [cd_type][ctx.player_id] = ctx 11 | 12 | local function queue_func(func, ...) 13 | local args = {...} 14 | local ok, msg = xpcall(function() 15 | func(table.unpack(args)) 16 | end, debug.traceback) 17 | if not ok then 18 | error(msg) 19 | end 20 | end 21 | 22 | local function get_incr_id() 23 | incr_id = incr_id + 1 24 | return incr_id 25 | end 26 | 27 | local function handle_cd_result(cd_type, id, ...) 28 | local cd = cache_cds[id] 29 | if not cd then 30 | return 31 | end 32 | local callback = cd_callbacks[cd_type] 33 | if not callback then 34 | return 35 | end 36 | cs(queue_func, callback, cd_type, id, ...) 37 | 38 | cd_ctrl.del_cd(id) 39 | return 40 | end 41 | 42 | function cd_ctrl.register_callback(cd_type, func) 43 | cd_callbacks[cd_type] = func 44 | end 45 | 46 | function cd_ctrl.register_listener(cd_type, ctx) 47 | cd_listeners[cd_type] = cd_listeners[cd_type] or {} 48 | cd_listeners[cd_type][ctx.player_id] = ctx 49 | end 50 | 51 | function cd_ctrl.unregister_listener(cd_type, ctx) 52 | if cd_listeners[cd_type] and cd_listeners[cd_type][ctx.player_id] then 53 | cd_listeners[cd_type][ctx.player_id] = nil 54 | end 55 | end 56 | 57 | function cd_ctrl.add_cd(cd_type, seconds, ...) 58 | local id = get_incr_id() 59 | local args = {...} 60 | local cd = { 61 | id = id, 62 | cd_type = cd_type, 63 | end_time = os.time() + seconds, 64 | args = args, 65 | } 66 | cache_cds[id] = cd 67 | skynet.timeout(seconds * 100, function() 68 | handle_cd_result(cd_type, id, table.unpack(args)) 69 | end) 70 | if cd_listeners[cd_type] then 71 | local event = { 72 | id = id, 73 | cd_type = cd.cd_type, 74 | cd_time = seconds, 75 | } 76 | for player_id, ctx in pairs(cd_listeners[cd_type]) do 77 | context.send_client_event(ctx, M_DESK.add_cd_event, event) 78 | end 79 | end 80 | return id 81 | end 82 | 83 | function cd_ctrl.del_cd(id) 84 | local cd = cache_cds[id] 85 | if cd then 86 | cache_cds[id] = nil 87 | if cd_listeners[cd.cd_type] then 88 | local event = { 89 | id = id, 90 | cd_type = cd.cd_type, 91 | } 92 | for player_id, ctx in pairs(cd_listeners[cd.cd_type]) do 93 | context.send_client_event(ctx, M_DESK.del_cd_event, event) 94 | end 95 | end 96 | end 97 | end 98 | 99 | function cd_ctrl.on_login(ctx) 100 | for _, v in pairs(cache_cds) do 101 | if cd_listeners[v.cd_type] and cd_listeners[v.cd_type][ctx.player_id] then 102 | local event = { 103 | id = id, 104 | cd_type = v.cd_type, 105 | cd_time = v.end_time - os.time(), 106 | } 107 | context.send_client_event(ctx, M_DESK.add_cd_event, event) 108 | end 109 | end 110 | end 111 | 112 | function cd_ctrl.on_logout(ctx) 113 | 114 | end 115 | 116 | return cd_ctrl -------------------------------------------------------------------------------- /proto/sproto/sproto/xpnn.sproto: -------------------------------------------------------------------------------- 1 | #牛牛 2 | # 牌定义: 3 | # 方块(Diamond): 0x01 0x02 0x03 0x04 0x05 0x06 0x07 0x08 0x09 0x0a 0x0b 0x0c 0x0d 方块A-10 J Q K 4 | # 梅花(Club) : 0x11 0x12 0x13 0x14 0x15 0x16 0x17 0x18 0x19 0x1a 0x1b 0x1c 0x1d 梅花A-10 J Q K 5 | # 红桃(Heart) : 0x21 0x22 0x23 0x24 0x25 0x26 0x27 0x28 0x29 0x2a 0x2b 0x2c 0x2d 红桃A-10 J Q K 6 | # 黑桃(Spade) : 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x3a 0x3b 0x3c 0x3d 黑桃A-10 J Q K 7 | # 游戏状态: 8 | # ready_begin = 0, qiang_banker = 1, bet = 2, open_card = 3, game_end = 4 9 | 10 | .xpnn { 11 | 12 | #坐位状态: 0x00-SeatNull 0x01-SeatUnready 0x02-SeatReady 13 | # 0x04-SeatGameing 0x08-SeatOffline 0x10-SeatExit 14 | 15 | .SeatState{ 16 | seat 0 : integer 17 | state 1 : integer 18 | } 19 | 20 | .PlayerInfo{ 21 | seat 0 : integer 22 | player_id 1 : integer 23 | nickname 2 : string 24 | head_id 3 : integer 25 | head_url 4 : string 26 | sex 5 : integer 27 | gold 6 : integer 28 | } 29 | 30 | .SeatCards { 31 | seat 0 : integer 32 | cards 1 : *integer 33 | card_type 2 : integer 34 | } 35 | 36 | #结算信息 37 | .Winlost { 38 | seat 0 : integer 39 | winlost 1 : integer 40 | fee 2 : integer #抽水 41 | } 42 | 43 | .TableBase { 44 | deal_id 0 : integer 45 | game_state 1 : integer 46 | } 47 | 48 | .Table { 49 | table_base 0 : TableBase 50 | player_map 1 : *PlayerInfo(seat) 51 | seat_state_map 2 : *SeatState(seat) 52 | 53 | banker 3 : integer 54 | qiang_times_map 4 : *integer #抢庄倍数 55 | bet_times_map 5 : *integer #玩家投注倍数 56 | player_cards_map 6 : *SeatCards(seat) 57 | 58 | open_card_map 7 : *boolean 59 | 60 | winlost_map 8 : *Winlost(seat) 61 | } 62 | 63 | .QryDeskReq { 64 | 65 | } 66 | 67 | .QryDeskReply { 68 | table_info 0 : Table 69 | } 70 | 71 | #抢庄 72 | .QiangBankerReq { 73 | times 0 : integer #抢庄倍数 74 | } 75 | 76 | .QiangBankerReply{ 77 | 78 | } 79 | 80 | .BetReq{ 81 | times 0 : integer 82 | } 83 | 84 | .BetReply{ 85 | 86 | } 87 | 88 | .DealInfo{ 89 | table_info 0 : Table 90 | } 91 | 92 | 93 | #坐位事件 94 | .SeatStateEvent{ 95 | player_map 0 : PlayerInfo 96 | seat_state_map 1 : SeatState 97 | } 98 | 99 | 100 | #游戏开始事件 101 | .GameStartEvent{ 102 | cards 0 : *integer 103 | } 104 | 105 | 106 | .GameEndEvent{ 107 | over 0 : integer 108 | } 109 | 110 | .ExitDeskEvent{ 111 | 112 | } 113 | 114 | #抢庄事件 115 | .QiangBankerEvent{ 116 | seat 0 : integer 117 | times 1 : integer 118 | banker 2 : integer 119 | random_banker_seats 3 : *integer 120 | } 121 | 122 | #投注事件 123 | .BetEvent{ 124 | seat 0 : integer 125 | times 1 : integer 126 | } 127 | 128 | #发最后一张牌 129 | .DealCardEvent{ 130 | card 0 : integer #发的最后一张牌 131 | card_type 1 : integer #牌型 132 | group_cards 2 : *integer #组好的排,前3个为一组 133 | } 134 | 135 | #开牌事件 136 | .OpenCardEvent{ 137 | seat 0 : integer 138 | card_type 1 : integer #牛牛牌型 139 | } 140 | 141 | #牌局结束事件 142 | #.GameEndEvent{ 143 | # player_map 0 : PlayerInfo(seat) 144 | # seat_state_map 1 : SeatState(seat) 145 | # player_cards_map 2 : *SeatCards(seat) 146 | # winlost_map 3 : *Winlost(seat) 147 | #} 148 | } -------------------------------------------------------------------------------- /lualib/cluster_monitor.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : cluster_monitor.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | local skynet = require "skynet" 9 | local share_memory = require "share_memory" 10 | local addr 11 | local cluster_monitor = {} 12 | local subscribe_nodes = {} 13 | local all_subscribe = false 14 | local all_subscribe_cb = nil 15 | 16 | local function init() 17 | addr = skynet.uniqueservice("cluster_monitord") 18 | end 19 | 20 | function cluster_monitor.get_current_nodename() 21 | return share_memory["current_nodename"] 22 | end 23 | 24 | function cluster_monitor.get_current_node() 25 | local nodename = share_memory["current_nodename"] 26 | if not nodename then 27 | return nil 28 | end 29 | local cluster_nodes = share_memory["cluster_nodes"] 30 | if not cluster_nodes then 31 | return nil 32 | end 33 | -- print("get_current_node =", table.tostring(cluster_nodes[nodename])) 34 | return cluster_nodes[nodename] 35 | end 36 | 37 | function cluster_monitor.get_cluster_nodes() 38 | local cluster_nodes = share_memory["cluster_nodes"] 39 | return cluster_nodes 40 | end 41 | 42 | function cluster_monitor.get_cluster_node(nodename) 43 | if not nodename then 44 | return 45 | end 46 | local cluster_nodes = cluster_monitor.get_cluster_nodes() 47 | 48 | if not cluster_nodes then 49 | return 50 | end 51 | return cluster_nodes[nodename] 52 | end 53 | 54 | function cluster_monitor.get_cluster_node_by_server(server_type) 55 | local cluster_nodes = cluster_monitor.get_cluster_nodes() 56 | if not cluster_nodes then return nil end 57 | for _, v in pairs(cluster_nodes) do 58 | if v.servertype == server_type then 59 | return v 60 | end 61 | end 62 | return nil 63 | end 64 | 65 | function cluster_monitor.subscribe_node(callback, nodename) 66 | if all_subscribe == false and table.empty(subscribe_nodes) then 67 | skynet.call(addr, "lua", "subscribe_monitor", skynet.self()) 68 | end 69 | 70 | if nodename == nil then 71 | all_subscribe = true 72 | all_subscribe_cb = callback 73 | else 74 | subscribe_nodes[nodename] = callback 75 | end 76 | end 77 | 78 | function cluster_monitor.unsubscribe_node(nodename) 79 | if nodename == nil then 80 | all_subscribe = false 81 | all_subscribe_cb = nil 82 | return 83 | end 84 | 85 | if subscribe_nodes[nodename] then 86 | subscribe_nodes[nodename] = nil 87 | end 88 | 89 | if not all_subscribe and table.empty(subscribe_nodes) then 90 | skynet.call(addr, "lua", "unsubscribe_monitor", skynet.self()) 91 | end 92 | end 93 | 94 | function cluster_monitor.get_subcribe_callback(nodename) 95 | if nodename == nil and all_subscribe == true then 96 | return all_subscribe_cb 97 | elseif nodename and subscribe_nodes[nodename] then 98 | return subscribe_nodes[nodename] 99 | end 100 | return nil 101 | end 102 | 103 | 104 | function cluster_monitor.start(redis_conf, current_conf) 105 | assert(redis_conf, "redis_conf is nil") 106 | assert(current_conf, "current_conf is nil") 107 | current_nodename = current_conf.nodename 108 | skynet.call(addr, "lua", "start", redis_conf, current_conf) 109 | end 110 | 111 | function cluster_monitor.open() 112 | skynet.call(addr, "lua", "open") 113 | end 114 | 115 | skynet.init(init) 116 | 117 | return cluster_monitor 118 | -------------------------------------------------------------------------------- /logic/desk/desk_ctrl.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local context = require "context" 3 | local desk_const = require "desk.desk_const" 4 | local db_helper = require "common.db_helper" 5 | local DESK_CALLBACK = desk_const.DESK_CALLBACK 6 | local desk_ctrl = {} 7 | 8 | local callbacks = {} 9 | local player_agents = {} -- player_id: room.agent 10 | local player_ctxs = {} -- player_id: ctx 11 | 12 | function desk_ctrl.register_callback(method, callback) 13 | if not callbacks[method] then 14 | callbacks[method] = callback 15 | end 16 | end 17 | 18 | function desk_ctrl.unregister_callback(method) 19 | if callbacks[method] then 20 | callbacks[method] = nil 21 | end 22 | end 23 | 24 | function desk_ctrl.incr_deal_id() 25 | return db_helper.call(DB_SERVICE.unique, "desk.incr_deal_id") 26 | end 27 | 28 | function desk_ctrl.get_player_agent(player_id) 29 | return player_agents[player_id] 30 | end 31 | 32 | function desk_ctrl.get_player_ctx(player_id) 33 | return player_ctxs[player_id] 34 | end 35 | 36 | function desk_ctrl.get_player_info(player_id) 37 | assert(player_id, "player_id is nil") 38 | local agent = desk_ctrl.get_player_agent(player_id) 39 | if not agent then 40 | return nil 41 | end 42 | local player_info = context.call_service(agent, "player.get_player_info") 43 | return player_info 44 | end 45 | 46 | function desk_ctrl.login_desk(ctx, agent) 47 | player_agents[ctx.player_id] = agent 48 | player_ctxs[ctx.player_id] = ctx 49 | if callbacks[DESK_CALLBACK.login_desk] then 50 | local ec = callbacks[DESK_CALLBACK.login_desk](ctx) 51 | if ec ~= SYSTEM_ERROR.success then 52 | return ec 53 | end 54 | end 55 | context.rpc_call(ctx.gate, ctx.watchdog, "login_desk", ctx.fd, skynet.self()) 56 | return SYSTEM_ERROR.success 57 | end 58 | 59 | function desk_ctrl.logout_desk(ctx) 60 | if callbacks[DESK_CALLBACK.logout_desk] then 61 | local ec = callbacks[DESK_CALLBACK.logout_desk](ctx) 62 | if ec ~= SYSTEM_ERROR.success then 63 | return ec 64 | end 65 | end 66 | player_agents[ctx.player_id] = nil 67 | player_ctxs[ctx.player_id] = nil 68 | context.rpc_call(ctx.gate, ctx.watchdog, "logout_desk", ctx.fd) 69 | return SYSTEM_ERROR.success 70 | end 71 | 72 | function desk_ctrl.player_disconnect() 73 | if callbacks[DESK_CALLBACK.player_disconnect] then 74 | local ec = callbacks[DESK_CALLBACK.player_disconnect](ctx) 75 | if ec ~= SYSTEM_ERROR.success then 76 | return ec 77 | end 78 | end 79 | return SYSTEM_ERROR.success 80 | end 81 | 82 | function desk_ctrl.player_reconnect(ctx) 83 | if callbacks[DESK_CALLBACK.player_reconnect] then 84 | local ec = callbacks[DESK_CALLBACK.player_reconnect](ctx) 85 | if ec ~= SYSTEM_ERROR.success then 86 | return ec 87 | end 88 | end 89 | context.rpc_call(ctx.gate, ctx.watchdog, "login_desk", ctx.fd, skynet.self()) 90 | return SYSTEM_ERROR.success 91 | end 92 | 93 | function desk_ctrl.can_group() 94 | if callbacks[DESK_CALLBACK.can_group] then 95 | local ec = callbacks[DESK_CALLBACK.can_group]() 96 | return ec 97 | end 98 | return false 99 | end 100 | 101 | function desk_ctrl.player_payout_end(player_id, player_info) 102 | if callbacks[DESK_CALLBACK.player_payout_end] then 103 | callbacks[DESK_CALLBACK.player_payout_end](player_id, player_info) 104 | end 105 | end 106 | 107 | function desk_ctrl.init() 108 | 109 | end 110 | 111 | return desk_ctrl -------------------------------------------------------------------------------- /logic/area/area_logic.lua: -------------------------------------------------------------------------------- 1 | require "skynet.manager" 2 | local skynet = require "skynet" 3 | local context = require "context" 4 | local db_helper = require "common.db_helper" 5 | local role_ctrl = require "role.role_ctrl" 6 | local area_logic = {} 7 | 8 | function area_logic.enter_area(ctx, req) 9 | local current_conf = req.current_conf 10 | local update_online_info = { 11 | state = ONLINE_STATE.onarea, 12 | agentnode = current_conf.nodename, 13 | agentver = current_conf.ver, 14 | server_id = req.server_id, 15 | game_id = req.game_id, 16 | } 17 | db_helper.call(DB_SERVICE.fd, "fd.set_fd", FD_TYPE.area, req.game_id, req.server_id, nil, nil, ctx.fd) 18 | db_helper.call(DB_SERVICE.hall, "hall.set_player_online", ctx.player_id, update_online_info) 19 | db_helper.call(DB_SERVICE.game, "room.server_incrby_fd", req.game_id, req.server_id, ctx.fd) 20 | context.rpc_call(ctx.gate, ctx.watchdog, "enter_area", ctx.fd, current_conf.nodename, current_conf.ver, req.game_id) 21 | return SYSTEM_ERROR.success 22 | end 23 | 24 | local function _exit_room(ctx, req) 25 | local online = db_helper.call(DB_SERVICE.hall, "hall.get_player_online", ctx.player_id) 26 | local room_addr = tonumber(online.roomaddr) 27 | context.call(room_addr, "exit_room", ctx, req) 28 | end 29 | 30 | local function _exit_area(ctx, req) 31 | db_helper.call(DB_SERVICE.fd, "fd.unset_fd", FD_TYPE.area, req.game_id, req.server_id, nil, nil, ctx.fd) 32 | db_helper.call(DB_SERVICE.hall, "hall.update_player_online", ctx.player_id, "state", ONLINE_STATE.online) 33 | db_helper.call(DB_SERVICE.hall, "hall.del_player_online_value", ctx.player_id, "game_id") 34 | db_helper.call(DB_SERVICE.hall, "hall.del_player_online_value", ctx.player_id, "server_id") 35 | db_helper.call(DB_SERVICE.hall, "hall.del_player_online_value", ctx.player_id, "role_id") 36 | db_helper.call(DB_SERVICE.hall, "hall.del_player_online_value", ctx.player_id, "agentnode") 37 | db_helper.call(DB_SERVICE.hall, "hall.del_player_online_value", ctx.player_id, "agentver") 38 | db_helper.call(DB_SERVICE.game, "room.exit_area", req.game_id, req.server_id, ctx.fd) 39 | context.rpc_call(ctx.gate, ctx.watchdog, "exit_area", ctx.fd) 40 | end 41 | 42 | function area_logic.exit_area(ctx, req) 43 | local online = db_helper.call(DB_SERVICE.hall, "hall.get_player_online", ctx.player_id) 44 | local state = tonumber(online.state) 45 | if state == ONLINE_STATE.onroom or state == ONLINE_STATE.ongroup then 46 | _exit_room(ctx, req) 47 | _exit_area(ctx, req) 48 | end 49 | if state == ONLINE_STATE.onarea then 50 | _exit_area(ctx, req) 51 | end 52 | return SYSTEM_ERROR.success 53 | end 54 | 55 | function area_logic.get_role(ctx, req) 56 | local ec, role_id = role_ctrl.get_role(ctx, req) 57 | if ec ~= SYSTEM_ERROR.success then 58 | return ec 59 | end 60 | db_helper.call(DB_SERVICE.hall, "hall.update_player_online", ctx.player_id, "role_id", role_id) 61 | context.rpc_call(ctx.gate, ctx.watchdog, "set_role_id", ctx.fd, role_id) 62 | return SYSTEM_ERROR.success 63 | end 64 | 65 | function area_logic.create_role(ctx, req) 66 | local ec, role_id = role_ctrl.create_role(ctx, req) 67 | if ec ~= SYSTEM_ERROR.success then 68 | return ec 69 | end 70 | db_helper.call(DB_SERVICE.hall, "hall.update_player_online", ctx.player_id, "role_id", role_id) 71 | context.rpc_call(ctx.gate, ctx.watchdog, "set_role_id", ctx.fd, role_id) 72 | return SYSTEM_ERROR.success 73 | end 74 | 75 | return area_logic -------------------------------------------------------------------------------- /lualib/connector.lua: -------------------------------------------------------------------------------- 1 | --[[ 2 | @ filename : connector.lua 3 | @ author : zhangshiqian1214@163.com 4 | @ modify : 2017-08-23 17:53 5 | @ company : zhangshiqian1214 6 | ]] 7 | 8 | 9 | local class = require "class" 10 | local skynet = require "skynet" 11 | local coroutine = require "skynet.coroutine" 12 | 13 | CONNECT_NONE = 0 --未连接状态 14 | CONNECT_OK = 2 --连接成功 15 | 16 | local connector = class() 17 | function connector:_init() 18 | self._connect_status = CONNECT_NONE 19 | self._useable = true 20 | self._reconnect_wait = 3 21 | self._connect_conf = nil 22 | self._connect_func = nil 23 | self._check_disconnect_func = nil 24 | self._connect_cb = nil 25 | self._disconnect_cb = nil 26 | self._co = nil 27 | end 28 | 29 | function connector:set_connect_conf(conf) 30 | self._connect_conf = conf 31 | end 32 | 33 | function connector:set_connect_func(connect_func) 34 | self._connect_func = connect_func 35 | end 36 | 37 | function connector:set_check_disconnect_fun(check_disconnect_func) 38 | self._check_disconnect_func = check_disconnect_func 39 | end 40 | 41 | function connector:set_connect_callback(connect_cb) 42 | self._connect_cb = connect_cb 43 | end 44 | 45 | function connector:set_disconnect_callback(disconnect_cb) 46 | self._disconnect_cb = disconnect_cb 47 | end 48 | 49 | function connector:set_status_connected() 50 | self._connect_status = CONNECT_OK 51 | end 52 | 53 | function connector:set_status_disconnect() 54 | self._connect_status = CONNECT_NONE 55 | end 56 | 57 | function connector:is_useable() 58 | return self._useable == true 59 | end 60 | 61 | function connector:is_connected() 62 | return self._connect_status == CONNECT_OK 63 | end 64 | 65 | function connector:get_connect_conf() 66 | return self._connect_conf 67 | end 68 | 69 | function connector:stop() 70 | self._useable = false 71 | if self._thread then 72 | self._thread = nil 73 | end 74 | end 75 | 76 | function connector:connect() 77 | local ret 78 | local ok, msg = xpcall(function() 79 | ret = self._connect_func(self._connect_conf) 80 | end, debug.traceback) 81 | if not ok or not ret then 82 | pcall(self._disconnect_cb, self._connect_conf) 83 | return false 84 | end 85 | 86 | self:set_status_connected() 87 | if self._connect_cb then 88 | pcall(self._connect_cb, self._connect_conf) 89 | end 90 | 91 | return true 92 | end 93 | 94 | function connector:check_disconnect() 95 | local ok, msg = xpcall(function() 96 | self._check_disconnect_func(self._connect_conf) 97 | end, debug.traceback) 98 | if not ok then 99 | if self:is_connected() and self._disconnect_cb then 100 | pcall(self._disconnect_cb, self._connect_conf) 101 | end 102 | self:set_status_disconnect() 103 | return false 104 | end 105 | return true 106 | end 107 | 108 | function connector:start() 109 | self._useable = true 110 | skynet.fork(function() 111 | self._thread = coroutine.running() 112 | while self:is_useable() and self._thread == coroutine.running() do 113 | if not self:connect() then 114 | skynet.sleep(self._reconnect_wait * 100) 115 | -- 添加,直接停止检测 116 | --return 117 | else 118 | self:check_disconnect() 119 | end 120 | end 121 | end) 122 | end 123 | 124 | function connector:reset() 125 | self._connect_status = CONNECT_NONE 126 | self._useable = false 127 | self._reconnect_wait = 3 128 | self._connect_conf = nil 129 | self._connect_func = nil 130 | self._check_disconnect_func = nil 131 | self._connect_cb = nil 132 | self._disconnect_cb = nil 133 | self._co = nil 134 | end 135 | 136 | return connector -------------------------------------------------------------------------------- /logic/locator/locator_ctrl.lua: -------------------------------------------------------------------------------- 1 | require "skynet.manager" 2 | local skynet = require "skynet" 3 | local db_helper = require "common.db_helper" 4 | local utils = require "utils" 5 | local cluster_config = require "config.cluster_config" 6 | local game_config = require "config.game_config" 7 | local locator_server_config = require "config.locator_server_config" 8 | local locator_ctrl = {} 9 | 10 | --只能执行一次 11 | local function set_unstart_server_map() 12 | local start_map = {} 13 | local unstart_map = {} 14 | for server_id, v in pairs(locator_server_config) do 15 | local game_id = v.game_id 16 | if start_map[game_id] ~= nil then 17 | skynet.error("cy=================gameid has store", game_id) 18 | elseif unstart_map[game_id] ~= nil then 19 | db_helper.call(DB_SERVICE.game, "room.lpush_server_id", game_id, server_id) 20 | else 21 | local num = db_helper.call(DB_SERVICE.game, "room.llen_server_id", game_id) 22 | if num > 0 then 23 | start_map[game_id] = {} 24 | start_map[game_id] = game_id 25 | else 26 | unstart_map[game_id] = {} 27 | unstart_map[game_id] = game_id 28 | db_helper.call(DB_SERVICE.game, "room.lpush_server_id", game_id, server_id) 29 | end 30 | end 31 | end 32 | end 33 | 34 | function locator_ctrl.init() 35 | set_unstart_server_map() 36 | end 37 | 38 | local function register_start_server(game_id) 39 | local server_id = 0 40 | local reply = db_helper.call(DB_SERVICE.game, "room.lpop_server_id", game_id) 41 | if reply ~= nil then 42 | server_id = tonumber(reply[1]) 43 | db_helper.call(DB_SERVICE.game, "room.register_server", game_id, server_id) 44 | return server_id 45 | end 46 | end 47 | 48 | local function start_server(game_id, server_id) 49 | local str = string.format("./run_%s.sh %d", game_config[game_id].module_name, server_id) 50 | os.execute(str) 51 | skynet.error("cy========locator start gameserver =", server_id) 52 | end 53 | 54 | local function stop_server(server_id) 55 | local str = string.format("./stop_game.sh %d", server_id) 56 | os.execute(str) 57 | skynet.error("cy========locator stop gameserver =", server_id) 58 | local game_id = locator_server_config[server_id].game_id 59 | db_helper.call(DB_SERVICE.game, "room.lpush_server_id", game_id, server_id) 60 | end 61 | 62 | local function check_stop_server() 63 | local reply = db_helper.call(DB_SERVICE.game, "room.lpop_unstop_server") 64 | if reply ~= nil then 65 | local server_id = tonumber(reply[1]) 66 | stop_server(server_id) 67 | end 68 | end 69 | 70 | local function get_server_id(game_id) 71 | local server_id = 0 72 | local reply = db_helper.call(DB_SERVICE.game, "room.get_max_server", game_id) 73 | if reply ~= nil then 74 | server_id = tonumber(reply[1]) 75 | local fd_num = tonumber(reply[2]) 76 | if fd_num == locator_server_config[server_id].player_limit then 77 | server_id = register_start_server(game_id) 78 | if server_id ~= 0 then 79 | start_server(game_id, server_id) 80 | end 81 | end 82 | end 83 | return server_id 84 | end 85 | 86 | function locator_ctrl.register_start_info(server_id) 87 | skynet.error("cy====================register_start_info", server_id) 88 | local game_id = locator_server_config[server_id].game_id 89 | db_helper.call(DB_SERVICE.game, "room.lrem_server_id", game_id, server_id) 90 | db_helper.call(DB_SERVICE.game, "room.register_server", game_id, server_id) 91 | return SYSTEM_ERROR.success 92 | end 93 | 94 | function locator_ctrl.route_sid(game_id) 95 | check_stop_server() 96 | local server_id = get_server_id(game_id) 97 | if server_id == 0 then 98 | skynet.error("no find server_id for game_id=", game_id) 99 | end 100 | return cluster_config[server_id].nodename 101 | end 102 | 103 | return locator_ctrl -------------------------------------------------------------------------------- /client/simplemessage.lua: -------------------------------------------------------------------------------- 1 | local socket = require "simplesocket" 2 | local sproto = require "sproto" 3 | local sproto_helper = require "sproto_helper" 4 | 5 | local proto_map = require "proto_map" 6 | local utils = require "utils" 7 | local message = {} 8 | local var = { 9 | session_id = 0 , 10 | session = {}, -- [var.session_id] = { name = funcName, req = args } 11 | object = {}, 12 | } 13 | 14 | sproto_helper.register_protos() 15 | 16 | function message.register(name) 17 | local f = assert(io.open(name .. ".s2c.sproto")) 18 | local t = f:read "a" 19 | f:close() 20 | --sproto:host([packagename]) creates a host object to deliver the rpc message. 21 | var.host = sproto.parse(t):host "package" 22 | local f = assert(io.open(name .. ".c2s.sproto")) 23 | local t = f:read "a" 24 | f:close() 25 | --host:attach(sprotoobj) creates a function(protoname, message, session, ud) to pack and encode request message with sprotoobj 26 | var.request = var.host:attach(sproto.parse(t)) 27 | end 28 | 29 | function message.peer(addr, port) 30 | var.addr = addr 31 | var.port = port 32 | end 33 | 34 | function message.connect() 35 | socket.connect(var.addr, var.port) 36 | socket.isconnect() 37 | end 38 | 39 | function message.bind(obj, handler) 40 | var.object[obj] = handler 41 | end 42 | 43 | function message.close() 44 | socket.close() 45 | end 46 | 47 | -- c2s request 48 | function message.request(header, data) 49 | var.session_id = var.session_id + 1 50 | var.session[var.session_id] = { name = header, req = data } 51 | socket.write(sproto_helper.pack(header, data)) 52 | return var.session_id 53 | end 54 | 55 | function message.update(ti) 56 | local msg = socket.read(ti) 57 | if not msg then 58 | return false 59 | end 60 | local header, data = sproto_helper.unpack(msg, #msg) 61 | local proto = proto_map.protos[header.protoid] 62 | local funcname = proto.name 63 | 64 | for obj, handler in pairs(var.object) do 65 | local f = handler[funcname] 66 | if f then 67 | local ok, err_msg = pcall(f, obj, header, data) 68 | if not ok then 69 | print(string.format("funcname for [%s] error : %s", funcname, tostring(obj), err_msg)) 70 | end 71 | end 72 | end 73 | 74 | --[[host:dispatch(blob [,sz])unpack and decode (sproto:pdecode) the binary string with type the host created (packagename). 75 | If .type is exist, it's a REQUEST message with .type , returns "REQUEST", protoname, message, responser, .ud. The responser is a function 76 | for encode the response message. The responser will be nil when .session is not exist. 77 | If .type is not exist, it's a RESPONSE message for .session . Returns "RESPONSE", .session, message, .ud . 78 | --]] 79 | 80 | --[[ 81 | local t, session_id, resp, err = var.host:dispatch(msg) 82 | if t == "REQUEST" then --s2c request 83 | --print("s2c request=", session_id, resp) 84 | for obj, handler in pairs(var.object) do 85 | local f = handler[session_id] -- session_id is request type 86 | if f then 87 | local ok, err_msg = pcall(f, obj, resp) -- resp is content of push 88 | if not ok then 89 | print(string.format("push %s for [%s] error : %s", session_id, tostring(obj), err_msg)) 90 | end 91 | end 92 | end 93 | else --s2c response 94 | --print("s2c response=", session_id, resp) 95 | local session = var.session[session_id] 96 | var.session[session_id] = nil 97 | 98 | for obj, handler in pairs(var.object) do 99 | if err then 100 | local f = handler.__error 101 | if f then 102 | local ok, err_msg = pcall(f, obj, session.name, err, session.req, session_id) 103 | if not ok then 104 | print(string.format("session %s[%d] error(%s) for [%s] error : %s", session.name, session_id, err, tostring(obj), err_msg)) 105 | end 106 | end 107 | else 108 | local f = handler[session.name] -- session_id => request type 109 | if f then 110 | local ok, err_msg = pcall(f, obj, session.req, resp, session_id) 111 | if not ok then 112 | print(string.format("session %s[%d] for [%s] error : %s", session.name, session_id, tostring(obj), err_msg)) 113 | end 114 | end 115 | end 116 | end 117 | end 118 | ]]-- 119 | return true 120 | end 121 | 122 | return message 123 | -------------------------------------------------------------------------------- /logic/login/login_ctrl.lua: -------------------------------------------------------------------------------- 1 | local skynet = require "skynet" 2 | local context = require "context" 3 | local cluster_monitor = require "cluster_monitor" 4 | local login_ctrl = {} 5 | 6 | local logic_svc_pool = {} 7 | local logic_svc_index = 1 8 | local request_sessions = {} 9 | 10 | local function init_logic_pool() 11 | local logic_count = skynet.getenv("login_logic_count") 12 | for i=1, logic_count do 13 | local svc = skynet.newservice("login_logic_svc") 14 | logic_svc_pool[#logic_svc_pool + 1] = svc 15 | end 16 | end 17 | 18 | local function get_logic_svc() 19 | local svc = logic_svc_pool[logic_svc_index] 20 | logic_svc_index = logic_svc_index + 1 21 | if logic_svc_index > #logic_svc_pool then 22 | logic_svc_index = 1 23 | end 24 | return svc 25 | end 26 | 27 | function login_ctrl.init() 28 | init_logic_pool() 29 | end 30 | 31 | function login_ctrl.cast_login(ctx, player_info) 32 | skynet.error("cy===============================logincast_loginsssssssssss") 33 | local hall_node = cluster_monitor.get_cluster_node_by_server(SERVER.HALL) 34 | if not hall_node then 35 | error("cast_login hallserver not online") 36 | end 37 | context.rpc_call(hall_node.nodename, SERVICE.HALL, "cast_login", ctx, player_info) 38 | end 39 | 40 | function login_ctrl.cast_logout(ctx, req) 41 | skynet.error("cy===============================logincast_logoutsssssssssss") 42 | local hall_node = cluster_monitor.get_cluster_node_by_server(SERVER.HALL) 43 | if not hall_node then 44 | error("cast_logout hallserver not online") 45 | end 46 | context.rpc_call(hall_node.nodename, SERVICE.HALL, "cast_logout", ctx, req) 47 | end 48 | 49 | function login_ctrl.signin_account(ctx, req) 50 | if not req.account then 51 | return AUTH_ERROR.account_nil 52 | end 53 | if not req.password then 54 | return AUTH_ERROR.password_nil 55 | end 56 | if not req.subid then 57 | return AUTH_ERROR.subid_nil 58 | end 59 | if ctx.player_id then 60 | return AUTH_ERROR.repeat_login 61 | end 62 | 63 | local session_info = request_sessions[ctx.session] 64 | 65 | if session_info then 66 | return SYSTEM_ERROR.busy 67 | end 68 | request_sessions[ctx.session] = true 69 | 70 | local svc = get_logic_svc() 71 | local ec, reply = context.call(svc, "signin_account", ctx, req) 72 | if ec ~= SYSTEM_ERROR.success then 73 | request_sessions[ctx.session] = nil 74 | return ec 75 | end 76 | 77 | ctx.account_type = ACCOUNT_TYPE.normal 78 | login_ctrl.cast_login(ctx, reply.player) 79 | request_sessions[ctx.session] = nil 80 | return SYSTEM_ERROR.success, reply 81 | end 82 | 83 | function login_ctrl.weixin_account(ctx, req) 84 | if not req.subid then 85 | return AUTH_ERROR.subid_nil 86 | end 87 | if ctx.player_id then 88 | return AUTH_ERROR.repeat_login 89 | end 90 | local session_info = request_sessions[ctx.session] 91 | if session_info then 92 | return SYSTEM_ERROR.busy 93 | end 94 | request_sessions[ctx.session] = true 95 | 96 | local svc = get_logic_svc() 97 | local ec, reply = context.call(svc, "weixin_account", ctx, req) 98 | if ec ~= SYSTEM_ERROR.success then 99 | request_sessions[ctx.session] = nil 100 | return ec 101 | end 102 | ctx.account_type = ACCOUNT_TYPE.weixin 103 | login_ctrl.cast_login(ctx, reply.player) 104 | request_sessions[ctx.session] = nil 105 | return SYSTEM_ERROR.success, reply 106 | end 107 | 108 | function login_ctrl.vistor_account(ctx, req) 109 | if not req.subid then 110 | return AUTH_ERROR.subid_nil 111 | end 112 | if ctx.player_id then 113 | return AUTH_ERROR.repeat_login 114 | end 115 | local session_info = request_sessions[ctx.session] 116 | if session_info then 117 | return SYSTEM_ERROR.busy 118 | end 119 | request_sessions[ctx.session] = true 120 | 121 | local svc = get_logic_svc() 122 | local ec, reply = context.call(svc, "vistor_account", ctx, req) 123 | if ec ~= SYSTEM_ERROR.success then 124 | request_sessions[ctx.session] = nil 125 | return ec 126 | end 127 | ctx.account_type = ACCOUNT_TYPE.vistor 128 | login_ctrl.cast_login(ctx, reply.player) 129 | request_sessions[ctx.session] = nil 130 | return SYSTEM_ERROR.success, reply 131 | end 132 | 133 | function login_ctrl.logout_account(ctx, req) 134 | login_ctrl.cast_logout(ctx, req) 135 | return SYSTEM_ERROR.success 136 | end 137 | 138 | return login_ctrl -------------------------------------------------------------------------------- /lualib/utils.lua: -------------------------------------------------------------------------------- 1 | local M = {} 2 | 3 | function M.Sleep(n) 4 | local t0 = os.clock() 5 | while os.clock() - t0 <= n do end 6 | end 7 | 8 | function M.str_2_table(str) 9 | local func_str = "return "..str 10 | local func = load(func_str) 11 | return func() 12 | end 13 | 14 | local function serialize(obj) 15 | local lua = "" 16 | local t = type(obj) 17 | if t == "number" then 18 | lua = lua .. obj 19 | elseif t == "boolean" then 20 | lua = lua .. tostring(obj) 21 | elseif t == "string" then 22 | lua = lua .. string.format("%q", obj) 23 | elseif t == "table" then 24 | lua = lua .. "{" 25 | for k, v in pairs(obj) do 26 | lua = lua .. "[" .. serialize(k) .. "]=" .. serialize(v) .. "," 27 | end 28 | local metatable = getmetatable(obj) 29 | if metatable ~= nil and type(metatable.__index) == "table" then 30 | for k, v in pairs(metatable.__index) do 31 | lua = lua .. "[" .. serialize(k) .. "]=" .. serialize(v) .. "," 32 | end 33 | end 34 | lua = lua .. "}" 35 | elseif t == "nil" then 36 | return "nil" 37 | elseif t == "userdata" then 38 | return "userdata" 39 | elseif t == "function" then 40 | return "function" 41 | elseif t == "thread" then 42 | return "thread" 43 | else 44 | error("can not serialize a " .. t .. " type.") 45 | end 46 | return lua 47 | end 48 | 49 | M.table_2_str = serialize 50 | 51 | function M.print(o) 52 | print(serialize(o)) 53 | end 54 | 55 | function M.printSky(o) 56 | --print(serialize(o)) 57 | return serialize(o) 58 | end 59 | 60 | function M.print_array(o) 61 | local str = "{" 62 | for k,v in ipairs(o) do 63 | str = str .. serialize(v) .. "," 64 | end 65 | str = str .. "}" 66 | print(str) 67 | end 68 | 69 | function M.dump_table_2_file(tbl, name) 70 | local str = M.table_2_str(tbl) 71 | 72 | str = "return "..str 73 | local file = io.open(name, "w"); 74 | file:write(str) 75 | file:close() 76 | end 77 | 78 | function M.copy_array(t) 79 | local tmp = {} 80 | for _,v in ipairs(t) do 81 | table.insert(tmp, v) 82 | end 83 | 84 | return tmp 85 | end 86 | 87 | --打印table 88 | function M.print_r ( t ) 89 | local print_r_cache={} 90 | local function sub_print_r(t,indent) 91 | if (print_r_cache[tostring(t)]) then 92 | print(indent.."*"..tostring(t)) 93 | else 94 | print_r_cache[tostring(t)]=true 95 | if (type(t)=="table") then 96 | for pos,val in pairs(t) do 97 | if (type(val)=="table") then 98 | print(indent.."["..pos.."] => "..tostring(t).." {") 99 | sub_print_r(val,indent..string.rep(" ",string.len(pos)+8)) 100 | print(indent..string.rep(" ",string.len(pos)+6).."}") 101 | elseif (type(val)=="string") then 102 | print(indent.."["..pos..'] => "'..val..'"') 103 | else 104 | print(indent.."["..pos.."] => "..tostring(val)) 105 | end 106 | end 107 | else 108 | print(indent..tostring(t)) 109 | end 110 | end 111 | end 112 | if (type(t)=="table") then 113 | print(tostring(t).." {") 114 | sub_print_r(t," ") 115 | print("}") 116 | else 117 | sub_print_r(t," ") 118 | end 119 | print() 120 | end 121 | 122 | function M.removebyvalue(array, value, removeadll) 123 | -- deleteNum用于接收/返回删除个数; i/max 构成控制while循环 124 | local deleteNum,i,max=0,1,#array 125 | while i<=max do 126 | if array[i] == value then 127 | -- 通过索引操作表的删除元素 128 | table.remove(array,i) 129 | -- 标记删除次数 130 | deleteNum = deleteNum+1 131 | i = i-1 132 | -- 控制while循环操作 133 | max = max-1 134 | -- 判断是否删除所有相同的value值 135 | if not removeadll then break end 136 | end 137 | i= i+1 138 | end 139 | -- 返回删除次数 140 | return deleteNum 141 | end 142 | 143 | function M.SleepSleep(n) 144 | os.execute("sleep " .. n) 145 | end 146 | 147 | return M 148 | --------------------------------------------------------------------------------