flatland-client-lib 0.2.3

Flatland3 remote game client library (TCP session, bots, game state)
Documentation
//! TCP session connect for remote flatland-server.

use std::net::SocketAddr;

use flatland_protocol::{AuthCredential, Hello, PROTOCOL_VERSION};
use uuid::Uuid;

use crate::remote::{self, RemoteSession};

pub use remote::connect_tcp;

#[derive(Debug, Clone)]
pub struct ConnectOptions {
    pub name: String,
    pub server: SocketAddr,
    pub auth: AuthCredential,
    pub character_id: Option<Uuid>,
}

impl ConnectOptions {
    pub fn to_server(name: impl Into<String>, server: SocketAddr) -> Self {
        Self {
            name: name.into(),
            server,
            auth: AuthCredential::DevLocal,
            character_id: None,
        }
    }

    pub fn with_auth(mut self, auth: AuthCredential, character_id: Option<Uuid>) -> Self {
        self.auth = auth;
        self.character_id = character_id;
        self
    }

    pub fn hello(&self) -> Hello {
        Hello {
            client_name: self.name.clone(),
            protocol_version: PROTOCOL_VERSION,
            auth: self.auth.clone(),
            character_id: self.character_id,
        }
    }
}

pub fn default_server_addr() -> SocketAddr {
    "127.0.0.1:7373".parse().expect("valid default addr")
}

pub async fn connect(opts: ConnectOptions) -> anyhow::Result<RemoteSession> {
    connect_tcp(opts.server, &opts.hello()).await
}