diff options
Diffstat (limited to 'server/src')
-rw-r--r-- | server/src/customer/mod.rs | 29 | ||||
-rw-r--r-- | server/src/data.rs | 21 | ||||
-rw-r--r-- | server/src/game.rs | 94 | ||||
-rw-r--r-- | server/src/protocol.rs | 40 |
4 files changed, 105 insertions, 79 deletions
diff --git a/server/src/customer/mod.rs b/server/src/customer/mod.rs index 7c6cd235..ecd99250 100644 --- a/server/src/customer/mod.rs +++ b/server/src/customer/mod.rs @@ -4,13 +4,13 @@ mod pathfinding; use crate::{ data::Gamedata, game::Game, - protocol::{ItemIndex, Message, PacketC, PacketS, PlayerID}, + protocol::{DemandIndex, ItemIndex, Message, PacketC, PacketS, PlayerID}, }; use glam::{IVec2, Vec2}; use log::{debug, error}; use movement::MovementBase; use pathfinding::{find_path, Path}; -use rand::thread_rng; +use rand::{random, thread_rng}; use std::{ collections::{HashMap, HashSet}, sync::Arc, @@ -36,7 +36,7 @@ struct DemandState { enum CustomerState { WalkingToChair { path: Path, chair: IVec2 }, - Waiting { chair: IVec2, demand: ItemIndex }, + Waiting { chair: IVec2, demand: DemandIndex }, Exiting { path: Path }, } @@ -83,8 +83,7 @@ pub async fn customer(game: Arc<RwLock<Game>>, mut grx: broadcast::Receiver<Pack match packet.unwrap() { PacketC::PutItem { .. } | PacketC::TakeItem { .. } - | PacketC::ProduceItem { .. } - | PacketC::ConsumeItem { .. } => { + | PacketC::SetTileItem { .. } => { let g = game.read().await; update_items(&mut state, &g) }, @@ -117,10 +116,10 @@ impl DemandState { // TODO insert sofa magic formula 5 } - pub fn generate_demand(&self) -> usize { + pub fn generate_demand(&self) -> DemandIndex { // TODO insert sofa magic formula - use rand::seq::IndexedRandom; - *self.data.demands.choose(&mut thread_rng()).unwrap() + + random::<usize>() % self.data.demands.len() } } @@ -165,7 +164,7 @@ impl CustomerManager { packets_out.push(( id, PacketS::Communicate { - message: Some(Message::Item(demand)), + message: Some(Message::Item(self.demand.data.demands[demand].from)), }, )); p.state = CustomerState::Waiting { @@ -186,11 +185,21 @@ impl CustomerManager { } }); if let Some(pos) = demand_pos { - if self.items.get(&pos) == Some(demand) { + let demand = &self.demand.data.demands[*demand]; + if self.items.get(&pos) == Some(&demand.from) { packets_out.push((id, PacketS::Communicate { message: None })); for edge in [true, false] { packets_out.push((id, PacketS::Interact { pos, edge })) } + packets_out.push(( + id, + PacketS::ReplaceHand { + item: Some(demand.to), + }, + )); + for edge in [true, false] { + packets_out.push((id, PacketS::Interact { pos, edge })) + } let path = find_path( &self.walkable, p.movement.position.as_ivec2(), diff --git a/server/src/data.rs b/server/src/data.rs index 3526d786..e467fefa 100644 --- a/server/src/data.rs +++ b/server/src/data.rs @@ -31,22 +31,34 @@ pub struct InitialMap { tiles: HashMap<String, String>, } +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DemandDecl { + from: String, + to: String, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Demand { + pub from: usize, + pub to: usize, +} + #[derive(Debug, Clone, Serialize, Deserialize, Default)] pub struct Gamedata { pub recipes: Vec<Recipe>, + pub demands: Vec<Demand>, pub item_names: Vec<String>, pub tile_names: Vec<String>, #[serde(skip)] pub initial_map: HashMap<IVec2, TileIndex>, pub chef_spawn: Vec2, pub customer_spawn: Vec2, - pub demands: Vec<usize>, } pub fn build_gamedata( recipes_in: Vec<RecipeDecl>, map_in: InitialMap, - demands_in: Vec<String>, + demands_in: Vec<DemandDecl>, ) -> Gamedata { let item_names = RwLock::new(Vec::new()); let tile_names = RwLock::new(Vec::new()); @@ -85,7 +97,10 @@ pub fn build_gamedata( } for d in demands_in { - demands.push(register(&item_names, d)) + demands.push(Demand { + from: register(&item_names, d.from), + to: register(&item_names, d.to), + }) } let mut chef_spawn = Vec2::new(0., 0.); diff --git a/server/src/game.rs b/server/src/game.rs index 225455bf..b48e1833 100644 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -80,8 +80,13 @@ impl Game { position: player.position, character: player.character, name: player.name.clone(), - item: player.item.as_ref().map(|i| i.kind), }); + if let Some(item) = &player.item { + out.push(PacketC::SetPlayerItem { + player: id, + item: Some(item.kind), + }) + } if let Some(c) = &player.communicate { out.push(PacketC::Communicate { player: id, @@ -101,9 +106,9 @@ impl Game { tile: tdata.kind.clone(), }); if let Some(item) = &tdata.item { - out.push(PacketC::ProduceItem { + out.push(PacketC::SetTileItem { tile, - item: item.kind, + item: Some(item.kind), }) } } @@ -133,7 +138,6 @@ impl Game { name, position: self.data.chef_spawn, character, - item: None, }); } PacketS::Leave => { @@ -145,9 +149,9 @@ impl Game { let pos = p.position.floor().as_ivec2(); if let Some(tile) = self.tiles.get_mut(&pos) { if tile.item.is_none() { - self.packet_out.push_back(PacketC::ProduceItem { + self.packet_out.push_back(PacketC::SetTileItem { tile: pos, - item: item.kind, + item: Some(item.kind), }); tile.item = Some(item); } @@ -204,21 +208,25 @@ impl Game { }), InteractEffect::Produce => { if tile_had_item { - self.packet_out - .push_back(PacketC::ConsumeItem { tile: pos }); + self.packet_out.push_back(PacketC::SetTileItem { + tile: pos, + item: None, + }); } if player_had_item { self.packet_out.push_back(PacketC::PutItem { player: pid, tile: pos, }); - self.packet_out - .push_back(PacketC::ConsumeItem { tile: pos }); + self.packet_out.push_back(PacketC::SetTileItem { + tile: pos, + item: None, + }); } if let Some(i) = &player.item { - self.packet_out.push_back(PacketC::ProduceItem { + self.packet_out.push_back(PacketC::SetTileItem { tile: pos, - item: i.kind, + item: Some(i.kind), }); self.packet_out.push_back(PacketC::TakeItem { player: pid, @@ -226,9 +234,9 @@ impl Game { }) } if let Some(i) = &tile.item { - self.packet_out.push_back(PacketC::ProduceItem { + self.packet_out.push_back(PacketC::SetTileItem { tile: pos, - item: i.kind, + item: Some(i.kind), }); } } @@ -245,6 +253,18 @@ impl Game { self.packet_out .push_back(PacketC::Communicate { player, message }) } + PacketS::ReplaceHand { item } => { + let pdata = self + .players + .get_mut(&player) + .ok_or(anyhow!("player does not exist"))?; + pdata.item = item.map(|i| Item { + kind: i, + active: None, + }); + self.packet_out + .push_back(PacketC::SetPlayerItem { player, item }) + } } Ok(()) } @@ -264,51 +284,13 @@ impl Game { .map(|i| i.progress), }), TickEffect::Produce => { - self.packet_out - .push_back(PacketC::ConsumeItem { tile: pos }); - if let Some(item) = &tile.item { - self.packet_out.push_back(PacketC::ProduceItem { - tile: pos, - item: item.kind, - }); - } + self.packet_out.push_back(PacketC::SetTileItem { + tile: pos, + item: tile.item.as_ref().map(|i| i.kind), + }); } } } - // let items = tile.items.iter().map(|e| self.items[e]).collect::<Vec<_>>(); - // tick_tile( - // dt, - // &self.data, - // tile.kind, - // &mut tile.active, - // items, - // |out| match out { - // Out::Take(_) | Out::Put => { - // unreachable!() - // } - // Out::Produce(kind) => { - // info!("produce"); - // let id = self.item_id_counter; - // self.item_id_counter += 1; - // self.items.insert(id, kind); - // tile.items.push(id); - // self.packet_out - // .push_back(PacketC::ProduceItem { id, pos, kind }); - // } - // Out::Consume(index) => { - // info!("consume"); - // let id = tile.items.remove(index); - // info!("left {:?}", tile.items); - // self.packet_out.push_back(PacketC::ConsumeItem { id, pos }); - // } - // Out::SetActive(progress) => { - // self.packet_out.push_back(PacketC::SetActive { - // tile: pos, - // progress, - // }); - // } - // }, - // ); } } } diff --git a/server/src/protocol.rs b/server/src/protocol.rs index 7eff2ba1..24fe2468 100644 --- a/server/src/protocol.rs +++ b/server/src/protocol.rs @@ -6,16 +6,36 @@ pub type PlayerID = i64; pub type ItemIndex = usize; pub type TileIndex = usize; pub type RecipeIndex = usize; +pub type DemandIndex = usize; #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(rename_all = "snake_case", tag = "type")] pub enum PacketS { - Join { name: String, character: usize }, + Join { + name: String, + character: usize, + }, Leave, - Position { pos: Vec2, rot: f32 }, - Interact { pos: IVec2, edge: bool }, - Collide { player: PlayerID, force: Vec2 }, - Communicate { message: Option<Message> }, + Position { + pos: Vec2, + rot: f32, + }, + Interact { + pos: IVec2, + edge: bool, + }, + Collide { + player: PlayerID, + force: Vec2, + }, + Communicate { + message: Option<Message>, + }, + + #[serde(skip)] + ReplaceHand { + item: Option<ItemIndex>, + }, } #[derive(Debug, Clone, Serialize, Deserialize)] @@ -37,7 +57,6 @@ pub enum PacketC { position: Vec2, character: usize, name: String, - item: Option<ItemIndex>, }, RemovePlayer { id: PlayerID, @@ -55,12 +74,13 @@ pub enum PacketC { player: PlayerID, tile: IVec2, }, - ProduceItem { + SetTileItem { tile: IVec2, - item: ItemIndex, + item: Option<ItemIndex>, }, - ConsumeItem { - tile: IVec2, + SetPlayerItem { + player: PlayerID, + item: Option<ItemIndex>, }, SetActive { tile: IVec2, |