feat: initial game logic
This commit is contained in:
170
src/main.rs
170
src/main.rs
@@ -4,6 +4,8 @@ use crate::types::{Color, MatchMaker, Role, AI};
|
||||
use futures_util::{SinkExt, StreamExt};
|
||||
use std::collections::HashMap;
|
||||
use std::net::SocketAddr;
|
||||
use std::num::ParseIntError;
|
||||
use std::str::FromStr;
|
||||
use std::sync::Arc;
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
use tokio::sync::RwLock;
|
||||
@@ -11,7 +13,7 @@ use tokio_tungstenite::{accept_async, tungstenite::Message};
|
||||
use tracing::{error, info, warn};
|
||||
use types::Client;
|
||||
|
||||
type Clients = Arc<RwLock<HashMap<SocketAddr, Client>>>;
|
||||
type Clients = Arc<RwLock<HashMap<SocketAddr, Client<'static>>>>;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), anyhow::Error> {
|
||||
@@ -140,6 +142,7 @@ async fn handle_connection(
|
||||
.as_ref()
|
||||
.unwrap(),
|
||||
Color::None,
|
||||
addr.to_string()
|
||||
));
|
||||
|
||||
let _ = clients
|
||||
@@ -150,15 +153,166 @@ async fn handle_connection(
|
||||
.send("READY:ACK");
|
||||
}
|
||||
else if text.starts_with("PLAY") {
|
||||
// TODO!
|
||||
// Check if client is valid
|
||||
// Check if it's their move
|
||||
// Check if valid move
|
||||
let read = clients.read().await;
|
||||
let client = read.get(&addr).unwrap();
|
||||
|
||||
// Check if client is valid
|
||||
if client.role != Role::Player || client.current_match.is_none() {
|
||||
let _ = clients
|
||||
.read()
|
||||
.await
|
||||
.get(&addr)
|
||||
.unwrap()
|
||||
.send("ERROR:INVALID:MOVE");
|
||||
continue;
|
||||
}
|
||||
|
||||
let current_match = client.current_match.unwrap();
|
||||
let ai = if *client.username.as_ref().unwrap() == current_match.player1.username
|
||||
{ ¤t_match.player1 } else { ¤t_match.player2 };
|
||||
let opponent = if *client.username.as_ref().unwrap() == current_match.player1.username
|
||||
{ ¤t_match.player2 } else { ¤t_match.player1 };
|
||||
|
||||
// Check if it's their move
|
||||
if (current_match.ledger.is_empty() &&
|
||||
current_match.first.username != *client.username.as_ref().unwrap()) ||
|
||||
(client.current_match.unwrap().ledger.last().unwrap().0.username == *client.username.as_ref().unwrap())
|
||||
{
|
||||
let _ = clients
|
||||
.read()
|
||||
.await
|
||||
.get(&addr)
|
||||
.unwrap()
|
||||
.send("ERROR:INVALID:MOVE");
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if valid move
|
||||
if let Ok(column) = text.split(":").collect::<Vec<&str>>()[1].parse::<usize>() {
|
||||
if current_match.board[column][4] != Color::None {
|
||||
let _ = clients
|
||||
.read()
|
||||
.await
|
||||
.get(&addr)
|
||||
.unwrap()
|
||||
.send("ERROR:INVALID:MOVE");
|
||||
continue;
|
||||
}
|
||||
|
||||
// Place it
|
||||
for i in 0..current_match.board[column].len() {
|
||||
if current_match.board[column][i] == Color::None {
|
||||
match_maker.write().await.matches.get_mut(¤t_match.id).unwrap().board[column][i] = ai.color.clone();
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
let _ = clients
|
||||
.read()
|
||||
.await
|
||||
.get(&addr)
|
||||
.unwrap()
|
||||
.send("ERROR:INVALID:MOVE");
|
||||
continue;
|
||||
}
|
||||
|
||||
// Place it
|
||||
// Check game end conditions
|
||||
// Broadcast move
|
||||
}
|
||||
let (winner, filled) = {
|
||||
let mut result = (Color::None, false);
|
||||
|
||||
let mut any_empty = true;
|
||||
for x in 0..6 {
|
||||
for y in 0..5 {
|
||||
let color = ¤t_match.board[x][y];
|
||||
let mut horizontal_end = true;
|
||||
let mut vertical_end = true;
|
||||
let mut diagonal_end = true;
|
||||
|
||||
if any_empty && *color == Color::None {
|
||||
any_empty = false;
|
||||
}
|
||||
|
||||
for i in 0..4 {
|
||||
if x + 3 < 6 && current_match.board[x + i][y] != *color {
|
||||
horizontal_end = false;
|
||||
}
|
||||
|
||||
if y + 3 < 5 && current_match.board[x + i][y] != *color {
|
||||
vertical_end = false;
|
||||
}
|
||||
|
||||
if x + 3 < 6 && y + 3 < 5 && current_match.board[x + i][y + i] != *color {
|
||||
diagonal_end = false;
|
||||
}
|
||||
}
|
||||
|
||||
if horizontal_end || vertical_end || diagonal_end { result = (color.clone(), false) }
|
||||
}
|
||||
}
|
||||
|
||||
if any_empty && result.0 == Color::None { result.1 = true; }
|
||||
|
||||
result
|
||||
};
|
||||
|
||||
if winner != Color::None {
|
||||
if winner == ai.color {
|
||||
let _ = clients
|
||||
.read()
|
||||
.await
|
||||
.get(&addr)
|
||||
.unwrap()
|
||||
.send("GAME:WINS");
|
||||
|
||||
let _ = clients
|
||||
.read()
|
||||
.await
|
||||
.get(&SocketAddr::from_str(&opponent.addr)?)
|
||||
.unwrap()
|
||||
.send("GAME:LOSS");
|
||||
} else {
|
||||
let _ = clients
|
||||
.read()
|
||||
.await
|
||||
.get(&addr)
|
||||
.unwrap()
|
||||
.send("GAME:LOSS");
|
||||
|
||||
let _ = clients
|
||||
.read()
|
||||
.await
|
||||
.get(&SocketAddr::from_str(&opponent.addr)?)
|
||||
.unwrap()
|
||||
.send("GAME:WINS");
|
||||
}
|
||||
}
|
||||
else if filled {
|
||||
let _ = clients
|
||||
.read()
|
||||
.await
|
||||
.get(&addr)
|
||||
.unwrap()
|
||||
.send("GAME:DRAW");
|
||||
|
||||
let _ = clients
|
||||
.read()
|
||||
.await
|
||||
.get(&SocketAddr::from_str(&opponent.addr)?)
|
||||
.unwrap()
|
||||
.send("GAME:DRAW");
|
||||
}
|
||||
else {
|
||||
let _ = clients
|
||||
.read()
|
||||
.await
|
||||
.get(&SocketAddr::from_str(&opponent.addr)?)
|
||||
.unwrap()
|
||||
.send(&format!("OPPONENT:{}", text.split(":").collect::<Vec<&str>>()[1].parse::<usize>()?));
|
||||
}
|
||||
|
||||
// TODO: remove match from matchmaker
|
||||
// TODO: broadcast moves to viewers
|
||||
}
|
||||
else {
|
||||
let _ = clients
|
||||
.read()
|
||||
|
||||
Reference in New Issue
Block a user