diff --git a/dev-proxy/index.ts b/dev-proxy/index.ts index 3402c5e..cad13ec 100644 --- a/dev-proxy/index.ts +++ b/dev-proxy/index.ts @@ -21,7 +21,7 @@ type DeviceQuestionData = { type ProxyOutput = | { ConnectPrompt: string } - | "WaitingForParty" + | { WaitingForParty: string } | { Question: DeviceQuestionData } | "Results" | { Error: string }; @@ -171,6 +171,7 @@ function connectApiSocket() { function toDeviceQuestionData(quizData: QuizState): DeviceQuestionData | null { if (!quizData.currentQuestion) return null; + if (quizData.status === "results") return null; const question = quizData.currentQuestion; const q_type = question.type === "choice" @@ -274,21 +275,25 @@ function handleApiMessage(e: MessageEvent) { if (event.type === "party_status") { const quizData = event.party?.data ?? null; if (!quizData) return; + if (quizData.status === "results") { + writeProxyOutput(socket, "Results"); + return; + } const question = toDeviceQuestionData(quizData); if (question) { writeProxyOutput(socket, { Question: question }); - } else if (quizData.status === "results") { - writeProxyOutput(socket, "Results"); } return; } if (event.type === "quiz_state") { + if (event.quiz.status === "results") { + writeProxyOutput(socket, "Results"); + return; + } const question = toDeviceQuestionData(event.quiz); if (question) { writeProxyOutput(socket, { Question: question }); - } else if (event.quiz.status === "results") { - writeProxyOutput(socket, "Results"); } return; } diff --git a/device-state/src/lib.rs b/device-state/src/lib.rs index 0b5f5f3..4fd66a8 100644 --- a/device-state/src/lib.rs +++ b/device-state/src/lib.rs @@ -172,6 +172,8 @@ impl DeviceState { self.wheel = future_wheel; } ProxyOutput::Results => { + self.question = None; + self.wheel = WheelData::empty(); self.view = ViewState::Results; } ProxyOutput::Error(_) => {} @@ -239,6 +241,13 @@ impl DeviceState { )); } + if self.view == ViewState::Results { + return Some(( + center_str::<16>("Quiz ended", 16).unwrap(), + center_str::<16>("See results", 16).unwrap(), + )); + } + if self.view != ViewState::Question { return None; } @@ -423,6 +432,25 @@ mod tests { state.apply_proxy_output(data); assert_eq!(state.view_state(), ViewState::Results); + let (line1, line2) = state.render_lines().unwrap(); + assert_eq!(line1.as_str(), " Quiz ended"); + assert_eq!(line2.as_str(), " See results"); + } + + #[test] + fn clears_question_state_when_results_arrive() { + let mut state = DeviceState::new(); + + state.apply_proxy_output( + parse_proxy_output( + r#"{"Question":{"text":"Q","points":1,"index":0,"q_type":"Choice"}}"#, + ) + .unwrap(), + ); + state.apply_proxy_output(parse_proxy_output(r#""Results""#).unwrap()); + + assert_eq!(state.view_state(), ViewState::Results); + assert!(state.question().is_none()); } #[test] diff --git a/esp32/src/main.rs b/esp32/src/main.rs index f9b9a7d..a2025a8 100644 --- a/esp32/src/main.rs +++ b/esp32/src/main.rs @@ -32,8 +32,8 @@ const WIFI_NETWORK: &str = "flamme"; const WIFI_PASSWORD: &str = "12345678"; const TARGET_IP: &str = "84.238.32.253"; const TARGET_PORT: u16 = 7070; -const WHEEL_PRECISION: i32 = 32; -const WHEEL_INVERTED: bool = false; +const WHEEL_PRECISION: i32 = 16; +const WHEEL_INVERTED: bool = true; const DEVICE_ID: &str = "esp32-1"; #[derive(Clone, Copy, Debug, Eq, PartialEq)]