|
|
|
#-*-coding utf-8-*-
|
|
|
|
# Общественное достояние, 2023, Алексей Безбородов (Alexei Bezborodov) <AlexeiBv+mirocod_platform_bot@narod.ru>
|
|
|
|
|
|
|
|
# Работа с сообщениями
|
|
|
|
|
|
|
|
class BotMessage:
|
|
|
|
def __init__(self, a_BotMessages, a_MessageName : str, a_MessageDesc : str, a_Language : str, a_PhotoID : str, a_DateTime):
|
|
|
|
self.m_BotMessages = a_BotMessages
|
|
|
|
self.m_MessageName = a_MessageName
|
|
|
|
self.m_MessageDesc = a_MessageDesc
|
|
|
|
self.m_Language = a_Language
|
|
|
|
self.m_PhotoID = a_PhotoID
|
|
|
|
self.m_DateTime = a_DateTime
|
|
|
|
|
|
|
|
def StaticCopy(self):
|
|
|
|
return BotMessage(None, self.m_MessageName, self.m_MessageDesc, self.m_Language, self.m_PhotoID, self.m_DateTime)
|
|
|
|
|
|
|
|
def GetName(self):
|
|
|
|
return self.m_MessageName
|
|
|
|
|
|
|
|
def GetDesc(self):
|
|
|
|
return self.m_MessageDesc
|
|
|
|
|
|
|
|
def UpdateDesc(self, a_Desc):
|
|
|
|
self.m_MessageDesc = a_Desc
|
|
|
|
|
|
|
|
def GetLanguage(self):
|
|
|
|
return self.m_Language
|
|
|
|
|
|
|
|
def GetPhotoID(self):
|
|
|
|
return self.m_PhotoID
|
|
|
|
|
|
|
|
def UpdatePhotoID(self, a_PhotoID):
|
|
|
|
self.m_PhotoID = a_PhotoID
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
msg = self.GetMessageForLang(self.m_Language)
|
|
|
|
return msg.GetDesc()
|
|
|
|
|
|
|
|
def GetCurLang(self, a_Language):
|
|
|
|
msg = self.m_BotMessages.GetMessages()
|
|
|
|
if not msg.get(a_Language, None):
|
|
|
|
a_Language = self.m_Language
|
|
|
|
if not msg.get(a_Language, None):
|
|
|
|
a_Language = self.m_BotMessages.a_DefaultLanguage
|
|
|
|
return a_Language
|
|
|
|
|
|
|
|
def FindMessageForLang(self, a_Name, a_Language):
|
|
|
|
if not self.m_BotMessages:
|
|
|
|
return None
|
|
|
|
msg = self.m_BotMessages.GetMessages()
|
|
|
|
a_Language = self.GetCurLang(a_Language)
|
|
|
|
if not msg.get(a_Language, None):
|
|
|
|
return None
|
|
|
|
new_msg = msg[a_Language].get(a_Name, None)
|
|
|
|
return new_msg
|
|
|
|
|
|
|
|
|
|
|
|
def GetMessageForLang(self, a_Language):
|
|
|
|
if not self.m_BotMessages:
|
|
|
|
return self
|
|
|
|
last_update = self.m_BotMessages.m_LastUpdate
|
|
|
|
new_msg = self
|
|
|
|
if self.m_DateTime < last_update:
|
|
|
|
a_Language = self.GetCurLang(a_Language)
|
|
|
|
new_msg = self.FindMessageForLang(self.m_MessageName, a_Language)
|
|
|
|
if not new_msg:
|
|
|
|
new_msg = self
|
|
|
|
elif a_Language == self.m_Language:
|
|
|
|
self.m_MessageDesc = new_msg.m_MessageDesc
|
|
|
|
self.m_Language = new_msg.m_Language
|
|
|
|
self.m_PhotoID = new_msg.m_PhotoID
|
|
|
|
self.m_DateTime = new_msg.m_DateTime
|
|
|
|
return new_msg
|
|
|
|
|
|
|
|
def MakeBotMessage(a_MessageDesc):
|
|
|
|
return BotMessage(None, '', a_MessageDesc, None, 0, None)
|
|
|
|
|
|
|
|
class BotMessages:
|
|
|
|
def __init__(self, a_DefaultLanguage):
|
|
|
|
self.m_DefaultLanguage = a_DefaultLanguage
|
|
|
|
self.m_Messages = {}
|
|
|
|
self.m_LastUpdate = None
|
|
|
|
|
|
|
|
def GetMessages(self):
|
|
|
|
return self.m_Messages
|
|
|
|
|
|
|
|
def UpdateSignal(self, a_DateTime):
|
|
|
|
self.m_LastUpdate = a_DateTime
|
|
|
|
|
|
|
|
def CreateMessage(self, a_MessageName, a_MessageDesc, a_DateTime, a_MessagePhotoID = 0, a_MessageLang = None):
|
|
|
|
if not a_MessageLang:
|
|
|
|
a_MessageLang = self.m_DefaultLanguage
|
|
|
|
cur_msg = BotMessage(self, a_MessageName, a_MessageDesc, a_MessageLang, a_MessagePhotoID, a_DateTime)
|
|
|
|
msg = self.GetMessages()
|
|
|
|
if not msg.get(a_MessageLang, None):
|
|
|
|
msg[a_MessageLang] = {}
|
|
|
|
msg[a_MessageLang][a_MessageName] = cur_msg
|
|
|
|
return cur_msg
|
|
|
|
|