| #!/usr/bin/python  | 
| # -*- coding: GBK -*-  | 
| #-------------------------------------------------------------------------------  | 
| #  | 
| #-------------------------------------------------------------------------------  | 
| #  | 
| ##@package PlayerDataCollect  | 
| #  | 
| # @todo: Íæ¼ÒÊý¾Ý²É¼¯  | 
| # @author zhangd  | 
| # @date 2011-07-29 16:00  | 
| # @version 1.5  | 
| #  | 
| # ÏêϸÃèÊö:  | 
| # @change: "2011-08-02 18:00" zhangd Ð޸Ŀͻ§¶Ë·¢°üDwordÀàÐÍkey±»C++½âÎöʱǿתintºó±ä¸ºÊý£¬³öÏÖ±£´æÎÊÌâ  | 
| # @change: "2011-08-23 15:30" panwei ¼Ç¼ÐÅÏ¢ÐÂÔöKey¼Ç¼  | 
| # @change: "2011-08-24 10:47" panwei ¼Ç¼ÐÅÏ¢KeyÐÞ¸Ä, ÐÂÔöÉÏÏ߼ǼÐÅÏ¢  | 
| # @change: "2011-08-25 15:00" panwei É¾³ýIPY_GameServerNew  | 
| # @change: "2012-08-02 09:30" jiang ´æ´¢Â·¾¶ÐÞ¸Ä  | 
| #------------------------------------------------------------------------------   | 
| """Version = 2012-08-02 09:30"""  | 
| #------------------------------------------------------------------------------   | 
| import GameWorld  | 
| import ChConfig  | 
| import IPY_GameServer  | 
| import base64  | 
| import ctypes  | 
| import os  | 
| #------------------------------------------------------------------------------   | 
| ##µÇ½ÏìÓ¦  | 
| # @param curPlayer Íæ¼ÒʵÀý  | 
| # @return None  | 
| # @remarks   | 
| def OnLogin(curPlayer):  | 
|     saveData = __GetServerUserData(curPlayer)  | 
|     #¼Ç¼·þÎñÆ÷¶ËÐÅÏ¢  | 
|     userInfoCollection = GameWorld.GetGameWorld().GetUserInfoCollection()  | 
|     userInfoCollection.AddClientUserData(curPlayer.GetPlayerID(), len(saveData), saveData)  | 
|     return  | 
|   | 
| #===============================================================================  | 
| # //01 20 ¿Í»§¶ËÉ豸ÐÅÏ¢#tagCClientMachineNote   | 
| # struct tagCClientMachineNote   | 
| # {   | 
| # tagHead Head;   | 
| # DWORD Key; //µ±Ç°ÎªMAC   | 
| # WORD DataLen;   | 
| # char Data[DataLen];   | 
| # };   | 
| #===============================================================================  | 
| ##¿Í»§¶ËÉ豸ÐÅÏ¢  | 
| #  @param index  Íæ¼ÒÔÚGameServerÖеÄindex  | 
| #  @param tick   µ÷ÓÃʱµÄtick  | 
| #  @return None  | 
| def RecvClientMachineInfo(index, tick):  | 
|     recvPack = IPY_GameServer.IPY_CClientMachineNote()  | 
|   | 
|     userInfoCollection = GameWorld.GetGameWorld().GetUserInfoCollection()  | 
|     key = recvPack.GetKey()  | 
|       | 
|     #ÐÞ¸ÄC++·â°ü½âÎöʱDWORD±»Ç¿×ª³ÉintÀàÐÍʱ£¬Êý¾ÝΪ¸º,´íÎó  | 
|     if key < 0:  | 
|         key = ctypes.c_ulong(key).value  | 
|           | 
|     userInfoCollection.AddClientMachineData(key,   | 
|                                             recvPack.GetDataLen(),   | 
|                                             recvPack.GetData())  | 
|   | 
|     return  | 
|   | 
|   | 
| #===============================================================================  | 
| # //01 21 ¿Í»§¶ËÓû§ÐÅÏ¢#tagCClientUserNote   | 
| # struct tagCClientUserNote   | 
| # {   | 
| # tagHead Head;   | 
| # DWORD Key; //µ±Ç°¶¨ÒåË÷ÒýΪ½ÇÉ«ID   | 
| # WORD DataLen;   | 
| # char Data[DataLen];   | 
| # };   | 
| #===============================================================================  | 
| ##¿Í»§¶ËÓû§ÐÅÏ¢  | 
| #  @param index  Íæ¼ÒÔÚGameServerÖеÄindex  | 
| #  @param tick   µ÷ÓÃʱµÄtick  | 
| #  @return None  | 
| def RecvClientUserNote(index, tick):  | 
|     curPlayer = GameWorld.GetPlayerManager().GetPlayerByIndex(index)  | 
|     curPlayerID = curPlayer.GetPlayerID()  | 
|     #»ñÈ¡·â°ü²ÎÊý  | 
|     recvPack = IPY_GameServer.IPY_CClientUserNote()  | 
|     key = recvPack.GetKey()  | 
|       | 
|     #keyÅÐ¶Ï  | 
|     if key != curPlayerID:  | 
|         GameWorld.ErrLog("Íæ¼ÒÊý¾Ý²É¼¯£¬Óû§ÐÅÏ¢key(%s)²»ÎªÍæ¼ÒID(%s)"%(key, curPlayerID), curPlayerID)  | 
|         return  | 
|       | 
|     data = recvPack.GetData() + ';' + __GetServerUserData(curPlayer)  | 
|     #¼Ç¼  | 
|     userInfoCollection = GameWorld.GetGameWorld().GetUserInfoCollection()  | 
|     userInfoCollection.AddClientUserData(key, len(data), data)  | 
|     return  | 
|   | 
| #---------------------------------------------------------------------  | 
| ##·µ»Ø·þÎñÆ÷¶ËÐèÒª¼Ç¼µÄÄÚÈÝ  | 
| # @param curPlayer Íæ¼ÒʵÀý  | 
| # @return ×Ö·û´®  | 
| # @remarks   | 
| def __GetServerUserData(curPlayer):  | 
|     curPlayerIP = curPlayer.GetIP()  | 
|     curPlayerName = curPlayer.GetName()  | 
|     curPlayerLv = curPlayer.GetLV()  | 
|     curPlayerSex = curPlayer.GetSex()  | 
|     curPlayerCountry = curPlayer.GetCountry()  | 
|     curPlayerJob = curPlayer.GetJob()  | 
|     curPlayerMapID = curPlayer.GetMapID()  | 
|     curPlayerLineNO = curPlayer.GetLineNO()  | 
|       | 
|     serverUserData = "SDataVer:%s;IP:%s;Name:%s;Lv:%s;Sex:%s;Country:%s;Job:%s;MapID:%s;LineNO:%s"%\  | 
|                     (ChConfig.Def_DataCollect_Ver, curPlayerIP, curPlayerName, curPlayerLv,   | 
|                      curPlayerSex, curPlayerCountry, curPlayerJob, curPlayerMapID, curPlayerLineNO)  | 
|       | 
|     return serverUserData  | 
|   | 
| ##²É¼¯Íæ¼ÒÊý¾Ý  | 
| #@param curTimeStr -> 2009-03-05 16:00:00 type -> str  | 
| #@return ·µ»ØÖµÎÞÒâÒå  | 
| def CollectAndClearPlayerData(curTimeStr):  | 
|     userInfoCollection = GameWorld.GetGameWorld().GetUserInfoCollection()  | 
|       | 
|     # »úÆ÷Êý¾ÝÌõÊý  | 
|     machineDataCnt = userInfoCollection.GetClientMachineDatasCount()  | 
|       | 
|     # ±éÀú»úÆ÷Êý¾Ý¼Ç¼µ½log\\CollectFile\\Machine\\  | 
|     for index in range(machineDataCnt):  | 
|         machineData = userInfoCollection.GetClientMachineDataByIndex(index)  | 
|         LogPlayerData(machineData.GetKey(), machineData.GetData(), 0, curTimeStr)  | 
|           | 
|     # Çå¿Õ»úÆ÷Êý¾Ý  | 
|     userInfoCollection.ClearClientMachineDatas()  | 
|       | 
|     # Íæ¼ÒÊý¾ÝÌõÊý  | 
|     userDataCnt = userInfoCollection.GetClientUserDatasCount()   | 
|          | 
|     # ±éÀúÍæ¼ÒÊý¾Ý¼Ç¼µ½log\\CollectFile\\User\\  | 
|     for index in range(userDataCnt):  | 
|         userData = userInfoCollection.GetClientUserDataByIndex(index)  | 
|         LogPlayerData(userData.GetKey(), userData.GetData(), 1, curTimeStr)  | 
|       | 
|     # Çå¿ÕÍæ¼ÒÊý¾Ý  | 
|     userInfoCollection.ClearClientUserDatas()  | 
|        | 
|     return  | 
|           | 
|   | 
| ##¼ÇÂ¼Íæ¼ÒÊý¾Ýµ½log  | 
| #@param dataKey ÊýÖµ×Öµä  | 
| #@param dataStr Íæ¼ÒÊý¾Ý  | 
| #@param dataType Êý¾ÝÀàÐÍ0:»úÆ÷Êý¾Ý£¬1£ºÍæ¼ÒÊý¾Ý  | 
| #@param curTimeStr -> 2009-03-05 16:00:00  | 
| #@return ·µ»ØÖµÎÞÒâÒå  | 
| def LogPlayerData(dataKey, dataStr, dataType, curTimeStr):  | 
|     curTimeStr = curTimeStr.split()[0]  | 
|       | 
|     #ChinGameServer.exeËùÔÚµÄÂß¼ÅÌ·û  | 
|     diskPath = ChConfig.GetAppPath().split("\\")[0]  | 
|       | 
|     #0: »úÆ÷Êý¾Ý  | 
|     if dataType == 0:  | 
|         dataStr = __EncryptStr(dataStr)  | 
|         logPath = os.path.join(diskPath, "\\ServerCollectFile\\%s\\Machine"%curTimeStr)  | 
|   | 
|     #1£ºÍæ¼ÒÊý¾Ý  | 
|     else:  | 
|         dataStr = __EncryptStr('playerID:%s;'%(dataKey) + dataStr)  | 
|         logPath = os.path.join(diskPath, "\\ServerCollectFile\\%s\\User"%curTimeStr)  | 
|       | 
|     if not os.path.isdir(logPath):  | 
|         os.makedirs(logPath)  | 
|       | 
|     #дÈëÊý¾Ý  | 
|     logFileName = curTimeStr + ".log"  | 
|     logFile = os.path.join(logPath, logFileName)  | 
|     saveFile = file(logFile, "ab")  | 
|     saveFile.write(dataStr + "\r\n")  | 
|     saveFile.close()  | 
|     return  | 
|   | 
| ##¶Ô×Ö·û´®½øÐмÓÃÜ  | 
| #@param inStr Òª¼ÓÃܵÄ×Ö·û´®  | 
| #@return ·µ»ØÖµ¼ÓÃܺóµÄ×Ö·û´®      | 
| def __EncryptStr(inStr):  | 
|   | 
|     outStr = ""  | 
|     for char in inStr:  | 
|         #ÿ¸ö¶¼ÒªºÍkeyÒì»ò  | 
|         outStr += chr(ord(char) ^ ChConfig.Def_PlayerDataCollect_Key)  | 
|       | 
|     outStr = base64.b64encode(outStr)  | 
|       | 
|     return outStr  | 
|      |