4ae1081aa9
Card pool: - TOTW cards (5): overall 88-92, rarity "totw" - Hero cards (5): overall 85-88, rarity "hero" - Icon cards (5): overall 93-95, rarity "icon" (permanent, non-loan) Packs: - TOTW Pack (30,000 coins): 5 TOTW cards guaranteed - Icon Pack (50,000 coins): 3 icon cards guaranteed - Hero Pack (20,000 coins): 5 hero cards guaranteed Objectives: - Milestone objectives (6): win 10/50, score 100/500 goals, 10 SBCs, 25 packs Formations: - GET /formations returns 12 valid formation strings Multiple named squads (#20): - POST /squad with no squad_id always creates a new squad - POST /squad with squad_id updates that specific squad - GET /squads: list all squads for the club (metadata only) - GET /squads/🆔 squad with players + chemistry - DELETE /squads/🆔 remove a squad Per-position goal stats (#41): - Migration 0003: position_goals table - POST /matches/result accepts optional goal_positions: ["ST","CAM",...] - GET /statistics now includes position_goals map Settings (#44-46): - GET /settings: { difficulty, preferred_formation } with defaults - PUT /settings: upsert any key-value combination Draft mode skeleton (#38): - GET /draft/squad?difficulty=... returns a randomly generated 11-player squad Integration tests: - 9 new tests covering: formations, pack buy/open, SBC submit, settings R/W, multiple squads, draft endpoint, goal position tracking, win streak Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
133 lines
3.9 KiB
Rust
133 lines
3.9 KiB
Rust
use crate::{db::Pool, error::AppResult, models::statistics::Statistics};
|
|
|
|
const SELECT_STATS: &str = "SELECT profile_id, matches_played, matches_won, matches_drawn, matches_lost, goals_scored, goals_conceded, packs_opened, sbcs_completed, total_coins_earned, win_streak, best_win_streak, updated_at FROM statistics WHERE profile_id = ?";
|
|
|
|
pub async fn get_or_create(pool: &Pool, profile_id: &str) -> AppResult<Statistics> {
|
|
if let Some(s) = sqlx::query_as::<_, Statistics>(SELECT_STATS)
|
|
.bind(profile_id)
|
|
.fetch_optional(pool)
|
|
.await?
|
|
{
|
|
return Ok(s);
|
|
}
|
|
|
|
let stats = Statistics::new(profile_id);
|
|
sqlx::query(
|
|
"INSERT INTO statistics (profile_id, matches_played, matches_won, matches_drawn, matches_lost, goals_scored, goals_conceded, packs_opened, sbcs_completed, total_coins_earned, win_streak, best_win_streak, updated_at) VALUES (?, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ?)",
|
|
)
|
|
.bind(profile_id)
|
|
.bind(&stats.updated_at)
|
|
.execute(pool)
|
|
.await?;
|
|
|
|
Ok(stats)
|
|
}
|
|
|
|
pub async fn record_match(
|
|
pool: &Pool,
|
|
profile_id: &str,
|
|
outcome: &str,
|
|
goals_for: i64,
|
|
goals_against: i64,
|
|
coins: i64,
|
|
) -> AppResult<()> {
|
|
let current = get_or_create(pool, profile_id).await?;
|
|
let now = chrono::Utc::now().to_rfc3339();
|
|
|
|
let (w, d, l) = match outcome {
|
|
"win" => (1i64, 0i64, 0i64),
|
|
"draw" => (0, 1, 0),
|
|
_ => (0, 0, 1),
|
|
};
|
|
|
|
let new_streak = if outcome == "win" {
|
|
current.win_streak + 1
|
|
} else {
|
|
0
|
|
};
|
|
let new_best = new_streak.max(current.best_win_streak);
|
|
|
|
sqlx::query(
|
|
"UPDATE statistics SET
|
|
matches_played = matches_played + 1,
|
|
matches_won = matches_won + ?,
|
|
matches_drawn = matches_drawn + ?,
|
|
matches_lost = matches_lost + ?,
|
|
goals_scored = goals_scored + ?,
|
|
goals_conceded = goals_conceded + ?,
|
|
total_coins_earned = total_coins_earned + ?,
|
|
win_streak = ?,
|
|
best_win_streak = ?,
|
|
updated_at = ?
|
|
WHERE profile_id = ?",
|
|
)
|
|
.bind(w)
|
|
.bind(d)
|
|
.bind(l)
|
|
.bind(goals_for)
|
|
.bind(goals_against)
|
|
.bind(coins)
|
|
.bind(new_streak)
|
|
.bind(new_best)
|
|
.bind(&now)
|
|
.bind(profile_id)
|
|
.execute(pool)
|
|
.await?;
|
|
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn increment_packs_opened(pool: &Pool, profile_id: &str) -> AppResult<()> {
|
|
get_or_create(pool, profile_id).await?;
|
|
let now = chrono::Utc::now().to_rfc3339();
|
|
sqlx::query(
|
|
"UPDATE statistics SET packs_opened = packs_opened + 1, updated_at = ? WHERE profile_id = ?",
|
|
)
|
|
.bind(&now)
|
|
.bind(profile_id)
|
|
.execute(pool)
|
|
.await?;
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn increment_sbcs_completed(pool: &Pool, profile_id: &str) -> AppResult<()> {
|
|
get_or_create(pool, profile_id).await?;
|
|
let now = chrono::Utc::now().to_rfc3339();
|
|
sqlx::query(
|
|
"UPDATE statistics SET sbcs_completed = sbcs_completed + 1, updated_at = ? WHERE profile_id = ?",
|
|
)
|
|
.bind(&now)
|
|
.bind(profile_id)
|
|
.execute(pool)
|
|
.await?;
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn record_position_goals(
|
|
pool: &Pool,
|
|
profile_id: &str,
|
|
positions: &[String],
|
|
) -> AppResult<()> {
|
|
for position in positions {
|
|
sqlx::query(
|
|
"INSERT INTO position_goals (profile_id, position, goals) VALUES (?, ?, 1) \
|
|
ON CONFLICT(profile_id, position) DO UPDATE SET goals = goals + 1",
|
|
)
|
|
.bind(profile_id)
|
|
.bind(position)
|
|
.execute(pool)
|
|
.await?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn get_position_goals(pool: &Pool, profile_id: &str) -> AppResult<Vec<(String, i64)>> {
|
|
let rows: Vec<(String, i64)> = sqlx::query_as(
|
|
"SELECT position, goals FROM position_goals WHERE profile_id = ? ORDER BY goals DESC",
|
|
)
|
|
.bind(profile_id)
|
|
.fetch_all(pool)
|
|
.await?;
|
|
Ok(rows)
|
|
}
|