Skip to content

Commit

Permalink
Merge pull request #24 from pwnsky/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
i0gan authored Oct 1, 2023
2 parents 6b97194 + 03d7a11 commit f9bc1f9
Show file tree
Hide file tree
Showing 16 changed files with 335 additions and 54 deletions.
16 changes: 13 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

**讨论QQ群:739065686**

**version:** 1.0.3
**version:** 1.0.4

---

Expand Down Expand Up @@ -428,9 +428,19 @@ vi /etc/clickhouse-server/users.xml



## 生成配置文件
## 修改配置文件

采用Office软件打开{project_path}/resource/excel/server/DB.xlsx,修改里面的IP为你搭建的数据库ip,默认为127.0.0.1。修改完毕之后,需要重新生产配置文件,需执行一个脚本进行生成。linux执行如下:
### 修改数据库IP

采用Office软件打开{project_path}/resource/excel/DB.xlsx,修改里面的IP为你搭建的数据库ip,默认为127.0.0.1。

### 修改服务器公网IP,

打开{project_path}/resource/excel/Server.xlsx, 修改PublicIP为服务器的公网IP。

### 生成配置

修改完毕之后,需要重新生产配置文件,需执行一个脚本进行生成。linux执行如下:

```
cd {project_path}/tools
Expand Down
13 changes: 9 additions & 4 deletions resource/script/multi_start.bat
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ cd bin
start cmd /c " squick type=master id=1 "
start cmd /c " squick type=login id=2 "
start cmd /c " squick type=world id=100 "
start cmd /c " squick type=world id=101 "
start cmd /c " squick type=db_proxy id=300 "
start cmd /c " squick type=db_proxy id=301 "
start cmd /c " squick type=game id=1000 "
start cmd /c " squick type=game id=1001 "
start cmd /c " squick type=game_mgr id=2000 "

start cmd /c " squick type=lobby id=1000 "
start cmd /c " squick type=lobby id=1001 "
start cmd /c " squick type=lobby id=1002 "

rem start cmd /c " squick type=game_mgr id=2000 "

start cmd /c " squick type=proxy id=500 "
start cmd /c " squick type=proxy id=501 "
start cmd /c " squick type=proxy id=501 "
start cmd /c " squick type=proxy id=502 "
11 changes: 11 additions & 0 deletions src/lua/lib/db/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,17 @@
-- Date : 2023-09-24
-- Description: db init
-----------------------------------------------------------------------------
function GetDbProxyID()
local id = 0
local area = Env.area
if(area == 1) then
id = 300
elseif(area == 2) then
id = 301
end
return id
end

require "lib.db.redis"
require "lib.db.mysql"
require "lib.db.mongo"
Expand Down
74 changes: 70 additions & 4 deletions src/lua/lib/db/mongo.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
-- Description: async mongo cli
-----------------------------------------------------------------------------

local DbProxyID = 300;
local DbProxyID = 0;
Mongo = Mongo and Mongo or QueryAsync


function Mongo:Bind()
Net:ClientRegister(ServerType.ST_DB_PROXY, DbProxyRPC.ACK_MONGO_INSERT, self, self.AckInsert)
Net:ClientRegister(ServerType.ST_DB_PROXY, DbProxyRPC.ACK_MONGO_FIND, self, self.AckFind)
Net:ClientRegister(ServerType.ST_DB_PROXY, DbProxyRPC.ACK_MONGO_UPDATE, self, self.AckUpdate)
Net:ClientRegister(ServerType.ST_DB_PROXY, DbProxyRPC.ACK_MONGO_DELETE, self, self.AckDelete)
Net:ClientRegister(ServerType.ST_DB_PROXY, DbProxyRPC.ACK_MONGO_CREATE_INDEX, self, self.AckCreateIndex)
DbProxyID = GetDbProxyID()
end

-- Insert
function Mongo:InsertAsync(db, collection, insert_json)
local query_id = self:QueryInit()
local req = {
Expand All @@ -25,14 +29,15 @@ function Mongo:InsertAsync(db, collection, insert_json)
Net:SendToServer(DbProxyID, DbProxyRPC.REQ_MONGO_INSERT, Squick:Encode("rpc.ReqMongoInsert", req))
local data = self:QueryAwait(query_id)
self:QueryClean(query_id)
return data.code
return data
end

function Mongo:AckInsert(guid, msg_data, msg_id, fd)
local data = Squick:Decode("rpc.AckMongoInsert", msg_data);
self:QueryResume(data.query_id, data)
end

-- Find
function Mongo:FindAsync(db, collection, condition_json)
local query_id = self:QueryInit()
local req = {
Expand All @@ -44,12 +49,73 @@ function Mongo:FindAsync(db, collection, condition_json)
Net:SendToServer(DbProxyID, DbProxyRPC.REQ_MONGO_FIND, Squick:Encode("rpc.ReqMongoFind", req))
local data = self:QueryAwait(query_id)
self:QueryClean(query_id)
return data.result_json
return data
end

function Mongo:AckFind(guid, msg_data, msg_id, fd)
local data = Squick:Decode("rpc.AckMongoFind", msg_data);
self:QueryResume(data.query_id, data)
end

-- Update
function Mongo:UpdateAsync(db, collection, condition_json, update_json)
local query_id = self:QueryInit()
local req = {
query_id = query_id,
db = db,
collection = collection,
condition_json = condition_json,
update_json = update_json,
}
Net:SendToServer(DbProxyID, DbProxyRPC.REQ_MONGO_UPDATE, Squick:Encode("rpc.ReqMongoUpdate", req))
local data = self:QueryAwait(query_id)
self:QueryClean(query_id)
return data
end

function Mongo:AckUpdate(guid, msg_data, msg_id, fd)
local data = Squick:Decode("rpc.AckMongoUpdate", msg_data);
self:QueryResume(data.query_id, data)
end

-- Delete
function Mongo:DeleteAsync(db, collection, condition_json)
local query_id = self:QueryInit()
local req = {
query_id = query_id,
db = db,
collection = collection,
condition_json = condition_json,
}
Net:SendToServer(DbProxyID, DbProxyRPC.REQ_MONGO_DELETE, Squick:Encode("rpc.ReqMongoDelete", req))
local data = self:QueryAwait(query_id)
self:QueryClean(query_id)
return data
end

function Mongo:AckDelete(guid, msg_data, msg_id, fd)
local data = Squick:Decode("rpc.AckMongoDelete", msg_data);
self:QueryResume(data.query_id, data)
end

-- Create index
function Mongo:CreateIndexAsync(db, collection, condition_json)
local query_id = self:QueryInit()
local req = {
query_id = query_id,
db = db,
collection = collection,
condition_json = condition_json,
}
Net:SendToServer(DbProxyID, DbProxyRPC.REQ_MONGO_CREATE_INDEX, Squick:Encode("rpc.ReqMongoCreateIndex", req))
local data = self:QueryAwait(query_id)
self:QueryClean(query_id)
return data
end

function Mongo:AckDelete(guid, msg_data, msg_id, fd)
local data = Squick:Decode("rpc.AckMongoCreateIndex", msg_data);
self:QueryResume(data.query_id, data)
end

Mongo:Bind()
3 changes: 2 additions & 1 deletion src/lua/lib/db/redis.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
-- Description: async redis cli
-----------------------------------------------------------------------------

local DbProxyID = 300;
local DbProxyID = 0;
Redis = Redis and Redis or QueryAsync

function Redis:Bind()
Net:ClientRegister(ServerType.ST_DB_PROXY, DbProxyRPC.ACK_REDIS_GET, self, self.AckGetString)
Net:ClientRegister(ServerType.ST_DB_PROXY, DbProxyRPC.ACK_REDIS_SET, self, self.AckSetString)
DbProxyID = GetDbProxyID()
end

function Redis:GetStringAsync(key)
Expand Down
8 changes: 4 additions & 4 deletions src/lua/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ function Main(context)
package.path = path .. '/?.lua;'

Squick:LogInfo("lua module execute");
Env.app_id = Squick:AppID()
Env.app_type= Squick:AppType()
Env.app_name= Squick:AppName()
Env.area = Squick:Area()
end


Expand All @@ -46,6 +42,10 @@ end

-- 在 ReadyUpdate 才进行初始化 Lua 模块,目的是让C++层的所有模块已链接完毕
function ReadyUpdate()
Env.app_id = Squick:AppID()
Env.app_type= Squick:AppType()
Env.app_name= Squick:AppName()
Env.area = Squick:Area()
Load()
ModuleMgr:Start()
end
Expand Down
82 changes: 62 additions & 20 deletions src/lua/node/lobby/player_mgr.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,29 @@ end

function PlayerMgr:GetPlayerDataFromMongo(account_id, account)
local result = Mongo:FindAsync(MONGO_PLAYERS_DB, "base", '{"account_id":"' .. account_id .. '"}')

local data = result[1]
local base_data = {}
if(data) then
base_data = Json.decode(data)
end
local player_id = base_data.player_id
local player_data = nil

if(player_id == nil) then
if(result.matched_count == 0) then
-- Find base table is have index
local index_result = Mongo:FindAsync(MONGO_PLAYERS_DB, "index", '{"base": true}')
-- Create index on base
if (index_result.matched_count == 0) then
Mongo:CreateIndexAsync(MONGO_PLAYERS_DB, "base", Json.encode({account_id = 1}))
Mongo:InsertAsync(MONGO_PLAYERS_DB, "index", Json.encode({ base = true }))
end

index_result = Mongo:FindAsync(MONGO_PLAYERS_DB, "index", '{"detail": true}')
-- Create index on detail
if (index_result.matched_count == 0) then
Mongo:CreateIndexAsync(MONGO_PLAYERS_DB, "detail", Json.encode({player_id = 1, account_id = 1}))
Mongo:InsertAsync(MONGO_PLAYERS_DB, "index", Json.encode({ detail = true }))
end

-- Create new player
player_data = {
account = account, account_id = account_id, player_id = Env.area .. "-" .. account_id, name = "none",
age = 0, level = 0, proxy_id = 0, last_login_time = os.time(), created_time = os.time(),
online = false, platform = "none", extra = {},
area = Env.area, mail = {}, itmes = {}, ip = "", ip_address = "",
area = Env.area, mail = {}, itmes = {}, ip = "", ip_address = "", last_offline_time = 0,
real = {
id_card = "",
name = "",
Expand All @@ -51,8 +58,10 @@ function PlayerMgr:GetPlayerDataFromMongo(account_id, account)
Mongo:InsertAsync(MONGO_PLAYERS_DB, "base", Json.encode(base_data))
Mongo:InsertAsync(MONGO_PLAYERS_DB, "detail", Json.encode(player_data))
else
local base_data = Json.decode(result.result_json[1])
local player_id = base_data.player_id
local result = Mongo:FindAsync(MONGO_PLAYERS_DB, "detail", '{"player_id" : "' .. player_id ..'"}')
player_data = Json.decode(result[1])
player_data = Json.decode(result.result_json[1])
end
return player_data;
end
Expand All @@ -63,8 +72,7 @@ end

function PlayerMgr:OnEnter(player_id, msg_data, msg_id, fd)
-- This just example for async handle request
local co = coroutine.create(function(player_id, msg_data, msg_id, fd)

self:AsyncHnalder(function(player_id, msg_data, msg_id, fd)
local req = Squick:Decode("rpc.PlayerEnterEvent", msg_data);
local account_id = req.account_id
local account = req.account
Expand All @@ -75,25 +83,52 @@ function PlayerMgr:OnEnter(player_id, msg_data, msg_id, fd)
account_id = account_id,
player_id = player_id,
}
print("Player Enter: ", account, player_id)

player_data.last_login_time = os.time()
player_data.node.proxy_fd = fd
player_data.node.proxy_id = req.proxy_id
player_data.online = true
player_data.ip = req.ip

PrintTable(player_data)
-- Update Player data to db
local update = {}
update["last_login_time"] = player_data.last_login_time
update["node.proxy_fd"] = fd
update["node.proxy_id"] = req.proxy_id
update["online"] = true
update["ip"] = req.ip
local result = Mongo:UpdateAsync(MONGO_PLAYERS_DB, "detail", Json.encode({player_id = player_id}),
'{"$set": '..Json.encode(update)..'}')

self:CachePlayerData(player_id, player_data)
Net:SendByFD(fd, PlayerEventRPC.PLAYER_BIND_EVENT, Squick:Encode("rpc.PlayerBindEvent", ack))
end)
local status, err = coroutine.resume(co, player_id, msg_data, msg_id, fd)
end, player_id, msg_data, msg_id, fd)
end

function PlayerMgr:OnLeave(player_id, msg_data, msg_id, fd)
self:AsyncHnalder(function(player_id, msg_data, msg_id, fd)
--local req = Squick:Decode("rpc.PlayerLeaveEvent", msg_data);
local update = {}
update["last_offline_time"] = os.time()
update["node.proxy_fd"] = 0
update["node.proxy_id"] = 0
update["online"] = false
local result = Mongo:UpdateAsync(MONGO_PLAYERS_DB, "detail", Json.encode({player_id = player_id}),
'{"$set": '..Json.encode(update)..'}')
self:CachePlayerData(player_id, nil)
end, player_id, msg_data, msg_id, fd)
end

function PlayerMgr:AsyncHnalder(func, ...)
local co = coroutine.create(func)
local status, err = coroutine.resume(co, ...)
if(err)then
print(err)
end
end

function PlayerMgr:OnLeave(player_id, msg_data, msg_id, fd)
print("Player offline: ", player_id)
function PlayerMgr:UpdatePlayerAsync(player_id, data)

end

function PlayerMgr:SendToPlayer(player_id, msg_id, data)
Expand All @@ -111,9 +146,16 @@ function PlayerMgr:OnReqPlayerData(player_id, msg_data, msg_id, fd)
end
local ack = {
account = player.account,
account_id = player.account_id,
player_id = player_id,
name = player.name,
level = 0,
ip = player.ip,
area = player.area,
created_time = player.created_time,
last_login_time = player.last_login_time,
last_offline_time = player.last_offline_time,
platform = player.platform
}
self:SendToPlayer(player_id, PlayerRPC.ACK_PLAYER_DATA, Squick:Encode("rpc.AckPlayerData", ack))
end
Expand All @@ -125,4 +167,4 @@ function PlayerMgr:CachePlayerData(player_id, player_data)
self.players[player_id] = player_data
end

return PlayerMgr
return PlayerMgr
Loading

0 comments on commit f9bc1f9

Please sign in to comment.