Structuring

This commit is contained in:
2026-02-23 02:02:22 +01:00
parent 56fb719cca
commit 7b532981a1
6 changed files with 746 additions and 31 deletions
+25
View File
@@ -0,0 +1,25 @@
pub const CLOAKS: &[&str] = &[
"mercedes_flow",
"glitch",
"crimson_mark",
"bmw",
"amg",
"amg_petronas",
"ferrari",
"redbull",
];
pub const HATS: &[&str] = &["horns_black", "horns_white", "halo", "halo_black"];
enum CosmeticKind {
Hat,
Cloak,
}
async fn buy(kind: CosmeticKind, id: &str) -> Result<String, String> {
Ok(format!("Item bought successfully, congrats!"))
}
async fn equip(kind: CosmeticKind, id: &str) -> Result<String, String> {
Ok(format!("Item equipped"))
}
+36 -13
View File
@@ -1,8 +1,12 @@
mod cosmetics;
mod methods;
mod user;
use serde::{Deserialize, Serialize};
use session_rs::server::SessionServer;
use ureq::http::StatusCode;
use crate::methods::AuthResponse;
pub mod methods;
use crate::user::User;
#[tokio::main(flavor = "current_thread")]
async fn main() -> session_rs::Result<()> {
@@ -11,17 +15,36 @@ async fn main() -> session_rs::Result<()> {
server
.session_loop(async |session, _| {
println!("Connected");
session
.on::<methods::Auth, _>(|_, req| async {
Ok(AuthResponse {
cloak: String::new(),
hat: String::new(),
cloaks: Vec::new(),
hats: Vec::new(),
})
})
.await;
session.on::<methods::Auth, _>(authenticate).await;
Ok(())
})
.await
}
#[derive(Debug, Deserialize, Serialize)]
struct MinecraftAuthResponse {
pub id: String,
pub name: String,
}
async fn authenticate(_req_id: u32, session_token: String) -> Result<User, String> {
let mut response = ureq::get("https://api.minecraftservices.com/minecraft/profile")
.header("Authorization", &format!("Bearer {session_token}"))
.call()
.map_err(|_| format!("Failed to validate session"))?;
if response.status() != StatusCode::OK {
return Err(format!(
"Authentication failed with code {}",
response.status()
));
}
let auth: MinecraftAuthResponse = response
.body_mut()
.read_json()
.map_err(|_| format!("Unable to parse auth response"))?;
user::get_put(auth.id).await
}
+2 -8
View File
@@ -1,13 +1,7 @@
use serde::{Deserialize, Serialize};
use session_rs::Method;
#[derive(Debug, Serialize, Deserialize)]
pub struct AuthResponse {
pub cloak: String,
pub hat: String,
pub cloaks: Vec<String>,
pub hats: Vec<String>,
}
use crate::user::User;
#[derive(Debug, Serialize, Deserialize)]
pub struct Auth;
@@ -15,7 +9,7 @@ pub struct Auth;
impl Method for Auth {
const NAME: &'static str = "auth";
type Request = String;
type Response = AuthResponse;
type Response = User;
type Error = String;
}
+15
View File
@@ -0,0 +1,15 @@
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
pub struct User {
pub cloak: String,
pub hat: String,
pub cloaks: Vec<String>,
pub hats: Vec<String>,
}
/// Get a user
pub async fn get(uuid: String) -> Result<User, String> {}
/// Create a user if it doesn't exist
pub async fn get_put(uuid: String) -> Result<User, String> {}