2024-04-05 22:38:41 -04:00
|
|
|
use serde_json::Result;
|
2024-04-12 02:04:58 -04:00
|
|
|
use std::fs;
|
2024-04-13 21:04:42 -04:00
|
|
|
use std::{
|
|
|
|
collections::HashMap,
|
|
|
|
env,
|
|
|
|
net::SocketAddr,
|
|
|
|
sync::{Arc, Mutex},
|
|
|
|
};
|
|
|
|
|
|
|
|
use futures_channel::mpsc::{unbounded, UnboundedSender};
|
|
|
|
use futures_util::{future, pin_mut, stream::TryStreamExt, StreamExt};
|
|
|
|
|
|
|
|
use tokio::net::{TcpListener, TcpStream};
|
|
|
|
use tokio_tungstenite::tungstenite::protocol::Message;
|
|
|
|
|
|
|
|
type Tx = UnboundedSender<Message>;
|
|
|
|
type PeerMap = Arc<Mutex<HashMap<SocketAddr, Tx>>>;
|
|
|
|
|
|
|
|
async fn handle_connection(peer_map: PeerMap, raw_stream: TcpStream, addr: SocketAddr) {
|
|
|
|
println!("Incoming TCP connection from: {}", addr);
|
|
|
|
|
|
|
|
let ws_stream = tokio_tungstenite::accept_async(raw_stream)
|
|
|
|
.await
|
|
|
|
.expect("Error during the websocket handshake occurred");
|
|
|
|
println!("WebSocket connection established: {}", addr);
|
|
|
|
|
|
|
|
// Insert the write part of this peer to the peer map.
|
|
|
|
let (tx, rx) = unbounded();
|
|
|
|
peer_map.lock().unwrap().insert(addr, tx);
|
|
|
|
|
|
|
|
let (outgoing, incoming) = ws_stream.split();
|
|
|
|
|
|
|
|
let broadcast_incoming = incoming.try_for_each(|msg| {
|
2024-04-15 02:26:26 -04:00
|
|
|
println!(
|
|
|
|
"Received a message from {}: {}",
|
|
|
|
addr,
|
|
|
|
msg.to_text().unwrap()
|
|
|
|
);
|
2024-04-13 21:04:42 -04:00
|
|
|
let peers = peer_map.lock().unwrap();
|
|
|
|
|
|
|
|
// We want to broadcast the message to everyone except ourselves.
|
2024-04-15 02:26:26 -04:00
|
|
|
let broadcast_recipients = peers
|
|
|
|
.iter()
|
|
|
|
.filter(|(peer_addr, _)| peer_addr != &&addr)
|
|
|
|
.map(|(_, ws_sink)| ws_sink);
|
2024-04-13 21:04:42 -04:00
|
|
|
|
|
|
|
for recp in broadcast_recipients {
|
|
|
|
recp.unbounded_send(msg.clone()).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
future::ok(())
|
|
|
|
});
|
|
|
|
|
|
|
|
let receive_from_others = rx.map(Ok).forward(outgoing);
|
|
|
|
|
|
|
|
pin_mut!(broadcast_incoming, receive_from_others);
|
|
|
|
future::select(broadcast_incoming, receive_from_others).await;
|
|
|
|
|
|
|
|
println!("{} disconnected", &addr);
|
|
|
|
peer_map.lock().unwrap().remove(&addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<()> {
|
|
|
|
test()?;
|
|
|
|
|
2024-04-15 02:26:26 -04:00
|
|
|
let addr = env::args()
|
|
|
|
.nth(1)
|
|
|
|
.unwrap_or_else(|| "127.0.0.1:8080".to_string());
|
2024-04-13 21:04:42 -04:00
|
|
|
|
|
|
|
let state = PeerMap::new(Mutex::new(HashMap::new()));
|
|
|
|
|
|
|
|
// Create the event loop and TCP listener we'll accept connections on.
|
|
|
|
let try_socket = TcpListener::bind(&addr).await;
|
|
|
|
let listener = try_socket.expect("Failed to bind");
|
|
|
|
println!("\nListening on: {}", addr);
|
|
|
|
|
|
|
|
// Let's spawn the handling of each connection in a separate task.
|
|
|
|
while let Ok((stream, addr)) = listener.accept().await {
|
|
|
|
tokio::spawn(handle_connection(state.clone(), stream, addr));
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
2024-04-05 22:38:41 -04:00
|
|
|
|
2024-04-09 04:51:30 -04:00
|
|
|
#[allow(non_snake_case)]
|
|
|
|
pub mod CAHd_game;
|
|
|
|
use crate::CAHd_game::*;
|
2024-04-05 22:38:41 -04:00
|
|
|
|
2024-04-06 22:38:00 -04:00
|
|
|
/// Parse json for card data
|
2024-04-05 22:38:41 -04:00
|
|
|
fn load_json(path: &str) -> Result<Vec<CAHCardSet>> {
|
|
|
|
let data: String = fs::read_to_string(path).expect("Error reading file");
|
|
|
|
let jayson: Vec<CAHCardSet> = serde_json::from_str(&data)?;
|
|
|
|
|
|
|
|
Ok(jayson)
|
|
|
|
}
|
|
|
|
|
2024-04-13 21:04:42 -04:00
|
|
|
fn test() -> Result<()> {
|
2024-04-06 22:38:00 -04:00
|
|
|
// choose decks
|
2024-04-05 22:38:41 -04:00
|
|
|
let cards_input_path: &str = "data/cah-cards-full.json";
|
2024-04-12 18:23:33 -04:00
|
|
|
|
|
|
|
// TODO: this should be a master card database and pointers
|
|
|
|
// to the cards should be passed to the game instead of actual cards
|
2024-04-12 02:04:58 -04:00
|
|
|
let chosen_packs: Vec<CAHCardSet> = load_json(cards_input_path)?;
|
2024-04-12 18:23:33 -04:00
|
|
|
println!("{}", &chosen_packs.len());
|
2024-04-05 22:38:41 -04:00
|
|
|
|
2024-04-12 02:04:58 -04:00
|
|
|
let test_player0 = CAHPlayer {
|
2024-04-12 18:35:13 -04:00
|
|
|
player_name: "Adam".to_string(),
|
|
|
|
role: PlayerRole::Host,
|
|
|
|
white: vec![],
|
|
|
|
black: vec![],
|
|
|
|
};
|
2024-04-12 02:04:58 -04:00
|
|
|
|
|
|
|
let test_player1 = CAHPlayer {
|
2024-04-12 18:35:13 -04:00
|
|
|
player_name: "Ferris".to_string(),
|
|
|
|
role: PlayerRole::Player,
|
|
|
|
white: vec![],
|
|
|
|
black: vec![],
|
|
|
|
};
|
2024-04-12 02:04:58 -04:00
|
|
|
|
|
|
|
// make some games
|
2024-04-12 18:23:33 -04:00
|
|
|
// use hashmap?
|
2024-04-12 02:04:58 -04:00
|
|
|
let mut games: Vec<CAHGame> = vec![];
|
|
|
|
|
|
|
|
// create game with/for player 0
|
2024-04-12 18:35:13 -04:00
|
|
|
let test_game0 = NewGameRequest {
|
|
|
|
name: "Test0".to_string(),
|
|
|
|
host: test_player0,
|
|
|
|
packs: chosen_packs,
|
|
|
|
};
|
|
|
|
|
2024-04-13 21:04:42 -04:00
|
|
|
games.push(CAHGame::new(test_game0)?);
|
2024-04-12 02:04:58 -04:00
|
|
|
|
2024-04-12 18:23:33 -04:00
|
|
|
// a new game request struct but this player is a player
|
|
|
|
games[0].create_player(test_player1)?;
|
2024-04-10 04:18:31 -04:00
|
|
|
|
2024-04-12 02:35:35 -04:00
|
|
|
// start round
|
2024-04-12 18:23:33 -04:00
|
|
|
games[0].game_start()?;
|
2024-04-13 21:04:42 -04:00
|
|
|
|
2024-04-24 02:24:10 -04:00
|
|
|
println!("----------------------");
|
|
|
|
for card in &games[0].players[0].white {
|
|
|
|
println!("{}", card.text);
|
|
|
|
};
|
2024-04-05 22:38:41 -04:00
|
|
|
Ok(())
|
|
|
|
}
|