add cells logic (not working yet)
This commit is contained in:
parent
8f79c33b6e
commit
868d777708
123
src/main.rs
123
src/main.rs
@ -6,6 +6,8 @@
|
|||||||
|
|
||||||
use core::fmt::{Debug, Write};
|
use core::fmt::{Debug, Write};
|
||||||
use core::str::from_utf8;
|
use core::str::from_utf8;
|
||||||
|
use core::sync::atomic::{AtomicBool, AtomicU32, Ordering};
|
||||||
|
use cortex_m::interrupt::Mutex;
|
||||||
use cyw43_pio::{DEFAULT_CLOCK_DIVIDER, PioSpi};
|
use cyw43_pio::{DEFAULT_CLOCK_DIVIDER, PioSpi};
|
||||||
use embassy_executor::Spawner;
|
use embassy_executor::Spawner;
|
||||||
use embassy_net::tcp::TcpSocket;
|
use embassy_net::tcp::TcpSocket;
|
||||||
@ -92,7 +94,7 @@ async fn main(spawner: Spawner) {
|
|||||||
let seed = rng.next_u64();
|
let seed = rng.next_u64();
|
||||||
|
|
||||||
// Init network stack
|
// Init network stack
|
||||||
static RESOURCES: StaticCell<StackResources<3>> = StaticCell::new();
|
static RESOURCES: StaticCell<StackResources<2>> = StaticCell::new();
|
||||||
let (stack, runner) = embassy_net::new(
|
let (stack, runner) = embassy_net::new(
|
||||||
net_device,
|
net_device,
|
||||||
config,
|
config,
|
||||||
@ -105,21 +107,21 @@ async fn main(spawner: Spawner) {
|
|||||||
//control.start_ap_open("cyw43", 5).await;
|
//control.start_ap_open("cyw43", 5).await;
|
||||||
control.start_ap_wpa2("cyw43", "password", 5).await;
|
control.start_ap_wpa2("cyw43", "password", 5).await;
|
||||||
|
|
||||||
// loop {
|
|
||||||
// info!("test");
|
|
||||||
// Timer::after_secs(0).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);
|
||||||
|
// bits [0; 8] : player zero board / bits [9; 17] : player one board
|
||||||
|
static board_atom: 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) {
|
||||||
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];
|
||||||
let mut res_head_buf = Vec::<u8, 4096>::new();
|
let mut res_head_buf = Vec::<u8, 4096>::new();
|
||||||
|
let mut res_buf = Vec::<u8, 4096>::new();
|
||||||
loop {
|
loop {
|
||||||
Timer::after_secs(0).await;
|
Timer::after_secs(0).await;
|
||||||
let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
|
let mut socket = TcpSocket::new(stack, &mut rx_buffer, &mut tx_buffer);
|
||||||
@ -204,13 +206,39 @@ async fn listen_task(stack: Stack<'static>, team: Team, port: u16) {
|
|||||||
let (code, res_content): (HttpResCode, &[u8]) = match path {
|
let (code, res_content): (HttpResCode, &[u8]) = match path {
|
||||||
"/" => (HttpResCode::Ok, include_bytes!("../web/index.html")),
|
"/" => (HttpResCode::Ok, include_bytes!("../web/index.html")),
|
||||||
"/htmx.min.js" => (HttpResCode::Ok, include_bytes!("../web/htmx.min.js")),
|
"/htmx.min.js" => (HttpResCode::Ok, include_bytes!("../web/htmx.min.js")),
|
||||||
p => {
|
p => 'res: {
|
||||||
// if p.starts_with("/ppp/cell") {
|
if p.starts_with("/ppp/cell") && p.len() == 10 {
|
||||||
// let clicked_c = match p[9] {
|
let clicked_c: Cell = match TryInto::<Cell>::try_into(p.chars().nth(9).unwrap()) {
|
||||||
// '0' =>
|
Ok(c) => c,
|
||||||
// };
|
Err(_) => {break 'res (HttpResCode::NotFound, &[])}
|
||||||
// }
|
};
|
||||||
(HttpResCode::NotFound, &[])
|
let mut board = board_atom.load(Ordering::Acquire);
|
||||||
|
if board & ((2^(clicked_c as u32)) + (2^(9+clicked_c as u32))) != 0 {
|
||||||
|
break 'res (HttpResCode::Forbidden, &[])
|
||||||
|
}
|
||||||
|
board = board | 2^((team as u32 * 9) + clicked_c as u32);
|
||||||
|
board_atom.store(board, Ordering::Release);
|
||||||
|
res_buf.clear();
|
||||||
|
for c in 0..=8 {
|
||||||
|
let picked_by = if board & (2^c) != 0 {
|
||||||
|
Some(Team::Zero)
|
||||||
|
} else if board & (2^(9+c)) != 0 {
|
||||||
|
Some(Team::One)
|
||||||
|
} else {None};
|
||||||
|
match picked_by {
|
||||||
|
Some(Team::Zero) => {res_buf.extend_from_slice(b"<div class=\"cell\" style=\"color:blue\"></div>").unwrap();},
|
||||||
|
Some(Team::One) => {res_buf.extend_from_slice(b"<div class=\"cell\" style=\"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();}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
(HttpResCode::Ok, &res_buf)
|
||||||
|
} else {
|
||||||
|
(HttpResCode::NotFound, &[])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -261,31 +289,76 @@ enum HttpRequestType {
|
|||||||
enum HttpResCode {
|
enum HttpResCode {
|
||||||
Ok,
|
Ok,
|
||||||
NotFound,
|
NotFound,
|
||||||
|
Forbidden
|
||||||
}
|
}
|
||||||
impl Into<&str> for HttpResCode {
|
impl Into<&str> for HttpResCode {
|
||||||
fn into(self) -> &'static str {
|
fn into(self) -> &'static str {
|
||||||
match self {
|
match self {
|
||||||
HttpResCode::Ok => "HTTP/1.1 200 OK",
|
HttpResCode::Ok => "HTTP/1.1 200 OK",
|
||||||
HttpResCode::NotFound => "HTTP/1.1 404 NOT FOUND",
|
HttpResCode::NotFound => "HTTP/1.1 404 NOT FOUND",
|
||||||
|
HttpResCode::Forbidden => "HTTP/1.1 403 FORBIDDEN"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
enum Team {
|
enum Team {
|
||||||
Zero,
|
Zero = 0,
|
||||||
One,
|
One = 1,
|
||||||
|
}
|
||||||
|
impl From<bool> for Team {
|
||||||
|
fn from(value: bool) -> Self {
|
||||||
|
if value {
|
||||||
|
Team::One
|
||||||
|
} else {
|
||||||
|
Team::Zero
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy)]
|
#[derive(Debug, Clone, Copy)]
|
||||||
enum Cell {
|
enum Cell {
|
||||||
C0,
|
C0 = 0,
|
||||||
C1,
|
C1 = 1,
|
||||||
C2,
|
C2 = 2,
|
||||||
C3,
|
C3 = 3,
|
||||||
C4,
|
C4 = 4,
|
||||||
C5,
|
C5 = 5,
|
||||||
C6,
|
C6 = 6,
|
||||||
C7,
|
C7 = 7,
|
||||||
C8,
|
C8 = 8,
|
||||||
|
}
|
||||||
|
impl TryFrom<char> for Cell {
|
||||||
|
type Error = ();
|
||||||
|
fn try_from(value: char) -> Result<Self, Self::Error> {
|
||||||
|
Ok(match value {
|
||||||
|
'0' => Cell::C0,
|
||||||
|
'1' => Cell::C1,
|
||||||
|
'2' => Cell::C2,
|
||||||
|
'3' => Cell::C3,
|
||||||
|
'4' => Cell::C4,
|
||||||
|
'5' => Cell::C5,
|
||||||
|
'6' => Cell::C6,
|
||||||
|
'7' => Cell::C7,
|
||||||
|
'8' => Cell::C8,
|
||||||
|
_ => return Err(())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl TryFrom<u8> for Cell {
|
||||||
|
type Error = ();
|
||||||
|
fn try_from(value: u8) -> Result<Self, Self::Error> {
|
||||||
|
Ok(match value {
|
||||||
|
0 => Cell::C0,
|
||||||
|
1 => Cell::C1,
|
||||||
|
2 => Cell::C2,
|
||||||
|
3 => Cell::C3,
|
||||||
|
4 => Cell::C4,
|
||||||
|
5 => Cell::C5,
|
||||||
|
6 => Cell::C6,
|
||||||
|
7 => Cell::C7,
|
||||||
|
8 => Cell::C8,
|
||||||
|
_ => return Err(())
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,12 @@
|
|||||||
<body>
|
<body>
|
||||||
<h1>TicTacToe</h1>
|
<h1>TicTacToe</h1>
|
||||||
<div id="grid">
|
<div id="grid">
|
||||||
|
<button
|
||||||
|
class="cell"
|
||||||
|
hx-post="/ttt/cell0"
|
||||||
|
hx-target=".grid"
|
||||||
|
hx-swap="innerHTML"
|
||||||
|
></button>
|
||||||
<button
|
<button
|
||||||
class="cell"
|
class="cell"
|
||||||
hx-post="/ttt/cell1"
|
hx-post="/ttt/cell1"
|
||||||
@ -76,13 +82,6 @@
|
|||||||
hx-target=".grid"
|
hx-target=".grid"
|
||||||
hx-swap="innerHTML"
|
hx-swap="innerHTML"
|
||||||
></button>
|
></button>
|
||||||
<button
|
|
||||||
class="cell"
|
|
||||||
hx-post="/ttt/cell9"
|
|
||||||
hx-trigger="click"
|
|
||||||
hx-target=".grid"
|
|
||||||
hx-swap="innerHTML"
|
|
||||||
></button>
|
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
Loading…
Reference in New Issue
Block a user