Phase 7 (Core): chemistry v2, expanded card pool & search filters
CI / Build, lint & test (push) Failing after 1m46s

- Chemistry v2: FUT-style link scoring with per-player breakdown
  (club +3/link cap 6, league +1/link cap 4, nation +1/link cap 3;
   player cap 10, team cap 100); chemistry response now includes
   per-player breakdown with club/league/nation link counts and pts
- Card pool: 34 new cards across Premier League, La Liga, Bundesliga
  with overlapping clubs/nations for meaningful chemistry testing
- Card search: extended GET /cards with nation, league, club,
  min_overall, max_overall, limit query params; results sorted by
  overall descending
- Match opponent: added "ultimate" difficulty band (85+ OVR);
  random formation selection from 6 tactical formations; falls back
  to lower OVR pool when not enough cards at the requested band
- Tests: 9 new integration tests (28 total, all passing)

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
funman300
2026-06-25 16:31:56 -07:00
parent 1ef2c436ab
commit 8fa125cfd6
7 changed files with 867 additions and 77 deletions
+59 -31
View File
@@ -105,6 +105,15 @@ pub async fn validate_formation(
Ok(())
}
/// Chemistry v2: FUT-style link scoring.
///
/// Each player earns up to 10 chemistry from three link types:
/// - Club links: strongest signal — +3 per shared club teammate (max 6 pts)
/// - League links: medium signal — +1 per shared league mate (max 4 pts)
/// - Nation links: weakest signal — +1 per shared nation mate (max 3 pts)
///
/// Individual player chemistry is capped at 10.
/// Team chemistry = sum of all player chemistries, capped at 100.
pub async fn calculate_chemistry(
pool: &Pool,
card_db: &CardDb,
@@ -112,10 +121,12 @@ pub async fn calculate_chemistry(
) -> AppResult<serde_json::Value> {
let starters: Vec<&SquadPlayer> = players.iter().filter(|p| !p.is_on_bench).collect();
let mut cards: Vec<CardDefinition> = Vec::new();
// Load all starter card definitions (N separate queries, fine for 11 players)
let mut player_cards: Vec<(String, CardDefinition)> = Vec::new();
for sp in &starters {
let owned = sqlx::query_as::<_, OwnedCard>(
"SELECT id, club_id, card_id, is_loan, loan_matches_remaining, acquired_at FROM owned_cards WHERE id = ?",
"SELECT id, club_id, card_id, is_loan, loan_matches_remaining, acquired_at \
FROM owned_cards WHERE id = ?",
)
.bind(&sp.owned_card_id)
.fetch_optional(pool)
@@ -123,43 +134,60 @@ pub async fn calculate_chemistry(
if let Some(o) = owned {
if let Some(card) = card_db.get(&o.card_id) {
cards.push(card.clone());
player_cards.push((sp.owned_card_id.clone(), card.clone()));
}
}
}
let player_chems: Vec<i64> = cards
.iter()
.enumerate()
.map(|(i, c)| {
let same_club = cards
.iter()
.enumerate()
.filter(|(j, o)| *j != i && o.club == c.club)
.count()
.min(4) as i64;
let same_league = cards
.iter()
.enumerate()
.filter(|(j, o)| *j != i && o.league == c.league)
.count()
.min(3) as i64;
let same_nation = cards
.iter()
.enumerate()
.filter(|(j, o)| *j != i && o.nation == c.nation)
.count()
.min(3) as i64;
(same_club + same_league + same_nation).min(10)
})
.collect();
let mut player_chemistries = Vec::with_capacity(player_cards.len());
let mut total_chem: i64 = 0;
let total: i64 = player_chems.iter().sum::<i64>().min(100);
for (i, (owned_id, card)) in player_cards.iter().enumerate() {
let club_links = player_cards
.iter()
.enumerate()
.filter(|(j, (_, c))| *j != i && c.club == card.club)
.count() as i64;
let league_links = player_cards
.iter()
.enumerate()
.filter(|(j, (_, c))| *j != i && c.league == card.league)
.count() as i64;
let nation_links = player_cards
.iter()
.enumerate()
.filter(|(j, (_, c))| *j != i && c.nation == card.nation)
.count() as i64;
let club_pts = (club_links * 3).min(6);
let league_pts = league_links.min(4);
let nation_pts = nation_links.min(3);
let player_chem = (club_pts + league_pts + nation_pts).min(10);
total_chem += player_chem;
player_chemistries.push(serde_json::json!({
"owned_card_id": owned_id,
"card_id": &card.id,
"name": &card.name,
"chemistry": player_chem,
"breakdown": {
"club_links": club_links,
"league_links": league_links,
"nation_links": nation_links,
"club_pts": club_pts,
"league_pts": league_pts,
"nation_pts": nation_pts,
}
}));
}
let team_chemistry = total_chem.min(100);
Ok(serde_json::json!({
"total": total,
"total": team_chemistry,
"max": 100,
"player_chemistries": player_chems,
"player_count": player_chemistries.len(),
"player_chemistries": player_chemistries,
}))
}