aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 4ee5e1448ed137f01e656912acacad24c70077f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
use crate::shared::champion::*;
use crate::shared::faction::*;
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<SocketAddr>,
    #[arg(long, num_args = 0)]
    server: bool,
    #[arg(long, default_value = "meele")]
    champion: Champion,
    #[arg(long, default_value = "yellow")]
    faction: Faction,
}

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,
            cli.faction,
        );
    } 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,
            cli.faction,
        );
    }
}