mirror of
https://github.com/jcreek/CosmicClash.git
synced 2026-09-13 08:12:03 +00:00
feat: enforce ranked admission policy
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package domain
|
||||
|
||||
import "fmt"
|
||||
|
||||
type RankedParticipant struct {
|
||||
PlayerID string
|
||||
SteamID string
|
||||
PartyID string
|
||||
IsBot bool
|
||||
IsBackfill bool
|
||||
}
|
||||
|
||||
type RankedArena struct {
|
||||
RandomEnabled bool
|
||||
ElevatedGoals bool
|
||||
}
|
||||
|
||||
func ValidateRankedAdmission(participants []RankedParticipant, arena RankedArena) error {
|
||||
if len(participants) != 6 || !arena.RandomEnabled || arena.ElevatedGoals {
|
||||
return fmt.Errorf("ranked admission requirements not met")
|
||||
}
|
||||
seenPlayers := make(map[string]bool, len(participants))
|
||||
seenSteam := make(map[string]bool, len(participants))
|
||||
for _, participant := range participants {
|
||||
if participant.PlayerID == "" || participant.SteamID == "" || participant.PartyID != "" || participant.IsBot || participant.IsBackfill || seenPlayers[participant.PlayerID] || seenSteam[participant.SteamID] {
|
||||
return fmt.Errorf("ranked requires six unique verified solo humans")
|
||||
}
|
||||
seenPlayers[participant.PlayerID] = true
|
||||
seenSteam[participant.SteamID] = true
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package domain
|
||||
|
||||
import "testing"
|
||||
|
||||
func rankedParticipants() []RankedParticipant {
|
||||
result := make([]RankedParticipant, 6)
|
||||
for i := range result {
|
||||
result[i] = RankedParticipant{PlayerID: string(rune('a' + i)), SteamID: string(rune('A' + i))}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func TestRankedAdmissionRequiresSixUniqueVerifiedSoloHumansAndEligibleArena(t *testing.T) {
|
||||
if err := ValidateRankedAdmission(rankedParticipants(), RankedArena{RandomEnabled: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cases := []struct {
|
||||
name string
|
||||
edit func([]RankedParticipant, *RankedArena)
|
||||
}{
|
||||
{"five players", func(p []RankedParticipant, _ *RankedArena) { p[5].PlayerID = "" }},
|
||||
{"party", func(p []RankedParticipant, _ *RankedArena) { p[0].PartyID = "party-1" }},
|
||||
{"bot", func(p []RankedParticipant, _ *RankedArena) { p[0].IsBot = true }},
|
||||
{"backfill", func(p []RankedParticipant, _ *RankedArena) { p[0].IsBackfill = true }},
|
||||
{"duplicate identity", func(p []RankedParticipant, _ *RankedArena) { p[1].SteamID = p[0].SteamID }},
|
||||
{"random disabled", func(_ []RankedParticipant, a *RankedArena) { a.RandomEnabled = false }},
|
||||
{"elevated arena", func(_ []RankedParticipant, a *RankedArena) { a.ElevatedGoals = true }},
|
||||
}
|
||||
for _, test := range cases {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
participants := rankedParticipants()
|
||||
arena := RankedArena{RandomEnabled: true}
|
||||
test.edit(participants, &arena)
|
||||
if err := ValidateRankedAdmission(participants, arena); err == nil {
|
||||
t.Fatal("invalid ranked admission accepted")
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user