banner
AcoFork

AcoFork

LOVETOLOVE

Phira多人聯機伺服器搭建教程

寫在前面#

  1. 如果你是 Windows 系統,可以直接下載別人構建好的文件(端口默認 12345 或 12346):Dmocken 的 Phira 下載站二叉樹樹的網盤

安裝 Rust#

Phira 多人聯機伺服器基於 Rust 編寫,所以你想要搭建就需要 Rust 環境

Windows#

  1. 前往Rust 下載頁,下載 Rust
    image
  2. 打開後會彈出一個 CMD 窗口,輸入 1(Quick Install)回車,等待 Visual Studio 安裝(如果此步 Visual Studio 下載很慢也可以手動下載
    Snipaste_2024-08-20_00-24-56
  3. 在 Visual Studio 中,勾選使用 C++ 的桌面開發,然後安裝
    image
    然後請跳過 Linux 教程直接閱讀構建 phira-mp

Linux#

  1. 執行:curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  2. 選擇 1 回車
  3. 執行:source $HOME/.cargo/env

構建 phira-mp#

  1. 克隆倉庫:git clone https://github.com/TeamFlos/phira-mp.git
  2. 將下述代碼替換到/phira-mp/phira-mp-server/src/main.rs
mod l10n;

mod room;
pub use room::*;

mod server;
pub use server::*;

mod session;
pub use session::*;

use anyhow::Result;
use clap::Parser;
use std::{
    collections::{
        hash_map::{Entry, VacantEntry},
        HashMap,
    },
    net::{Ipv6Addr, SocketAddr},
    path::Path,
};
use tokio::{net::TcpListener, sync::RwLock};
use tracing::warn;
use tracing_appender::non_blocking::WorkerGuard;
use uuid::Uuid;

pub type SafeMap<K, V> = RwLock<HashMap<K, V>>;
pub type IdMap<V> = SafeMap<Uuid, V>;

fn vacant_entry<V>(map: &mut HashMap<Uuid, V>) -> VacantEntry<'_, Uuid, V> {
    let mut id = Uuid::new_v4();
    while map.contains_key(&id) {
        // 修正此處的語法錯誤
        id = Uuid::new_v4();
    }
    match map.entry(id) {
        Entry::Vacant(entry) => entry,
        _ => unreachable!(),
    }
}

pub fn init_log(file: &str) -> Result<WorkerGuard> {
    use tracing::{metadata::LevelFilter, Level};
    use tracing_log::LogTracer;
    use tracing_subscriber::{filter, fmt, prelude::*, EnvFilter};

    let log_dir = Path::new("log");
    if log_dir.exists() {
        if !log_dir.is_dir() {
            panic!("log exists and is not a folder");
        }
    } else {
        std::fs::create_dir(log_dir).expect("failed to create log folder");
    }

    LogTracer::init()?;

    let (non_blocking, guard) =
        tracing_appender::non_blocking(tracing_appender::rolling::hourly(log_dir, file));

    let subscriber = tracing_subscriber::registry()
        .with(
            fmt::layer()
                .with_writer(non_blocking)
                .with_filter(LevelFilter::DEBUG),
        )
        .with(
            fmt::layer()
                .with_writer(std::io::stdout)
                .with_filter(EnvFilter::from_default_env()),
        )
        .with(
            filter::Targets::new()
                .with_target("hyper", Level::INFO)
                .with_target("rustls", Level::INFO)
                .with_target("isahc", Level::INFO)
                .with_default(Level::TRACE),
        );

    tracing::subscriber::set_global_default(subscriber).expect("unable to set global subscriber");
    Ok(guard)
}

/// Command line arguments
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Args {
    #[clap(
        short,
        long,
        default_value_t = 12346,
        help = "Specify the port number to use for the server"
    )]
    port: u16,
}

#[tokio::main]
async fn main() -> Result<()> {
    let _guard = init_log("phira-mp")?;

    let args = Args::parse();
    let port = args.port;
    let addrs: &[SocketAddr] = &[
        SocketAddr::new(Ipv6Addr::UNSPECIFIED.into(), port),
    ];

    // 打印本地地址和端口
    for addr in addrs {
        println!("Local Address: {}", addr);
    }

    let listener: Server = TcpListener::bind(addrs).await?.into();

    loop {
        if let Err(err) = listener.accept().await {
            warn!("failed to accept: {err:?}");
        }
    }
}
  1. cd phira-mp
  2. 更新依賴:cargo update
  3. 構建:cargo build --release -p phira-mp-server
  4. 運行程序並將 log 打印到終端,會顯示你監聽的端口:RUST_LOG=info target/release/phira-mp-server
    (如果你需要指定端口號:RUST_LOG=info target/release/phira-mp-server --port 8080
    Snipaste_2024-08-20_01-33-32
載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。