fix practice mode no sound and score system during practice

This commit is contained in:
GitBluub
2024-01-17 02:14:56 +01:00
committed by Clément Le Bihan
parent 9fd70d3110
commit a24a960184
5 changed files with 44 additions and 11 deletions

View File

@@ -202,6 +202,8 @@ const PartitionMagic = ({
easing: Easing.inOut(Easing.ease),
}
);
if (playType == 'practice')
return;
cursor.notes.forEach((note) => {
piano.current?.start({
note: note.note,

View File

@@ -8,6 +8,7 @@ import Song from '../../models/Song';
import { useTheme } from 'native-base';
type PlayViewControlBarProps = {
playType: 'practice' | 'normal' | null;
song: Song;
time: number;
paused: boolean;
@@ -19,6 +20,7 @@ type PlayViewControlBarProps = {
};
const PlayViewControlBar = ({
playType,
song,
time,
paused,
@@ -107,6 +109,7 @@ const PlayViewControlBar = ({
gap: isPhone ? 10 : 25,
}}
>
{ playType != 'practice' &&
<IconButton
size="sm"
variant="solid"
@@ -117,7 +120,7 @@ const PlayViewControlBar = ({
name: paused ? 'play' : 'pause',
}}
onPress={paused ? onResume : onPause}
/>
/>}
<IconButton
size="sm"
colorScheme="coolGray"
@@ -162,7 +165,8 @@ const PlayViewControlBar = ({
minWidth: 120,
}}
>
<MetronomeControls paused={paused} bpm={bpm.current} />
{ playType != 'practice' &&
<MetronomeControls paused={paused} bpm={bpm.current} />}
</View>
</Row>
);

View File

@@ -32,6 +32,7 @@ type PlayViewProps = {
// this a hot fix this should be reverted soon
let scoroBaseApiUrl = process.env.EXPO_PUBLIC_SCORO_URL!;
let interval: NodeJS.Timeout;
if (process.env.NODE_ENV != 'development' && Platform.OS === 'web') {
Linking.getInitialURL().then((initUrl) => {
@@ -146,8 +147,16 @@ const PlayView = ({ songId }: PlayViewProps) => {
bearer: accessToken,
})
);
interval = setInterval(() => {
webSocket.current!.send(
JSON.stringify({
type: 'ping',
})
);
}, 15000);
};
webSocket.current.onclose = () => {
clearInterval(interval);
console.log('Websocket closed', endMsgReceived);
if (!endMsgReceived) {
toast.show({ description: 'Connection lost with Scorometer' });
@@ -165,25 +174,28 @@ const PlayView = ({ songId }: PlayViewProps) => {
toast.show({ description: 'Scoro: ' + data.error });
return;
}
if (data.type == 'pong')
return;
if (data.type == 'end') {
const maxPoints = data.score.max_score || 1;
const points = data.overallScore;
endMsgReceived = true;
webSocket.current?.close();
setScore(Math.floor((Math.max(points, 0) * 100) / maxPoints));
setEndResult({ songId: song.data!.id, ...data });
return;
}
if (data.type == 'step') {
console.log(data.timestamp)
setTime(data.timestamp)
//set idx += 1
return
}
console.log(data);
const points = data.info.score;
const maxPoints = data.info.max_score || 1;
setScore(Math.floor((Math.max(points, 0) * 100) / maxPoints));
if (data.type == 'step') {
setTime(data.timestamp)
return
}
let formattedMessage = '';
let messageColor: ColorSchemeType | undefined;
@@ -416,6 +428,7 @@ const PlayView = ({ songId }: PlayViewProps) => {
/>
</View>
<PlayViewControlBar
playType={playType}
score={score}
time={time}
paused={paused}

View File

@@ -17,6 +17,11 @@ class StartMessage(ValidatedDC):
mode: Literal["normal", "practice"]
type: Literal["start"] = "start"
@dataclass
class PingMessage(ValidatedDC):
type: Literal["ping"] = "ping"
@dataclass
class EndMessage(ValidatedDC):
@@ -52,6 +57,7 @@ message_map = {
"note_on": NoteOnMessage,
"note_off": NoteOffMessage,
"pause": PauseMessage,
"ping": PingMessage,
}
@@ -62,7 +68,8 @@ def getMessage() -> (
| NoteOnMessage
| NoteOffMessage
| PauseMessage
| InvalidMessage,
| InvalidMessage
| PingMessage,
str,
]
):

View File

@@ -17,6 +17,7 @@ from chroma_case.Message import (
NoteOffMessage,
NoteOnMessage,
PauseMessage,
PingMessage,
StartMessage,
getMessage,
)
@@ -215,6 +216,9 @@ class Scorometer:
def practiceCheck(self):
if self.to_play == self.keys_down_practice:
self.info["perfect"] += len(self.to_play)
self.info["score"] += 100 * len(self.to_play)
if len(self.practice_partition) == 0:
self.endGamePractice()
self.send({"type": "step", "timestamp": self.practice_partition[0][0].start + 1})
@@ -250,13 +254,16 @@ class Scorometer:
| NoteOnMessage
| NoteOffMessage
| PauseMessage
| InvalidMessage,
| InvalidMessage
| PingMessage,
line: str,
):
match message:
case InvalidMessage(error):
logging.warning(f"Invalid message {line} with error: {error}")
self.send({"error": f"Invalid message {line} with error: {error}"})
case PingMessage():
self.send({"type": "pong"})
case NoteOnMessage():
if self.mode == NORMAL:
self.handleNoteOn(message)