Files
Scoreboard/server/internal/domain/records.go
T
AskaEth 6bd7bd1627 feat: 「个人+团体」项目真正落地——通道级不计入单位 + 队内平均排名
口径(用户确认):
- 个人分按个人成绩排名给,**不计入单位总分**(下沉到通道级开关,不再动全局系数)
- 团体成绩 = 每队成员成绩的**平均值** → 排名(并列顺延)→ 团体模板给分 → 计入单位总分
- 平均值口径是**小项级单选项**:默认「只算有效成绩」;「全部报名人员计入」= 分母为报名条数、
  没成绩按 0 加入分子;在**成绩录入页**就地切换并立即保存

服务端:
- 迁移 0017:score_ledger 加 count_to_team(重建,无子表);event_score_channels 加 count_to_team;
  events 加 avg_mode(默认 valid)
- 规则包 Channel 加 countToTeam(缺省 true);channelTemplates 支持 individual_team →
  默认 athlete(result, 不计入单位) + team(aggregate 队内平均)
- 计分引擎:SumByTeam 跳过 count_to_team=0 的流水;实现 basis=aggregate 的队内平均团体排名
  (valid 分母=有有效成绩人数;all 分母=报名人数;某队 0 有效成绩不参与)

前端:
- 计分通道页:每行通道「计入单位总分」勾选 + basis 下拉「按队内平均」
- 成绩录入页:仅有队内平均通道的小项显示「平均值口径」单选(默认只算有效成绩),改动即存
- 录入页「实时积分预览」按新口径对齐(原先按主体求和且双通道重复计入,与发布后不一致)
- 大屏/管理端团体成绩只显示数值,不出现「平均」字样

自检:服务端 12 包全绿;前端 vue-tsc + build、node --test 134/134;console 16 文件通过。
2026-09-10 12:02:00 +08:00

193 lines
5.1 KiB
Go

package domain
import "sort"
// levelPoints 按档位编码取分值;分值全部来自规则包,代码不内置任何数字。
func (c RecordBonusConfig) levelPoints(code string) (float64, bool) {
for _, l := range c.Levels {
if l.Code == code {
return l.Points, true
}
}
return 0, false
}
// RecordBonusPoints 把「已审核通过」的破纪录登记按报名项聚合:
// 同一人同时破多级时,max 取最高档、sum 累加,口径由配置决定。
// 未配置的档位视为无加分,避免脏 level 凭空加分。
func RecordBonusPoints(cfg RecordBonusConfig, breaks []RecordBreak) []RecordBonus {
return recordBonusPoints(cfg, breaks, nil)
}
// recordBonusPoints 是聚合实现;modeByEntry 给出每个报名项命中通道所属模板的口径,
// 缺失时退回全局 cfg.Mode,保证直接调用 RecordBonusPoints 的旧口径不变。
func recordBonusPoints(cfg RecordBonusConfig, breaks []RecordBreak, modeByEntry map[int64]RecordBonusMode) []RecordBonus {
if !cfg.Enabled || len(breaks) == 0 {
return nil
}
type group struct {
first RecordBreak
levels []string
points []float64
}
order := make([]int64, 0, len(breaks))
groups := make(map[int64]*group)
for _, b := range breaks {
if b.State != RecordBreakApproved {
continue
}
pts, ok := cfg.levelPoints(b.Level)
if !ok {
continue
}
g, exists := groups[b.EntryID]
if !exists {
g = &group{first: b}
groups[b.EntryID] = g
order = append(order, b.EntryID)
}
seen := false
for _, code := range g.levels {
if code == b.Level {
seen = true
break
}
}
if !seen {
g.levels = append(g.levels, b.Level)
g.points = append(g.points, pts)
}
}
out := make([]RecordBonus, 0, len(order))
for _, entryID := range order {
g := groups[entryID]
bonus := RecordBonus{
EntryID: entryID,
EventID: g.first.EventID,
AthleteID: g.first.AthleteID,
TeamID: g.first.TeamID,
Levels: g.levels,
}
mode := cfg.Mode
if m, ok := modeByEntry[entryID]; ok {
mode = m
}
if mode == RecordBonusSum {
for _, p := range g.points {
bonus.Points += p
}
} else {
for _, p := range g.points {
if p > bonus.Points {
bonus.Points = p
}
}
}
out = append(out, bonus)
}
sort.SliceStable(out, func(i, j int) bool { return out[i].EntryID < out[j].EntryID })
return out
}
// recordBonusLedger 把破纪录加分写成流水。
// 加分与成绩分相互独立:不乘项目权重、也不乘组别权重(case 6 的口径)。
// 只处理本组别的报名项:上层按组别逐个调用 ScoreEvent,不加组别过滤会让同一笔加分每组各记一次。
func recordBonusLedger(in EventScoreInput) ([]LedgerEntry, error) {
if in.RecordBonus == nil || !in.RecordBonus.Enabled || len(in.RecordBreaks) == 0 {
return nil, nil
}
// 只对「可破纪录」的项目加分;登记接口另有一层校验,这里是引擎侧的最后防线。
if !in.Event.RecordEligible {
return nil, nil
}
breaks := make([]RecordBreak, 0, len(in.RecordBreaks))
for _, b := range in.RecordBreaks {
if in.Event.ID != 0 && b.EventID != 0 && b.EventID != in.Event.ID {
continue
}
breaks = append(breaks, b)
}
modes := make(map[int64]RecordBonusMode, len(in.Entries))
for _, e := range in.Entries {
if in.Category.ID != 0 && e.CategoryID != in.Category.ID {
continue
}
if m, ok := recordBonusModeFor(in, e); ok {
modes[e.ID] = m
}
}
bonuses := recordBonusPoints(*in.RecordBonus, breaks, modes)
if len(bonuses) == 0 {
return nil, nil
}
byID := make(map[int64]Entry, len(in.Entries))
for _, e := range in.Entries {
if in.Category.ID != 0 && e.CategoryID != in.Category.ID {
continue
}
byID[e.ID] = e
}
var out []LedgerEntry
for _, b := range bonuses {
if b.Points == 0 {
continue
}
e, ok := byID[b.EntryID]
if !ok {
continue
}
athleteID := b.AthleteID
if athleteID == 0 {
athleteID = e.AthleteID
}
for _, s := range e.resolvedShares(in.Options.UnionAttribution) {
out = append(out, LedgerEntry{
TeamID: s.TeamID,
AthleteID: athleteID,
EventID: in.Event.ID,
CategoryID: in.Category.ID,
EntryID: e.ID,
SourceType: SourceRecordBonus,
SourceID: e.ID,
Points: b.Points * s.Weight,
CountToTeam: true,
})
}
}
return out, nil
}
// recordBonusModeFor 取报名项「命中通道」所属模板的破纪录口径。
// 命中通道按 channelBefore 的优先级挑选(athlete → team → 通道 id 升序),
// 与奖牌去重同一口径;没有可命中通道或模板缺失时返回 false,由调用方退回全局口径。
func recordBonusModeFor(in EventScoreInput, e Entry) (RecordBonusMode, bool) {
var (
picked Scope
pickedID int64
profileID int64
found bool
)
for _, ch := range in.Channels {
if ch.Scope == ScopeAthlete && e.AthleteID == 0 {
continue
}
if !found || channelBefore(ch.Scope, ch.ID, picked, pickedID) {
picked, pickedID, profileID, found = ch.Scope, ch.ID, ch.ProfileID, true
}
}
if !found {
return "", false
}
p, ok := in.Profiles[profileID]
if !ok {
return "", false
}
return p.bonusMode(), true
}