97 lines
4.0 KiB
Lua
97 lines
4.0 KiB
Lua
local skynet = require "skynet"
|
|
local oo = require "Class"
|
|
local gameCmd = require "GameCmd"
|
|
local json =require "json"
|
|
local log = require "Log"
|
|
local sqlUrl = require "SqlUrl"
|
|
local errorInfo = require "ErrorInfo"
|
|
local dataType = require "DataType"
|
|
local serverId = tonumber(skynet.getenv "serverId")
|
|
local redisKeyUrl = require "RedisKeyUrl"
|
|
local serverManage = require "ServerManage"
|
|
local manageCmd = require "ManageCmd"
|
|
local DataCopyService = oo.class()
|
|
|
|
|
|
--初始化
|
|
function DataCopyService:Init()
|
|
|
|
end
|
|
|
|
function DataCopyService:DataCopy( c2sData , s2cData )
|
|
if not c2sData.fromPlayerAccount then
|
|
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
|
|
return s2cData
|
|
end
|
|
|
|
-- 查询被复制的玩家的数据
|
|
local isSuc , queryData = skynet.server.dataService:GetUserDBData( c2sData.fromPlayerAccount )
|
|
if not isSuc then
|
|
s2cData.code = errorInfo.ErrorCode.NoExistUser
|
|
return s2cData
|
|
end
|
|
local data = {}
|
|
local gameData = json:decode( queryData.GameData )
|
|
data.gameData = gameData
|
|
s2cData.data = data
|
|
log.info(string.format("成功将玩家 %s 的数据复制出来", c2sData.fromPlayerAccount ))
|
|
end
|
|
|
|
function DataCopyService:DataCopyTo( c2sData , s2cData )
|
|
if not c2sData.toPlayerAccount then
|
|
s2cData.code = errorInfo.ErrorCode.ErrRequestParam
|
|
return s2cData
|
|
end
|
|
-- 查询将要覆盖数据的玩家的数据
|
|
local userDBInfo = skynet.server.redisCenter:GetRedisDBInfo( c2sData.toPlayerAccount )
|
|
if not userDBInfo then
|
|
log.info("数据中心 获取用户DB数据失败1" , c2sData.toPlayerAccount )
|
|
s2cData.code = errorInfo.ErrorCode.NoExistUser
|
|
return s2cData
|
|
end
|
|
--强制下线
|
|
local data = {}
|
|
local redisKey = string.format( redisKeyUrl.RouteServerLoginInfoHash , c2sData.toPlayerAccount )
|
|
local gameServerId = tonumber( skynet.server.redis:hget( redisKey , "GameServerID" ) or 0 )
|
|
data.cmd = manageCmd.ForceUserOffline
|
|
data.userId = userDBInfo.UserID
|
|
data.account = c2sData.toPlayerAccount
|
|
skynet.server.multiServer:SendMsgToServer( gameServerId , skynet.server.clusterServer.Center2All_ServerManageCmd , data )
|
|
|
|
local dbIndex =userDBInfo.DBIndex
|
|
local userId = userDBInfo.UserID
|
|
-- 从数据库中获取玩家具体数据
|
|
local sql1 = string.format(sqlUrl.queryAccountFromPlayer, userId)
|
|
local queryData = skynet.server.db:QueryPlayer(dbIndex, sql1)
|
|
if not queryData and not queryData[1] then
|
|
log.info("数据中心 获取用户DB数据失败2" , c2sData.toPlayerAccount , dbIndex , userId )
|
|
s2cData.code = errorInfo.ErrorCode.NoExistUser
|
|
return s2cData
|
|
end
|
|
queryData = queryData[1]
|
|
local toGameData = json:decode( queryData.GameData )
|
|
local basicInfo = json:decode(queryData.BasicInfo)
|
|
local fromGameData =json:decode(c2sData.needCopyData)
|
|
--好友信息不覆盖
|
|
fromGameData.gameData.partner = toGameData.partner
|
|
fromGameData.gameData.mail = toGameData.mail
|
|
fromGameData.gameData.announcement = toGameData.announcement
|
|
fromGameData.gameData.announcementUpdateTime = toGameData.announcementUpdateTime
|
|
--保存该玩家数据
|
|
local sql = string.format(sqlUrl.saveAccountToPlayer, json:encode(basicInfo), json:encode(fromGameData.gameData), userId)
|
|
skynet.server.db:QueryPlayer(dbIndex, sql)
|
|
--[[
|
|
local redisKey = string.format( redisKeyUrl.AccountServerUserList , c2sData.toPlayerAccount )
|
|
local curRedis = skynet.server.redisCenter:GetAccountRedis( c2sData.toPlayerAccount )
|
|
skynet.server.redis:hset(redisKey , "saveMySqlTime" , skynet.GetTime())
|
|
curRedis:hmset(redisKey , "saveMySqlTime" , skynet.GetTime() , "saveMongoTime" , 0)
|
|
]]
|
|
skynet.server.redisCenter:SetRedisDBInfo( c2sData.toPlayerAccount , "saveMySqlTime" , skynet.GetTime() , "saveMongoTime" , 0 )
|
|
log.info(string.format("成功将玩家 %s 的数据复制到 玩家 %s", c2sData.fromPlayerAccount , c2sData.toPlayerAccount))
|
|
end
|
|
|
|
|
|
|
|
|
|
skynet.server.dataCopyService = DataCopyService
|
|
return DataCopyService |