Skip to content

Commit

Permalink
refactor: use pub(crate)
Browse files Browse the repository at this point in the history
  • Loading branch information
Erb3 committed Jul 28, 2024
1 parent 7e4a109 commit 7608e7f
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 63 deletions.
14 changes: 7 additions & 7 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clap::{command, Parser};

#[derive(clap::ValueEnum, Clone, Debug)]
pub enum LoggingLevel {
pub(crate) enum LoggingLevel {
Trace,
Debug,
Info,
Expand All @@ -25,25 +25,25 @@ impl From<LoggingLevel> for tracing_subscriber::filter::LevelFilter {

#[derive(Parser)]
#[command(name = "Sveio", version, about, author)]
pub struct Cli {
pub(crate) struct Cli {
/// Optional port to use. Default is 8085
#[arg(short, long, env = "SVEIO_PORT")]
pub port: Option<u32>,
pub(crate) port: Option<u32>,

/// Optional logging level to use. Default is info
#[arg(short, long, env = "SVEIO_LOGGING_LEVEL")]
pub logging: Option<LoggingLevel>,
pub(crate) logging: Option<LoggingLevel>,

/// Optional amount of seconds to allow guessing. Default is 7s
#[arg(long, env = "SVEIO_GUESS_TIME")]
pub guess_time: Option<u64>,
pub(crate) guess_time: Option<u64>,

/// Optional amount of seconds where players can see where the others
/// guessed. Default is 3s
#[arg(long, env = "SVEIO_SHOWCASE_TIME")]
pub showcase_time: Option<u64>,
pub(crate) showcase_time: Option<u64>,
}

pub fn get_settings() -> Cli {
pub(crate) fn get_settings() -> Cli {
Cli::parse()
}
24 changes: 12 additions & 12 deletions src/datasource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ use rand::thread_rng;
use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize, Clone)]
pub struct City {
pub name: String,
pub country: String,
pub latitude: f64,
pub longitude: f64,
pub(crate) struct City {
pub(crate) name: String,
pub(crate) country: String,
pub(crate) latitude: f64,
pub(crate) longitude: f64,
}

impl City {
pub fn anonymize(self) -> AnonymizedCity {
pub(crate) fn anonymize(self) -> AnonymizedCity {
AnonymizedCity {
country: self.country,
name: self.name,
Expand All @@ -20,17 +20,17 @@ impl City {
}

#[derive(Serialize, Clone)]
pub struct AnonymizedCity {
pub country: String,
pub name: String,
pub(crate) struct AnonymizedCity {
pub(crate) country: String,
pub(crate) name: String,
}

pub struct Datasource {
pub cities: Vec<City>,
pub(crate) struct Datasource {
pub(crate) cities: Vec<City>,
}

impl Datasource {
pub async fn new() -> Datasource {
pub(crate) async fn new() -> Datasource {
let mut cities: Vec<City> = serde_json::from_str(include_str!("../cities.json"))
.expect("cities.json does not have correct format.");

Expand Down
10 changes: 5 additions & 5 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ use std::time::Duration;
use tracing::{debug, info};

#[derive(Clone)]
pub struct GameOptions {
pub guess_time: u64,
pub showcase_time: u64,
pub(crate) struct GameOptions {
pub(crate) guess_time: u64,
pub(crate) showcase_time: u64,
}

pub fn on_connect(socket: SocketRef) {
pub(crate) fn on_connect(socket: SocketRef) {
debug!("🆕 Client connected with client id {}", socket.id);

socket.on(
Expand Down Expand Up @@ -88,7 +88,7 @@ pub fn on_connect(socket: SocketRef) {
});
}

pub async fn game_loop(opts: GameOptions, io: Arc<SocketIo>, state: state::GameState) {
pub(crate) async fn game_loop(opts: GameOptions, io: Arc<SocketIo>, state: state::GameState) {
let guessing_time = Duration::from_secs(opts.guess_time);
let showcase_time = Duration::from_secs(opts.showcase_time);
let mut last_city: Option<datasource::City>;
Expand Down
30 changes: 15 additions & 15 deletions src/packets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@ use crate::{datasource, state};
use serde::{Deserialize, Serialize};

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct GuessPacket {
pub lat: f32,
pub long: f32,
pub(crate) struct GuessPacket {
pub(crate) lat: f32,
pub(crate) long: f32,
}

#[derive(Serialize)]
pub struct SolutionPacket {
pub location: datasource::City,
pub guesses: state::GuessMap,
pub leaderboard: state::PlayerMap,
pub(crate) struct SolutionPacket {
pub(crate) location: datasource::City,
pub(crate) guesses: state::GuessMap,
pub(crate) leaderboard: state::PlayerMap,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct JoinMessage {
pub game: String,
pub username: String,
pub(crate) struct JoinMessage {
pub(crate) game: String,
pub(crate) username: String,
}

#[derive(Serialize, Debug)]
pub struct GameMetadataMessage {
pub guess_time: u64,
pub showcase_time: u64,
pub(crate) struct GameMetadataMessage {
pub(crate) guess_time: u64,
pub(crate) showcase_time: u64,
}

#[derive(Serialize, Debug)]
pub struct DisconnectPacket {
pub message: String,
pub(crate) struct DisconnectPacket {
pub(crate) message: String,
}
8 changes: 4 additions & 4 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ use tower_http::cors::CorsLayer;
use tower_http::timeout::TimeoutLayer;
use tracing::info;

pub struct ServerOptions {
pub game: game::GameOptions,
pub port: Option<u32>,
pub(crate) struct ServerOptions {
pub(crate) game: game::GameOptions,
pub(crate) port: Option<u32>,
}

pub async fn create_server(opts: ServerOptions) -> Option<axum::Router> {
pub(crate) async fn create_server(opts: ServerOptions) -> Option<axum::Router> {
let socketio_state = state::GameState::new(opts.game.clone());

let (socketio_layer, io) = SocketIoBuilder::new()
Expand Down
40 changes: 20 additions & 20 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,19 @@ use tokio::sync::RwLock;

#[derive(Deserialize, Serialize, Debug, Clone)]
#[serde(transparent)]
pub struct Username(String);
pub(crate) struct Username(String);

#[derive(Debug, Clone, Serialize)]
pub struct Player {
pub username: Username,
pub score: u64,
pub(crate) struct Player {
pub(crate) username: Username,
pub(crate) score: u64,

#[serde(skip_serializing)]
pub last_packet: i64,
pub(crate) last_packet: i64,
}

impl Player {
pub fn new(username: String) -> Player {
pub(crate) fn new(username: String) -> Player {
Player {
username: Username(username),
score: 0,
Expand All @@ -30,18 +30,18 @@ impl Player {
}
}

pub type GuessMap = HashMap<Sid, packets::GuessPacket>;
pub type PlayerMap = HashMap<Sid, Player>;
pub(crate) type GuessMap = HashMap<Sid, packets::GuessPacket>;
pub(crate) type PlayerMap = HashMap<Sid, Player>;

#[derive(Clone)]
pub struct GameState {
pub(crate) struct GameState {
guesses: Arc<RwLock<GuessMap>>,
players: Arc<RwLock<PlayerMap>>,
pub options: GameOptions,
pub(crate) options: GameOptions,
}

impl GameState {
pub fn new(options: GameOptions) -> GameState {
pub(crate) fn new(options: GameOptions) -> GameState {
GameState {
guesses: Arc::new(RwLock::new(GuessMap::new())),
players: Arc::new(RwLock::new(PlayerMap::new())),
Expand All @@ -51,48 +51,48 @@ impl GameState {

// Guesses

pub async fn get_guesses(&self) -> GuessMap {
pub(crate) async fn get_guesses(&self) -> GuessMap {
self.guesses.read().await.clone()
}

pub async fn clear_guesses(&self) {
pub(crate) async fn clear_guesses(&self) {
self.guesses.write().await.clear()
}

pub async fn insert_guess(&self, sid: Sid, guess: packets::GuessPacket) {
pub(crate) async fn insert_guess(&self, sid: Sid, guess: packets::GuessPacket) {
self.guesses.write().await.insert(sid, guess);
}

// Players

pub async fn get_players(&self) -> PlayerMap {
pub(crate) async fn get_players(&self) -> PlayerMap {
self.players.read().await.clone()
}

pub async fn insert_player(&self, sid: Sid, player: Player) {
pub(crate) async fn insert_player(&self, sid: Sid, player: Player) {
self.players.write().await.insert(sid, player);
}

pub async fn get_player(&self, sid: Sid) -> Option<Player> {
pub(crate) async fn get_player(&self, sid: Sid) -> Option<Player> {
self.players
.read()
.await
.get(&sid)
.map(|player| player.to_owned())
}

pub async fn remove_player(&self, sid: Sid) {
pub(crate) async fn remove_player(&self, sid: Sid) {
self.players.write().await.remove(&sid);
}

pub async fn is_username_taken(&self, wanted: String) -> bool {
pub(crate) async fn is_username_taken(&self, wanted: String) -> bool {
self.get_players()
.await
.into_iter()
.any(|v| v.1.username.0 == wanted)
}

pub async fn update_last_packet(&self, sid: Sid) {
pub(crate) async fn update_last_packet(&self, sid: Sid) {
let player = self.get_player(sid).await;

if let Some(mut p) = player {
Expand Down

0 comments on commit 7608e7f

Please sign in to comment.