#!/usr/bin/python
|
# -*- coding: GBK -*-
|
#-------------------------------------------------------------------------------
|
#
|
##@package GameWorldLogic.GameWorldEvent
|
#
|
# @todo:·þÎñÆ÷ÊÀ½çʼþ
|
# @author hxp
|
# @date 2025-05-15
|
# @version 1.0
|
#
|
# ÏêϸÃèÊö: ·þÎñÆ÷ÊÀ½çʼþ
|
#
|
#-------------------------------------------------------------------------------
|
#"""Version = 2025-05-15 10:35"""
|
#-------------------------------------------------------------------------------
|
|
import DBDataMgr
|
import GameWorld
|
import IpyGameDataPY
|
import PlayerEventCounter
|
import ShareDefine
|
import PlayerTeam
|
import PyGameData
|
import NetPackCommon
|
|
import datetime
|
import time
|
|
def Get_Server_Year(): return DBDataMgr.GetEventTrigMgr().GetValue(ShareDefine.Def_Server_Year)
|
def Set_Server_Year():
|
DBDataMgr.GetEventTrigMgr().SetValue(ShareDefine.Def_Server_Year, datetime.datetime.today().year)
|
return
|
def Get_Server_Month(): return DBDataMgr.GetEventTrigMgr().GetValue(ShareDefine.Def_Server_Month)
|
def Set_Server_Month():
|
DBDataMgr.GetEventTrigMgr().SetValue(ShareDefine.Def_Server_Month, datetime.datetime.today().month)
|
return
|
def Get_Server_Week(): return DBDataMgr.GetEventTrigMgr().GetValue(ShareDefine.Def_Server_Week)
|
def Set_Server_Week():
|
DBDataMgr.GetEventTrigMgr().SetValue(ShareDefine.Def_Server_Week, datetime.datetime.isocalendar(datetime.datetime.today())[1])
|
return
|
def Get_Server_Day(): return DBDataMgr.GetEventTrigMgr().GetValue(ShareDefine.Def_Server_Day)
|
def Set_Server_Day():
|
DBDataMgr.GetEventTrigMgr().SetValue(ShareDefine.Def_Server_Day, datetime.datetime.today().day)
|
return
|
def Get_Server_Hour(): return DBDataMgr.GetEventTrigMgr().GetValue(ShareDefine.Def_Server_Hour)
|
def Set_Server_Hour():
|
DBDataMgr.GetEventTrigMgr().SetValue(ShareDefine.Def_Server_Hour, datetime.datetime.today().hour)
|
return
|
|
def InitServerTime():
|
## ³õʼ»¯·þÎñÆ÷ʱ¼ä
|
if not Get_Server_Year():
|
Set_Server_Hour()
|
Set_Server_Day()
|
Set_Server_Week()
|
Set_Server_Month()
|
Set_Server_Year()
|
GameWorld.Log('###·þÎñÆ÷пª, ³õʼ»¯·þÎñÆ÷ʱ¼ä !')
|
|
return Get_Server_Hour(), Get_Server_Day(), Get_Server_Week(), Get_Server_Month(), Get_Server_Year()
|
|
#µ÷Óøú¯Êýʱ£¬C++ÒѾÉèÖúÃÇø·þ£¬Èç¹û¸Ä±äÇø·þÐèÖØÆô
|
def StartDB(tick):
|
#³õʼ»¯Êý¾Ý¿â, »Ö¸´±¸µµ£¨°´Çø·þ¼Ç¼£©£¬ ¼ÓÔØ¾²Ì¬±í
|
from PyMongoDB import PyMongoMain
|
PyMongoMain.StartDB()
|
InitGameWorld(tick)
|
return
|
|
def InitGameWorld(tick):
|
## GameWorld³õʼ»¯
|
if PyGameData.g_initGameTime:
|
return
|
serverID = GameWorld.GetGameWorld().GetServerID()
|
GameWorld.Log("·þÎñÆ÷Æô¶¯³õʼ»¯InitGameWorld: serverID=%s" % serverID)
|
DBDataMgr.OnServerStart() # ÓÅÏȼÓÔØ¹«¹²Êý¾Ý
|
LoadDBPlayer()
|
PyGameData.g_initGameTime = int(time.time()) # ·Åµ½¼ÓÔØÊý¾ÝÖ®ºó
|
|
#³õʼ»°¿ª·þʱ¼ä¡¢ÐÇÆÚ¼¸
|
initOpenServerTime = DBDataMgr.GetEventTrigMgr().GetValue(ShareDefine.Def_InitOpenServerTime)
|
openServerWeekday = DBDataMgr.GetEventTrigMgr().GetValue(ShareDefine.Def_OpenServerWeekday)
|
if initOpenServerTime <= 0 or openServerWeekday <= 0:
|
#¼Ç¼Ê״οª·¢Ê±¼ä(ÏÖʵʱ¼ä´Á)
|
SetInitOpenServerTime(initOpenServerTime if initOpenServerTime else int(time.time()))
|
|
#ºÏ·þÊ×´ÎÆô¶¯¼ÓÔØ´¦Àí
|
__DoMixServerInit()
|
|
# ÆäËû¹¦Äܳõʼ»¯
|
|
|
OnHighProcess(tick) # Ö÷¶¯´¥·¢Ò»´Î¸ß¾«¶ÈµÄµ÷Óã¬È·±£·þÎñÆ÷Æô¶¯ºóһЩ¹¦ÄܻµÈµÄÊý¾Ý»á±»¼ÓÔØ
|
OnMinute(tick)
|
|
GameWorld.Log("=============================== ·þÎñÆ÷Æô¶¯³É¹¦ ===================================")
|
isCrossServer = GameWorld.IsCrossServer()
|
if isCrossServer:
|
pass
|
else:
|
serverGroupID = GameWorld.GetServerGroupID()
|
GameWorld.Log("·þÎñÆ÷Æô¶¯³É¹¦: ServerGroupID=%s,serverID=%s" % (serverGroupID, serverID))
|
PyGameData.g_serverInitOK = True
|
return
|
|
def AllMapServerInitOK(tick):
|
return
|
|
def LoadDBPlayer():
|
if GameWorld.IsCrossServer():
|
return
|
#PlayerDBOper.FindDBOper(PlayerDBOper.Table_DBPlayer, {}, {"PlayerID":1, "AccID":1, "_id":0}, LoadDBPlayerRet)
|
return
|
|
def GMSetOpenServerTime(openServerTime):
|
'''GMÉèÖÿª·þʱ¼ä
|
@param openServerTime: ¿ª·þʱ¼ä´Á
|
@return: 1-³É¹¦£»-1-ÉèÖõÄʱ¼äÒì³££»-2-ÒѾÉèÖùýÇÒ¿ª·þÁË£¬²»ÄÜÖØ¸´ÉèÖÃ
|
'''
|
serverID = GameWorld.GetGameWorld().GetServerID()
|
curTime = int(time.time())
|
if openServerTime < curTime:
|
GameWorld.ErrLog("µ±Ç°Ê±¼äÒѾ³¬¹ýÉèÖõĿª·þʱ¼ä£¬²»ÄÜÉèÖÃ! serverID=%s" % serverID)
|
#GMCommon.GMCommandResult(orderId, gmCmdDict, GMCommon.Def_InvalidTime, "The set time is less than the current time.")
|
return -1
|
setOpenServerTime = DBDataMgr.GetEventTrigMgr().GetValue(ShareDefine.Def_SetOpenServerTime)
|
if setOpenServerTime and curTime > setOpenServerTime:
|
GameWorld.ErrLog("ÒѾÉèÖùýÇÒ¿ª·þÁË£¬²»ÄÜÖØ¸´ÉèÖÃ! serverID=%s" % serverID)
|
#GMCommon.GMCommandResult(orderId, gmCmdDict, GMCommon.Def_ServerAlreadyOpen, "The server has been opened.")
|
return -2
|
|
DBDataMgr.GetEventTrigMgr().SetValue(ShareDefine.Def_SetOpenServerTime, openServerTime)
|
GameWorld.Log("GMÉèÖÿª·þʱ¼ä£º serverID=%s,%s,%s" % (serverID, GameWorld.ChangeTimeNumToStr(openServerTime), openServerTime))
|
if openServerTime == curTime:
|
OnNewServerOpen(GameWorld.GetGameWorld().GetTick())
|
|
return 1
|
|
def DoCheckNewServerOpen(tick):
|
setOpenServerTime = DBDataMgr.GetEventTrigMgr().GetValue(ShareDefine.Def_SetOpenServerTime)
|
if not setOpenServerTime:
|
#GameWorld.DebugLog("ûÓÐÖ¸¶¨¿ª·þʱ¼ä£¡")
|
return
|
curOpenServerTime = DBDataMgr.GetEventTrigMgr().GetValue(ShareDefine.Def_InitOpenServerTime)
|
if curOpenServerTime == setOpenServerTime:
|
#GameWorld.DebugLog("ÒѾÕýʽ¿ª·þ¹ýÁË£¡")
|
return
|
curTime = int(time.time())
|
if curTime < setOpenServerTime:
|
#GameWorld.DebugLog("»¹Î´µ½Õýʽ¿ª·þʱ¼ä£¡")
|
return
|
OnNewServerOpen(tick)
|
return
|
|
def OnNewServerOpen(tick):
|
'''зþ¿ªÆôÐèÒª´¦ÀíµÄÂß¼'''
|
|
serverID = GameWorld.GetGameWorld().GetServerID()
|
GameWorld.Log("Ö´ÐÐÕýʽ¿ª·þÖØÖÃÂß¼... serverID=%s" % serverID)
|
|
setOpenServerTime = DBDataMgr.GetEventTrigMgr().GetValue(ShareDefine.Def_SetOpenServerTime)
|
if not setOpenServerTime:
|
return
|
|
#ÇåDBGSTrig
|
excludeList = [ShareDefine.Def_InitOpenServerTime, ShareDefine.Def_SetOpenServerTime]
|
DBDataMgr.GetEventTrigMgr().DelAllKey(excludeList)
|
#Çå¼Ò×å
|
DBDataMgr.GetFamilyMgr().DelAllFamily()
|
#ÇåÓʼþ
|
DBDataMgr.GetMailMgr().DelAllMail()
|
DBDataMgr.GetPlayerViewCacheMgr().DelAllCache()
|
|
SetInitOpenServerTime(setOpenServerTime)
|
AllMapServerInitOK(tick)
|
|
GameWorld.GetGameWorld().SaveGameServerData()
|
GameWorld.SendGameError("ClearOpenServerOK")
|
return
|
|
def SetInitOpenServerTime(initTime):
|
openDatetime = GameWorld.ChangeTimeNumToDatetime(initTime)
|
openServerWeekday = openDatetime.weekday() + 1
|
DBDataMgr.GetEventTrigMgr().SetValue(ShareDefine.Def_InitOpenServerTime, initTime)
|
DBDataMgr.GetEventTrigMgr().SetValue(ShareDefine.Def_OpenServerWeekday, openServerWeekday)
|
GameWorld.Log("ÉèÖÿª·þʱ¼ä: %s(%s), ÖÜ%s" % (openDatetime, initTime, openServerWeekday))
|
# ÐèÒªÕâ¸ö²ÎÊýÖ÷ÒªÊÇGM¹¤¾ß»áÅжÏÊÇ·ñÓÐÉèÖùý£¬ËùÒÔ¸ÃÖµÒ»°ãÖ»ÓÐGMÃüÁî»ò¹¤¾ß²ÅÄÜÉèÖÃ
|
setOpenServerTime = DBDataMgr.GetEventTrigMgr().GetValue(ShareDefine.Def_SetOpenServerTime)
|
if setOpenServerTime and setOpenServerTime != initTime:
|
DBDataMgr.GetEventTrigMgr().SetValue(ShareDefine.Def_SetOpenServerTime, initTime)
|
GameWorld.Log(" ͬ²½¸üРDef_SetOpenServerTime=%s" % initTime)
|
return
|
|
def __DoMixServerInit():
|
isMixServer = DBDataMgr.GetEventTrigMgr().GetValue(ShareDefine.Def_IsMixServer)
|
if not isMixServer:
|
GameWorld.DebugLog("·ÇºÏ·þ£¬²»´¦ÀíºÏ·þÊ×´ÎÆô¶¯Êý¾Ý¼ÓÔØ£¡")
|
return
|
|
isMixServerInitOK = DBDataMgr.GetEventTrigMgr().GetValue(ShareDefine.Def_IsMixServerInitOK)
|
if isMixServerInitOK:
|
GameWorld.DebugLog("ºÏ·þÊ×´ÎÆô¶¯¼ÓÔØÒѾ´¦Àí¹ý£¡")
|
return
|
|
GameWorld.Log("ºÏ·þÊ×´ÎÆô¶¯¼ÓÔØ´¦Àí...")
|
|
# Çå³ýÖ¸¶¨Í¨ÓÃÊý¾Ý£¬Ò»°ãÊǺϷþ¹¤¾ßɾ£¬ÕâÀï¿ÉÒÔÔÙ×ö¸ö²¹³ä
|
#universalRecMgr = GameWorld.GetUniversalRecMgr()
|
#delRecTypeList = []
|
#GameWorld.Log("Çå³ýÖ¸¶¨Í¨ÓÃÊý¾Ý delRecTypeList=%s" % str(delRecTypeList))
|
#for delRecType in delRecTypeList:
|
# universalRecMgr.Delete(delRecType)
|
|
# ɾ³ý¹ýÆÚµÄͨÓÃÊý¾Ý
|
#__DelOutofdayRecData(universalRecMgr)
|
|
# ÉèÖúϷþÊ×´ÎÆô¶¯¼ÓÔØ³É¹¦
|
DBDataMgr.GetEventTrigMgr().SetValue(ShareDefine.Def_IsMixServerInitOK, 1)
|
|
# ÉèÖñ¾´ÎºÏ·þʱµÄ¿ª·þÌìÊý¡¢ÊÀ½çµÈ¼¶
|
serverDay = DBDataMgr.GetEventTrigMgr().GetValue(ShareDefine.Def_ServerDay)
|
DBDataMgr.GetEventTrigMgr().SetValue(ShareDefine.Def_LastMixServerDay, serverDay)
|
GameWorld.Log(" ºÏ·þÊ×´ÎÆô¶¯¼ÓÔØ³É¹¦! serverDay=%s" % (serverDay))
|
return
|
|
def OnHighProcess(tick):
|
## ÿÃë·ÖÖÓÖ´ÐÐÒ»´Î
|
if not PyGameData.g_initGameTime:
|
return
|
if tick - PyGameData.g_highProcessTick < 1000:
|
return
|
PyGameData.g_highProcessTick = tick
|
OnMinute(tick)
|
|
# ÆäËûÂß¼
|
|
|
|
return
|
|
def OnMinute(tick):
|
## ÿÕû·ÖÖÓÖ´ÐÐÒ»´Î
|
curTime = GameWorld.GetCurrentTime()
|
curMinute = curTime.minute
|
if curMinute == PyGameData.g_minuteProcess:
|
return
|
PyGameData.g_minuteProcess = curMinute
|
|
#¼ì²é·þÎñÆ÷Õýʽ¿ª·þ
|
DoCheckNewServerOpen(tick)
|
DBDataMgr.OnMinute(curTime)
|
DoLogic_GameWorldEvenByTime(tick)
|
|
#ÆäËû¹¦ÄÜÂß¼
|
#GameWorldActionControl.Dispose_OperationActionState()
|
#GameWorldActionControl.Dispose_DailyActionState()
|
#GameWorldActionControl.Dispose_FBStateTime()
|
|
PlayerTeam.OnCheckTeamPlayerDisconnectTimeout(tick)
|
__CheckIpyDataRecycle(curTime)
|
return
|
|
def DoLogic_GameWorldEvenByTime(tick):
|
#-----------·þÎñÆ÷µ±Ç°Ê±¼ä
|
curTime = datetime.datetime.today()
|
curTimeHour = curTime.hour
|
curTimeDay = curTime.day
|
curTimeWeek = datetime.datetime.isocalendar(curTime)[1]
|
curTimeMonth = curTime.month
|
curTimeYear = curTime.year
|
|
#-----------·þÎñÆ÷¼Ç¼µÄʱ¼ä
|
serverHour , serverDay , serverWeek , serverMonth , serverYear = InitServerTime()
|
|
#-----------¿ªÊ¼±È¶Ôʱ¼ä
|
#OnHourʼþ
|
if (serverHour != curTimeHour or serverDay != curTimeDay or serverMonth != curTimeMonth or serverYear != curTimeYear):
|
Set_Server_Hour()
|
OnHour(tick)
|
|
#OnDayʼþ
|
if (serverDay != curTimeDay or serverMonth != curTimeMonth or serverYear != curTimeYear):
|
Set_Server_Day()
|
OnDay(tick)
|
|
#OnWeekʼþ
|
#²»¿¼ÂǸôÄêÇé¿ö£¬Èç2009,12,31Èչطþ->2010,12,31ÈÕ¿ª·þ(²»´¥·¢OnWeek)
|
if (serverWeek != curTimeWeek):
|
Set_Server_Week()
|
OnWeek(tick)
|
|
#OnMonthʼþ
|
if (serverMonth != curTimeMonth or serverYear != curTimeYear):
|
Set_Server_Month()
|
OnMonth(tick)
|
|
#OnYearʼþ
|
if serverYear != curTimeYear:
|
Set_Server_Year()
|
OnYear(tick)
|
|
return
|
|
def OnHour(tick):
|
GameWorld.Log("Server -> OnHour!")
|
PlayerEventCounter.DoLogic_OnHour(tick)
|
return
|
|
def OnDay(tick):
|
GameWorld.Log("Server -> OnDay!")
|
#É趨·þÎñÆ÷µ±Ç°Ììʼþ
|
eventMgr = DBDataMgr.GetEventTrigMgr()
|
eventMgr.SetValueIncrement(ShareDefine.Def_ServerDay)
|
isMixServer = eventMgr.GetValue(ShareDefine.Def_IsMixServer)
|
if isMixServer:
|
eventMgr.SetValueIncrement(ShareDefine.Def_MixServerDay)
|
|
PlayerEventCounter.DoLogic_OnDay(tick)
|
return
|
|
def OnWeek(tick):
|
GameWorld.Log("Server -> OnWeek!")
|
eventMgr = DBDataMgr.GetEventTrigMgr()
|
eventMgr.SetValueIncrement(ShareDefine.Def_ServerWeek)
|
|
PlayerEventCounter.DoLogic_OnWeek(tick)
|
return
|
|
def OnMonth(tick):
|
GameWorld.Log("Server -> OnMonth!")
|
PlayerEventCounter.DoLogic_OnMonth(tick)
|
return
|
|
def OnYear(tick):
|
GameWorld.Log("Server -> OnYear!")
|
PlayerEventCounter.DoLogic_OnYear(tick)
|
return
|
|
|
def __CheckIpyDataRecycle(timeNow):
|
## ¼ì²éIpyDataÊý¾Ý»ØÊÕ
|
playerCount = GameWorld.GetPlayerManager().OnlineCount()
|
if playerCount:
|
PyGameData.g_ipyDataRecycleCheckTime = 0
|
#GameWorld.DebugLog("µØÍ¼»¹ÓÐÍæ¼ÒÔÚÏß! playerCount=%s" % playerCount)
|
return
|
|
curTime = GameWorld.ChangeDatetimeToNum(timeNow)
|
if not PyGameData.g_ipyDataRecycleCheckTime:
|
PyGameData.g_ipyDataRecycleCheckTime = curTime
|
#GameWorld.DebugLog("µØÍ¼Ã»ÓÐÍæ¼ÒÔÚÏß")
|
return
|
|
if PyGameData.g_ipyDataRecycleCheckTime == 1:
|
# ÒѾ»ØÊÕÁË
|
#GameWorld.DebugLog("±¾´ÎÒѾ»ØÊÕ¹ýÁË")
|
return
|
|
if curTime - PyGameData.g_ipyDataRecycleCheckTime < 24 * 3600:
|
#GameWorld.DebugLog("»¹Î´µ½´ï»ØÊÕʱ¼ä, passSeconds=%s" % (curTime - PyGameData.g_ipyDataRecycleCheckTime))
|
return
|
PyGameData.g_ipyDataRecycleCheckTime = 1
|
IpyGameDataPY.IPYData.Recycle()
|
return
|
|
|
# ¹Ø±ÕµØÍ¼Í¨Öªpy´¦Àí
|
def CloseMap(tick):
|
from PyMongoDB.DBCommon import CommonDefine
|
from PyMongoDB.Common import CommFunc
|
#³õʼ»¯Êý¾Ý¿â, »Ö¸´±¸µµ£¨°´Çø·þ¼Ç¼£©£¬ ¼ÓÔØ¾²Ì¬±í
|
try:
|
pass
|
finally:
|
#»Ø·¢µØÍ¼ ´¦ÀíÍê³É,±ØÐë±»µ÷ÓõØÍ¼²ÅÄÜÕý³£¹Ø±Õ
|
data = ''
|
data = CommFunc.WriteBYTE(data, CommonDefine.dgPlayerSaveGameServerData)
|
NetPackCommon.SendPyPackToMapServerSelf(data, len(data))
|
GameWorld.Log("python ÒÑ´¦ÀíµØÍ¼¹Ø±ÕÂß¼")
|
return
|