You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.4 KiB

mod game;
mod leaderboard;
use valence::prelude::{event::CommandExecution, *};
use crate::{GameState, db::Connection};
pub struct Command {
pub name: String,
pub args: Option<Vec<String>>,
}
impl Command {
pub fn parse(command: String) -> Self {
let command = command
.split(" ")
.map(|e| e.to_string())
.collect::<Vec<String>>();
let name = command[0].clone();
let args = if command.len() > 1 {
Some(command.as_slice()[1..].to_vec())
} else {
None
};
Self { name, args }
}
}
pub fn command_executor(
mut clients: Query<&mut Client>,
uuids: Query<&UniqueId, With<Client>>,
mut events: EventReader<CommandExecution>,
mut next_state: ResMut<NextState<GameState>>,
state: Res<State<GameState>>,
mut connection: ResMut<Connection>
) {
for event in events.iter() {
let cmd = Command::parse(event.command.to_string());
let mut client = clients.get_mut(event.client).unwrap();
let uuid = uuids.get(event.client).unwrap();
match cmd.name.as_str() {
"game" => game::run(cmd, &mut client, uuid.0, &mut next_state, &state),
"leaderboard" => leaderboard::run(&mut client, &mut connection.0),
_ => client.send_message(format!("Command {} not found!", cmd.name)),
}
}
}