feat: Phase 1 — core game loop complete

New endpoints:
- GET  /cards/:card_id       single card lookup
- POST /packs/buy            purchase pack with coins
- POST /objectives/claim     claim completed objective reward
- GET  /matches              match history (limit/mode query params)
- GET  /sbc/:sbc_id          single SBC lookup

Card pool expanded:
- Bronze: 11 → 30 cards (19 nations)
- Silver: 0 → 20 cards (20 nations)
- Rare Gold: 0 → 10 cards (10 nations)

SBC pool expanded:
- Added 5 new SBCs (tri_nations, silver_to_gold, league_loyalty,
  african_stars, gold_standard)

Infrastructure:
- SQLite WAL mode enabled on pool init
- Foreign key enforcement enabled

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
funman300
2026-06-25 15:25:30 -07:00
parent 1ffe0ffa9f
commit 34bce2ce75
14 changed files with 378 additions and 196 deletions
+8
View File
@@ -52,14 +52,22 @@ pub async fn build(pool: Pool, cfg: Config) -> Result<Router> {
.route("/profile", get(routes::profile::get_profile))
.route("/club", get(routes::club::get_club))
.route("/cards", get(routes::cards::get_cards))
.route("/cards/:card_id", get(routes::cards::get_card))
.route("/collection", get(routes::cards::get_collection))
.route("/packs", get(routes::packs::get_packs))
.route("/packs/buy", post(routes::packs::post_buy_pack))
.route("/packs/open/:pack_id", post(routes::packs::post_open_pack))
.route("/squad", get(routes::squad::get_squad))
.route("/squad", post(routes::squad::post_squad))
.route("/objectives", get(routes::objectives::get_objectives))
.route(
"/objectives/claim",
post(routes::objectives::post_claim_objective),
)
.route("/matches", get(routes::matches::get_matches))
.route("/matches/result", post(routes::matches::post_match_result))
.route("/sbc", get(routes::sbc::get_sbcs))
.route("/sbc/:sbc_id", get(routes::sbc::get_sbc))
.route("/sbc/submit", post(routes::sbc::post_sbc_submit))
.route("/market", get(routes::market::get_market))
.route("/market/buy", post(routes::market::post_market_buy))
+4
View File
@@ -10,6 +10,10 @@ pub async fn init_pool(database_url: &str) -> Result<Pool> {
.max_connections(5)
.connect(database_url)
.await?;
sqlx::query("PRAGMA journal_mode=WAL")
.execute(&pool)
.await?;
sqlx::query("PRAGMA foreign_keys=ON").execute(&pool).await?;
Ok(pool)
}
+8
View File
@@ -55,3 +55,11 @@ pub struct ObjectiveWithProgress {
pub completed: bool,
pub claimed: bool,
}
#[derive(Debug, Serialize)]
pub struct ClaimRewardResult {
pub objective_id: String,
pub coins_granted: i64,
pub xp_granted: i64,
pub pack_granted: Option<String>,
}
+13 -2
View File
@@ -1,5 +1,5 @@
use axum::{
extract::{Query, State},
extract::{Path, Query, State},
Json,
};
use serde::Deserialize;
@@ -7,7 +7,7 @@ use serde_json::{json, Value};
use crate::{
app::AppState,
error::AppResult,
error::{AppError, AppResult},
models::card::OwnedCard,
services::{club as club_svc, profile as profile_svc},
};
@@ -18,6 +18,17 @@ pub struct CardQuery {
pub position: Option<String>,
}
pub async fn get_card(
State(state): State<AppState>,
Path(card_id): Path<String>,
) -> AppResult<Json<Value>> {
let card = state
.card_db
.get(&card_id)
.ok_or_else(|| AppError::NotFound(format!("card '{card_id}' not found")))?;
Ok(Json(json!({ "card": card })))
}
pub async fn get_cards(
State(state): State<AppState>,
Query(query): Query<CardQuery>,
+42 -2
View File
@@ -1,12 +1,52 @@
use axum::{extract::State, Json};
use axum::{
extract::{Query, State},
Json,
};
use serde::Deserialize;
use serde_json::{json, Value};
use crate::{
app::AppState,
error::AppResult,
models::match_result::{MatchRewardResult, SubmitMatchRequest},
models::match_result::{Match, MatchRewardResult, SubmitMatchRequest},
services::{club as club_svc, match_service, profile as profile_svc},
};
#[derive(Deserialize)]
pub struct MatchHistoryQuery {
pub limit: Option<i64>,
pub mode: Option<String>,
}
pub async fn get_matches(
State(state): State<AppState>,
Query(query): Query<MatchHistoryQuery>,
) -> AppResult<Json<Value>> {
let profile = profile_svc::get_active_profile(&state.pool).await?;
let limit = query.limit.unwrap_or(20).clamp(1, 100);
let matches = if let Some(mode) = &query.mode {
sqlx::query_as::<_, Match>(
"SELECT id, profile_id, squad_id, opponent_name, goals_for, goals_against, outcome, coins_awarded, xp_awarded, mode, played_at FROM matches WHERE profile_id = ? AND mode = ? ORDER BY played_at DESC LIMIT ?"
)
.bind(&profile.id)
.bind(mode)
.bind(limit)
.fetch_all(&state.pool)
.await?
} else {
sqlx::query_as::<_, Match>(
"SELECT id, profile_id, squad_id, opponent_name, goals_for, goals_against, outcome, coins_awarded, xp_awarded, mode, played_at FROM matches WHERE profile_id = ? ORDER BY played_at DESC LIMIT ?"
)
.bind(&profile.id)
.bind(limit)
.fetch_all(&state.pool)
.await?
};
Ok(Json(json!({ "matches": matches, "total": matches.len() })))
}
pub async fn post_match_result(
State(state): State<AppState>,
Json(req): Json<SubmitMatchRequest>,
+24 -1
View File
@@ -1,10 +1,11 @@
use axum::{extract::State, Json};
use serde::Deserialize;
use serde_json::{json, Value};
use crate::{
app::AppState,
error::AppResult,
services::{objective as obj_svc, profile as profile_svc},
services::{club as club_svc, objective as obj_svc, profile as profile_svc},
};
pub async fn get_objectives(State(state): State<AppState>) -> AppResult<Json<Value>> {
@@ -13,3 +14,25 @@ pub async fn get_objectives(State(state): State<AppState>) -> AppResult<Json<Val
obj_svc::get_objectives_with_progress(&state.pool, &profile.id, &state.obj_defs).await?;
Ok(Json(json!({ "objectives": objectives })))
}
#[derive(Deserialize)]
pub struct ClaimRequest {
pub objective_id: String,
}
pub async fn post_claim_objective(
State(state): State<AppState>,
Json(req): Json<ClaimRequest>,
) -> 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 reward = obj_svc::claim_objective(
&state.pool,
&profile.id,
&club.id,
&state.obj_defs,
&req.objective_id,
)
.await?;
Ok(Json(json!({ "claimed": true, "reward": reward })))
}
+24
View File
@@ -2,6 +2,7 @@ use axum::{
extract::{Path, State},
Json,
};
use serde::Deserialize;
use serde_json::{json, Value};
use crate::{
@@ -11,6 +12,29 @@ use crate::{
services::{club as club_svc, objective, pack as pack_svc, profile as profile_svc, statistics},
};
#[derive(Deserialize)]
pub struct BuyPackRequest {
pub pack_definition_id: String,
}
pub async fn post_buy_pack(
State(state): State<AppState>,
Json(req): Json<BuyPackRequest>,
) -> 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 pack = pack_svc::buy_pack(
&state.pool,
&state.pack_defs,
&club.id,
&req.pack_definition_id,
)
.await?;
Ok(Json(
json!({ "pack": pack, "message": "Pack purchased successfully" }),
))
}
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?;
+17 -2
View File
@@ -1,9 +1,12 @@
use axum::{extract::State, Json};
use axum::{
extract::{Path, State},
Json,
};
use serde_json::{json, Value};
use crate::{
app::AppState,
error::AppResult,
error::{AppError, AppResult},
models::sbc::{SbcResult, SubmitSbcRequest},
services::{club as club_svc, profile as profile_svc, sbc as sbc_svc},
};
@@ -12,6 +15,18 @@ pub async fn get_sbcs(State(state): State<AppState>) -> AppResult<Json<Value>> {
Ok(Json(json!({ "sbcs": state.sbc_defs })))
}
pub async fn get_sbc(
State(state): State<AppState>,
Path(sbc_id): Path<String>,
) -> AppResult<Json<Value>> {
let sbc = state
.sbc_defs
.iter()
.find(|s| s.id == sbc_id)
.ok_or_else(|| AppError::NotFound(format!("SBC '{sbc_id}' not found")))?;
Ok(Json(json!({ "sbc": sbc })))
}
pub async fn post_sbc_submit(
State(state): State<AppState>,
Json(req): Json<SubmitSbcRequest>,
+55 -2
View File
@@ -1,7 +1,9 @@
use crate::{
db::Pool,
error::AppResult,
models::objective::{ObjectiveDefinition, ObjectiveProgress, ObjectiveWithProgress},
error::{AppError, AppResult},
models::objective::{
ClaimRewardResult, ObjectiveDefinition, ObjectiveProgress, ObjectiveWithProgress,
},
};
use anyhow::Context;
use std::path::Path;
@@ -120,3 +122,54 @@ pub async fn increment_metric(
Ok(completed_ids)
}
pub async fn claim_objective(
pool: &Pool,
profile_id: &str,
club_id: &str,
defs: &[ObjectiveDefinition],
objective_id: &str,
) -> AppResult<ClaimRewardResult> {
let prog = sqlx::query_as::<_, ObjectiveProgress>(
"SELECT id, profile_id, objective_id, current, completed, claimed, updated_at FROM objective_progress WHERE profile_id = ? AND objective_id = ?"
)
.bind(profile_id)
.bind(objective_id)
.fetch_optional(pool)
.await?
.ok_or_else(|| AppError::NotFound("objective not started yet".into()))?;
if !prog.completed {
return Err(AppError::BadRequest("objective not completed yet".into()));
}
if prog.claimed {
return Err(AppError::Conflict("reward already claimed".into()));
}
let def = defs
.iter()
.find(|d| d.id == objective_id)
.ok_or_else(|| AppError::NotFound(format!("objective '{objective_id}' not found")))?;
sqlx::query("UPDATE objective_progress SET claimed = 1 WHERE id = ?")
.bind(&prog.id)
.execute(pool)
.await?;
if def.reward_coins > 0 {
crate::services::club::add_coins(pool, club_id, def.reward_coins).await?;
}
if def.reward_xp > 0 {
crate::services::profile::add_xp(pool, profile_id, def.reward_xp).await?;
}
if let Some(pack_id) = &def.reward_pack_id {
crate::services::pack::grant_pack(pool, club_id, pack_id).await?;
}
Ok(ClaimRewardResult {
objective_id: objective_id.to_string(),
coins_granted: def.reward_coins,
xp_granted: def.reward_xp,
pack_granted: def.reward_pack_id.clone(),
})
}
+17
View File
@@ -133,6 +133,23 @@ pub async fn open_pack(
})
}
pub async fn buy_pack(
pool: &Pool,
pack_defs: &[PackDefinition],
club_id: &str,
definition_id: &str,
) -> AppResult<Pack> {
let def = pack_defs
.iter()
.find(|d| d.id == definition_id)
.ok_or_else(|| {
AppError::NotFound(format!("pack definition '{definition_id}' not found"))
})?;
crate::services::club::spend_coins(pool, club_id, def.cost_coins).await?;
grant_pack(pool, club_id, definition_id).await
}
pub async fn get_unopened_packs(pool: &Pool, club_id: &str) -> AppResult<Vec<Pack>> {
let packs = sqlx::query_as::<_, Pack>(
"SELECT id, club_id, definition_id, opened, created_at FROM packs WHERE club_id = ? AND opened = 0"