aboutsummaryrefslogtreecommitdiff
path: root/core/src/data/game/game_cli.rs
blob: fd715561750b61e1e0c2cdd412db4b6c0a64d628 (plain) (blame)
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
use std::sync::{Arc, Mutex};
use clap::{Args, Parser, Subcommand};
use clearscreen::clear;
use log::{info, warn};
use nogamepads::entry_mutex;
use crate::data::game::game_runtime::GameRuntime;
use crate::data::player::player_data::Player;

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct GameCli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand, Debug)]
enum Commands {

    #[command(about = "Clean the screen")]
    Clear,

    LockGame,

    UnlockGame,

    #[command(about = "Close the game")]
    Close,

    OnlineList,

    BannedList,

    Ban(PlayerIndex),

    Pardon(PlayerIndex),

    Kick(PlayerIndex),

    Event(SendEventArgs),

    Message(SendMessageArgs),

    Pop,

    PopAll,
}

#[derive(Args, Debug)]
struct PlayerIndex {
    index: usize,
}

#[derive(Args, Debug)]
struct SendEventArgs {
    index: usize,
    event: u8
}

#[derive(Args, Debug)]
struct SendMessageArgs {
    index: usize,
    msg: String
}

pub fn process_game_cli(runtime: Arc<Mutex<GameRuntime>>, cmd: GameCli) -> bool {
    match cmd.command {
        Commands::Clear => {
            clear().expect("Failed to clear screen");
        }

        Commands::LockGame => {
            entry_mutex!(runtime, |guard| {
                guard.lock_game();
            });
        }

        Commands::UnlockGame => {
            entry_mutex!(runtime, |guard| {
                guard.unlock_game();
            });
        }

        Commands::Close => {
            entry_mutex!(runtime, |guard| {
                guard.close_game();
            });
            return false;
        }

        Commands::OnlineList => {
            entry_mutex!(runtime, |guard| {
                let mut i = 0;
                for account in guard.data.online_accounts() {
                    info!("{}.{}", i, account.id);
                    i += 1;
                }
            });
        }

        Commands::BannedList => {
            entry_mutex!(runtime, |guard| {
                let mut i = 0;
                for account in guard.data.banned_accounts() {
                    info!("{}.{}", i, account.id);
                    i += 1;
                }
            });
        }

        Commands::Ban(args) => {
            entry_mutex!(runtime, |guard| {
                if let Some(account) = guard.data.online_accounts().get(args.index) {
                    if let Some(service_type) = guard.data.get_service_type(account) {
                        guard.ban_player(&Player::from(account.clone()), service_type);
                        info!("Account {} banned.", account.id);
                    }
                } else {
                    warn!("Account number {} not found", args.index);
                }
            });
        }

        Commands::Pardon(args) => {
            entry_mutex!(runtime, |guard| {
                if let Some(account) = guard.data.banned_accounts().get(args.index) {
                    guard.pardon_player(&Player::from(account.clone()));
                    info!("Account {} pardoned.", account.id);
                } else {
                    warn!("Account number {} not found", args.index);
                }
            });
        }

        Commands::Kick(args) => {
            entry_mutex!(runtime, |guard| {
                if let Some(account) = guard.data.online_accounts().get(args.index) {
                    if let Some(service_type) = guard.data.get_service_type(account) {
                        guard.kick_player(&Player::from(account.clone()), service_type);
                        info!("Account {} kicked.", account.id);
                    }
                } else {
                    warn!("Account number {} not found", args.index);
                }
            });
        }

        Commands::Event(args) => {
            entry_mutex!(runtime, |guard| {
                if let Some(account) = guard.data.online_accounts().get(args.index) {
                    if let Some(service_type) = guard.data.get_service_type(account) {
                        guard.send_event(account, args.event, service_type);
                        info!("Sent event {} to {}.", args.event, account.id);
                    }
                } else {
                    warn!("Account number {} not found", args.index);
                }
            });
        }

        Commands::Message(args) => {
            entry_mutex!(runtime, |guard| {
                if let Some(account) = guard.data.online_accounts().get(args.index) {
                    if let Some(service_type) = guard.data.get_service_type(account) {
                        guard.send_message(account, args.msg.clone(), service_type);
                        info!("Sent message \"{}\" to {}.", args.msg, account.id);
                    }
                } else {
                    warn!("Account number {} not found", args.index);
                }
            });
        }

        Commands::Pop => {
            entry_mutex!(runtime, |guard| {
                if let Some((account, message)) = guard.pop_control_event() {
                    info!("{}: {:?}", account.id, message);
                } else {
                    info!("None")
                }
            });
        }

        Commands::PopAll => {
            entry_mutex!(runtime, |guard| {
                while let Some((account, message)) = guard.pop_control_event() {
                    info!("{}: {:?}", account.id, message);
                }
            });
        }
    }
    true
}