Browse Source

ClientLocation struct

pbs
jake 5 years ago
parent
commit
d1dbd8d043
  1. 2
      src/main.rs
  2. 106
      src/ship/location.rs
  3. 3
      src/ship/mod.rs
  4. 3
      src/ship/ship.rs

2
src/main.rs

@ -1,4 +1,6 @@
#![allow(incomplete_features)]
#![feature(const_generics)]
#![feature(maybe_uninit_extra)]
mod common;

106
src/ship/location.rs

@ -0,0 +1,106 @@
use crate::common::serverstate::ClientId;
// TODO: room passwords?
#[derive(Copy, Clone)]
pub struct ClientArea<const N: usize> {
clients: [Option<ClientId>; N],
}
impl<const N: usize> ClientArea<{N}> {
pub fn new() -> ClientArea<{N}> {
let mut clients: [std::mem::MaybeUninit<Option<ClientId>>; N] = unsafe {
std::mem::MaybeUninit::uninit().assume_init()
};
for i in clients.iter_mut() {
i.write(None);
}
ClientArea {
clients: unsafe { (&clients as *const _ as *const [Option<ClientId>; N]).read()}
}
}
fn add(&mut self, id: ClientId) -> Option<usize> {
for (i, client) in self.clients.iter_mut().enumerate() {
if client.is_none() {
*client = Some(id);
return Some(i);
}
}
return None;
}
fn remove(&mut self, id: ClientId) {
for client in self.clients.iter_mut() {
if *client == Some(id) {
*client = None
}
}
}
}
pub type Lobby = ClientArea<12>;
pub type Room = ClientArea<4>;
pub type LobbyId = usize;
pub type RoomId = usize;
pub struct ClientLocation {
lobbies: [Lobby; 15],
rooms: [Option<Room>; 128],
}
enum JoinRoomError {
RoomDoesNotExist,
RoomFull,
}
enum JoinLobbyError {
LobbyDoesNotExist,
LobbyFull,
}
impl ClientLocation {
pub fn new() -> ClientLocation {
ClientLocation {
lobbies: [Lobby::new(); 15],
rooms: [None; 128],
}
}
pub fn add_to_lobby(&mut self, id: ClientId, lobby: LobbyId) -> Result<usize, JoinLobbyError> {
self.lobbies.get_mut(lobby)
.ok_or(JoinLobbyError::LobbyDoesNotExist)?
.add(id)
.ok_or(JoinLobbyError::LobbyFull)
}
pub fn add_to_room(&mut self, id: ClientId, room: RoomId) -> Result<usize, JoinRoomError> {
self.rooms.get_mut(room)
.ok_or(JoinRoomError::RoomDoesNotExist)?
.as_mut()
.ok_or(JoinRoomError::RoomDoesNotExist)?
.add(id)
.ok_or(JoinRoomError::RoomFull)
}
pub fn get_client_neighbors(&self, id: ClientId) -> Vec<ClientId> {
for lobby in self.lobbies.iter() {
if lobby.clients.contains(&Some(id)) {
return lobby.clients.iter().filter(|c| c.is_some()).map(|c| c.unwrap()).collect();
}
}
for room in self.rooms.iter() {
if let Some(room) = room {
if room.clients.contains(&Some(id)) {
return room.clients.iter().filter(|c| c.is_some()).map(|c| c.unwrap()).collect();
}
}
}
panic!()
}
}

3
src/ship/mod.rs

@ -1 +1,2 @@
pub mod ship;
pub mod ship;
pub mod location;

3
src/ship/ship.rs

@ -16,6 +16,7 @@ use crate::entity::gateway::EntityGateway;
use crate::entity::account::{UserAccount, USERFLAG_NEWCHAR, USERFLAG_DRESSINGROOM};
use crate::entity::character::Character;
use crate::login::login::get_login_status;
use crate::ship::location::ClientLocation;
pub const SHIP_PORT: u16 = 23423;
@ -73,6 +74,7 @@ struct ClientState {
character: Character,
session: Session,
block: u32,
client_location: ClientLocation,
}
impl ClientState {
@ -82,6 +84,7 @@ impl ClientState {
character: Character::default(),
session: Session::new(),
block: 0,
client_location: ClientLocation::new(),
}
}
}

Loading…
Cancel
Save