Structuring

This commit is contained in:
2026-02-23 02:25:38 +01:00
parent 7b532981a1
commit cdb40f64f4
6 changed files with 1373 additions and 20 deletions
+74 -4
View File
@@ -1,3 +1,7 @@
use sqlx::SqlitePool;
use crate::user::get_put;
pub const CLOAKS: &[&str] = &[
"mercedes_flow",
"glitch",
@@ -16,10 +20,76 @@ enum CosmeticKind {
Cloak,
}
async fn buy(kind: CosmeticKind, id: &str) -> Result<String, String> {
Ok(format!("Item bought successfully, congrats!"))
// Buy a cosmetic
pub async fn buy(
kind: CosmeticKind,
uuid: &str,
id: &str,
pool: &SqlitePool,
) -> Result<String, String> {
let mut user = get_put(uuid, pool).await?;
match kind {
CosmeticKind::Hat => {
if !HATS.contains(&id) {
return Err("Hat does not exist".into());
}
if !user.hats.contains(&id.to_string()) {
user.hats.push(id.to_string());
}
}
CosmeticKind::Cloak => {
if !CLOAKS.contains(&id) {
return Err("Cloak does not exist".into());
}
if !user.cloaks.contains(&id.to_string()) {
user.cloaks.push(id.to_string());
}
}
}
sqlx::query("UPDATE users SET cloaks = ?, hats = ? WHERE uuid = ?")
.bind(serde_json::to_string(&user.cloaks).unwrap())
.bind(serde_json::to_string(&user.hats).unwrap())
.bind(uuid)
.execute(pool)
.await
.map_err(|e| e.to_string())?;
Ok(format!("Item {} bought successfully!", id))
}
async fn equip(kind: CosmeticKind, id: &str) -> Result<String, String> {
Ok(format!("Item equipped"))
// Equip a cosmetic
pub async fn equip(
kind: CosmeticKind,
uuid: &str,
id: &str,
pool: &SqlitePool,
) -> Result<String, String> {
let mut user = get_put(uuid, pool).await?;
match kind {
CosmeticKind::Hat => {
if !user.hats.contains(&id.to_string()) {
return Err("You don't own this hat".into());
}
user.hat = id.to_string();
}
CosmeticKind::Cloak => {
if !user.cloaks.contains(&id.to_string()) {
return Err("You don't own this cloak".into());
}
user.cloak = id.to_string();
}
}
sqlx::query("UPDATE users SET cloak = ?, hat = ? WHERE uuid = ?")
.bind(&user.cloak)
.bind(&user.hat)
.bind(uuid)
.execute(pool)
.await
.map_err(|e| e.to_string())?;
Ok(format!("Item {} equipped successfully!", id))
}
+40 -8
View File
@@ -2,22 +2,41 @@ mod cosmetics;
mod methods;
mod user;
use std::{pin::Pin, sync::Arc};
use serde::{Deserialize, Serialize};
use session_rs::server::SessionServer;
use sqlx::SqlitePool;
use ureq::http::StatusCode;
use crate::user::User;
#[tokio::main(flavor = "current_thread")]
async fn main() -> session_rs::Result<()> {
let pool = Arc::new(init_db().await);
let server = SessionServer::bind("127.0.0.1:8080").await?;
server
.session_loop(async |session, _| {
println!("Connected");
session.on::<methods::Auth, _>(authenticate).await;
.session_loop({
let pool = Arc::clone(&pool);
move |session, _| {
let pool = Arc::clone(&pool);
Ok(())
Box::pin(async move {
println!("Connected");
session
.on::<methods::Auth, _>(move |req_id, token| {
let req_id = req_id.clone();
let token = token.clone();
authenticate(req_id.clone(), token, Arc::clone(&pool))
})
.await;
Ok::<(), session_rs::Error>(())
}) as Pin<Box<dyn Future<Output = _> + Send>>
}
})
.await
}
@@ -28,11 +47,15 @@ struct MinecraftAuthResponse {
pub name: String,
}
async fn authenticate(_req_id: u32, session_token: String) -> Result<User, String> {
async fn authenticate(
_req_id: u32,
session_token: String,
pool: Arc<SqlitePool>,
) -> 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"))?;
.map_err(|_| "Failed to validate session".to_string())?;
if response.status() != StatusCode::OK {
return Err(format!(
@@ -44,7 +67,16 @@ async fn authenticate(_req_id: u32, session_token: String) -> Result<User, Strin
let auth: MinecraftAuthResponse = response
.body_mut()
.read_json()
.map_err(|_| format!("Unable to parse auth response"))?;
.map_err(|_| "Unable to parse auth response".to_string())?;
user::get_put(auth.id).await
user::get_put(&auth.id, &pool).await
}
async fn init_db() -> SqlitePool {
let pool = SqlitePool::connect("sqlite:cosmetics.db").await.unwrap();
sqlx::query(include_str!("schema.sql"))
.execute(&pool)
.await
.unwrap();
pool
}
+7
View File
@@ -0,0 +1,7 @@
CREATE TABLE IF NOT EXISTS users (
uuid TEXT PRIMARY KEY,
cloak TEXT DEFAULT "",
hat TEXT DEFAULT "",
cloaks TEXT DEFAULT '[]',
hats TEXT DEFAULT '[]'
);
+75 -4
View File
@@ -1,4 +1,5 @@
use serde::{Deserialize, Serialize};
use sqlx::{Row, SqlitePool};
#[derive(Debug, Serialize, Deserialize)]
pub struct User {
@@ -8,8 +9,78 @@ pub struct User {
pub hats: Vec<String>,
}
/// Get a user
pub async fn get(uuid: String) -> Result<User, String> {}
pub const CLOAKS: &[&str] = &[
"mercedes_flow",
"glitch",
"crimson_mark",
"bmw",
"amg",
"amg_petronas",
"ferrari",
"redbull",
];
/// Create a user if it doesn't exist
pub async fn get_put(uuid: String) -> Result<User, String> {}
pub const HATS: &[&str] = &["horns_black", "horns_white", "halo", "halo_black"];
pub enum CosmeticKind {
Hat,
Cloak,
}
// Initialize SQLite connection pool
pub async fn init_db(db_path: &str) -> SqlitePool {
let pool = SqlitePool::connect(db_path).await.unwrap();
sqlx::query(include_str!("schema.sql"))
.execute(&pool)
.await
.unwrap();
pool
}
// Get user by UUID
pub async fn get(uuid: &str, pool: &SqlitePool) -> Result<User, String> {
let row = sqlx::query("SELECT cloak, hat, cloaks, hats FROM users WHERE uuid = ?")
.bind(uuid)
.fetch_optional(pool)
.await
.map_err(|e| e.to_string())?;
if let Some(r) = row {
let user = User {
cloak: r.get::<String, _>("cloak"),
hat: r.get::<String, _>("hat"),
cloaks: serde_json::from_str(&r.get::<String, _>("cloaks")).unwrap_or_default(),
hats: serde_json::from_str(&r.get::<String, _>("hats")).unwrap_or_default(),
};
Ok(user)
} else {
Err("User not found".into())
}
}
// Create user if not exists
pub async fn get_put(uuid: &str, pool: &SqlitePool) -> Result<User, String> {
match get(uuid, pool).await {
Ok(user) => Ok(user),
Err(_) => {
let user = User {
cloak: "".into(),
hat: "".into(),
cloaks: vec![],
hats: vec![],
};
sqlx::query(
"INSERT INTO users (uuid, cloak, hat, cloaks, hats) VALUES (?, ?, ?, ?, ?)",
)
.bind(uuid)
.bind(&user.cloak)
.bind(&user.hat)
.bind(serde_json::to_string(&user.cloaks).unwrap())
.bind(serde_json::to_string(&user.hats).unwrap())
.execute(pool)
.await
.map_err(|e| e.to_string())?;
Ok(user)
}
}
}