diff options
Diffstat (limited to 'server')
-rw-r--r-- | server/Cargo.toml | 3 | ||||
-rw-r--r-- | server/examples/client.rs | 4 | ||||
-rw-r--r-- | server/protocol/Cargo.toml | 9 | ||||
-rw-r--r-- | server/protocol/src/lib.rs (renamed from server/src/protocol.rs) | 23 | ||||
-rw-r--r-- | server/replaytool/Cargo.toml | 18 | ||||
-rw-r--r-- | server/replaytool/src/main.rs | 153 | ||||
-rw-r--r-- | server/src/bin/graph.rs | 2 | ||||
-rw-r--r-- | server/src/customer/mod.rs | 8 | ||||
-rw-r--r-- | server/src/customer/movement.rs | 3 | ||||
-rw-r--r-- | server/src/customer/pathfinding.rs | 6 | ||||
-rw-r--r-- | server/src/data.rs | 6 | ||||
-rw-r--r-- | server/src/entity/conveyor.rs | 3 | ||||
-rw-r--r-- | server/src/entity/mod.rs | 4 | ||||
-rw-r--r-- | server/src/game.rs | 19 | ||||
-rw-r--r-- | server/src/interaction.rs | 2 | ||||
-rw-r--r-- | server/src/lib.rs | 1 | ||||
-rw-r--r-- | server/src/main.rs | 7 | ||||
-rw-r--r-- | server/src/state.rs | 7 |
18 files changed, 237 insertions, 41 deletions
diff --git a/server/Cargo.toml b/server/Cargo.toml index 6b9037ac..da084855 100644 --- a/server/Cargo.toml +++ b/server/Cargo.toml @@ -5,7 +5,6 @@ edition = "2021" default-run = "hurrycurry-server" [dependencies] -glam = { version = "0.28.0", features = ["serde"] } log = "0.4.22" env_logger = "0.11.3" anyhow = "1.0.86" @@ -20,3 +19,5 @@ shlex = "1.3.0" clap = { version = "4.5.8", features = ["derive"] } fake = "2.9.2" pollster = "0.3.0" + +hurrycurry-protocol = { path = "protocol" } diff --git a/server/examples/client.rs b/server/examples/client.rs index 487c9738..70b1bb00 100644 --- a/server/examples/client.rs +++ b/server/examples/client.rs @@ -15,15 +15,13 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ +use hurrycurry_protocol::{glam::Vec2, PacketC, PacketS}; use std::{ io::{stdin, BufRead, BufReader, Write}, net::TcpStream, thread, }; -use glam::Vec2; -use hurrycurry_server::protocol::{PacketC, PacketS}; - fn main() { let mut sock = TcpStream::connect("127.0.0.1:27031").unwrap(); diff --git a/server/protocol/Cargo.toml b/server/protocol/Cargo.toml new file mode 100644 index 00000000..8fc63121 --- /dev/null +++ b/server/protocol/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "hurrycurry-protocol" +version = "0.1.0" +edition = "2021" + +[dependencies] +serde = { version = "1.0.204", features = ["derive"] } +glam = { version = "0.28.0", features = ["serde"] } + diff --git a/server/src/protocol.rs b/server/protocol/src/lib.rs index fc8abda4..36a496a0 100644 --- a/server/src/protocol.rs +++ b/server/protocol/src/lib.rs @@ -15,9 +15,11 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -use crate::data::Gamedata; use glam::{IVec2, Vec2}; use serde::{Deserialize, Serialize}; +use std::collections::HashSet; + +pub use glam; #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord, Hash)] #[serde(transparent)] @@ -36,6 +38,16 @@ pub struct RecipeIndex(pub usize); #[serde(transparent)] pub struct DemandIndex(pub usize); +#[derive(Debug, Clone, Serialize, Deserialize, Default)] +#[rustfmt::skip] +pub struct ClientGamedata { + pub item_names: Vec<String>, + pub tile_names: Vec<String>, + pub tile_collide: Vec<bool>, + pub tile_interact: Vec<bool>, + pub map_names: HashSet<String>, +} + #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "snake_case", tag = "type")] pub enum PacketS { @@ -67,6 +79,10 @@ pub enum PacketS { ReplaceHand { item: Option<ItemIndex>, }, + /// For use in replay sessions only + ReplayTick { + dt: f64, + }, } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -84,7 +100,7 @@ pub enum PacketC { id: PlayerID, }, Data { - data: Gamedata, + data: ClientGamedata, }, AddPlayer { id: PlayerID, @@ -144,6 +160,9 @@ pub enum PacketC { Error { message: String, }, + + /// For use in replay sessions only + ReplayStart, } #[derive(Debug, Clone, Serialize, Deserialize, Copy)] diff --git a/server/replaytool/Cargo.toml b/server/replaytool/Cargo.toml new file mode 100644 index 00000000..e6c1cc23 --- /dev/null +++ b/server/replaytool/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "hurrycurry-replaytool" +version = "0.1.0" +edition = "2021" + +[dependencies] +log = "0.4.22" +env_logger = "0.11.3" +anyhow = "1.0.86" +serde = { version = "1.0.204", features = ["derive"] } +tokio = { version = "1.38.0", features = ["full"] } +serde_json = "1.0.120" +tokio-tungstenite = { version = "0.23.1", features = ["native-tls"] } +futures-util = "0.3.30" +rand = "0.9.0-alpha.1" +clap = { version = "4.5.8", features = ["derive"] } + +hurrycurry-protocol = { path = "../protocol" } diff --git a/server/replaytool/src/main.rs b/server/replaytool/src/main.rs new file mode 100644 index 00000000..eae75676 --- /dev/null +++ b/server/replaytool/src/main.rs @@ -0,0 +1,153 @@ +/* + Hurry Curry! - a game about cooking + Copyright 2024 metamuffin + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +*/ +use anyhow::anyhow; +use clap::Parser; +use futures_util::{SinkExt, StreamExt}; +use hurrycurry_protocol::{PacketC, PacketS}; +use log::{debug, info, warn, LevelFilter}; +use serde::{Deserialize, Serialize}; +use std::{path::PathBuf, time::Instant}; +use tokio::{ + fs::File, + io::{AsyncBufReadExt, AsyncWriteExt, BufReader, BufWriter}, + net::TcpListener, +}; +use tokio_tungstenite::tungstenite::Message; + +#[derive(Parser)] +enum Args { + Record { url: String, output: PathBuf }, + Replay { input: PathBuf }, +} + +#[derive(Serialize, Deserialize)] +struct Event { + ts: f64, + packet: PacketC, +} + +#[tokio::main] +async fn main() -> anyhow::Result<()> { + env_logger::builder() + .filter_level(LevelFilter::Info) + .parse_env("LOG") + .init(); + + let args = Args::parse(); + + match args { + Args::Record { url, output } => { + let mut file = BufWriter::new(File::create(&output).await?); + info!("connecting to {url:?}..."); + let (mut sock, _) = tokio_tungstenite::connect_async(url).await?; + info!("starting recording."); + let start = Instant::now(); + + while let Some(Ok(message)) = sock.next().await { + match message { + Message::Text(line) => { + let packet: PacketC = match serde_json::from_str(&line) { + Ok(p) => p, + Err(e) => { + warn!("invalid packet: {e}"); + break; + } + }; + debug!("<- {packet:?}"); + file.write_all( + format!( + "{}\n", + serde_json::to_string(&Event { + ts: start.elapsed().as_secs_f64(), + packet: packet + }) + .unwrap() + ) + .as_bytes(), + ) + .await? + } + Message::Close(_) => break, + _ => (), + } + } + } + Args::Replay { input } => { + let ws_listener = TcpListener::bind("0.0.0.0:27032").await?; + info!("listening for websockets on {}", ws_listener.local_addr()?); + + loop { + let mut file = BufReader::new(File::open(&input).await?).lines(); + let mut next_event = + serde_json::from_str::<Event>(&file.next_line().await?.ok_or(anyhow!("eof"))?)?; + let mut time = 0.; + + info!("ready"); + let (sock, addr) = ws_listener.accept().await?; + let Ok(mut sock) = tokio_tungstenite::accept_async(sock).await else { + warn!("invalid ws handshake"); + continue; + }; + info!("{addr} connected via ws"); + + sock.send(tokio_tungstenite::tungstenite::Message::Text( + serde_json::to_string(&PacketC::ReplayStart).unwrap(), + )) + .await?; + while let Some(Ok(message)) = sock.next().await { + match message { + Message::Text(line) => { + let packet: PacketS = match serde_json::from_str(&line) { + Ok(p) => p, + Err(e) => { + warn!("invalid packet: {e}"); + break; + } + }; + debug!("<- {packet:?}"); + + match packet { + PacketS::ReplayTick { dt } => { + time += dt; + while next_event.ts < time { + debug!("<- {:?}", next_event.packet); + sock.send(tokio_tungstenite::tungstenite::Message::Text( + serde_json::to_string(&next_event.packet).unwrap(), + )) + .await?; + + let Some(next) = &file.next_line().await? else { + info!("reached end"); + break; + }; + next_event = serde_json::from_str::<Event>(next)?; + } + } + _ => (), + } + } + Message::Close(_) => break, + _ => (), + } + } + info!("{addr} left"); + } + } + } + Ok(()) +} diff --git a/server/src/bin/graph.rs b/server/src/bin/graph.rs index ace1b676..888119aa 100644 --- a/server/src/bin/graph.rs +++ b/server/src/bin/graph.rs @@ -16,10 +16,10 @@ */ use anyhow::{anyhow, Result}; +use hurrycurry_protocol::{ItemIndex, RecipeIndex}; use hurrycurry_server::{ data::{DataIndex, Demand}, interaction::Recipe, - protocol::{ItemIndex, RecipeIndex}, }; #[tokio::main] diff --git a/server/src/customer/mod.rs b/server/src/customer/mod.rs index 191f9e3b..10788206 100644 --- a/server/src/customer/mod.rs +++ b/server/src/customer/mod.rs @@ -18,14 +18,10 @@ pub mod movement; mod pathfinding; -use crate::{ - data::Gamedata, - game::Tile, - protocol::{DemandIndex, Message, PacketS, PlayerID}, -}; +use crate::{data::Gamedata, game::Tile}; use anyhow::{anyhow, Result}; use fake::{faker, Fake}; -use glam::IVec2; +use hurrycurry_protocol::{glam::IVec2, DemandIndex, Message, PacketS, PlayerID}; use log::debug; use movement::MovementBase; use pathfinding::{find_path, Path}; diff --git a/server/src/customer/movement.rs b/server/src/customer/movement.rs index ca7afdbe..34ed5b16 100644 --- a/server/src/customer/movement.rs +++ b/server/src/customer/movement.rs @@ -16,8 +16,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ -use crate::protocol::PacketS; -use glam::{IVec2, Vec2}; +use hurrycurry_protocol::{glam::{IVec2, Vec2}, PacketS}; use std::collections::HashSet; const PLAYER_SIZE: f32 = 0.4; diff --git a/server/src/customer/pathfinding.rs b/server/src/customer/pathfinding.rs index 743515b9..29ee4e00 100644 --- a/server/src/customer/pathfinding.rs +++ b/server/src/customer/pathfinding.rs @@ -16,8 +16,10 @@ */ use super::movement::MovementBase; -use crate::protocol::PacketS; -use glam::{IVec2, Vec2}; +use hurrycurry_protocol::{ + glam::{IVec2, Vec2}, + PacketS, +}; use log::debug; use std::{ cmp::Ordering, diff --git a/server/src/data.rs b/server/src/data.rs index bca7f543..9fd3e95c 100644 --- a/server/src/data.rs +++ b/server/src/data.rs @@ -19,10 +19,12 @@ use crate::{ entity::{construct_entity, Entity, EntityDecl}, interaction::Recipe, - protocol::{DemandIndex, ItemIndex, RecipeIndex, TileIndex}, }; use anyhow::{anyhow, bail, Result}; -use glam::{IVec2, Vec2}; +use hurrycurry_protocol::{ + glam::{IVec2, Vec2}, + DemandIndex, ItemIndex, RecipeIndex, TileIndex, +}; use serde::{Deserialize, Serialize}; use std::{ collections::{HashMap, HashSet}, diff --git a/server/src/entity/conveyor.rs b/server/src/entity/conveyor.rs index 0a73b55c..4d11ffe1 100644 --- a/server/src/entity/conveyor.rs +++ b/server/src/entity/conveyor.rs @@ -19,10 +19,9 @@ use super::EntityT; use crate::{ data::Gamedata, game::{interact_effect, Tile}, - protocol::{ItemLocation, PacketC}, }; use anyhow::{anyhow, Result}; -use glam::IVec2; +use hurrycurry_protocol::{glam::IVec2, ItemLocation, PacketC}; use std::collections::{HashMap, VecDeque}; #[derive(Debug, Default, Clone)] diff --git a/server/src/entity/mod.rs b/server/src/entity/mod.rs index 5cb3ed0f..925ed5f4 100644 --- a/server/src/entity/mod.rs +++ b/server/src/entity/mod.rs @@ -16,10 +16,10 @@ */ pub mod conveyor; -use crate::{data::Gamedata, game::Tile, protocol::PacketC}; +use crate::{data::Gamedata, game::Tile}; use anyhow::{anyhow, Result}; use conveyor::Conveyor; -use glam::IVec2; +use hurrycurry_protocol::{glam::IVec2, PacketC}; use serde::{Deserialize, Serialize}; use std::collections::{HashMap, VecDeque}; diff --git a/server/src/game.rs b/server/src/game.rs index 851d485c..32fb7ac9 100644 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -20,16 +20,16 @@ use crate::{ data::Gamedata, entity::{Entity, EntityT}, interaction::{interact, tick_slot, InteractEffect, TickEffect}, - protocol::{ - ItemIndex, ItemLocation, Message, PacketC, PacketS, PlayerID, RecipeIndex, TileIndex, - }, }; use anyhow::{anyhow, bail, Result}; -use glam::{IVec2, Vec2}; +use hurrycurry_protocol::{ + glam::{IVec2, Vec2}, + ClientGamedata, ItemIndex, ItemLocation, Message, PacketC, PacketS, PlayerID, RecipeIndex, + TileIndex, +}; use log::{info, warn}; use std::{ collections::{HashMap, VecDeque}, - ops::Deref, sync::Arc, time::{Duration, Instant}, }; @@ -164,7 +164,13 @@ impl Game { pub fn prime_client(&self) -> Vec<PacketC> { let mut out = Vec::new(); out.push(PacketC::Data { - data: self.data.deref().to_owned(), + data: ClientGamedata { + item_names: self.data.item_names.clone(), + tile_names: self.data.tile_names.clone(), + tile_collide: self.data.tile_collide.clone(), + tile_interact: self.data.tile_interact.clone(), + map_names: self.data.map_names.clone(), + }, }); for (&id, player) in &self.players { out.push(PacketC::AddPlayer { @@ -408,6 +414,7 @@ impl Game { item, }) } + PacketS::ReplayTick { .. } => bail!("packet not supported in this session"), } if self.points != points_before { diff --git a/server/src/interaction.rs b/server/src/interaction.rs index 25b96813..b3f6af8c 100644 --- a/server/src/interaction.rs +++ b/server/src/interaction.rs @@ -18,8 +18,8 @@ use crate::{ data::Gamedata, game::{Involvement, Item}, - protocol::{ItemIndex, TileIndex}, }; +use hurrycurry_protocol::{ItemIndex, TileIndex}; use log::info; use serde::{Deserialize, Serialize}; diff --git a/server/src/lib.rs b/server/src/lib.rs index a6d29c03..6d0d4e26 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -21,5 +21,4 @@ pub mod data; pub mod entity; pub mod game; pub mod interaction; -pub mod protocol; pub mod state; diff --git a/server/src/main.rs b/server/src/main.rs index 99834fbd..4c6bcc34 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -18,6 +18,8 @@ use anyhow::{anyhow, Result}; use clap::Parser; use futures_util::{SinkExt, StreamExt}; +use hurrycurry_protocol::{PacketC, PacketS, PlayerID}; +use hurrycurry_server::{data::DATA_DIR, state::State}; use log::{debug, info, warn, LevelFilter}; use std::{path::PathBuf, process::exit, str::FromStr, sync::Arc, time::Duration}; use tokio::{ @@ -27,11 +29,6 @@ use tokio::{ time::interval, }; use tokio_tungstenite::tungstenite::Message; -use hurrycurry_server::{ - data::DATA_DIR, - protocol::{PacketC, PacketS, PlayerID}, - state::State, -}; #[derive(Parser)] struct Args { diff --git a/server/src/state.rs b/server/src/state.rs index 72d3e911..5af6f181 100644 --- a/server/src/state.rs +++ b/server/src/state.rs @@ -17,13 +17,10 @@ */ use std::time::Duration; -use crate::{ - data::DataIndex, - game::Game, - protocol::{Message, PacketC, PacketS, PlayerID}, -}; +use crate::{data::DataIndex, game::Game}; use anyhow::{anyhow, bail, Result}; use clap::{Parser, ValueEnum}; +use hurrycurry_protocol::{Message, PacketC, PacketS, PlayerID}; use log::debug; use tokio::sync::broadcast::Sender; |