use crate::shared::champion::*; use clap::Parser; use lightyear::transport::io::TransportConfig; use rand::Rng; use shared::SERVER_PORT; use std::net::Ipv4Addr; use std::net::SocketAddr; use std::thread; mod client; mod protocol; mod server; mod shared; #[derive(Parser, Debug)] struct Cli { #[arg(long, value_name = "HOST:PORT")] connect_to: Option, #[arg(long, num_args = 0)] server: bool, #[arg(long, default_value = "meele")] champion: Champion, } fn main() { let cli = Cli::parse(); let mut rng = rand::thread_rng(); let client_id = rng.gen_range(1..=SERVER_PORT); if cli.connect_to.is_some() && cli.server { panic!("cannot specify both `--connect-to` and `--server`"); } if cli.server { let server_addr = SocketAddr::new(Ipv4Addr::new(0, 0, 0, 0).into(), SERVER_PORT); server::main(TransportConfig::UdpSocket(server_addr)); } else if let Some(server_addr) = cli.connect_to { let client_addr = SocketAddr::new(Ipv4Addr::UNSPECIFIED.into(), SERVER_PORT + client_id); client::main( Some(server_addr), client_id as u64, TransportConfig::UdpSocket(client_addr), cli.champion, ); } else { let (from_server_send, from_server_recv) = crossbeam_channel::unbounded(); let (to_server_send, to_server_recv) = crossbeam_channel::unbounded(); thread::spawn(|| { let server_addr = SocketAddr::new(Ipv4Addr::new(0, 0, 0, 0).into(), SERVER_PORT); server::main(TransportConfig::Channels { channels: [(server_addr, to_server_recv, from_server_send)].to_vec(), }); }); client::main( None, client_id as u64, TransportConfig::LocalChannel { recv: from_server_recv, send: to_server_send, }, cli.champion, ); } }