Implement event system

This commit is contained in:
Jeff 2024-06-14 16:29:57 -04:00
parent 021f8154c8
commit 8a90dfb77b
4 changed files with 271 additions and 100 deletions

110
Cargo.lock generated
View File

@ -45,6 +45,12 @@ dependencies = [
"memchr", "memchr",
] ]
[[package]]
name = "allocator-api2"
version = "0.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f"
[[package]] [[package]]
name = "android-tzdata" name = "android-tzdata"
version = "0.1.1" version = "0.1.1"
@ -60,6 +66,55 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "anstream"
version = "0.6.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "418c75fa768af9c03be99d17643f93f79bbba589895012a80e3452a19ddda15b"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"is_terminal_polyfill",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "038dfcf04a5feb68e9c60b21c9625a54c2c0616e79b72b0fd87075a056ae1d1b"
[[package]]
name = "anstyle-parse"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c03a11a9034d92058ceb6ee011ce58af4a9bf61491aa7e1e59ecd24bd40d22d4"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ad186efb764318d35165f1758e7dcef3b10628e26d41a44bc5550652e6804391"
dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "61a38449feb7068f52bb06c12759005cf459ee52bb4adc1d5a7c4322d716fb19"
dependencies = [
"anstyle",
"windows-sys 0.52.0",
]
[[package]] [[package]]
name = "approx" name = "approx"
version = "0.5.1" version = "0.5.1"
@ -317,6 +372,12 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b" checksum = "3d7b894f5411737b7867f4827955924d7c254fc9f4d91a6aad6b097804b1018b"
[[package]]
name = "colorchoice"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422"
[[package]] [[package]]
name = "combine" name = "combine"
version = "4.6.7" version = "4.6.7"
@ -537,6 +598,29 @@ dependencies = [
"syn 2.0.66", "syn 2.0.66",
] ]
[[package]]
name = "env_filter"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a009aa4810eb158359dda09d0c87378e4bbb89b5a801f016885a4707ba24f7ea"
dependencies = [
"log",
"regex",
]
[[package]]
name = "env_logger"
version = "0.11.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "38b35839ba51819680ba087cd351788c9a3c476841207e0b8cee0b04722343b9"
dependencies = [
"anstream",
"anstyle",
"env_filter",
"humantime",
"log",
]
[[package]] [[package]]
name = "equivalent" name = "equivalent"
version = "1.0.1" version = "1.0.1"
@ -727,6 +811,9 @@ checksum = "40ecd4077b5ae9fd2e9e169b102c6c330d0605168eb0e8bf79952b256dbefffd"
name = "group-bot" name = "group-bot"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"env_logger",
"hashbrown 0.14.5",
"log",
"serde", "serde",
"tokio", "tokio",
"toml", "toml",
@ -760,6 +847,11 @@ name = "hashbrown"
version = "0.14.5" version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
dependencies = [
"ahash 0.8.11",
"allocator-api2",
"equivalent",
]
[[package]] [[package]]
name = "heck" name = "heck"
@ -884,6 +976,12 @@ version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904" checksum = "d897f394bad6a705d5f4104762e116a75639e470d80901eed05a860a95cb1904"
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]] [[package]]
name = "hyper" name = "hyper"
version = "1.3.1" version = "1.3.1"
@ -1027,6 +1125,12 @@ version = "2.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3" checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3"
[[package]]
name = "is_terminal_polyfill"
version = "1.70.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8478577c03552c21db0e2724ffb8986a5ce7af88107e6be5d2ee6e158c12800"
[[package]] [[package]]
name = "itertools" name = "itertools"
version = "0.10.5" version = "0.10.5"
@ -2407,6 +2511,12 @@ dependencies = [
"percent-encoding", "percent-encoding",
] ]
[[package]]
name = "utf8parse"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821"
[[package]] [[package]]
name = "uuid" name = "uuid"
version = "1.8.0" version = "1.8.0"

View File

@ -10,6 +10,9 @@ veloren-client = { git = "https://gitlab.com/veloren/veloren", branch = "master"
veloren-common-net = { git = "https://gitlab.com/veloren/veloren", branch = "master" } veloren-common-net = { git = "https://gitlab.com/veloren/veloren", branch = "master" }
toml = "0.8.14" toml = "0.8.14"
serde = { version = "1.0.203", features = ["derive"] } serde = { version = "1.0.203", features = ["derive"] }
env_logger = "0.11.3"
log = "0.4.21"
hashbrown = { version = "0.14.5", features = ["equivalent"] }
[patch.crates-io] [patch.crates-io]
specs = { git = "https://github.com/amethyst/specs.git", rev = "4e2da1df29ee840baa9b936593c45592b7c9ae27" } specs = { git = "https://github.com/amethyst/specs.git", rev = "4e2da1df29ee840baa9b936593c45592b7c9ae27" }

View File

@ -1,7 +1,8 @@
use std::{sync::Arc, time::Duration}; use std::{collections::VecDeque, sync::Arc, time::Duration};
use log::{debug, info};
use tokio::runtime::Runtime; use tokio::runtime::Runtime;
use veloren_client::{addr::ConnectionArgs, Client, Event}; use veloren_client::{addr::ConnectionArgs, Client, Event as VelorenEvent};
use veloren_common::{ use veloren_common::{
clock::Clock, clock::Clock,
comp::{invite::InviteKind, ChatType, ControllerInputs}, comp::{invite::InviteKind, ChatType, ControllerInputs},
@ -13,11 +14,21 @@ use veloren_common_net::msg::PlayerInfo;
use crate::Config; use crate::Config;
enum Event {
Admin(String),
Ban(String),
Cheese,
Invite(Uid),
Kick(Uid),
Unban(String),
}
pub struct Bot { pub struct Bot {
client: Client, client: Client,
clock: Clock, clock: Clock,
admin_list: Vec<Uuid>, admin_list: Vec<Uuid>,
ban_list: Vec<Uuid>, ban_list: Vec<Uuid>,
events: VecDeque<Event>,
} }
impl Bot { impl Bot {
@ -27,6 +38,8 @@ impl Bot {
admin_list: Vec<Uuid>, admin_list: Vec<Uuid>,
ban_list: Vec<Uuid>, ban_list: Vec<Uuid>,
) -> Result<Self, String> { ) -> Result<Self, String> {
info!("Connecting to veloren");
let client = connect_to_veloren(username, password)?; let client = connect_to_veloren(username, password)?;
let clock = Clock::new(Duration::from_secs_f64(0.1)); let clock = Clock::new(Duration::from_secs_f64(0.1));
@ -35,10 +48,13 @@ impl Bot {
clock, clock,
admin_list, admin_list,
ban_list, ban_list,
events: VecDeque::new(),
}) })
} }
pub fn select_character(&mut self) -> Result<(), String> { pub fn select_character(&mut self) -> Result<(), String> {
info!("Selecting a character");
self.client.load_character_list(); self.client.load_character_list();
while self.client.character_list().loading { while self.client.character_list().loading {
@ -70,13 +86,23 @@ impl Bot {
} }
pub fn tick(&mut self) -> Result<(), String> { pub fn tick(&mut self) -> Result<(), String> {
let events = self let veloren_events = self
.client .client
.tick(ControllerInputs::default(), self.clock.dt()) .tick(ControllerInputs::default(), self.clock.dt())
.map_err(|error| format!("{error:?}"))?; .map_err(|error| format!("{error:?}"))?;
for event in events { debug!("Tick! Handling {} bot events", self.events.len());
self.handle_event(event)?;
while !self.events.is_empty() {
if let Some(event) = self.events.pop_front() {
self.handle_event(event)?;
}
}
debug!("Tick! Handling {} veloren events", veloren_events.len());
for veloren_event in veloren_events {
self.handle_veloren_event(veloren_event)?;
} }
self.client.cleanup(); self.client.cleanup();
@ -85,8 +111,8 @@ impl Bot {
Ok(()) Ok(())
} }
fn handle_event(&mut self, event: Event) -> Result<(), String> { fn handle_veloren_event(&mut self, event: VelorenEvent) -> Result<(), String> {
if let Event::Chat(message) = event { if let VelorenEvent::Chat(message) = event {
match message.chat_type { match message.chat_type {
ChatType::Tell(sender, _) | ChatType::Group(sender, _) => { ChatType::Tell(sender, _) | ChatType::Group(sender, _) => {
let sender_info = self.client.player_list().get(&sender).unwrap().clone(); let sender_info = self.client.player_list().get(&sender).unwrap().clone();
@ -105,59 +131,106 @@ impl Bot {
fn handle_message(&mut self, content: &str, sender: &PlayerInfo) -> Result<(), String> { fn handle_message(&mut self, content: &str, sender: &PlayerInfo) -> Result<(), String> {
let mut words = content.split_whitespace(); let mut words = content.split_whitespace();
let command = if let Some(command) = words.next() {
command
} else {
return Ok(());
};
if let Some(command) = words.next() { match command {
match command { "admin" => {
"admin" => { if self.admin_list.contains(&sender.uuid) || self.admin_list.is_empty() {
if self.admin_list.contains(&sender.uuid) || self.admin_list.is_empty() { for word in words {
self.adminify_players(words)?; self.events.push_back(Event::Admin(word.to_string()));
} }
} }
"ban" => { }
if self.admin_list.contains(&sender.uuid) { "ban" => {
self.kick_players(words.clone()); if self.admin_list.contains(&sender.uuid) {
self.ban_players(words)?; for word in words {
let uid = self.find_uid(word)?;
self.events.push_back(Event::Kick(uid.clone()));
self.events.push_back(Event::Ban(word.to_string()));
} }
} }
"cheese" => self }
.client "cheese" => {
.send_command("group".to_string(), vec!["I love cheese!".to_string()]), let uid = self.find_uid(&sender.player_alias)?;
"inv" => {
if !self.ban_list.contains(&sender.uuid) { if self.client.group_members().contains_key(&uid) {
if content == "inv" { self.events.push_back(Event::Cheese);
self.invite_players([sender.player_alias.as_str()].into_iter()); }
} else { }
self.invite_players(words); "inv" => {
if !self.ban_list.contains(&sender.uuid) {
if content == "inv" {
let uid = self.find_uid(&sender.player_alias)?;
self.events.push_back(Event::Invite(uid));
} else {
for word in words {
let uid = self.find_uid(word)?;
self.events.push_back(Event::Invite(uid));
} }
} }
} }
"kick" => { }
if self.admin_list.contains(&sender.uuid) { "kick" => {
self.kick_players(words); if self.admin_list.contains(&sender.uuid) {
for word in words {
let uid = self.find_uid(word)?;
self.events.push_back(Event::Kick(uid));
} }
} }
"unban" => { }
if self.admin_list.contains(&sender.uuid) { "unban" => {
self.unban_players(words)?; if self.admin_list.contains(&sender.uuid) {
for word in words {
self.events.push_back(Event::Unban(word.to_string()));
} }
} }
_ => {} }
_ => {}
}
Ok(())
}
fn handle_event(&mut self, event: Event) -> Result<(), String> {
match event {
Event::Admin(name) => {
self.adminify_player(&name)?;
}
Event::Ban(name) => {
self.ban_player(&name)?;
}
Event::Cheese => self
.client
.send_command("group".to_string(), vec!["I love cheese!".to_string()]),
Event::Invite(uid) => {
self.client.send_invite(uid, InviteKind::Group);
}
Event::Kick(uid) => {
self.client.kick_from_group(uid);
}
Event::Unban(name) => {
self.unban_player(&name)?;
} }
} }
Ok(()) Ok(())
} }
fn adminify_players<'a, T: Iterator<Item = &'a str>>( fn adminify_player(&mut self, name: &str) -> Result<(), String> {
&mut self, info!("Adminifying {name}");
names: T,
) -> Result<(), String> { let uuid = self.find_uuid(name)?;
for name in names {
if let Some(uuid) = self.find_uuid(&name) { if !self.admin_list.contains(&uuid) && !self.ban_list.contains(&uuid) {
if !self.admin_list.contains(&uuid) && !self.ban_list.contains(&uuid) { self.admin_list.push(uuid);
self.admin_list.push(uuid);
}
}
} }
let old_config = Config::read()?; let old_config = Config::read()?;
@ -173,13 +246,13 @@ impl Bot {
Ok(()) Ok(())
} }
fn ban_players<'a, T: Iterator<Item = &'a str>>(&mut self, names: T) -> Result<(), String> { fn ban_player(&mut self, name: &str) -> Result<(), String> {
for name in names { info!("Banning {name}");
if let Some(uuid) = self.find_uuid(&name) {
if !self.admin_list.contains(&uuid) && !self.ban_list.contains(&uuid) { let uuid = self.find_uuid(name)?;
self.ban_list.push(uuid);
} if !self.admin_list.contains(&uuid) && !self.ban_list.contains(&uuid) {
} self.ban_list.push(uuid);
} }
let old_config = Config::read()?; let old_config = Config::read()?;
@ -195,43 +268,18 @@ impl Bot {
Ok(()) Ok(())
} }
fn invite_players<'a, T: Iterator<Item = &'a str>>(&mut self, names: T) { fn unban_player(&mut self, name: &str) -> Result<(), String> {
for name in names { info!("Unbanning {name}");
if let Some(player_id) = self.find_uid(name) {
self.client
.send_invite(player_id.clone(), InviteKind::Group);
} else {
eprintln!("{name} could not be invited")
}
}
}
fn kick_players<'a, T: Iterator<Item = &'a str>>(&mut self, names: T) { let uuid = self.find_uuid(name)?;
for name in names {
if let Some(uid) = self.find_uid(&name) {
if let Some(uuid) = self.find_uuid(&name) {
if !self.admin_list.contains(&uuid) {
self.client.kick_from_group(uid.clone());
}
}
}
}
}
fn unban_players<'a, T: Iterator<Item = &'a str>>(&mut self, names: T) -> Result<(), String> { if let Some(uuid) = self
let uuids = names .ban_list
.filter_map(|name| self.find_uuid(name)) .iter()
.collect::<Vec<Uuid>>(); .enumerate()
.find_map(|(index, banned)| if &uuid == banned { Some(index) } else { None })
for uuid in uuids { {
if let Some(index) = self self.ban_list.remove(uuid);
.ban_list
.iter()
.enumerate()
.find_map(|(index, banned)| if &uuid == banned { Some(index) } else { None })
{
self.ban_list.remove(index);
}
} }
let old_config = Config::read()?; let old_config = Config::read()?;
@ -247,24 +295,32 @@ impl Bot {
Ok(()) Ok(())
} }
fn find_uid<'a>(&'a self, name: &str) -> Option<&'a Uid> { fn find_uid(&self, name: &str) -> Result<Uid, String> {
self.client.player_list().iter().find_map(|(id, info)| { self.client
if info.player_alias == name { .player_list()
Some(id) .iter()
} else { .find_map(|(id, info)| {
None if info.player_alias == name {
} Some(id.clone())
}) } else {
None
}
})
.ok_or(format!("Failed to find uid for player {}", name))
} }
fn find_uuid(&self, name: &str) -> Option<Uuid> { fn find_uuid(&self, name: &str) -> Result<Uuid, String> {
self.client.player_list().iter().find_map(|(_, info)| { self.client
if info.player_alias == name { .player_list()
Some(info.uuid) .iter()
} else { .find_map(|(_, info)| {
None if info.player_alias == name {
} Some(info.uuid)
}) } else {
None
}
})
.ok_or(format!("Failed to find uid for player {}", name))
} }
} }

View File

@ -34,6 +34,8 @@ impl Config {
} }
fn main() { fn main() {
env_logger::init();
let config = Config::read().unwrap(); let config = Config::read().unwrap();
let mut bot = Bot::new( let mut bot = Bot::new(
&config.username, &config.username,