1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/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
    
    if not GameWorld.IsNormalPlayer(curPlayer):
        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