feat: M0-M2 骨架落地(服务端/前端/宿主/数据库/积分引擎)
- server: Go 单二进制骨架,embed 内嵌前端,配置/迁移/HTTP 路由 - store: system.db + event.db 两套迁移,业务表 + 种子数据 - domain: 积分引擎(并列三口径/计分通道/流水/排名/破纪录)+ 68 个测试 - web: Vite + Vue 三入口骨架 + shared 层(类型/API/WebSocket 客户端) - console: Electron 宿主骨架(选屏/防熄屏/进程守护/托盘)
This commit is contained in:
1 parent
a61d793d79
commit
7d5d94f815
76 files changed
+10131
No files matched your search
@@ -0,0 +1,279 @@
|
||||
package domain
|
||||
|
||||
import "sort"
|
||||
|
||||
// StandingsInput 是排名聚合的输入:流水 + 名次明细 + 单位/运动员基础信息。
|
||||
type StandingsInput struct {
|
||||
Teams []Team
|
||||
Athletes []Athlete
|
||||
Ledger []LedgerEntry
|
||||
Ranked []RankedResult
|
||||
// IndividualToTeamWeight 个人积分折算进单位总分的系数;<=0 时按 1 处理。
|
||||
IndividualToTeamWeight float64
|
||||
// ExcludeTeamEventMedals 为 true 时团体奖牌榜不计团体项目奖牌;默认计入。
|
||||
ExcludeTeamEventMedals bool
|
||||
}
|
||||
|
||||
type medalTally struct {
|
||||
gold, silver, bronze int
|
||||
}
|
||||
|
||||
// rankMedal 把名次映射成奖牌:1 金 / 2 银 / 3 铜,其余无名次。
|
||||
func rankMedal(rank int) int {
|
||||
switch rank {
|
||||
case 1:
|
||||
return 1
|
||||
case 2:
|
||||
return 2
|
||||
case 3:
|
||||
return 3
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
// BuildTeamStandings 生成总积分榜。
|
||||
// 总分 = Σ(仅记单位的流水) + 系数 × Σ(记到个人的流水);排序:总分 → 金 → 银 → 铜 → 单位编码。
|
||||
func BuildTeamStandings(in StandingsInput) []StandingsRow {
|
||||
weight := effectiveWeight(in.IndividualToTeamWeight)
|
||||
sums := SumByTeam(in.Ledger, weight)
|
||||
medals := teamMedals(in)
|
||||
|
||||
rows := make([]StandingsRow, 0, len(in.Teams))
|
||||
seen := make(map[int64]bool, len(in.Teams))
|
||||
for _, t := range in.Teams {
|
||||
if seen[t.ID] {
|
||||
continue
|
||||
}
|
||||
seen[t.ID] = true
|
||||
rows = append(rows, buildTeamRow(t, sums[t.ID], medals[t.ID]))
|
||||
}
|
||||
// 兜底:流水/奖牌里出现但单位表没提供的单位也要上榜,否则分数会被静默吞掉。
|
||||
for id := range sums {
|
||||
if !seen[id] {
|
||||
seen[id] = true
|
||||
rows = append(rows, buildTeamRow(Team{ID: id}, sums[id], medals[id]))
|
||||
}
|
||||
}
|
||||
for id, m := range medals {
|
||||
if !seen[id] {
|
||||
seen[id] = true
|
||||
rows = append(rows, buildTeamRow(Team{ID: id}, sums[id], m))
|
||||
}
|
||||
}
|
||||
|
||||
sort.SliceStable(rows, func(i, j int) bool { return lessTeamRow(rows[i], rows[j]) })
|
||||
for i := range rows {
|
||||
rows[i].Rank = i + 1
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// BuildMedalStandings 生成团体奖牌榜。
|
||||
// 排序口径与总积分榜一致(总分优先),只是列不同,保证两块榜的单位顺序不会打架。
|
||||
func BuildMedalStandings(in StandingsInput) []MedalRow {
|
||||
teamRows := BuildTeamStandings(in)
|
||||
out := make([]MedalRow, len(teamRows))
|
||||
for i, r := range teamRows {
|
||||
out[i] = MedalRow{
|
||||
Rank: r.Rank,
|
||||
TeamID: r.TeamID,
|
||||
Code: r.Code,
|
||||
Name: r.Name,
|
||||
ShortName: r.ShortName,
|
||||
Points: r.Points,
|
||||
Gold: r.Gold,
|
||||
Silver: r.Silver,
|
||||
Bronze: r.Bronze,
|
||||
Total: r.Gold + r.Silver + r.Bronze,
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// BuildAthleteStandings 生成个人积分奖牌榜。
|
||||
// 排序:总分 → 金 → 银 → 铜 → 姓名;个人榜的奖牌只统计个人项目(团体项目奖牌归团体榜)。
|
||||
func BuildAthleteStandings(in StandingsInput) []AthleteStandingsRow {
|
||||
points := SumByAthlete(in.Ledger)
|
||||
medals := athleteMedals(in)
|
||||
|
||||
names := make(map[int64]string, len(in.Athletes))
|
||||
teams := make(map[int64]int64, len(in.Athletes))
|
||||
for _, a := range in.Athletes {
|
||||
names[a.ID] = a.Name
|
||||
teams[a.ID] = a.TeamID
|
||||
}
|
||||
for _, rr := range in.Ranked {
|
||||
if rr.AthleteID == 0 {
|
||||
continue
|
||||
}
|
||||
if names[rr.AthleteID] == "" {
|
||||
names[rr.AthleteID] = rr.AthleteName
|
||||
}
|
||||
if _, ok := teams[rr.AthleteID]; !ok {
|
||||
teams[rr.AthleteID] = rr.TeamID
|
||||
}
|
||||
}
|
||||
|
||||
teamByID := make(map[int64]Team, len(in.Teams))
|
||||
for _, t := range in.Teams {
|
||||
teamByID[t.ID] = t
|
||||
}
|
||||
|
||||
seen := make(map[int64]bool, len(points)+len(medals))
|
||||
ids := make([]int64, 0, len(points)+len(medals))
|
||||
add := func(id int64) {
|
||||
if id == 0 || seen[id] {
|
||||
return
|
||||
}
|
||||
seen[id] = true
|
||||
ids = append(ids, id)
|
||||
}
|
||||
for id := range points {
|
||||
add(id)
|
||||
}
|
||||
for id := range medals {
|
||||
add(id)
|
||||
}
|
||||
|
||||
rows := make([]AthleteStandingsRow, 0, len(ids))
|
||||
for _, id := range ids {
|
||||
t := teamByID[teams[id]]
|
||||
m := medals[id]
|
||||
rows = append(rows, AthleteStandingsRow{
|
||||
AthleteID: id,
|
||||
Name: names[id],
|
||||
TeamID: teams[id],
|
||||
TeamCode: t.Code,
|
||||
TeamName: t.Name,
|
||||
Points: points[id],
|
||||
Gold: m.gold,
|
||||
Silver: m.silver,
|
||||
Bronze: m.bronze,
|
||||
Medals: m.gold + m.silver + m.bronze,
|
||||
})
|
||||
}
|
||||
|
||||
sort.SliceStable(rows, func(i, j int) bool { return lessAthleteRow(rows[i], rows[j]) })
|
||||
for i := range rows {
|
||||
rows[i].Rank = i + 1
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
func effectiveWeight(weight float64) float64 {
|
||||
if weight <= 0 {
|
||||
return 1
|
||||
}
|
||||
return weight
|
||||
}
|
||||
|
||||
func buildTeamRow(t Team, p TeamPoints, m medalTally) StandingsRow {
|
||||
return StandingsRow{
|
||||
TeamID: t.ID,
|
||||
Code: t.Code,
|
||||
Name: t.Name,
|
||||
ShortName: t.ShortName,
|
||||
Points: p.Total,
|
||||
TeamOnlyPoints: p.TeamOnly,
|
||||
IndividualPoints: p.Individual,
|
||||
Gold: m.gold,
|
||||
Silver: m.silver,
|
||||
Bronze: m.bronze,
|
||||
Medals: m.gold + m.silver + m.bronze,
|
||||
}
|
||||
}
|
||||
|
||||
// teamMedals 统计团体奖牌榜。联队拆分时两个单位各记一枚:
|
||||
// 联合组队拿到的名次对每个成员单位都是「本单位取得的名次」。
|
||||
func teamMedals(in StandingsInput) map[int64]medalTally {
|
||||
out := make(map[int64]medalTally)
|
||||
for _, rr := range in.Ranked {
|
||||
if rr.Scope == ScopeTeam && in.ExcludeTeamEventMedals {
|
||||
continue
|
||||
}
|
||||
medal := rankMedal(rr.Rank)
|
||||
if medal == 0 {
|
||||
continue
|
||||
}
|
||||
for _, s := range rr.Shares {
|
||||
t := out[s.TeamID]
|
||||
switch medal {
|
||||
case 1:
|
||||
t.gold++
|
||||
case 2:
|
||||
t.silver++
|
||||
case 3:
|
||||
t.bronze++
|
||||
}
|
||||
out[s.TeamID] = t
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// athleteMedals 只统计个人项目(scope=athlete)的奖牌。
|
||||
func athleteMedals(in StandingsInput) map[int64]medalTally {
|
||||
out := make(map[int64]medalTally)
|
||||
for _, rr := range in.Ranked {
|
||||
if rr.Scope != ScopeAthlete || rr.AthleteID == 0 {
|
||||
continue
|
||||
}
|
||||
medal := rankMedal(rr.Rank)
|
||||
if medal == 0 {
|
||||
continue
|
||||
}
|
||||
t := out[rr.AthleteID]
|
||||
switch medal {
|
||||
case 1:
|
||||
t.gold++
|
||||
case 2:
|
||||
t.silver++
|
||||
case 3:
|
||||
t.bronze++
|
||||
}
|
||||
out[rr.AthleteID] = t
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func lessTeamRow(a, b StandingsRow) bool {
|
||||
if !nearlyEqual(a.Points, b.Points) {
|
||||
return a.Points > b.Points
|
||||
}
|
||||
if a.Gold != b.Gold {
|
||||
return a.Gold > b.Gold
|
||||
}
|
||||
if a.Silver != b.Silver {
|
||||
return a.Silver > b.Silver
|
||||
}
|
||||
if a.Bronze != b.Bronze {
|
||||
return a.Bronze > b.Bronze
|
||||
}
|
||||
if a.Code != b.Code {
|
||||
return a.Code < b.Code
|
||||
}
|
||||
return a.TeamID < b.TeamID
|
||||
}
|
||||
|
||||
func lessAthleteRow(a, b AthleteStandingsRow) bool {
|
||||
if !nearlyEqual(a.Points, b.Points) {
|
||||
return a.Points > b.Points
|
||||
}
|
||||
if a.Gold != b.Gold {
|
||||
return a.Gold > b.Gold
|
||||
}
|
||||
if a.Silver != b.Silver {
|
||||
return a.Silver > b.Silver
|
||||
}
|
||||
if a.Bronze != b.Bronze {
|
||||
return a.Bronze > b.Bronze
|
||||
}
|
||||
if a.Name != b.Name {
|
||||
return a.Name < b.Name
|
||||
}
|
||||
if a.TeamCode != b.TeamCode {
|
||||
return a.TeamCode < b.TeamCode
|
||||
}
|
||||
return a.AthleteID < b.AthleteID
|
||||
}
|
||||
Reference in new issue
Block a user