~yujiri/roguelike

bdebbf953e9997005995e41eb3205aa72e713ec9 — Evin Yulo 9 months ago 83ac7df
add make public/private button
4 files changed, 36 insertions(+), 3 deletions(-)

M client/src/lobby.rs
M client/src/main.rs
M common/src/lib.rs
M server/src/main.rs
M client/src/lobby.rs => client/src/lobby.rs +9 -3
@@ 5,7 5,7 @@ pub struct State {
}
impl State {
	pub fn from_create(me: PlayerExternal, code: LobbyCode) -> Self {
		Self{ lobby: LobbyExternal{code, players: vec![me]} }
		Self{ lobby: LobbyExternal{code, players: vec![me], public: false} }
	}
	pub fn from_join(me: PlayerExternal, mut lobby: LobbyExternal) -> Self {
		lobby.players.push(me);


@@ 21,12 21,18 @@ impl State {
		let x = mode.w as i16 / 2;
		let y = mode.h as i16 - 108;
		sdl.draw_text_centered(&to_hex(&self.lobby.code), WHITE, x as i32, y as i32);
		let clicked = sdl.button(x - 108, y, "Leave", GREEN, BLACK);
		let clicked = sdl.button(x - 216, y, "Leave", GREEN, BLACK);
		if clicked {
			outgoing.lock().unwrap().push(GlobalAction::Leave);
			return true;
		}
		let clicked = sdl.button(x + 108, y, "Start", GREEN, BLACK);
		let toggle_public_text = if self.lobby.public { "Make (P)rivate" } else { "Make (P)ublic" };
		let clicked = sdl.button(x, y, toggle_public_text, GREEN, BLACK);
		if clicked || sdl.input.keys_down.iter().any(|k| *k == Keycode::P) {
			outgoing.lock().unwrap().push(GlobalAction::SetPublic(!self.lobby.public));
			self.lobby.public = !self.lobby.public;
		}
		let clicked = sdl.button(x + 216, y, "Start", GREEN, BLACK);
		if clicked {
			outgoing.lock().unwrap().push(GlobalAction::Start);
		}

M client/src/main.rs => client/src/main.rs +3 -0
@@ 60,6 60,9 @@ impl Globals {
			(State::Search(_), GlobalEvent::Accepted(lobby)) => {
				self.state = State::Lobby(lobby::State::from_join(self.me.clone(), lobby));
			},
			(State::Lobby(state), GlobalEvent::LobbyChanged(lobby)) => {
				state.lobby = lobby;
			},
			(State::Lobby(state), GlobalEvent::Joined(player)) => {
				state.lobby.players.push(player);
			},

M common/src/lib.rs => common/src/lib.rs +2 -0
@@ 25,6 25,7 @@ pub enum GlobalEvent {
	Left(Id),
	Available(LobbyExternal),
	Unavailable(LobbyCode),
	LobbyChanged(LobbyExternal),
	Started,
	Tick,
	Action(Id, Vec<u8>),


@@ 39,5 40,6 @@ pub struct PlayerExternal {
#[derive(Clone, Serialize, Deserialize)]
pub struct LobbyExternal {
	pub code: LobbyCode,
	pub public: bool,
	pub players: Vec<PlayerExternal>,
}

M server/src/main.rs => server/src/main.rs +22 -0
@@ 39,6 39,7 @@ impl Lobby {
	pub fn external(&self) -> LobbyExternal {
		LobbyExternal {
			code: self.code,
			public: self.public,
			players: self.players.iter().map(|p| p.read().unwrap().external.clone()).collect(),
		}
	}


@@ 150,6 151,27 @@ fn handle_lobby_action(ctx: &Arc<RwLock<Context>>, player: Arc<RwLock<Player>>, 
				}
			}
		},
		GlobalAction::SetPublic(public) => {
			let lobby = match &player.read().unwrap().status {
				PlayerStatus::Lobby(v) => Arc::clone(v),
				_ => return,
			};
			let mut lobby_unlocked = lobby.write().unwrap();
			if lobby_unlocked.in_game || lobby_unlocked.public == public { return }
			lobby_unlocked.public = public;
			let lobby_external = lobby_unlocked.external();
			for player in &lobby_unlocked.players {
				player.write().unwrap().outgoing.push(GlobalEvent::LobbyChanged(lobby_external.clone()));
			}
			let event = if lobby_unlocked.public {
				GlobalEvent::Available(lobby_unlocked.external())
			} else {
				GlobalEvent::Unavailable(lobby_unlocked.code)
			};
			for searcher in &ctx.read().unwrap().searchers {
				searcher.write().unwrap().outgoing.push(event.clone());
			}
		},
		GlobalAction::Start => {
			let lobby = match &player.read().unwrap().status {
				PlayerStatus::Lobby(v) => Arc::clone(v),