提交 | 用户 | age
|
a972cc
|
1 |
#!/usr/bin/python
|
H |
2 |
# -*- coding: GBK -*-
|
|
3 |
#-------------------------------------------------------------------------------
|
|
4 |
#
|
|
5 |
##@package Player.PlayerBackup
|
|
6 |
#
|
|
7 |
# @todo:玩家备档
|
|
8 |
# @author hxp
|
|
9 |
# @date 2022-12-06
|
|
10 |
# @version 1.0
|
|
11 |
#
|
|
12 |
# 详细描述: 玩家备档
|
|
13 |
#
|
|
14 |
#-------------------------------------------------------------------------------
|
|
15 |
#"""Version = 2022-12-06 18:30"""
|
|
16 |
#-------------------------------------------------------------------------------
|
|
17 |
|
|
18 |
import ChConfig
|
|
19 |
import PlayerControl
|
|
20 |
import ReadChConfig
|
|
21 |
import GameWorld
|
|
22 |
|
|
23 |
import shutil
|
|
24 |
import time
|
|
25 |
import os
|
|
26 |
|
|
27 |
def CheckPlayerBackup(curPlayer):
|
|
28 |
PlayerBakRoot = ReadChConfig.GetPyMongoConfig("Backup", "PlayerBakRoot", isLog=False)
|
|
29 |
if not PlayerBakRoot:
|
|
30 |
#GameWorld.DebugLog("未启用备档")
|
|
31 |
return
|
b2e6e0
|
32 |
|
59c0f7
|
33 |
if not GameWorld.IsNormalPlayer(curPlayer):
|
b2e6e0
|
34 |
return
|
H |
35 |
|
a972cc
|
36 |
curTime = int(time.time())
|
H |
37 |
backupTime = curPlayer.NomalDictGetProperty(ChConfig.Def_PDict_BackupTime)
|
|
38 |
if not backupTime:
|
|
39 |
PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_BackupTime, curTime)
|
|
40 |
return
|
|
41 |
|
|
42 |
playerID = curPlayer.GetPlayerID()
|
|
43 |
BackupCDTimes = GameWorld.ToIntDef(ReadChConfig.GetPyMongoConfig("Backup", "BackupMinutes"))
|
|
44 |
if not BackupCDTimes or curTime - backupTime < BackupCDTimes * 60:
|
|
45 |
#GameWorld.DebugLog("备档cd中, BackupCDTimes=%s,pass=%s" % (BackupCDTimes, curTime - backupTime), playerID)
|
|
46 |
return
|
|
47 |
PlayerControl.NomalDictSetProperty(curPlayer, ChConfig.Def_PDict_BackupTime, curTime)
|
|
48 |
|
|
49 |
PlayerBakDir = os.path.join(PlayerBakRoot, str(playerID))
|
|
50 |
BakCopyDir = os.path.join(PlayerBakDir, "Backup")
|
|
51 |
if not os.path.exists(PlayerBakDir):
|
|
52 |
os.makedirs(PlayerBakDir)
|
|
53 |
os.makedirs(BakCopyDir)
|
|
54 |
|
|
55 |
fileType = ReadChConfig.GetPyMongoConfig("Backup", "PlayerBakFileType", defaultValue=".pdbak")
|
|
56 |
moveList, copyList = [], []
|
|
57 |
for parent, _, filenames in os.walk(PlayerBakDir):
|
|
58 |
for filename in filenames:
|
|
59 |
if not filename.endswith(fileType):
|
|
60 |
continue
|
|
61 |
fullPath = os.path.join(parent, filename)
|
|
62 |
if parent == BakCopyDir:
|
|
63 |
bakTime = GameWorld.ToIntDef(filename[:filename.index(".")].split("_")[1])
|
|
64 |
copyList.append([bakTime, fullPath])
|
|
65 |
else:
|
|
66 |
if not os.path.exists(os.path.join(BakCopyDir, filename)):
|
|
67 |
moveList.append(fullPath)
|
|
68 |
else:
|
|
69 |
os.remove(fullPath)
|
|
70 |
|
|
71 |
for filePath in moveList:
|
|
72 |
shutil.move(filePath, BakCopyDir)
|
|
73 |
|
|
74 |
copyList.sort()
|
|
75 |
BackupCopyMax = GameWorld.ToIntDef(ReadChConfig.GetPyMongoConfig("Backup", "BackupCopy")) # 保留旧备档份数
|
|
76 |
delCopyCount = len(copyList) + len(moveList) - BackupCopyMax
|
|
77 |
for _, copyFilePath in copyList:
|
|
78 |
if delCopyCount > 0:
|
|
79 |
delCopyCount -= 1
|
|
80 |
os.remove(copyFilePath)
|
|
81 |
else:
|
|
82 |
break
|
|
83 |
|
|
84 |
# 玩家备档文件格式: 玩家ID_备档时间戳.pb
|
|
85 |
pdBakPath = os.path.join(PlayerBakDir, "%s_%s%s" % (playerID, curTime, fileType))
|
|
86 |
curPlayer.RealTimeBackupSinglePlayerLogic(pdBakPath)
|
|
87 |
GameWorld.DebugLog("DoPlayerBackup: %s" % pdBakPath, playerID)
|
|
88 |
return
|
|
89 |
|
|
90 |
|