New file |
| | |
| | | #!/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
|
| | |
|
| | |
|