summaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 487db6f2c86463535df5cc64bc094592aa22a6ea (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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// -*- coding: utf-8 -*-
//
// Copyright 2021 Michael Buesch <m@bues.ch>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//

#![windows_subsystem="windows"]

mod board;
mod coord;
mod game_state;
#[cfg(feature="gui")]
mod gtk_helpers;
#[cfg(feature="gui")]
mod main_window;
#[cfg(feature="server")]
mod net;
mod player;
mod print;
mod random;

use anyhow as ah;
#[cfg(feature="gui")]
use crate::main_window::MainWindow;
#[cfg(feature="gui")]
use crate::player::PlayerMode;
#[cfg(feature="server")]
use crate::net::server::Server;
#[cfg(feature="gui")]
use expect_exit::{ExpectedWithError, exit};
#[cfg(feature="gui")]
use gio::prelude::*;
#[cfg(feature="gui")]
use gtk::prelude::*;
//use std::env;
use structopt::StructOpt;
use crate::print::Print;

#[derive(StructOpt, Debug)]
#[structopt(name="wolfsmühle")]
struct Opts {
    /// Set the log level.
    /// 0 = Silent. Don't print anything.
    /// 1 = Only print errors.
    /// 2 = Print errors and warnings.
    /// 3 = Print errors, warnings and info.
    /// 4 = Print errors, warnings, info and debug.
    #[structopt(short="L", long, default_value="3")]
    log_level: u8,

    /// Run a dedicated server.
    #[cfg(feature="gui")]
    #[structopt(short, long)]
    server: bool,

    /// Bind the server to this address.
    #[cfg(feature="server")]
    #[structopt(short="b", long, default_value="0.0.0.0")]
    server_bind: String,

    /// Maximum number of connections to accept in server mode.
    #[cfg(feature="server")]
    #[structopt(short="M", long, default_value="10")]
    max_connections: u16,

    /// Server room to open (server) or join (client).
    #[structopt(short, long)]
    room: Option<Vec<String>>,

    /// Restrict the player modes that can join a room.
    /// With this option set, only one Wolf player and only
    /// one Sheep player can join a room.
    /// In restricted mode, the player mode "both" is not allowed.
    #[cfg(feature="server")]
    #[structopt(short="R", long)]
    restrict_player_modes: bool,

    /// Connect to a server.
    #[cfg(feature="gui")]
    #[structopt(short, long)]
    connect: Option<String>,

    /// Use this port for server or client connection.
    #[structopt(short, long, default_value="5596")]
    port: u16,

    /// Use this player name when joining a room, instead of an auto generated one.
    #[cfg(feature="gui")]
    #[structopt(short="n", long)]
    player_name: Option<String>,

    /// Use this player mode when joining a room.
    /// May be "wolf", "sheep", "both" or "spectator".
    #[cfg(feature="gui")]
    #[structopt(short="m", long, default_value="both")]
    player_mode: String,
}

#[cfg(feature="server")]
fn server_fn(opt: &Opts) -> ah::Result<()> {
    let addr = format!("{}:{}", opt.server_bind, opt.port);

    Print::info(&format!("Running dedicated server on {} ...", addr));
    let mut s = Server::new(addr,
                            opt.max_connections,
                            opt.restrict_player_modes)?;

    let default_rooms = vec!["default".to_string()];
    let rooms = match opt.room.as_ref() {
        Some(r) => r,
        None => {
            Print::warning(&format!("No server rooms specified. Using '{}'.",
                                    default_rooms[0]));
            &default_rooms
        },
    };

    s.run(rooms)?;
    Ok(())
}

#[cfg(feature="gui")]
fn app_fn(app: &gtk::Application) {
    let opt = Opts::from_args();

    let connect = match opt.connect {
        Some(connect) => Some(format!("{}:{}", connect, opt.port)),
        None => None,
    };

    let default_room = "default".to_string();
    let room_name = match opt.room {
        Some(rooms) => {
            if rooms.len() >= 1 {
                rooms[0].to_string()
            } else {
                default_room
            }
        },
        None => default_room,
    };

    let player_mode = match &opt.player_mode.to_lowercase().trim()[..] {
        "wolf" => PlayerMode::Wolf,
        "sheep" => PlayerMode::Sheep,
        "both" => PlayerMode::Both,
        "spectator" => PlayerMode::Spectator,
        _ => exit("Invalid --player-mode."),
    };

    MainWindow::new(app,
                    connect,
                    room_name,
                    opt.player_name,
                    player_mode)
        .expect_or_exit_perror_("Startup failed")
        .borrow()
        .main_window()
        .show();
}

fn main() -> ah::Result<()> {
    let opt = Opts::from_args();
    Print::set_level_number(opt.log_level);

    #[cfg(feature="gui")]
    let run_server = opt.server;
    #[cfg(not(feature="gui"))]
    let run_server = true;

    #[cfg(feature="server")]
    if run_server {
        server_fn(&opt)?;
        return Ok(());
    }

    #[cfg(feature="gui")]
    if !run_server {
        let app = gtk::Application::new(None, gio::ApplicationFlags::FLAGS_NONE);
        app.connect_activate(app_fn);
        let args: Vec<&str> = vec![];
        app.run_with_args(&args);
    }
    Ok(())
}

// vim: ts=4 sw=4 expandtab
bues.ch cgit interface