use the info object instead of class properties

This commit is contained in:
GitBluub
2023-04-15 22:48:38 +09:00
parent 7b20792a51
commit c45f425a5d
2 changed files with 52 additions and 58 deletions

View File

@@ -7,72 +7,72 @@ from validated_dc import ValidatedDC, get_errors, is_valid
@dataclass
class InvalidMessage:
message: str
message: str
@dataclass
class StartMessage(ValidatedDC):
id: int
bearer: str
mode: Literal["normal", "practice"]
type: Literal["start"] = "start"
id: int
bearer: str
mode: Literal["normal", "practice"]
type: Literal["start"] = "start"
@dataclass
class EndMessage(ValidatedDC):
type: Literal["end"] = "end"
type: Literal["end"] = "end"
@dataclass
class NoteOnMessage(ValidatedDC):
time: int
note: int
id: int
type: Literal["note_on"] = "note_on"
time: int
note: int
id: int
type: Literal["note_on"] = "note_on"
@dataclass
class NoteOffMessage(ValidatedDC):
time: int
note: int
id: int
type: Literal["note_off"] = "note_off"
time: int
note: int
id: int
type: Literal["note_off"] = "note_off"
@dataclass
class PauseMessage(ValidatedDC):
paused: bool
time: int
type: Literal["pause"] = "pause"
paused: bool
time: int
type: Literal["pause"] = "pause"
message_map = {
"start": StartMessage,
"end": EndMessage,
"note_on": NoteOnMessage,
"note_off": NoteOffMessage,
"pause": PauseMessage,
"start": StartMessage,
"end": EndMessage,
"note_on": NoteOnMessage,
"note_off": NoteOffMessage,
"pause": PauseMessage,
}
def getMessage() -> (
Tuple[
StartMessage
| EndMessage
| NoteOnMessage
| NoteOffMessage
| PauseMessage
| InvalidMessage,
str,
]
Tuple[
StartMessage
| EndMessage
| NoteOnMessage
| NoteOffMessage
| PauseMessage
| InvalidMessage,
str,
]
):
try:
msg = input()
obj = json.loads(msg)
res = message_map[obj["type"]](**obj)
if is_valid(res):
return res, msg
else:
return InvalidMessage(str(get_errors(res))), msg
except Exception as e:
return InvalidMessage(str(e)), ""
try:
msg = input()
obj = json.loads(msg)
res = message_map[obj["type"]](**obj)
if is_valid(res):
return res, msg
else:
return InvalidMessage(str(get_errors(res))), msg
except Exception as e:
return InvalidMessage(str(e)), ""

View File

@@ -4,7 +4,6 @@ import json
import logging
import operator
import os
import select
import sys
from typing import TypedDict
@@ -55,11 +54,6 @@ class Scorometer:
self.mode: int = mode
self.song_id: int = song_id
self.user_id: int = user_id
self.score: int = 0
self.missed: int = 0
self.perfect: int = 0
self.great: int = 0
self.good: int = 0
self.wrong_ids = []
self.difficulties = {}
self.info: ScoroInfo = {
@@ -130,7 +124,7 @@ class Scorometer:
logging.debug({"note_on": f"{perf} on {message.note}"})
self.send({"type": "timing", "id": message.id, "timing": perf})
else:
self.score -= 50
self.info["score"] -= 50
self.wrong_ids += [message.id]
logging.debug({"note_on": f"wrong key {message.note}"})
self.send({"type": "timing", "id": message.id, "timing": "wrong"})
@@ -158,7 +152,7 @@ class Scorometer:
)
if to_play:
perf = self.getDurationScore(key, to_play)
self.score += (
self.info["score"] += (
100
if perf == "perfect"
else 75
@@ -216,7 +210,7 @@ class Scorometer:
)
if keys_to_play is None:
logging.info("Invalid key.")
self.score -= 50
self.info["score"] -= 50
# TODO: I dont think this if is right
# self.sendScore(message.id, "wrong key", "wrong key")
return
@@ -296,16 +290,16 @@ class Scorometer:
def endGame(self):
for i in self.partition.notes:
if i.done is False:
self.score -= 50
self.missed += 1
self.info["score"] -= 50
self.info["missed"] += 1
send(
{
"overallScore": self.score,
"overallScore": self.info["score"],
"score": {
"missed": self.missed,
"good": self.good,
"great": self.great,
"perfect": self.perfect,
"missed": self.info["missed"],
"good": self.info["good"],
"great": self.info["great"],
"perfect": self.info["perfect"],
"maxScore": len(self.partition.notes) * 100,
},
}
@@ -316,7 +310,7 @@ class Scorometer:
json={
"songID": self.song_id,
"userID": self.user_id,
"score": self.score,
"score": self.info["score"],
"difficulties": self.difficulties,
},
)