Skip to content

Commit

Permalink
Add misc vote support.
Browse files Browse the repository at this point in the history
Add `add_vote` & `rem_vote` commands.
Refactor and clarify the command execution order for server & mods.
  • Loading branch information
Jupeyy committed Jan 12, 2025
1 parent a281e27 commit 4055353
Show file tree
Hide file tree
Showing 27 changed files with 1,458 additions and 628 deletions.
6 changes: 3 additions & 3 deletions examples/wasm-modules/console/src/console/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,23 +44,23 @@ impl Console {
name: "test".to_string(),
usage: "test".to_string(),
description: "test".to_string(),
cmd: Rc::new(|_, _, _| Ok("".to_string())),
cmd: Rc::new(|_, _, _, _| Ok("".to_string())),
args: vec![],
allows_partial_cmds: false,
}),
ConsoleEntry::Cmd(ConsoleEntryCmd {
name: "test2".to_string(),
usage: "test2".to_string(),
description: "test2".to_string(),
cmd: Rc::new(|_, _, _| Ok("".to_string())),
cmd: Rc::new(|_, _, _, _| Ok("".to_string())),
args: vec![],
allows_partial_cmds: false,
}),
ConsoleEntry::Cmd(ConsoleEntryCmd {
name: "test3".to_string(),
usage: "test3".to_string(),
description: "test3".to_string(),
cmd: Rc::new(|_, _, _| Ok("".to_string())),
cmd: Rc::new(|_, _, _, _| Ok("".to_string())),
args: vec![],
allows_partial_cmds: false,
}),
Expand Down
6 changes: 3 additions & 3 deletions game/api-state/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use game_interface::ghosts::GhostResult;
use game_interface::interface::{
GameStateCreate, GameStateCreateOptions, GameStateStaticInfo, MAX_MAP_NAME_LEN,
};
use game_interface::rcon_commands::ExecRconCommand;
use game_interface::rcon_entries::ExecRconInput;
use game_interface::settings::GameStateSettings;
use game_interface::tick_result::TickResult;
use game_interface::types::character_info::NetworkCharacterInfo;
Expand Down Expand Up @@ -148,8 +148,8 @@ impl GameStateInterface for ApiState {
fn rcon_command(
&mut self,
player_id: Option<PlayerId>,
cmd: ExecRconCommand,
) -> Vec<NetworkString<65536>> {
cmd: ExecRconInput,
) -> Vec<Result<NetworkString<65536>, NetworkString<65536>>> {
}

#[guest_func_call_from_host_auto(option)]
Expand Down
36 changes: 18 additions & 18 deletions game/client-console/src/console/local_console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ impl LocalConsoleBuilder {
name: "push".into(),
usage: "push <var>".into(),
description: "Push a new item to a config variable of type array.".into(),
cmd: Rc::new(|config_engine, config_game, path| {
cmd: Rc::new(|config_engine, config_game, _, path| {
let path = syn_vec_to_config_val(path).unwrap_or_default();
if config_engine
.try_set_from_str(path.clone(), None, None, None, ConfigFromStrOperation::Push)
Expand Down Expand Up @@ -135,7 +135,7 @@ impl LocalConsoleBuilder {
name: "pop".into(),
usage: "pop <var>".into(),
description: "Pop the last item of a config variable of type array.".into(),
cmd: Rc::new(|config_engine, config_game, path| {
cmd: Rc::new(|config_engine, config_game, _, path| {
let path = syn_vec_to_config_val(path).unwrap_or_default();
if config_engine
.try_set_from_str(path.clone(), None, None, None, ConfigFromStrOperation::Pop)
Expand Down Expand Up @@ -164,7 +164,7 @@ impl LocalConsoleBuilder {
name: "rem".into(),
usage: "rem <var>[key]".into(),
description: "Remove an item from a config variable of type object.".into(),
cmd: Rc::new(|config_engine, config_game, path| {
cmd: Rc::new(|config_engine, config_game, _, path| {
let path = syn_vec_to_config_val(path).unwrap_or_default();
if config_engine
.try_set_from_str(path.clone(), None, None, None, ConfigFromStrOperation::Rem)
Expand Down Expand Up @@ -193,7 +193,7 @@ impl LocalConsoleBuilder {
name: "reset".into(),
usage: "reset <var>".into(),
description: "Reset the value of a config variable to its default.".into(),
cmd: Rc::new(|config_engine, config_game, path| {
cmd: Rc::new(|config_engine, config_game, _, path| {
let path = syn_vec_to_config_val(path).unwrap_or_default();
if path.is_empty() {
return Err(anyhow::anyhow!("You cannot reset the whole config at once"));
Expand Down Expand Up @@ -286,7 +286,7 @@ impl LocalConsoleBuilder {
name: "toggle".into(),
usage: "toggle <var> <arg> <arg>".into(),
description: "Toggle a config variable between two args.".into(),
cmd: Rc::new(|config_engine, config_game, path| {
cmd: Rc::new(|config_engine, config_game, _, path| {
toggle(config_engine, config_game, path)
}),
args: vec![CommandArg {
Expand All @@ -301,7 +301,7 @@ impl LocalConsoleBuilder {
description:
"Toggle a config variable between two args until the pressed key is released again."
.into(),
cmd: Rc::new(|config_engine, config_game, path| {
cmd: Rc::new(|config_engine, config_game, _, path| {
toggle(config_engine, config_game, path)
}),
args: vec![CommandArg {
Expand All @@ -320,7 +320,7 @@ impl LocalConsoleBuilder {
name: name.to_string(),
usage: format!("triggers a player action: {}", name),
description: format!("Triggers the player action: {}", name),
cmd: Rc::new(move |_config_engine, _config_game, _path| {
cmd: Rc::new(move |_config_engine, _config_game, _, _path| {
events.push(LocalConsoleEvent::LocalPlayerAction(action));
Ok(String::default())
}),
Expand Down Expand Up @@ -446,7 +446,7 @@ impl LocalConsoleBuilder {
name: "bind".to_string(),
usage: "dummy".to_string(),
description: "dummy".to_string(),
cmd: Rc::new(|_, _, _| Ok("".into())),
cmd: Rc::new(|_, _, _, _| Ok("".into())),
args: vec![keys_arg.clone()],
allows_partial_cmds: false,
})]),
Expand Down Expand Up @@ -575,7 +575,7 @@ impl LocalConsoleBuilder {
name: "bind".into(),
usage: "bind <keys> <commands>".into(),
description: "Binds commands to a single key or key chain.".into(),
cmd: Rc::new(move |_config_engine, config_game, path| {
cmd: Rc::new(move |_config_engine, config_game, _, path| {
bind(
config_game.profiles.main as usize,
false,
Expand Down Expand Up @@ -608,7 +608,7 @@ impl LocalConsoleBuilder {
usage: "bind_dummy <keys> <commands>".into(),
description: "Binds commands to a single key or key chain for the dummy profile."
.into(),
cmd: Rc::new(move |_config_engine, config_game, path| {
cmd: Rc::new(move |_config_engine, config_game, _, path| {
bind(
config_game.profiles.dummy.index as usize,
true,
Expand Down Expand Up @@ -639,7 +639,7 @@ impl LocalConsoleBuilder {
name: "unbind".into(),
usage: "unbind <keys>".into(),
description: "Unbinds commands from a single key or key chain.".into(),
cmd: Rc::new(move |_config_engine, config_game, path| {
cmd: Rc::new(move |_config_engine, config_game, _, path| {
unbind(
config_game.profiles.main as usize,
false,
Expand All @@ -662,7 +662,7 @@ impl LocalConsoleBuilder {
usage: "unbind_dummy <keys>".into(),
description: "Unbinds commands from a single key or key chain for the dummy profile."
.into(),
cmd: Rc::new(move |_config_engine, config_game, path| {
cmd: Rc::new(move |_config_engine, config_game, _, path| {
unbind(
config_game.profiles.dummy.index as usize,
true,
Expand All @@ -683,7 +683,7 @@ impl LocalConsoleBuilder {
name: "exec".into(),
usage: "exec <file_path>".into(),
description: "Executes a file of command lines.".into(),
cmd: Rc::new(move |_, _, path| {
cmd: Rc::new(move |_, _, _, path| {
let Syn::Text(file_path_str) = &path[0].0 else {
panic!("Command parser returned a non requested command arg");
};
Expand All @@ -703,7 +703,7 @@ impl LocalConsoleBuilder {
name: "echo".into(),
usage: "echo <text>".into(),
description: "Echos text to the console and a client component.".into(),
cmd: Rc::new(move |_, _, path| {
cmd: Rc::new(move |_, _, _, path| {
let Syn::Text(text) = &path[0].0 else {
panic!("Command parser returned a non requested command arg");
};
Expand All @@ -723,7 +723,7 @@ impl LocalConsoleBuilder {
name: "connect".into(),
usage: "connect <ip:port>".into(),
description: "Connects to a server of the given ip & port.".into(),
cmd: Rc::new(move |_, _, path| {
cmd: Rc::new(move |_, _, _, path| {
let (Syn::Text(text), _) = path
.first()
.ok_or_else(|| anyhow!("expected ip & port, but found nothing"))?
Expand Down Expand Up @@ -754,7 +754,7 @@ impl LocalConsoleBuilder {
name: "change_dummy".into(),
usage: "change_dummy <index>".into(),
description: "Switches to a dummy, or the main player (index 0).".into(),
cmd: Rc::new(move |_, _, path| {
cmd: Rc::new(move |_, _, _, path| {
let (Syn::Number(index), _) = path
.first()
.ok_or_else(|| anyhow!("expected an index, but found nothing"))?
Expand Down Expand Up @@ -783,7 +783,7 @@ impl LocalConsoleBuilder {
name: "toggle_dummy".into(),
usage: "toggle_dummy".into(),
description: "Toggles between a dummy and the main player.".into(),
cmd: Rc::new(move |_, _, _| {
cmd: Rc::new(move |_, _, _, _| {
console_events_cmd.push(LocalConsoleEvent::ToggleDummy);
Ok("".to_string())
}),
Expand All @@ -795,7 +795,7 @@ impl LocalConsoleBuilder {
name: "quit".into(),
usage: "quit the client".into(),
description: "Closes the client.".into(),
cmd: Rc::new(move |_, _, _| {
cmd: Rc::new(move |_, _, _, _| {
console_events.push(LocalConsoleEvent::Quit);
Ok("Bye bye".to_string())
}),
Expand Down
24 changes: 16 additions & 8 deletions game/client-console/src/console/remote_console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ use base::network_string::NetworkString;
use client_types::console::{ConsoleEntry, ConsoleEntryCmd};
use command_parser::parser::{format_args, CommandArg, CommandArgType};
use egui::Color32;
use game_interface::rcon_commands::RconCommand;
use game_interface::rcon_entries::RconEntry;
use hiarc::{hiarc_safer_rc_refcell, Hiarc};
use ui_base::ui::UiCreator;

use super::console::ConsoleRender;

#[derive(Debug, Hiarc)]
pub enum RemoteConsoleEvent {
Exec { name: String, args: String },
Exec {
/// The raw ident text (with all modifiers as is)
ident_text: String,
/// The args of the command
args: String,
},
}

#[hiarc_safer_rc_refcell]
Expand Down Expand Up @@ -71,11 +76,14 @@ impl RemoteConsole {
usage
}

pub fn fill_entries(&mut self, cmds: HashMap<NetworkString<65536>, RconCommand>) {
pub fn fill_entries(
&mut self,
cmds: HashMap<NetworkString<65536>, RconEntry>,
vars: HashMap<NetworkString<65536>, RconEntry>,
) {
self.entries.clear();
for (name, cmd) in cmds {
for (name, cmd) in cmds.into_iter().chain(vars.into_iter()) {
let cmds = self.user.clone();
let name_clone = name.clone();
self.entries.push(ConsoleEntry::Cmd(ConsoleEntryCmd {
name: name.to_string(),
usage: if cmd.usage.is_empty() {
Expand All @@ -84,12 +92,12 @@ impl RemoteConsole {
cmd.usage.to_string()
},
description: cmd.description.to_string(),
cmd: Rc::new(move |_config_engine, _config_game, path| {
cmd: Rc::new(move |_config_engine, _config_game, ident_text, path| {
cmds.push(RemoteConsoleEvent::Exec {
name: name_clone.to_string(),
ident_text: ident_text.to_string(),
args: format_args(path),
});
Ok(format!("{name_clone} {}", format_args(path)))
Ok(format!("{ident_text} {}", format_args(path)))
}),
args: cmd.args,
allows_partial_cmds: true,
Expand Down
10 changes: 9 additions & 1 deletion game/client-types/src/console.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,16 @@ impl Debug for ConsoleEntryVariable {
}
}

/// A function that takes the config vars,
/// the raw & unparsed ident (so with all modifiers)
/// and the arguments and their range.
pub type ConsoleCmdCb = Rc<
dyn Fn(&mut ConfigEngine, &mut ConfigGame, &[(Syn, Range<usize>)]) -> anyhow::Result<String>,
dyn Fn(
&mut ConfigEngine,
&mut ConfigGame,
&str,
&[(Syn, Range<usize>)],
) -> anyhow::Result<String>,
>;

#[derive(Clone)]
Expand Down
2 changes: 1 addition & 1 deletion game/client-ui/src/console/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ pub fn run_command(
})
{
let cmd = cmd.unwrap_full_or_partial_cmd_ref();
match (entry_cmd.cmd)(config_engine, config_game, &cmd.args) {
match (entry_cmd.cmd)(config_engine, config_game, &cmd.cmd_text, &cmd.args) {
Ok(msg) => Ok(msg),
Err(err) => Err(format!("Parsing error: {}\n", err)),
}
Expand Down
4 changes: 3 additions & 1 deletion game/client-ui/src/ingame_menu/call_vote/main_frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ pub fn render(ui: &mut egui::Ui, pipe: &mut UiRenderPipe<UserData>, ui_state: &m
"Map" => super::map::render(ui, pipe, ui_state),
"Player" => super::players::render(ui, pipe),
// Misc
_ => {}
_ => {
super::misc::render(ui, pipe);
}
}
},
);
Expand Down
Loading

0 comments on commit 4055353

Please sign in to comment.