hxp
2025-05-21 acf66892de825190045053d1c7892b0ac5911d0a
ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/PyMongoDB/LogicProcess/UserCtrlDB.py
@@ -156,7 +156,7 @@
    lockPlayerID.release()#解锁
    return
def CallDispatchFamilyID(db, fromPack, type, self):
def CallDispatchFamilyID(db, self):
    global lockFamilyID
    lockFamilyID.acquire()#加锁    
    self.isDispatchingFamilyID = True
@@ -185,8 +185,11 @@
                    recs.append({'FamilyID':id})
                collection = db[UCN_Dispatch_FamilyID] 
                collection.insert(recs)
                #新分配,通知GameServer
                self.CallBackToSendFamilyIDPack(fromPack, type, minID, maxID)
                for newID in xrange(minID, maxID+1):
                    if newID not in self.m_emptyFamilyIDList:
                        self.m_emptyFamilyIDList.append(newID)
                mylog.info("m_emptyFamilyIDList len=%s"%len(self.m_emptyFamilyIDList))
        client.close()
    except Exception, e:
        mylog.error("IDDispatch  CallDispatchFamilyID Connect Exception %s"%e)
@@ -332,8 +335,9 @@
            self.translator = DBController.EncodeStringManipulator(DBConfig.encoding)
        #是否正在请求分配新PlayerID池
        self.isDispatchingPlayerID = False
        #是否正在请求分配新PlayerID池
        #是否正在请求分配新FamilyID池
        self.isDispatchingFamilyID = False
        self.m_emptyFamilyIDList = [] # 当前可用的仙盟ID列表
        self.loginHeap = 0
        self.loginStartTime = time()
        self.lastCleanMergePlayerTime = time()
@@ -410,12 +414,6 @@
        requestType, pos = CommFunc.ReadBYTE(pack.getBuffer(), pos)
        
        mylog.debug("begin process request type = %d"%requestType)
        if requestType == CommonDefine.gstDiapatchFamilyID:
            oFuncGrade = self.GetFuncGrade('gstDiapatchFamilyID')
            oFuncGrade.Start()
            self.OnDiapatchFamilyID(db, pack)
            oFuncGrade.End()
        
        if requestType == CommonDefine.gstGeneralDBOper:
            oFuncGrade = self.GetFuncGrade('gstGeneralDBOper')
@@ -822,55 +820,52 @@
        mylog.warning('%s not processed!'%pack.outputString())
        return True
    
    #发送通知GameServer可用的家族ID列表#tagDGCanUseFamilyIDList封包
    def CallBackToSendFamilyIDPack(self, fromPack, UpdateType, minID, maxID):
        sendPack = SendPackProtocol.tagDGCanUseFamilyIDList()
        sendPack.Type = CommonDefine.dgDiapatchFamilyID
        sendPack.UpdateType = UpdateType
        for id in range(minID, maxID + 1):
            sendPack.FamilyIDList.append(id)
        sendPack.Count = len(sendPack.FamilyIDList)
        self.sendString(fromPack, sendPack.GetBuffer())
    def OnFamilyIDInit(self, serverFamilyIDList):
        ## 仙盟ID初始化
        # @param serverFamilyIDList: 服务器已经创建的仙盟ID列表
        #初始化请求获取已分配的家族ID列表
        db = self.db
        collection = db[UCN_Dispatch_FamilyID]
        result = collection.find()
        if result:
            idCnt = result.count()
            mylog.debug('OnFamilyIDInit  db[UCN_Dispatch_FamilyID] idCnt=%s' % idCnt)
            for rec in result:
                familyID = rec['FamilyID']
                if familyID in serverFamilyIDList:
                    continue
                if familyID in self.m_emptyFamilyIDList:
                    continue
                self.m_emptyFamilyIDList.append(familyID)
        mylog.debug('OnFamilyIDInit  db[UCN_Dispatch_FamilyID] emptyIDCount=%s' % len(self.m_emptyFamilyIDList))
        self.__checkDiapatchFamilyID()
        return
    
    def OnDiapatchFamilyID(self,db, pack):
        buf = pack.getBuffer()
        recvPack = RecvPackProtocol.tagGDRequestCanUseFamilyIDList()
        pos = 0
        recvPack.ReadData(buf, pos)
        mylog.debug('OnDiapatchFamilyID pack = %s'%(recvPack.OutputString()))
        if recvPack.CallType == 0:
            #初始化请求获取已分配的家族ID列表
            collection = db[UCN_Dispatch_FamilyID]
            result = collection.find()
            mylog.debug('OnDiapatchFamilyID  db[UCN_Dispatch_FamilyID]  result = %s'%result)
            if not result or result.count() == 0:
                #未分配过  需要立即分配
                if self.isDispatchingFamilyID:
                    return
                import threading
                t = threading.Thread(target = CallDispatchFamilyID, args =(db, pack, recvPack.CallType, self))
                t.start()
                mylog.debug('CallType 0 need dispatch')
            else:
                sendPack = SendPackProtocol.tagDGCanUseFamilyIDList()
                sendPack.Type = CommonDefine.dgDiapatchFamilyID
                sendPack.UpdateType = recvPack.CallType
                for rec in result:
                    sendPack.FamilyIDList.append(rec['FamilyID'])
                sendPack.Count = len(sendPack.FamilyIDList)
                self.sendString(pack, sendPack.GetBuffer())
                mylog.debug('CallType 0 sendPack.FamilyIDList %s'%sendPack.OutputString())
        else:
            #请求添加新的家族ID
            if self.isDispatchingFamilyID:
                return
            import threading
            t = threading.Thread(target = CallDispatchFamilyID, args =(db, pack, recvPack.CallType, self))
            t.start()
            mylog.debug('CallType 1 need dispatch')
    def __checkDiapatchFamilyID(self):
        if len(self.m_emptyFamilyIDList) >= 10:
            return
        if self.isDispatchingFamilyID:
            return
        import threading
        t = threading.Thread(target = CallDispatchFamilyID, args =(self.db, self))
        t.start()
        return
    
    def GetNewFamilyID(self):
        ## 获取一个新仙盟ID
        self.__checkDiapatchFamilyID()
        if self.m_emptyFamilyIDList:
            newFamilyID = self.m_emptyFamilyIDList.pop(0)
            #mylog.debug('pop newFamilyID=%s, emptyIDCount=%s' % (newFamilyID, len(self.m_emptyFamilyIDList)))
            return newFamilyID
        return 0
    def FreeFamilyID(self, familyID):
        ## 释放可用的仙盟ID,一般是删除仙盟时返还
        if familyID not in self.m_emptyFamilyIDList:
            self.m_emptyFamilyIDList.insert(0, familyID)
            #mylog.debug('free familyID=%s, emptyIDCount=%s, %s' % (familyID, len(self.m_emptyFamilyIDList), self.m_emptyFamilyIDList))
        return
    
    def sendGameServerDBOperResult(self, sessionID, result, resultSet, errMsg):
        sendPack = SendPackProtocol.tagDGGeneralDBOperResult()