mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-11 00:14:00 +00:00
feat: gate proposals on formed match policy
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
type PreparedProposal struct {
|
||||
Proposal Proposal
|
||||
CasualLineup []CasualSlot
|
||||
}
|
||||
|
||||
// PrepareProposal is the boundary between matchmaking and proposal state. It
|
||||
// never creates a proposal for a casual formation without one human per team,
|
||||
// or for a ranked formation whose verified identity/arena metadata fails the
|
||||
// ranked admission policy.
|
||||
func PrepareProposal(id string, playlist Playlist, formation MatchFormation, rankedParticipants []RankedParticipant, arena RankedArena, now time.Time) (PreparedProposal, error) {
|
||||
if len(formation.Selection.Players) < 2 || len(formation.Selection.Players) > 6 {
|
||||
return PreparedProposal{}, fmt.Errorf("invalid formed player count")
|
||||
}
|
||||
playerIDs := make([]string, 0, len(formation.Selection.Players))
|
||||
seen := make(map[string]bool, len(formation.Selection.Players))
|
||||
for _, player := range formation.Selection.Players {
|
||||
if player.PlayerID == "" || seen[player.PlayerID] {
|
||||
return PreparedProposal{}, fmt.Errorf("invalid formed player identity")
|
||||
}
|
||||
seen[player.PlayerID] = true
|
||||
playerIDs = append(playerIDs, player.PlayerID)
|
||||
}
|
||||
var lineup []CasualSlot
|
||||
switch playlist {
|
||||
case Casual:
|
||||
participants := make([]ConnectParticipant, 0, len(formation.Selection.Players))
|
||||
for _, player := range formation.Teams.Team0 {
|
||||
participants = append(participants, ConnectParticipant{PlayerID: player.PlayerID, Team: 0})
|
||||
}
|
||||
for _, player := range formation.Teams.Team1 {
|
||||
participants = append(participants, ConnectParticipant{PlayerID: player.PlayerID, Team: 1})
|
||||
}
|
||||
var err error
|
||||
lineup, err = BuildCasualLineup(participants)
|
||||
if err != nil {
|
||||
return PreparedProposal{}, err
|
||||
}
|
||||
case Ranked:
|
||||
if len(rankedParticipants) != len(playerIDs) {
|
||||
return PreparedProposal{}, fmt.Errorf("ranked metadata does not match formed players")
|
||||
}
|
||||
metadata := make(map[string]bool, len(rankedParticipants))
|
||||
for _, participant := range rankedParticipants {
|
||||
metadata[participant.PlayerID] = true
|
||||
}
|
||||
if len(metadata) != len(playerIDs) {
|
||||
return PreparedProposal{}, fmt.Errorf("ranked metadata has duplicate or unknown players")
|
||||
}
|
||||
for _, playerID := range playerIDs {
|
||||
if !metadata[playerID] {
|
||||
return PreparedProposal{}, fmt.Errorf("ranked metadata missing formed player")
|
||||
}
|
||||
}
|
||||
if err := ValidateRankedAdmission(rankedParticipants, arena); err != nil {
|
||||
return PreparedProposal{}, err
|
||||
}
|
||||
default:
|
||||
return PreparedProposal{}, fmt.Errorf("unsupported playlist")
|
||||
}
|
||||
proposal, err := NewProposal(id, playlist, playerIDs, now)
|
||||
if err != nil {
|
||||
return PreparedProposal{}, err
|
||||
}
|
||||
return PreparedProposal{Proposal: proposal, CasualLineup: lineup}, nil
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func testFormation(t *testing.T, count int) MatchFormation {
|
||||
t.Helper()
|
||||
now := time.Unix(1000, 0)
|
||||
players := make([]Candidate, count)
|
||||
for i := range players {
|
||||
players[i] = Candidate{TicketID: string(rune('a' + i)), PlayerID: string(rune('p' + i)), Rating: 1500, EnqueuedAt: now, PredictedRTT: map[string]float64{"EU": 40}}
|
||||
}
|
||||
selection, err := SelectCandidates(players[0], players[1:], count, now)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
teams, err := PartitionTeams(selection.Players)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return MatchFormation{Selection: selection, Teams: teams}
|
||||
}
|
||||
|
||||
func TestPrepareProposalBuildsCasualLineupBeforeCreatingProposal(t *testing.T) {
|
||||
prepared, err := PrepareProposal("proposal-casual-123456", Casual, testFormation(t, 2), nil, RankedArena{}, time.Unix(1000, 0))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if prepared.Proposal.Playlist != Casual || len(prepared.Proposal.Participants) != 2 || len(prepared.CasualLineup) != 6 {
|
||||
t.Fatalf("prepared casual proposal = %+v", prepared)
|
||||
}
|
||||
humans := 0
|
||||
teams := map[int]bool{}
|
||||
for _, slot := range prepared.CasualLineup {
|
||||
if !slot.IsBot {
|
||||
humans++
|
||||
teams[slot.Team] = true
|
||||
}
|
||||
}
|
||||
if humans != 2 || len(teams) != 2 {
|
||||
t.Fatalf("casual lineup humans/teams = %d/%v", humans, teams)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrepareProposalRejectsInvalidRankedMetadataAndAcceptsVerifiedSix(t *testing.T) {
|
||||
formation := testFormation(t, 6)
|
||||
participants := make([]RankedParticipant, 6)
|
||||
for i, player := range formation.Selection.Players {
|
||||
participants[i] = RankedParticipant{PlayerID: player.PlayerID, SteamID: "steam-" + player.PlayerID}
|
||||
}
|
||||
if _, err := PrepareProposal("proposal-ranked-123456", Ranked, formation, participants, RankedArena{RandomEnabled: true}, time.Unix(1000, 0)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
participants[0].IsBot = true
|
||||
if _, err := PrepareProposal("proposal-ranked-654321", Ranked, formation, participants, RankedArena{RandomEnabled: true}, time.Unix(1000, 0)); err == nil {
|
||||
t.Fatal("ranked bot metadata accepted")
|
||||
}
|
||||
participants[0].IsBot = false
|
||||
participants[0].PlayerID = "unknown"
|
||||
if _, err := PrepareProposal("proposal-ranked-000000", Ranked, formation, participants, RankedArena{RandomEnabled: true}, time.Unix(1000, 0)); err == nil {
|
||||
t.Fatal("ranked unknown player metadata accepted")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user