hxp
2025-12-02 863a38aa50e3c28e1740f99550448be30fcc1841
ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/PyMongoDB/LogicProcess/UserCtrlDB.py
@@ -19,6 +19,7 @@
from ServerClientShareDefine import *
import ConfigParser
import IpyGameDataPY
import PlayerCoin
#import IPY_GameWorld
#from Config import (DBConfig,)
DBConfig = __import__('Config.DBConfig')
@@ -404,6 +405,16 @@
            
        return
    
    def onSecondProcess(self):
        '''子类实现 - 每秒处理'''
        if not self.Connected:
            return
        if self.IsMergeServer():
            pass
        else:
            self.OnQueryRechargeByLoop()
        return
    def loadDBPlayerIDMap(self):
        ## 加载本服玩家映射关系
        col = self.db[UCN_DBPlayer]
@@ -454,6 +465,15 @@
            dbPlayer.readRecord(rec)
            return dbPlayer
        return rec
    def updateDBPlayerName(self, playerID, newName):
        ## 根据玩家ID更新dbPlayer的玩家名,一般是改名用
        col = self.db[UCN_DBPlayer]
        dbPlayer = DataServerPlayerData.tagDBPlayer()
        if not dbPlayer.adoLoadCEx(col, {"PlayerID":playerID}):
            return
        dbPlayer.PlayerName = newName
        return dbPlayer.adoUpdateC(col)
    
    def requestLogicProcess(self, pack):
        db = self.db
@@ -1217,86 +1237,147 @@
    def OnFinishRecharge(self, db, pack):
        # map完成兑换,假设此处没有回复,则此单变成丢单,暂由人工维护
        # 若真频繁发生则改为上线恢复
        try:
            buf = pack.getBuffer()
            packLen = pack.getLength()
            recvPack = RecvPackProtocol.tagMDFinishRecharge()
            pos = 0
            recvPack.ReadData(buf, pos)
            orderID = recvPack.OrderID.strip(chr(0))
            mylog.debug('OnFinishRecharge ...%s'%orderID)
            col = db[UCN_PayInfo]
            result = col.find({"OrderID":orderID})
        return
#        try:
#            buf = pack.getBuffer()
#            packLen = pack.getLength()
#            recvPack = RecvPackProtocol.tagMDFinishRecharge()
#            pos = 0
#            recvPack.ReadData(buf, pos)
#            orderID = recvPack.OrderID.strip(chr(0))
#            mylog.debug('OnFinishRecharge ...%s'%orderID)
#
#            col = db[UCN_PayInfo]
#            result = col.find({"OrderID":orderID})
#
#            if not result or result.count() == 0:
#                # 没有充值订单
#                return
#
#            curTime = datetime.datetime.today()
#            curTimeStr = str(curTime)
#            curTimeStr = curTimeStr.split(".")[0]
#            col.update({"OrderID":orderID}, {"$set":{"IsProcess":1, 'EndOrderTime':curTimeStr}})
#
#            mylog.debug('OnFinishRecharge OrderID = %s'%(orderID))
#
#            return
#        except Exception, e:
#            msg = error.formatMsg('error', error.ERROR_NO_170, "OnFinishRecharge error %s!pack = %s"%(e, binascii.b2a_hex(buf)))
#            mylog.error(msg)
#        except:
#            msg = error.formatMsg('error', error.ERROR_NO_170, "OnFinishRecharge error!pack = %s"%(binascii.b2a_hex(buf)))
#            mylog.error(msg)
#        return
    def OnQueryRechargeByLoop(self):
        '''定时处理充值订单兑换' 每秒处理一次
            充值流程
            客户端支付商品 -> 平台回调游戏中心服务器 -> 游戏服务器插入订单tagPayInfo
            商品兑换旧流程:
            curPlayer.SendDBQueryRecharge() -> gstQueryRecharge -> OnQueryRecharge -> 【GameServerRefresh.PushRecharge】 -> PlayerCoin.PlayerCoinToGold ->
                -> curPlayer.SendDBFinishRecharge(orderID) -> gstFinishRecharge -> OnFinishRecharge
            发起方式:
            1. 游戏服务器每秒轮询未处理的订单进行兑换商品 -> MT_GetCoinReqs -> curPlayer.SendDBQueryRecharge()
            2. A8 06 查询充值结果 #tagCMQueryRecharge -> curPlayer.SendDBQueryRecharge()
            
            if not result or result.count() == 0:
                # 没有充值订单
            商品兑换新流程:
            1. 游戏服务器每秒轮询未处理的订单进行兑换商品 -> 直接调用 PlayerCoin.OnDBPushRecharge -> 直接更新订单 IsProcess
            2. 废弃, A8 06 查询充值结果 #tagCMQueryRecharge,只使用后端轮询即可,后续有问题再补充优化
        '''
        PayRechargeCount = 5 # 单次处理条数
        if PayRechargeCount <= 0:
            return
        if not PyGameData.g_onlineAccIDList:
            return
        if PyGameData.g_ctgOrderIDList:
            GameWorld.DebugLog("还有正在处理兑换商品的充值订单! %s" % PyGameData.g_ctgOrderIDList)
            return
        try:
            col = self.db[UCN_PayInfo]
            spec = {'AccID':{'$in':PyGameData.g_onlineAccIDList}, 'IsProcess':0}
            fields = {"_id":0, "AccID":1, "OrderID":1, "OrderInfo":1, "OrderAmount":1}
            result = col.find(spec, fields).limit(PayRechargeCount)
            if not result or not result.count():
                # 没有在线玩家未兑换的充值订单
                return
            
            curTime = datetime.datetime.today()
            curTimeStr = str(curTime)
            curTimeStr = curTimeStr.split(".")[0]
            col.update({"OrderID":orderID}, {"$set":{"IsProcess":1, 'EndOrderTime':curTimeStr}})
            
            mylog.debug('OnFinishRecharge OrderID = %s'%(orderID))
            return
        except Exception, e:
            msg = error.formatMsg('error', error.ERROR_NO_170, "OnFinishRecharge error %s!pack = %s"%(e, binascii.b2a_hex(buf)))
            mylog.error(msg)
            for billInfo in result:
                accID = fix_outgoingText(billInfo['AccID'])
                orderID = fix_outgoingText(billInfo['OrderID'])
                orderInfo = fix_outgoingText(billInfo['OrderInfo'])
                orderAmount = billInfo['OrderAmount']
                curPlayer = GameWorld.GetPlayerManager().FindPlayerByAccID(accID)
                if not curPlayer or curPlayer.IsEmpty():
                    GameWorld.Log("充值兑换商品时玩家不在线! accID=%s,orderAmount=%s,orderInfo=%s,orderID=%s" % (accID, orderAmount, orderInfo, orderID))
                    continue
                if orderID in PyGameData.g_ctgOrderIDList:
                    continue
                PyGameData.g_ctgOrderIDList.append(orderID)
                # 直接设置为已处理,订单如果实际异常未到账的情况由人工补偿处理
                col.update({"OrderID":orderID}, {"$set":{"IsProcess":1, 'EndOrderTime':curTimeStr}})
                PlayerCoin.OnDBPushRecharge(curPlayer, orderID, orderInfo, orderAmount)
        except:
            msg = error.formatMsg('error', error.ERROR_NO_170, "OnFinishRecharge error!pack = %s"%(binascii.b2a_hex(buf)))
            msg = error.formatMsg('error', error.ERROR_NO_170, "traceback = %s" % (traceback.format_exc()))
            mylog.error(msg)
        PyGameData.g_ctgOrderIDList = []
        return
    
    def OnQueryRecharge(self, db, pack):
        # map查询新的充值订单, 查询充值表
        try:
            buf = pack.getBuffer()
            packLen = pack.getLength()
            recvPack = RecvPackProtocol.tagMDQueryRecharge()
            pos = 0
            recvPack.ReadData(buf, pos)
            account = recvPack.Account.strip(chr(0))
            mylog.debug('OnQueryRecharge ...%s'%account)
            col = db[UCN_PayInfo]
            result = col.find({'AccID':account, 'IsProcess':0, 'EndOrderTime':0})
            if not result or result.count() == 0:
                # 没有充值订单
                return
            # 一单一单发
            # 先设置IsProcess为1兑换中,等回复结果在真正完成订单
            billInfo = result[0]
            sendPack = SendPackProtocol.tagDMRechargePush()
            sendPack.Type = CommonDefine.dgDBToMapServer
            sendPack.SubType = CommonDefine.gmReChargePush
            sendPack.OrderID = fix_outgoingText(billInfo['OrderID'])
            sendPack.OrderIDLen = len(sendPack.OrderID)
            sendPack.OrderInfo = fix_outgoingText(billInfo['OrderInfo'])
            sendPack.OrderInfoLen = len(sendPack.OrderInfo)
            sendPack.Money = int(billInfo['OrderAmount'])
            sendPack.Extras = fix_outgoingText(billInfo['Extras'])
            sendPack.ExtrasLen = len(sendPack.Extras)
            col.update({"OrderID":sendPack.OrderID}, {"$set":{"IsProcess":1}})
            self.sendString(pack, sendPack.GetBuffer())
            mylog.debug('OnQueryRecharge OrderID = %s'%(sendPack.OrderID))
            #mylog.debug('send %s'%sendPack.OutputString())
            return
        except Exception, e:
            msg = error.formatMsg('error', error.ERROR_NO_170, "OnQueryRecharge error %s!pack = %s"%(e, binascii.b2a_hex(buf)))
            mylog.error(msg)
        except:
            msg = error.formatMsg('error', error.ERROR_NO_170, "OnQueryRecharge error!pack = %s"%(binascii.b2a_hex(buf)))
            mylog.error(msg)
        # 暂废弃
        return
#        try:
#            buf = pack.getBuffer()
#            packLen = pack.getLength()
#            recvPack = RecvPackProtocol.tagMDQueryRecharge()
#            pos = 0
#            recvPack.ReadData(buf, pos)
#            account = recvPack.Account.strip(chr(0))
#            mylog.debug('OnQueryRecharge ...%s'%account)
#
#            col = db[UCN_PayInfo]
#            result = col.find({'AccID':account, 'IsProcess':0, 'EndOrderTime':0})
#
#            if not result or result.count() == 0:
#                # 没有充值订单
#                return
#
#            # 一单一单发
#            # 先设置IsProcess为1兑换中,等回复结果在真正完成订单
#            billInfo = result[0]
#
#            sendPack = SendPackProtocol.tagDMRechargePush()
#            sendPack.Type = CommonDefine.dgDBToMapServer
#            sendPack.SubType = CommonDefine.gmReChargePush
#            sendPack.OrderID = fix_outgoingText(billInfo['OrderID'])
#            sendPack.OrderIDLen = len(sendPack.OrderID)
#            sendPack.OrderInfo = fix_outgoingText(billInfo['OrderInfo'])
#            sendPack.OrderInfoLen = len(sendPack.OrderInfo)
#            sendPack.Money = int(billInfo['OrderAmount'])
#            sendPack.Extras = fix_outgoingText(billInfo['Extras'])
#            sendPack.ExtrasLen = len(sendPack.Extras)
#
#            col.update({"OrderID":sendPack.OrderID}, {"$set":{"IsProcess":1}})
#
#            self.sendString(pack, sendPack.GetBuffer())
#            mylog.debug('OnQueryRecharge OrderID = %s'%(sendPack.OrderID))
#            #mylog.debug('send %s'%sendPack.OutputString())
#            return
#        except Exception, e:
#            msg = error.formatMsg('error', error.ERROR_NO_170, "OnQueryRecharge error %s!pack = %s"%(e, binascii.b2a_hex(buf)))
#            mylog.error(msg)
#        except:
#            msg = error.formatMsg('error', error.ERROR_NO_170, "OnQueryRecharge error!pack = %s"%(binascii.b2a_hex(buf)))
#            mylog.error(msg)
#
#        return
    
    def OnQueryNewGuyCardState(self, db, pack):
        try:
@@ -1492,8 +1573,11 @@
            DataDumper.DumpData(GlobalFunctions.getAppPath(), 'UserLogs\\SaveFailDump', '%s.mdat'%playerID, saveData)      
            
        if accID in PyGameData.g_onlineAccIDList:
            PyGameData.g_onlineAccIDList.remove(accID)
        # 玩家下线恢复充值兑换中的订单,IsProcee为1,但endtime为空的情况
        self.RevoverBillProcess(db, accID)
        #self.RevoverBillProcess(db, accID)
        mylog.info("onSaveMapServerPlayerData result = %s, playerID = %s"%(result, playerID))
        
        # 下线成功入库后同步移动玩家备档备份文件夹
@@ -1601,26 +1685,24 @@
        return
    
    # 玩家下线恢复充值兑换中的订单,IsProcee为1,但endtime为空的情况
    def RevoverBillProcess(self, db, accID):
        try:
            if not accID:
                return
            account = accID.strip(chr(0))
            col = db[UCN_PayInfo]
            result = col.find({'AccID':account, 'IsProcess':1, 'EndOrderTime':0})
            if not result or result.count() == 0:
                # 没有充值订单
                return
            # 一单一单 恢复
            billInfo = result[0]
            orderID = billInfo['OrderID']
            col.update({"OrderID":orderID}, {"$set":{"IsProcess":0}})
            mylog.debug('RevoverBillProcess success...%s'%orderID)
        except:
            mylog.error("RevoverBillProcess error %s"%traceback.format_exc())
#    def RevoverBillProcess(self, db, accID):
#        try:
#            if not accID:
#                return
#            account = accID.strip(chr(0))
#            col = db[UCN_PayInfo]
#            result = col.find({'AccID':account, 'IsProcess':1, 'EndOrderTime':0})
#
#            if not result or result.count() == 0:
#                # 没有充值订单
#                return
#
#            for billInfo in result:
#                orderID = billInfo['OrderID']
#                col.update({"OrderID":orderID}, {"$set":{"IsProcess":0}})
#                mylog.debug('RevoverBillProcess success...%s'%orderID)
#        except:
#            mylog.error("RevoverBillProcess error %s"%traceback.format_exc())
    
    def SavePlayerMapServerData(self, db, saveData):
        playerRec = DataServerPlayerData.tagDBPlayer()
@@ -3002,7 +3084,7 @@
            #===================================================================
            
            #if "@test@" in accountRec.ACCID or "@yun@" in accountRec.ACCID or accountRec.RegIP == "127.0.0.1":
            if CommonDefine.IsDebug() or accountRec.RegIP == "127.0.0.1":
            if CommonDefine.IsDebug() or accountRec.RegIP == "127.0.0.1" or CommonDefine.IsInsideLogin():
                #debug内部服务器不验证token, 脱机挂玩家不验证
                mylog.debug('iner no check')
                pass
@@ -3282,7 +3364,9 @@
                    sendPack.MapID = GameWorld.GetGameWorld().GetMapID()
                    mylog.info('gstPlayerDetail Send OnCreate accid = %s'%(accountRec.ACCID))
                    NetPackCommon.SendPyPackToMapServerSelf(sendPack.GetBuffer(), sendPack.GetLength())
        if authAccID not in PyGameData.g_onlineAccIDList:
            PyGameData.g_onlineAccIDList.append(authAccID)
        return True
    
    def sendOKString(self, type, fromPacket, buf):
@@ -4138,7 +4222,6 @@
        for i in xrange(0, recCnt):
            rec.clear()
            readLen = rec.readData(data, pos, len(data))
            rec.dumpString()
            if -1 == readLen:
                mylog.error('%s.%s readData failed, index = %s'%(self.__class__.__name_, inspect.stack()[0][3], i) )
                raise CommonDefine.ShortBuf(collectionName)    #如果读取出错,后面全错了