1use std::num::IntErrorKind;
2use teehistorian::chunks::ConsoleCommand;
3
4pub enum Command<'a> {
5 Team(i32),
6 ToggleLock,
7 SetLock(bool),
8 SaveEmpty,
9 Save(&'a [u8]),
10 Load(&'a [u8]),
11 Kill,
12 Team0Mode,
13 Pause,
14 Spec,
15}
16
17impl<'a> Command<'a> {
18 pub fn i32(param: &[u8]) -> i32 {
19 let _p: i64 = 0;
20 let param: &str = std::str::from_utf8(param).unwrap_or("0");
28 match param.parse::<i64>() {
29 Ok(i) => i as i32,
30 Err(err) => match err.kind() {
31 IntErrorKind::PosOverflow => i32::MAX,
32 IntErrorKind::NegOverflow => 0,
33 _ => 0,
34 },
35 }
36 }
37
38 pub fn from_teehistorian(cmd: &ConsoleCommand<'a>) -> Option<Self> {
39 match cmd.cmd {
40 b"team" => {
41 let arg = cmd.args.first()?;
42 let arg = std::str::from_utf8(arg).ok()?;
43 let team = arg.parse::<i32>().ok()?;
44 Some(Command::Team(team))
45 }
46 b"lock" => {
47 if let Some(arg) = cmd.args.first() {
48 let i = Command::i32(arg);
49 Some(Command::SetLock(i != 0))
50 } else {
51 Some(Command::ToggleLock)
52 }
53 }
54 b"unlock" => Some(Command::SetLock(false)),
55 b"load" => cmd.args.first().map(|arg| Command::Load(arg)),
56 b"save" => {
57 if let Some(arg) = cmd.args.first() {
58 Some(Command::Save(arg))
59 } else {
60 Some(Command::SaveEmpty)
61 }
62 }
63 b"kill" => Some(Command::Kill),
64 b"team0mode" => Some(Command::Team0Mode),
65 b"pause" => Some(Command::Pause),
66 b"spec" => Some(Command::Spec),
67 _ => None,
68 }
69 }
70}