#!/usr/bin/python  
 | 
# -*- coding: GBK -*-  
 | 
#-------------------------------------------------------------------------------  
 | 
#  
 | 
##@package Player.PlayerBackup  
 | 
#  
 | 
# @todo:Íæ¼Ò±¸µµ  
 | 
# @author hxp  
 | 
# @date 2022-12-06  
 | 
# @version 1.0  
 | 
#  
 | 
# ÏêϸÃèÊö: Íæ¼Ò±¸µµ  
 | 
#  
 | 
#-------------------------------------------------------------------------------  
 | 
#"""Version = 2022-12-06 18:30"""  
 | 
#-------------------------------------------------------------------------------  
 | 
  
 | 
import ChConfig  
 | 
import PlayerControl  
 | 
import ReadChConfig  
 | 
import GameWorld  
 | 
  
 | 
import shutil  
 | 
import time  
 | 
import os  
 | 
  
 | 
def CheckPlayerBackup(curPlayer):  
 | 
    PlayerBakRoot = ReadChConfig.GetPyMongoConfig("Backup", "PlayerBakRoot", isLog=False)  
 | 
    if not PlayerBakRoot:  
 | 
        #GameWorld.DebugLog("δÆôÓñ¸µµ")  
 | 
        return  
 | 
    curTime = int(time.time())  
 | 
    backupTime = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_BackupTime)  
 | 
    if not backupTime:  
 | 
        PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_BackupTime, curTime)  
 | 
        return  
 | 
      
 | 
    playerID = curPlayer.GetPlayerID()  
 | 
    BackupCDTimes = GameWorld.ToIntDef(ReadChConfig.GetPyMongoConfig("Backup", "BackupMinutes"))  
 | 
    if not BackupCDTimes or curTime - backupTime < BackupCDTimes * 60:  
 | 
        #GameWorld.DebugLog("±¸µµcdÖÐ, BackupCDTimes=%s,pass=%s" % (BackupCDTimes, curTime - backupTime), playerID)  
 | 
        return  
 | 
    PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_BackupTime, curTime)  
 | 
      
 | 
    PlayerBakDir = os.path.join(PlayerBakRoot, str(playerID))  
 | 
    BakCopyDir = os.path.join(PlayerBakDir, "Backup")  
 | 
    if not os.path.exists(PlayerBakDir):  
 | 
        os.makedirs(PlayerBakDir)  
 | 
        os.makedirs(BakCopyDir)  
 | 
          
 | 
    fileType = ReadChConfig.GetPyMongoConfig("Backup", "PlayerBakFileType", defaultValue=".pdbak")  
 | 
    moveList, copyList = [], []  
 | 
    for parent, _, filenames in os.walk(PlayerBakDir):  
 | 
        for filename in filenames:  
 | 
            if not filename.endswith(fileType):  
 | 
                continue  
 | 
            fullPath = os.path.join(parent, filename)  
 | 
            if parent == BakCopyDir:  
 | 
                bakTime = GameWorld.ToIntDef(filename[:filename.index(".")].split("_")[1])  
 | 
                copyList.append([bakTime, fullPath])  
 | 
            else:  
 | 
                if not os.path.exists(os.path.join(BakCopyDir, filename)):  
 | 
                    moveList.append(fullPath)  
 | 
                else:  
 | 
                    os.remove(fullPath)  
 | 
                      
 | 
    for filePath in moveList:  
 | 
        shutil.move(filePath, BakCopyDir)  
 | 
          
 | 
    copyList.sort()  
 | 
    BackupCopyMax = GameWorld.ToIntDef(ReadChConfig.GetPyMongoConfig("Backup", "BackupCopy")) # ±£Áô¾É±¸µµ·ÝÊý  
 | 
    delCopyCount = len(copyList) + len(moveList) - BackupCopyMax  
 | 
    for _, copyFilePath in copyList:  
 | 
        if delCopyCount > 0:  
 | 
            delCopyCount -= 1  
 | 
            os.remove(copyFilePath)  
 | 
        else:  
 | 
            break  
 | 
          
 | 
    # Íæ¼Ò±¸µµÎļþ¸ñʽ:  Íæ¼ÒID_±¸µµÊ±¼ä´Á.pb  
 | 
    pdBakPath = os.path.join(PlayerBakDir, "%s_%s%s" % (playerID, curTime, fileType))  
 | 
    curPlayer.RealTimeBackupSinglePlayerLogic(pdBakPath)  
 | 
    GameWorld.DebugLog("DoPlayerBackup: %s" % pdBakPath, playerID)  
 | 
    return  
 | 
  
 | 
  
 |