Beispiel #1
0
 def createParam(self, clientForm: object) -> InitParam:
     form = clientForm["init"]
     return InitParam(
             ValueUtils.getInt(form, "compe_no"),
             str(form["member_id"]),
             ReceptLevel.parse(ValueUtils.toInt(form["recept_level"]))
             )
Beispiel #2
0
 def createParam(self, clientForm: object) -> SendMessageParam:
     form = clientForm["send_message"]
     return SendMessageParam(
             SendType.parse(ValueUtils.getInt(form, "send_type")),
             ValueUtils.getStr(form, "dest_member_id"),
             ValueUtils.getStr(form, "message"),
             ValueUtils.getStr(form, "stamp_id")
             )
Beispiel #3
0
 def validate(self, clientForm: object) -> ValidationInfo:
     info = ValidationInfo(self)
     form = ValueUtils.getAny(clientForm, "get_new_messages")
     if form == None:
         return info.addError("get_new_messagesパラメータは必須です。");
     count = ValueUtils.getStr(form, "count")
     if count == None:
         info.addError("countは必須です。");
     elif not ValueUtils.isNumeric(count):
         info.addError("countは数値を設定してください。");
     return info.valid();
Beispiel #4
0
 def on_message(self, message):
     sessionId = self.id
     if log.isDebug():
         self.logger.debug("セッションID:" + sessionId + ", メッセージ:" + message)
     try:
         form = json.loads(message)
     except ValueError:
         if log.isError():
             self.logger.error("クライアントパラメータのjson変換エラー セッションID:" +
                               sessionId + ", メッセージ:" + message)
         self.write_message(
             ServerResult.fromStatus(ResultStatus.ParamError).toJson())
         return
     method = MethodType.parse(ValueUtils.getInt(form, "method"))
     if method is None:
         if log.isError():
             self.logger.error("クライアントパラメータの処理タイプ未定義 セッションID:" + sessionId +
                               ", メッセージ:" + message)
         self.write_message(
             ServerResult.fromStatus(ResultStatus.MethodError).toJson())
         return
     service: ServiceBase = CompeChatHandler.getService(method)
     result = self.executeService(message, form, service)
     result.method = method.value
     self.write_message(result.toJson())
Beispiel #5
0
 def validate(self, clientForm: object) -> ValidationInfo:
     info = ValidationInfo(self)
     form = ValueUtils.getAny(clientForm, "init")
     if form is None:
         return info.addError("initパラメータは必須です。")
     compeNo = ValueUtils.getStr(form, "compe_no")
     if compeNo is None:
         info.addError("compe_noは必須です。")
     elif not ValueUtils.isNumeric(compeNo):
         info.addError("compe_noは数値を設定してください。");
     memberId = ValueUtils.getStr(form, "member_id")
     if memberId is None:
         info.addError("member_idは必須です。")
     receptLevel = ReceptLevel.parse(ValueUtils.getInt(form, "recept_level"))
     if receptLevel is None:
         info.addError("recept_levelは必須です。")
     return info.valid();
Beispiel #6
0
 def execute(self, sessionInfo: SessionInfo, param: SendMessageParam) -> ServerResult:
     compeNo = sessionInfo.compeNo
     memberId = sessionInfo.memberId
     sendType = param.send_type
     destMemberId = param.dest_member_id
     data = SendMessageData(
             None,
             sendType.value,
             compeNo,
             destMemberId,
             memberId,
             ValueUtils.getTimeInMillis(),
             param.message,
             param.stamp_id,
             False
             )
     with MessageDatRepository() as messageRepository:
         messageRepository.save(data);
     message = Serializable()
     message.send_type = data.send_type
     message.message_id = data.message_id
     message.compe_no = data.compe_no
     message.member_id = data.member_id
     message.time = data.time
     message.message = data.message
     with StampMstRepository() as stampRepository:
         stamp = stampRepository.findStamp(data.stamp_id)
         if stamp is not None:
             message.stamp = stamp.stamp_url
     otherResult = ServerResult.fromStatus(ResultStatus.Success)
     otherResult.messages = list()
     otherResult.messages.append(message)
     methodType = MethodType.GetMessagesFromSend
     otherResult.method = methodType.value
     otherMessage = otherResult.toJson()
     sendList: list[SessionInfo]
     if sendType == SendType.User:
         # 個人宛の場合、送信者と宛先のみ
         sendList = [
             SessionManager.getSessionInfo(compeNo, memberId),
             SessionManager.getSessionInfo(compeNo, destMemberId)
             ]
     elif sendType == SendType.All:
         sendList = SessionManager.getSessionInfos(compeNo)
     else:
         sendList = filter(lambda it:it.receptLevel == ReceptLevel.All, SessionManager.getSessionInfos(compeNo))
     for info in sendList:
         if info is None:
             continue
         info.session.write_message(otherMessage)
         if log.isDebug():
             self.logger.debug("Send message to others sessions id:" + info.session.id)
     return ServerResult.fromStatus(ResultStatus.Success)
Beispiel #7
0
 def validate(self, clientForm: object) -> ValidationInfo:
     info = ValidationInfo(self)
     form = ValueUtils.getAny(clientForm, "send_message")
     if form is None:
         return info.addError("send_messageパラメータは必須です。")
     if ValueUtils.isEmpty(ValueUtils.getStr(form, "message")) and ValueUtils.isEmpty(ValueUtils.getStr(form, "stamp_id")):
         info.addError("send_messageには、messageとstamp_idのいずれかが必須です。")
     # 送信先のパラメータは厳密にチェックし、誤送信をなるべく防ぐ。
     sendType = SendType.parse(ValueUtils.getInt(form, "send_type"))
     if sendType is None:
         return info.addError("send_message.send_typeが正しくありません。")
     destMemberId = ValueUtils.getStr(form, "dest_member_id")
     if sendType == SendType.All:
         if destMemberId is not None:
             return info.addError("全員に送信する場合は、send_message.dest_member_idをnullにしてください。")
     elif sendType == SendType.Compe:
         if destMemberId is not None:
             return info.addError("コンペ指定で送信する場合は、send_message.dest_member_idをnullにしてください。")
     elif sendType == SendType.User:
         if destMemberId is None:
             return info.addError("ユーザー指定で送信する場合は、send_message.dest_member_idは必須です。")
     return info.valid();
Beispiel #8
0
 def createParam(self, clientForm: object) -> GetMessagesParam:
     form = clientForm["get_messages"]
     return GetMessagesParam(
             ValueUtils.getInt(form, "before_time"),
             ValueUtils.getInt(form, "count")
             )
Beispiel #9
0
 def createParam(self, clientForm: object) -> GetNewMessagesParam:
     form = clientForm["get_new_messages"]
     return GetNewMessagesParam(
             ValueUtils.getInt(form, "count")
             )
import mysql.connector
import dataclasses
import configparser
from src.util import ValueUtils
from builtins import str
from src import log
from src.enums import ReceptLevel

inifile = configparser.ConfigParser()
inifile.read('./config.ini', 'UTF-8')
DB_HOST = inifile.get('db', 'host')
DB_PORT = ValueUtils.toInt(inifile.get('db', 'port'))
DB_USER = inifile.get('db', 'user')
DB_PASSWORD = inifile.get('db', 'password')
DB_SCHEMA = inifile.get('db', 'schema')


def get_connection() -> mysql.connector:
    return mysql.connector.connect(host=DB_HOST,
                                   port=DB_PORT,
                                   user=DB_USER,
                                   password=DB_PASSWORD,
                                   database=DB_SCHEMA,
                                   auth_plugin='mysql_native_password')


class RepositoryException(Exception):
    def __init__(self, message, *errors):
        Exception.__init__(self, message)
        self.errors = errors