Initial commit: OpenFUT Core

Offline Ultimate Team backend — game-independent REST API.

- 19 API endpoints: auth, profiles, clubs, cards, packs, squads,
  objectives, SBCs, match rewards, NPC market, statistics
- Axum + SQLite + SQLx with full migrations
- Weighted pack generator, SBC validation engine
- JSON-driven mod data (cards, packs, objectives, SBCs)
- 5 integration tests passing

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
funman300
2026-06-25 14:52:06 -07:00
commit 1ffe0ffa9f
60 changed files with 6152 additions and 0 deletions
+30
View File
@@ -0,0 +1,30 @@
use axum::{extract::State, Json};
use serde_json::{json, Value};
use crate::{
app::AppState,
error::AppResult,
models::{club::Club, profile::CreateProfileRequest},
seed,
services::{club as club_svc, profile as profile_svc},
};
pub async fn post_auth_local(
State(state): State<AppState>,
Json(req): Json<CreateProfileRequest>,
) -> AppResult<Json<Value>> {
let username = req.username.unwrap_or_else(|| "Player 1".into());
let profile = profile_svc::create_profile(&state.pool, &username).await?;
let club = Club::new(&profile.id, "OpenFUT FC", 5000);
club_svc::create_club(&state.pool, &club).await?;
seed::grant_starter_pack(&state.pool, &club.id, &state.pack_defs).await?;
Ok(Json(json!({
"profile": profile,
"club": club,
"message": "Welcome to OpenFUT FC! Your club has been created."
})))
}
+75
View File
@@ -0,0 +1,75 @@
use axum::{
extract::{Query, State},
Json,
};
use serde::Deserialize;
use serde_json::{json, Value};
use crate::{
app::AppState,
error::AppResult,
models::card::OwnedCard,
services::{club as club_svc, profile as profile_svc},
};
#[derive(Debug, Deserialize)]
pub struct CardQuery {
pub rarity: Option<String>,
pub position: Option<String>,
}
pub async fn get_cards(
State(state): State<AppState>,
Query(query): Query<CardQuery>,
) -> AppResult<Json<Value>> {
let cards = state
.card_db
.all()
.into_iter()
.filter(|c| {
query
.rarity
.as_ref()
.map(|r| format!("{:?}", c.rarity).to_lowercase() == r.to_lowercase())
.unwrap_or(true)
&& query
.position
.as_ref()
.map(|p| c.position.to_lowercase() == p.to_lowercase())
.unwrap_or(true)
})
.collect::<Vec<_>>();
Ok(Json(json!({ "cards": cards, "total": cards.len() })))
}
pub async fn get_collection(State(state): State<AppState>) -> AppResult<Json<Value>> {
let profile = profile_svc::get_active_profile(&state.pool).await?;
let club = club_svc::get_club_by_profile(&state.pool, &profile.id).await?;
let owned = sqlx::query_as::<_, OwnedCard>(
"SELECT id, club_id, card_id, is_loan, loan_matches_remaining, acquired_at FROM owned_cards WHERE club_id = ?"
)
.bind(&club.id)
.fetch_all(&state.pool)
.await?;
let with_defs: Vec<Value> = owned
.iter()
.filter_map(|o| {
state.card_db.get(&o.card_id).map(|def| {
json!({
"owned_card_id": o.id,
"is_loan": o.is_loan,
"loan_matches_remaining": o.loan_matches_remaining,
"acquired_at": o.acquired_at,
"card": def,
})
})
})
.collect();
Ok(Json(
json!({ "collection": with_defs, "total": with_defs.len() }),
))
}
+13
View File
@@ -0,0 +1,13 @@
use crate::{
app::AppState,
error::AppResult,
models::club::Club,
services::{club as club_svc, profile as profile_svc},
};
use axum::{extract::State, Json};
pub async fn get_club(State(state): State<AppState>) -> AppResult<Json<Club>> {
let profile = profile_svc::get_active_profile(&state.pool).await?;
let club = club_svc::get_club_by_profile(&state.pool, &profile.id).await?;
Ok(Json(club))
}
+13
View File
@@ -0,0 +1,13 @@
use axum::{http::StatusCode, Json};
use serde_json::{json, Value};
pub async fn get_health() -> (StatusCode, Json<Value>) {
(
StatusCode::OK,
Json(json!({
"status": "ok",
"service": "openfut-core",
"version": env!("CARGO_PKG_VERSION")
})),
)
}
+47
View File
@@ -0,0 +1,47 @@
use axum::{extract::State, Json};
use serde_json::{json, Value};
use crate::{
app::AppState,
error::AppResult,
models::market::{BuyListingRequest, SellCardRequest},
services::{club as club_svc, market as market_svc, profile as profile_svc},
};
pub async fn get_market(State(state): State<AppState>) -> AppResult<Json<Value>> {
let listings = market_svc::get_active_listings(&state.pool, &state.card_db).await?;
Ok(Json(
json!({ "listings": listings, "total": listings.len() }),
))
}
pub async fn post_market_buy(
State(state): State<AppState>,
Json(req): Json<BuyListingRequest>,
) -> AppResult<Json<Value>> {
let profile = profile_svc::get_active_profile(&state.pool).await?;
let club = club_svc::get_club_by_profile(&state.pool, &profile.id).await?;
let card = market_svc::buy_listing(&state.pool, &state.card_db, &club.id, &req).await?;
Ok(Json(
json!({ "purchased_card": card, "message": "Card purchased successfully" }),
))
}
pub async fn post_market_sell(
State(state): State<AppState>,
Json(req): Json<SellCardRequest>,
) -> AppResult<Json<Value>> {
let profile = profile_svc::get_active_profile(&state.pool).await?;
let club = club_svc::get_club_by_profile(&state.pool, &profile.id).await?;
let new_balance = market_svc::sell_card(&state.pool, &club.id, &req).await?;
Ok(Json(
json!({ "new_coin_balance": new_balance, "message": "Card sold to NPC market" }),
))
}
pub async fn post_market_refresh(State(state): State<AppState>) -> AppResult<Json<Value>> {
let count = market_svc::refresh_npc_listings(&state.pool, &state.card_db).await?;
Ok(Json(json!({ "listings_generated": count })))
}
+22
View File
@@ -0,0 +1,22 @@
use axum::{extract::State, Json};
use crate::{
app::AppState,
error::AppResult,
models::match_result::{MatchRewardResult, SubmitMatchRequest},
services::{club as club_svc, match_service, profile as profile_svc},
};
pub async fn post_match_result(
State(state): State<AppState>,
Json(req): Json<SubmitMatchRequest>,
) -> AppResult<Json<MatchRewardResult>> {
let profile = profile_svc::get_active_profile(&state.pool).await?;
let club = club_svc::get_club_by_profile(&state.pool, &profile.id).await?;
let result =
match_service::process_match(&state.pool, &profile.id, &club.id, &req, &state.obj_defs)
.await?;
Ok(Json(result))
}
+12
View File
@@ -0,0 +1,12 @@
pub mod auth;
pub mod cards;
pub mod club;
pub mod health;
pub mod market;
pub mod matches;
pub mod objectives;
pub mod packs;
pub mod profile;
pub mod sbc;
pub mod squad;
pub mod statistics;
+15
View File
@@ -0,0 +1,15 @@
use axum::{extract::State, Json};
use serde_json::{json, Value};
use crate::{
app::AppState,
error::AppResult,
services::{objective as obj_svc, profile as profile_svc},
};
pub async fn get_objectives(State(state): State<AppState>) -> AppResult<Json<Value>> {
let profile = profile_svc::get_active_profile(&state.pool).await?;
let objectives =
obj_svc::get_objectives_with_progress(&state.pool, &profile.id, &state.obj_defs).await?;
Ok(Json(json!({ "objectives": objectives })))
}
+57
View File
@@ -0,0 +1,57 @@
use axum::{
extract::{Path, State},
Json,
};
use serde_json::{json, Value};
use crate::{
app::AppState,
error::AppResult,
models::pack::PackOpenResult,
services::{club as club_svc, objective, pack as pack_svc, profile as profile_svc, statistics},
};
pub async fn get_packs(State(state): State<AppState>) -> AppResult<Json<Value>> {
let profile = profile_svc::get_active_profile(&state.pool).await?;
let club = club_svc::get_club_by_profile(&state.pool, &profile.id).await?;
let packs = pack_svc::get_unopened_packs(&state.pool, &club.id).await?;
let with_defs: Vec<Value> = packs
.iter()
.map(|p| {
let def = state.pack_defs.iter().find(|d| d.id == p.definition_id);
json!({
"pack_id": p.id,
"definition_id": p.definition_id,
"name": def.map(|d| &d.name),
"description": def.map(|d| &d.description),
"created_at": p.created_at,
})
})
.collect();
Ok(Json(json!({ "packs": with_defs })))
}
pub async fn post_open_pack(
State(state): State<AppState>,
Path(pack_id): Path<String>,
) -> AppResult<Json<PackOpenResult>> {
let profile = profile_svc::get_active_profile(&state.pool).await?;
let club = club_svc::get_club_by_profile(&state.pool, &profile.id).await?;
let result = pack_svc::open_pack(
&state.pool,
&state.card_db,
&state.pack_defs,
&club.id,
&pack_id,
)
.await?;
statistics::increment_packs_opened(&state.pool, &profile.id).await?;
objective::increment_metric(&state.pool, &profile.id, &state.obj_defs, "packsopened", 1)
.await?;
Ok(Json(result))
}
+9
View File
@@ -0,0 +1,9 @@
use crate::{
app::AppState, error::AppResult, models::profile::Profile, services::profile as profile_svc,
};
use axum::{extract::State, Json};
pub async fn get_profile(State(state): State<AppState>) -> AppResult<Json<Profile>> {
let profile = profile_svc::get_active_profile(&state.pool).await?;
Ok(Json(profile))
}
+34
View File
@@ -0,0 +1,34 @@
use axum::{extract::State, Json};
use serde_json::{json, Value};
use crate::{
app::AppState,
error::AppResult,
models::sbc::{SbcResult, SubmitSbcRequest},
services::{club as club_svc, profile as profile_svc, sbc as sbc_svc},
};
pub async fn get_sbcs(State(state): State<AppState>) -> AppResult<Json<Value>> {
Ok(Json(json!({ "sbcs": state.sbc_defs })))
}
pub async fn post_sbc_submit(
State(state): State<AppState>,
Json(req): Json<SubmitSbcRequest>,
) -> AppResult<Json<SbcResult>> {
let profile = profile_svc::get_active_profile(&state.pool).await?;
let club = club_svc::get_club_by_profile(&state.pool, &profile.id).await?;
let result = sbc_svc::submit_sbc(
&state.pool,
&state.card_db,
&state.sbc_defs,
&state.obj_defs,
&profile.id,
&club.id,
&req,
)
.await?;
Ok(Json(result))
}
+49
View File
@@ -0,0 +1,49 @@
use axum::{extract::State, Json};
use serde_json::{json, Value};
use crate::{
app::AppState,
error::AppResult,
models::squad::SaveSquadRequest,
services::{club as club_svc, profile as profile_svc, squad as squad_svc},
};
pub async fn get_squad(State(state): State<AppState>) -> AppResult<Json<Value>> {
let profile = profile_svc::get_active_profile(&state.pool).await?;
let club = club_svc::get_club_by_profile(&state.pool, &profile.id).await?;
let (squad, players) = squad_svc::get_squad(&state.pool, &club.id).await?;
let enriched: Vec<Value> = players
.iter()
.map(|sp| {
json!({
"squad_player_id": sp.id,
"owned_card_id": sp.owned_card_id,
"position_index": sp.position_index,
"is_captain": sp.is_captain,
"is_on_bench": sp.is_on_bench,
})
})
.collect();
Ok(Json(json!({
"squad": {
"id": squad.id,
"name": squad.name,
"formation": squad.formation,
},
"players": enriched,
})))
}
pub async fn post_squad(
State(state): State<AppState>,
Json(req): Json<SaveSquadRequest>,
) -> AppResult<Json<Value>> {
let profile = profile_svc::get_active_profile(&state.pool).await?;
let club = club_svc::get_club_by_profile(&state.pool, &profile.id).await?;
let squad = squad_svc::save_squad(&state.pool, &club.id, &req).await?;
Ok(Json(json!({ "squad": squad })))
}
+14
View File
@@ -0,0 +1,14 @@
use axum::{extract::State, Json};
use crate::{
app::AppState,
error::AppResult,
models::statistics::Statistics,
services::{profile as profile_svc, statistics as stats_svc},
};
pub async fn get_statistics(State(state): State<AppState>) -> AppResult<Json<Statistics>> {
let profile = profile_svc::get_active_profile(&state.pool).await?;
let stats = stats_svc::get_or_create(&state.pool, &profile.id).await?;
Ok(Json(stats))
}