feat: Phase 2 — game feel complete

Chemistry & squads:
- Chemistry calculation on GET /squad (club/league/nation links, max 100)
- Formation validation on POST /squad (exactly 11 starters, exactly 1 GK)

Objectives:
- Weekly objectives JSON (4 objectives: warrior, goals, dedicated, SBC)
- Daily objectives auto-reset at midnight UTC (background task)

SBC validation expanded:
- max_overall per-player enforcement
- required_clubs validation
- min_players_from_same_nation validation
- min_players_from_same_club validation

Market:
- GET /market?min_overall=X&position=Y filtering
- Expiry cleanup runs before every NPC refresh
- NPC market auto-refresh every 24h (background task, runs at startup)

Matches:
- GET /matches/opponent?difficulty=beginner|professional|world_class|legendary
  generates a random AI opponent squad from the card pool

Statistics:
- win_streak and best_win_streak tracking (migration 0002)
- GET /statistics/history?limit=N — last N matches with summary stats

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
funman300
2026-06-25 15:33:09 -07:00
parent 34bce2ce75
commit 0afce0dd59
13 changed files with 516 additions and 55 deletions
+59 -2
View File
@@ -1,9 +1,14 @@
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::statistics::Statistics,
models::{match_result::Match, statistics::Statistics},
services::{profile as profile_svc, statistics as stats_svc},
};
@@ -12,3 +17,55 @@ pub async fn get_statistics(State(state): State<AppState>) -> AppResult<Json<Sta
let stats = stats_svc::get_or_create(&state.pool, &profile.id).await?;
Ok(Json(stats))
}
#[derive(Deserialize)]
pub struct HistoryQuery {
pub limit: Option<i64>,
}
pub async fn get_statistics_history(
State(state): State<AppState>,
Query(query): Query<HistoryQuery>,
) -> 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 = 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?;
let total = matches.len();
let wins = matches.iter().filter(|m| m.outcome == "win").count();
let draws = matches.iter().filter(|m| m.outcome == "draw").count();
let losses = matches.iter().filter(|m| m.outcome == "loss").count();
let goals_for: i64 = matches.iter().map(|m| m.goals_for).sum();
let goals_against: i64 = matches.iter().map(|m| m.goals_against).sum();
let win_rate = if total > 0 {
wins as f64 / total as f64
} else {
0.0
};
let avg_goals = if total > 0 {
goals_for as f64 / total as f64
} else {
0.0
};
Ok(Json(json!({
"matches": matches,
"summary": {
"total": total,
"wins": wins,
"draws": draws,
"losses": losses,
"win_rate": (win_rate * 100.0).round() / 100.0,
"total_goals_for": goals_for,
"total_goals_against": goals_against,
"avg_goals_per_game": (avg_goals * 100.0).round() / 100.0,
}
})))
}