save
This commit is contained in:
parent
5b827c75f2
commit
8956acfcb4
67
src/main.rs
67
src/main.rs
@ -108,15 +108,18 @@ async fn main(spawner: Spawner) {
|
||||
control.start_ap_wpa2("cyw43", "password", 5).await;
|
||||
|
||||
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
|
||||
static board_atom: AtomicU32 = AtomicU32::new(0);
|
||||
static BOARD: AtomicU32 = AtomicU32::new(0);
|
||||
|
||||
#[embassy_executor::task(pool_size = 2)]
|
||||
async fn listen_task(stack: Stack<'static>, team: Team, port: u16) {
|
||||
loop {
|
||||
info!("team:{:?}", team);
|
||||
}
|
||||
let mut rx_buffer = [0; 4096];
|
||||
let mut tx_buffer = [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"),
|
||||
),
|
||||
p => 'res: {
|
||||
if p.starts_with("/ttt/cell") && p.len() == 10 {
|
||||
let clicked_c: Cell =
|
||||
match TryInto::<Cell>::try_into(p.chars().nth(9).unwrap()) {
|
||||
Ok(c) => c,
|
||||
Err(_) => break 'res (HttpResCode::NotFound, "", &[]),
|
||||
};
|
||||
let mut board = board_atom.load(Ordering::Acquire);
|
||||
if board
|
||||
& ((2_u32.pow(clicked_c as u32)) + (2_u32.pow(9 + clicked_c as u32)))
|
||||
!= 0
|
||||
{
|
||||
break 'res (HttpResCode::Forbidden, "", &[]);
|
||||
if (p.starts_with("/ttt/cell") && p.len() == 10) || p == "/ttt/board" {
|
||||
let mut board = BOARD.load(Ordering::Acquire);
|
||||
|
||||
if p.starts_with("/ttt/cell") {
|
||||
let clicked_c: Cell =
|
||||
match TryInto::<Cell>::try_into(p.chars().nth(9).unwrap()) {
|
||||
Ok(c) => c,
|
||||
Err(_) => break 'res (HttpResCode::NotFound, "", &[]),
|
||||
};
|
||||
if board
|
||||
& ((2_u32.pow(clicked_c as u32))
|
||||
+ (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();
|
||||
for c in 0..=8 {
|
||||
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 {
|
||||
None
|
||||
};
|
||||
info!("{} : {:?}", c, picked_by);
|
||||
match picked_by {
|
||||
Some(Team::Zero) => {
|
||||
res_buf
|
||||
.extend_from_slice(
|
||||
b"<div class=\"cell\" style=\"color:blue\"></div>",
|
||||
b"<div class=\"cell\" style=\"background-color:blue\"></div>",
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
Some(Team::One) => {
|
||||
res_buf
|
||||
.extend_from_slice(
|
||||
b"<div class=\"cell\" style=\"color:red\"></div>",
|
||||
res_buf.extend_from_slice(
|
||||
b"<div class=\"cell\" style=\"background-color:red\"></div>",
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
None => {
|
||||
write!(
|
||||
&mut res_buf,
|
||||
"<button class=\"cell\" hx-post=\"/ttt/cell{}\" hx-trigger=\"click\" hx-target=\"#grid\" hx-swap=\"innerHTML\"></button>",
|
||||
c
|
||||
).unwrap();
|
||||
&mut res_buf,
|
||||
"<button class=\"cell\" hx-post=\"/ttt/cell{}\" hx-trigger=\"click\" hx-target=\"#grid\" hx-swap=\"innerHTML\"></button>",
|
||||
c
|
||||
).unwrap();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
5261
web/htmx.js
Normal file
5261
web/htmx.js
Normal file
File diff suppressed because it is too large
Load Diff
@ -19,69 +19,11 @@
|
||||
<html>
|
||||
<body>
|
||||
<h1>TicTacToe</h1>
|
||||
<div id="grid">
|
||||
<button
|
||||
class="cell"
|
||||
hx-post="/ttt/cell0"
|
||||
hx-target="#grid"
|
||||
hx-swap="innerHTML"
|
||||
></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>
|
||||
<div
|
||||
id="grid"
|
||||
hx-post="/ttt/board"
|
||||
hx-swap="innerHTML"
|
||||
hx-trigger="load"
|
||||
></div>
|
||||
</body>
|
||||
</html>
|
||||
|
Loading…
Reference in New Issue
Block a user