From acf66892de825190045053d1c7892b0ac5911d0a Mon Sep 17 00:00:00 2001
From: hxp <ale99527@vip.qq.com>
Date: 星期三, 21 五月 2025 09:31:15 +0800
Subject: [PATCH] 16 卡牌服务端(仙盟ID分配优化;)

---
 ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/GM/Commands/CreateFamily.py          |    2 
 ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/Player/PlayerFamily.py               |    4 
 ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/ShareDefine.py                       |    1 
 ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/DB/StructData/DBFamily.py            |   11 +++
 ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/PyMongoDB/LogicProcess/UserCtrlDB.py |  105 ++++++++++++++++------------------
 5 files changed, 64 insertions(+), 59 deletions(-)

diff --git a/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/DB/StructData/DBFamily.py b/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/DB/StructData/DBFamily.py
index c8bf380..9ef6384 100644
--- a/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/DB/StructData/DBFamily.py
+++ b/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/DB/StructData/DBFamily.py
@@ -20,6 +20,7 @@
 import GameWorld
 import ShareDefine
 import PlayerControl
+import PyMongoMain
 import DBDataMgr
 import ChConfig
 import DBComm
@@ -430,9 +431,14 @@
         self.__familyList.sort(key=lambda f: (f.GetFightPowerTotal(), f.GetLV()), reverse=True)
         return
     
-    def AddFamily(self, familyID, familyName, serverID):
+    def AddFamily(self, familyName, serverID, familyID=None):
         ## 创建新仙盟
         newFamily = None
+        if familyID == None:
+            familyID = PyMongoMain.GetUserCtrlDB().GetNewFamilyID()
+            if familyID <= 0:
+                GameWorld.ErrLog("创建仙盟时生成新ID异常!")
+                return newFamily
         if familyID in self.__familyIDDict:
             GameWorld.ErrLog("创建仙盟时ID已存在! familyID=%s" % familyID)
             return newFamily
@@ -478,6 +484,8 @@
                 self.__familyList.remove(family)
             family.OnDelete()
         self.__familyIDDict.pop(familyID, None)
+        if familyID > ShareDefine.FackFamilyIDMax:
+            PyMongoMain.GetUserCtrlDB().FreeFamilyID(familyID) # 归还仙盟ID,重复使用
         return family
     
     def DelAllFamily(self):
@@ -628,6 +636,7 @@
             action = self.__actionMgr.GetFamilyAction(familyID, actionType)
             action.InitActionInstance(dbData)
             
+        PyMongoMain.GetUserCtrlDB().OnFamilyIDInit(self.__familyIDDict.keys())
         return pos
     
 def OnMinute():
diff --git a/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/GM/Commands/CreateFamily.py b/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/GM/Commands/CreateFamily.py
index bd743ce..4b997da 100644
--- a/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/GM/Commands/CreateFamily.py
+++ b/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/GM/Commands/CreateFamily.py
@@ -188,7 +188,7 @@
             break
         
         fackFamilyName = "%s%s" % (FakeFamilyName, fackFamilyID)
-        fackFamily = familyMgr.AddFamily(fackFamilyID, fackFamilyName, serverID)
+        fackFamily = familyMgr.AddFamily(fackFamilyName, serverID, fackFamilyID)
         fackFamilyID += 1
         if not fackFamily:
             continue
diff --git a/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/Player/PlayerFamily.py b/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/Player/PlayerFamily.py
index 6e63e0f..6b434b8 100644
--- a/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/Player/PlayerFamily.py
+++ b/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/Player/PlayerFamily.py
@@ -194,12 +194,12 @@
         if not PlayerControl.PayMoney(curPlayer, moneyType, needMoney, "CreateFamily"):
             return
         
-    newFamilyID = random.randint(10000, 20000) # 先随机,之后替换接口
     serverID = GameWorld.GetPlayerServerID(curPlayer)
-    curFamily = familyMgr.AddFamily(newFamilyID, fullFamilyName, serverID)
+    curFamily = familyMgr.AddFamily(fullFamilyName, serverID)
     if curFamily == None:
         GameWorld.ErrLog("创建家族失败", playerID)
         return
+    newFamilyID = curFamily.GetID()
     curFamily.SetLV(1)
     emblemIDList = PlayerFamilyEmblem.GetDefaultFamilyEmblemIDList()
     if not emblemID or emblemID not in emblemIDList:
diff --git a/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/PyMongoDB/LogicProcess/UserCtrlDB.py b/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/PyMongoDB/LogicProcess/UserCtrlDB.py
index 6459dcf..e3f5a6a 100644
--- a/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/PyMongoDB/LogicProcess/UserCtrlDB.py
+++ b/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()
diff --git a/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/ShareDefine.py b/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/ShareDefine.py
index 3695710..fe1fb61 100644
--- a/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/ShareDefine.py
+++ b/ServerPython/ZoneServerGroup/map1_8G/MapServer/MapServerData/Script/ShareDefine.py
@@ -62,6 +62,7 @@
 #支持超20亿数值的数值点单位数值, 1亿
 Def_PerPointValue = 100000000
 
+#真实玩家ID、仙盟ID是从100000开始
 #假玩家ID
 FackPlayerIDStart = 1
 FackPlayerIDMax = 9999

--
Gitblit v1.8.0