This commit is contained in:
Arkitu 2025-04-07 18:49:18 +02:00
parent 5b827c75f2
commit 8956acfcb4
3 changed files with 5299 additions and 99 deletions

View File

@ -108,15 +108,18 @@ async fn main(spawner: Spawner) {
control.start_ap_wpa2("cyw43", "password", 5).await; control.start_ap_wpa2("cyw43", "password", 5).await;
spawner.spawn(listen_task(stack, Team::Zero, 80)).unwrap(); spawner.spawn(listen_task(stack, Team::Zero, 80)).unwrap();
// spawner.spawn(listen_task(stack, Team::One, 81)).unwrap(); spawner.spawn(listen_task(stack, Team::One, 81)).unwrap();
} }
static current_turn: AtomicBool = AtomicBool::new(false); static TURN: AtomicBool = AtomicBool::new(false);
// bits [0; 8] : player zero board / bits [9; 17] : player one board // bits [0; 8] : player zero board / bits [9; 17] : player one board
static board_atom: AtomicU32 = AtomicU32::new(0); static BOARD: AtomicU32 = AtomicU32::new(0);
#[embassy_executor::task(pool_size = 2)] #[embassy_executor::task(pool_size = 2)]
async fn listen_task(stack: Stack<'static>, team: Team, port: u16) { async fn listen_task(stack: Stack<'static>, team: Team, port: u16) {
loop {
info!("team:{:?}", team);
}
let mut rx_buffer = [0; 4096]; let mut rx_buffer = [0; 4096];
let mut tx_buffer = [0; 4096]; let mut tx_buffer = [0; 4096];
let mut buf = [0; 4096]; let mut buf = [0; 4096];
@ -216,30 +219,26 @@ async fn listen_task(stack: Stack<'static>, team: Team, port: u16) {
include_bytes!("../web/htmx.js"), include_bytes!("../web/htmx.js"),
), ),
p => 'res: { p => 'res: {
if p.starts_with("/ttt/cell") && p.len() == 10 { if (p.starts_with("/ttt/cell") && p.len() == 10) || p == "/ttt/board" {
let clicked_c: Cell = let mut board = BOARD.load(Ordering::Acquire);
match TryInto::<Cell>::try_into(p.chars().nth(9).unwrap()) {
Ok(c) => c, if p.starts_with("/ttt/cell") {
Err(_) => break 'res (HttpResCode::NotFound, "", &[]), let clicked_c: Cell =
}; match TryInto::<Cell>::try_into(p.chars().nth(9).unwrap()) {
let mut board = board_atom.load(Ordering::Acquire); Ok(c) => c,
if board Err(_) => break 'res (HttpResCode::NotFound, "", &[]),
& ((2_u32.pow(clicked_c as u32)) + (2_u32.pow(9 + clicked_c as u32))) };
!= 0 if board
{ & ((2_u32.pow(clicked_c as u32))
break 'res (HttpResCode::Forbidden, "", &[]); + (2_u32.pow(9 + clicked_c as u32)))
!= 0
{
break 'res (HttpResCode::Forbidden, "", &[]);
}
board = board | 2_u32.pow((team as u32 * 9) + clicked_c as u32);
BOARD.store(board, Ordering::Release);
} }
info!("{:?}", clicked_c);
info!("{:?}", clicked_c as u32);
info!("{:?}", team as u32 * 9);
info!("{:?}", 2_u32.pow((team as u32 * 9) + clicked_c as u32));
info!(
"{:?}",
board | 2_u32.pow((team as u32 * 9) + clicked_c as u32)
);
board = board | 2_u32.pow((team as u32 * 9) + clicked_c as u32);
board_atom.store(board, Ordering::Release);
info!("board : {}", board);
res_buf.clear(); res_buf.clear();
for c in 0..=8 { for c in 0..=8 {
let picked_by = if board & 2_u32.pow(c) != 0 { let picked_by = if board & 2_u32.pow(c) != 0 {
@ -249,28 +248,26 @@ async fn listen_task(stack: Stack<'static>, team: Team, port: u16) {
} else { } else {
None None
}; };
info!("{} : {:?}", c, picked_by);
match picked_by { match picked_by {
Some(Team::Zero) => { Some(Team::Zero) => {
res_buf res_buf
.extend_from_slice( .extend_from_slice(
b"<div class=\"cell\" style=\"color:blue\"></div>", b"<div class=\"cell\" style=\"background-color:blue\"></div>",
) )
.unwrap(); .unwrap();
} }
Some(Team::One) => { Some(Team::One) => {
res_buf res_buf.extend_from_slice(
.extend_from_slice( b"<div class=\"cell\" style=\"background-color:red\"></div>",
b"<div class=\"cell\" style=\"color:red\"></div>",
) )
.unwrap(); .unwrap();
} }
None => { None => {
write!( write!(
&mut res_buf, &mut res_buf,
"<button class=\"cell\" hx-post=\"/ttt/cell{}\" hx-trigger=\"click\" hx-target=\"#grid\" hx-swap=\"innerHTML\"></button>", "<button class=\"cell\" hx-post=\"/ttt/cell{}\" hx-trigger=\"click\" hx-target=\"#grid\" hx-swap=\"innerHTML\"></button>",
c c
).unwrap(); ).unwrap();
} }
}; };
} }

5261
web/htmx.js Normal file

File diff suppressed because it is too large Load Diff

View File

@ -19,69 +19,11 @@
<html> <html>
<body> <body>
<h1>TicTacToe</h1> <h1>TicTacToe</h1>
<div id="grid"> <div
<button id="grid"
class="cell" hx-post="/ttt/board"
hx-post="/ttt/cell0" hx-swap="innerHTML"
hx-target="#grid" hx-trigger="load"
hx-swap="innerHTML" ></div>
></button>
<button
class="cell"
hx-post="/ttt/cell1"
hx-trigger="click"
hx-target="#grid"
hx-swap="innerHTML"
></button>
<button
class="cell"
hx-post="/ttt/cell2"
hx-trigger="click"
hx-target="#grid"
hx-swap="innerHTML"
></button>
<button
class="cell"
hx-post="/ttt/cell3"
hx-trigger="click"
hx-target="#grid"
hx-swap="innerHTML"
></button>
<button
class="cell"
hx-post="/ttt/cell4"
hx-trigger="click"
hx-target="#grid"
hx-swap="innerHTML"
></button>
<button
class="cell"
hx-post="/ttt/cell5"
hx-trigger="click"
hx-target="#grid"
hx-swap="innerHTML"
></button>
<button
class="cell"
hx-post="/ttt/cell6"
hx-trigger="click"
hx-target="#grid"
hx-swap="innerHTML"
></button>
<button
class="cell"
hx-post="/ttt/cell7"
hx-trigger="click"
hx-target="#grid"
hx-swap="innerHTML"
></button>
<button
class="cell"
hx-post="/ttt/cell8"
hx-trigger="click"
hx-target="#grid"
hx-swap="innerHTML"
></button>
</div>
</body> </body>
</html> </html>