diff --git a/src/apps/index.rs b/src/apps/index.rs index 80c6af1..1e18da5 100644 --- a/src/apps/index.rs +++ b/src/apps/index.rs @@ -1,3 +1,6 @@ +use heapless::Vec; +use pico_website::unwrap; + use crate::{ apps::Content, socket::{HttpRequestType, HttpResCode}, @@ -22,17 +25,50 @@ impl App for IndexApp { "html", Some(include_str!("./index.html").into()), ), - "/ttt" => ( - HttpResCode::Ok, - "html", - Some(include_str!("ttt.html").into()), - ), - "/ttt.js" => ( - HttpResCode::Ok, - "javascript", - Some(include_str!("ttt.js").into()), - ), - _ => (HttpResCode::NotFound, "", None), + path => { + let (path, args) = path.split_once('?').unwrap_or((path, "")); + let mut team = None; + for arg in args.split('&') { + match arg.split_once('=') { + Some(("team", "0")) => team = Some("0"), + Some(("team", "1")) => team = Some("1"), + _ => {} + } + } + if path == "/ttt" { + let Some(team) = team else { + return (HttpResCode::BadRequest, "", None); + }; + let html = include_str!("ttt.html"); + let mut content = Vec::new(); + let r: Result<(), &str> = try { + let (html1, html2) = html.split_once("/ttt.js").ok_or("")?; + + content.push(html1)?; + content.push("/ttt.js?team=")?; + content.push(team)?; + content.push(html2)?; + }; + unwrap(r).await; + (HttpResCode::Ok, "html", Some(Content(content))) + } else if path == "/ttt.js" { + let Some(team) = team else { + return (HttpResCode::BadRequest, "", None); + }; + + let mut content = Vec::new(); + let r: Result<(), &str> = try { + content.push("const team = ")?; + content.push(team)?; + content.push(";\n")?; + content.push(include_str!("ttt.js"))?; + }; + unwrap(r).await; + (HttpResCode::Ok, "javascript", Some(Content(content))) + } else { + (HttpResCode::NotFound, "", None) + } + } } } } diff --git a/src/apps/ttt.html b/src/apps/ttt.html index 4a2d0c5..6e05c56 100644 --- a/src/apps/ttt.html +++ b/src/apps/ttt.html @@ -29,7 +29,7 @@