fix: 审查修复波次(P0x4 + P1x30+)
- domain/rules: 计分与排名解耦、破纪录按组别、tieBreak 生效、调整分不丢、移除 aggregate 假能力 - realtime: 新建 internal/dto,快照三项契约对齐,/ws 只认会话身份 - httpapi: 流水幂等、standings 整表载荷、广播接线、强制改密、XFF 防伪造、records 写入、审计、赛事包内容、发布前备份、result.points、实例列表 - web: FormData 上传、YAML 解析、快照归一化、大屏解析层、bootstrap 降级、访客判定、主题切换 - console: 优雅关闭握手、中文化、免验证改管理员账号、选屏与日志轮转修复 - docs: 用户文档三份 + README - 主代理: 破纪录加分接线(levels 未加载导致恒 0)
This commit is contained in:
1 parent
8dae63ee70
commit
7f9701c465
61 files changed
+3844
-333
No files matched your search
@@ -63,8 +63,8 @@ func teamManualChannel(profileID int64) ScoreChannel {
|
||||
return ScoreChannel{ID: 2, EventID: 1, Scope: ScopeTeam, Basis: BasisManual, ProfileID: profileID}
|
||||
}
|
||||
|
||||
func teamAggregateChannel(profileID int64, method AggregateMethod) ScoreChannel {
|
||||
return ScoreChannel{ID: 3, EventID: 1, Scope: ScopeTeam, Basis: BasisAggregate, ProfileID: profileID, Aggregate: method}
|
||||
func teamAggregateChannel(profileID int64) ScoreChannel {
|
||||
return ScoreChannel{ID: 3, EventID: 1, Scope: ScopeTeam, Basis: BasisAggregate, ProfileID: profileID}
|
||||
}
|
||||
|
||||
// participant 是构造「报名项 + 成绩」的最小描述。
|
||||
|
||||
@@ -227,3 +227,50 @@ func TestActiveLedger(t *testing.T) {
|
||||
t.Fatalf("未作废流水 = %+v,期望只有 id=1", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRecomputeEventLedgerKeepsAdjustments 验证重算只作废 result/record_bonus,
|
||||
// 人工调整原样保留——否则每重算一次就把人工扣/加分抹掉。
|
||||
func TestRecomputeEventLedgerKeepsAdjustments(t *testing.T) {
|
||||
existing := []LedgerEntry{
|
||||
{ID: 1, EventID: 1, SourceType: SourceResult, SourceID: 100, TeamID: 1, Points: 10},
|
||||
{ID: 2, EventID: 1, SourceType: SourceRecordBonus, SourceID: 100, TeamID: 1, Points: 2},
|
||||
{ID: 3, EventID: 1, SourceType: SourceAdjustment, SourceID: 7, TeamID: 1, Points: -3},
|
||||
}
|
||||
fresh := []LedgerEntry{{EventID: 1, SourceType: SourceResult, SourceID: 100, TeamID: 1, Points: 6}}
|
||||
|
||||
out, err := RecomputeEventLedger(existing, 1, fresh, "judge", "成绩更正")
|
||||
if err != nil {
|
||||
t.Fatalf("重算失败: %v", err)
|
||||
}
|
||||
for _, e := range out {
|
||||
if e.SourceType == SourceAdjustment {
|
||||
if e.Voided {
|
||||
t.Fatalf("人工调整被误作废: %+v", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
if got := SumByTeam(out, 1)[1].Total; !nearlyEqual(got, 3) {
|
||||
t.Fatalf("重算后总分 = %v,期望 6(新成绩分)- 3(调整保留)= 3", got)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRecomputeEventLedgerIdempotentWithAdjustments 验证带人工调整时重复重算也幂等。
|
||||
func TestRecomputeEventLedgerIdempotentWithAdjustments(t *testing.T) {
|
||||
existing := []LedgerEntry{
|
||||
{ID: 1, EventID: 1, SourceType: SourceResult, SourceID: 100, TeamID: 1, Points: 6},
|
||||
{ID: 2, EventID: 1, SourceType: SourceAdjustment, SourceID: 7, TeamID: 1, Points: -3},
|
||||
}
|
||||
fresh := []LedgerEntry{{EventID: 1, SourceType: SourceResult, SourceID: 100, TeamID: 1, Points: 6}}
|
||||
out, err := RecomputeEventLedger(existing, 1, fresh, "judge", "重复发布")
|
||||
if err != nil {
|
||||
t.Fatalf("重算失败: %v", err)
|
||||
}
|
||||
if len(out) != len(existing) {
|
||||
t.Fatalf("流水条数 = %d,期望 %d(幂等)", len(out), len(existing))
|
||||
}
|
||||
for _, e := range out {
|
||||
if e.Voided {
|
||||
t.Fatalf("幂等重算不应作废任何流水: %+v", e)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -193,3 +193,41 @@ func TestRecordBonusLedger(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestRecordBonusLedgerFiltersByCategory 验证按「小项+组别」过滤:
|
||||
// 上层逐组别调用 ScoreEvent,别的组别的破纪录登记不能在本组别重复记一次加分。
|
||||
func TestRecordBonusLedgerFiltersByCategory(t *testing.T) {
|
||||
entries, results := fixture(lineup(2))
|
||||
entries[1].CategoryID = 2 // entry2 属于另一个组别
|
||||
cfg := bonusConfig(RecordBonusMax)
|
||||
es := mustScore(t, EventScoreInput{
|
||||
Event: individualEvent(1), Category: testCategory(),
|
||||
Channels: []ScoreChannel{athleteResultChannel(1)},
|
||||
Profiles: map[int64]ScoringProfile{1: individualTop6()},
|
||||
Entries: entries, Results: results,
|
||||
RecordBonus: &cfg,
|
||||
RecordBreaks: []RecordBreak{
|
||||
{ID: 1, EventID: 1, EntryID: 1, AthleteID: 101, TeamID: 1001, Level: "meet", State: RecordBreakApproved},
|
||||
{ID: 2, EventID: 1, EntryID: 2, AthleteID: 102, TeamID: 1002, Level: "meet", State: RecordBreakApproved},
|
||||
},
|
||||
})
|
||||
|
||||
var thisCategory, otherCategory int
|
||||
for _, e := range es.Ledger {
|
||||
if e.SourceType != SourceRecordBonus {
|
||||
continue
|
||||
}
|
||||
switch e.EntryID {
|
||||
case 1:
|
||||
thisCategory++
|
||||
case 2:
|
||||
otherCategory++
|
||||
}
|
||||
}
|
||||
if thisCategory != 1 {
|
||||
t.Fatalf("本组别加分流水 = %d 条,期望 1", thisCategory)
|
||||
}
|
||||
if otherCategory != 0 {
|
||||
t.Fatalf("别的组别的加分不应记入本组别,实际 %d 条", otherCategory)
|
||||
}
|
||||
}
|
||||
@@ -63,6 +63,9 @@ type RankedResult struct {
|
||||
func ScoreEvent(in EventScoreInput) (EventScore, error) {
|
||||
var out EventScore
|
||||
rankOnly := len(in.Channels) == 0
|
||||
// 只排名不计分:scored=false 或没有计分通道时都不产出任何流水(含破纪录加分)。
|
||||
scoreEnabled := in.Event.Scored && !rankOnly
|
||||
|
||||
channels := sortedChannels(in.Channels)
|
||||
if rankOnly {
|
||||
channels = []ScoreChannel{rankOnlyChannel(in)}
|
||||
@@ -71,7 +74,7 @@ func ScoreEvent(in EventScoreInput) (EventScore, error) {
|
||||
for _, ch := range channels {
|
||||
// 不计分时不需要积分表:既不查模板,也不可能算出非零分。
|
||||
profile := ScoringProfile{}
|
||||
if in.Event.Scored && !rankOnly {
|
||||
if scoreEnabled {
|
||||
p, ok := in.Profiles[ch.ProfileID]
|
||||
if !ok {
|
||||
return EventScore{}, fmt.Errorf("%w: 通道 %d 引用模板 %d", ErrProfileNotFound, ch.ID, ch.ProfileID)
|
||||
@@ -82,18 +85,18 @@ func ScoreEvent(in EventScoreInput) (EventScore, error) {
|
||||
if err != nil {
|
||||
return EventScore{}, err
|
||||
}
|
||||
if !in.Event.Scored {
|
||||
if !scoreEnabled {
|
||||
for i := range ranked {
|
||||
ranked[i].Points = 0
|
||||
}
|
||||
}
|
||||
out.Ranked = append(out.Ranked, ranked...)
|
||||
if in.Event.Scored {
|
||||
if scoreEnabled {
|
||||
out.Ledger = append(out.Ledger, ledgerFromRanked(in, ch, ranked)...)
|
||||
}
|
||||
}
|
||||
|
||||
if !in.Event.Scored {
|
||||
if !scoreEnabled {
|
||||
return out, nil
|
||||
}
|
||||
bonus, err := recordBonusLedger(in)
|
||||
|
||||
@@ -191,7 +191,7 @@ func TestScopeAndLedgerAttribution(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
// TestAggregateChannel 覆盖预留的 aggregate 通道:先汇总、再排名。
|
||||
// TestAggregateChannel 覆盖 aggregate 通道:先按主体汇总(求和)、再排名。
|
||||
func TestAggregateChannel(t *testing.T) {
|
||||
build := func() ([]Entry, []Result) {
|
||||
return fixture([]participant{
|
||||
@@ -202,19 +202,12 @@ func TestAggregateChannel(t *testing.T) {
|
||||
}
|
||||
cases := []struct {
|
||||
name string
|
||||
method AggregateMethod
|
||||
compare CompareMode
|
||||
wantA float64
|
||||
wantB float64
|
||||
}{
|
||||
{"sum 升序", AggregateSum, CompareAsc, 15, 10},
|
||||
{"avg 升序", AggregateAvg, CompareAsc, 15, 10},
|
||||
{"max 升序", AggregateMax, CompareAsc, 15, 10},
|
||||
{"min 升序", AggregateMin, CompareAsc, 15, 10},
|
||||
{"count 降序", AggregateCount, CompareDesc, 15, 10},
|
||||
{"count 升序", AggregateCount, CompareAsc, 10, 15},
|
||||
{"sum 降序", AggregateSum, CompareDesc, 10, 15},
|
||||
{"默认方法按 sum", "", CompareAsc, 15, 10},
|
||||
{"求和后升序", CompareAsc, 15, 10},
|
||||
{"求和后降序", CompareDesc, 10, 15},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
@@ -223,7 +216,7 @@ func TestAggregateChannel(t *testing.T) {
|
||||
ev.Compare = tc.compare
|
||||
es := mustScore(t, EventScoreInput{
|
||||
Event: ev, Category: testCategory(),
|
||||
Channels: []ScoreChannel{teamAggregateChannel(2, tc.method)},
|
||||
Channels: []ScoreChannel{teamAggregateChannel(2)},
|
||||
Profiles: map[int64]ScoringProfile{2: teamTop6()},
|
||||
Entries: entries, Results: results,
|
||||
})
|
||||
@@ -242,7 +235,7 @@ func TestAggregateChannel(t *testing.T) {
|
||||
})
|
||||
es := mustScore(t, EventScoreInput{
|
||||
Event: teamEvent(1), Category: testCategory(),
|
||||
Channels: []ScoreChannel{teamAggregateChannel(2, AggregateSum)},
|
||||
Channels: []ScoreChannel{teamAggregateChannel(2)},
|
||||
Profiles: map[int64]ScoringProfile{2: teamTop6()},
|
||||
Entries: entries, Results: results,
|
||||
})
|
||||
@@ -320,7 +313,7 @@ func TestDraftState(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestScoredFlagAndNoChannels 验证「只排名不计分」与「没有通道」都不产生流水。
|
||||
// TestScoredFlagAndNoChannels 验证「只排名不计分」:不产生流水,但必须照样产出名次。
|
||||
func TestScoredFlagAndNoChannels(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
@@ -342,13 +335,50 @@ func TestScoredFlagAndNoChannels(t *testing.T) {
|
||||
Profiles: map[int64]ScoringProfile{1: individualTop6()},
|
||||
Entries: entries, Results: results,
|
||||
})
|
||||
if len(es.Ledger) != 0 || len(es.Ranked) != 0 {
|
||||
t.Fatalf("流水/名次应为空,实际 %d/%d", len(es.Ledger), len(es.Ranked))
|
||||
if len(es.Ledger) != 0 {
|
||||
t.Fatalf("只排名不计分不应产生流水,实际 %d 条: %+v", len(es.Ledger), es.Ledger)
|
||||
}
|
||||
if len(es.Ranked) != 3 {
|
||||
t.Fatalf("名次条数 = %d,期望 3(排名与计分解耦)", len(es.Ranked))
|
||||
}
|
||||
for _, rr := range es.Ranked {
|
||||
if rr.Points != 0 {
|
||||
t.Errorf("只排名不计分时 Points = %v,期望 0", rr.Points)
|
||||
}
|
||||
}
|
||||
if ranks := rankByEntry(es.Ranked); ranks[1] != 1 || ranks[2] != 2 || ranks[3] != 3 {
|
||||
t.Fatalf("名次 = %v,期望 1/2/3", ranks)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// TestRankOnlyRespectsResultMode 验证没有通道时按小项自身的录入方式排名(manual 直接读录入名次)。
|
||||
func TestRankOnlyRespectsResultMode(t *testing.T) {
|
||||
ps := lineup(3)
|
||||
for i := range ps {
|
||||
ps[i].noValue = true
|
||||
}
|
||||
ps[0].rank, ps[1].rank, ps[2].rank = 2, 3, 1
|
||||
entries, results := fixture(ps)
|
||||
|
||||
ev := individualEvent(1)
|
||||
ev.Scored = false
|
||||
ev.ResultMode = ResultModeManual
|
||||
ev.Compare = CompareNone
|
||||
es := mustScore(t, EventScoreInput{
|
||||
Event: ev, Category: testCategory(),
|
||||
Entries: entries, Results: results,
|
||||
})
|
||||
if len(es.Ledger) != 0 {
|
||||
t.Fatalf("只排名不计分不应产生流水,实际 %d 条", len(es.Ledger))
|
||||
}
|
||||
ranks := rankByEntry(es.Ranked)
|
||||
if ranks[3] != 1 || ranks[1] != 2 || ranks[2] != 3 {
|
||||
t.Fatalf("名次 = %v,期望 entry3→1 / entry1→2 / entry2→3", ranks)
|
||||
}
|
||||
}
|
||||
|
||||
// TestScoreEventErrors 覆盖配置类错误,避免错配置静默算错分。
|
||||
func TestScoreEventErrors(t *testing.T) {
|
||||
entries, results := fixture(lineup(1))
|
||||
@@ -376,7 +406,7 @@ func TestScoreEventErrors(t *testing.T) {
|
||||
}
|
||||
|
||||
noCompareAgg := noCompare
|
||||
noCompareAgg.Channels = []ScoreChannel{teamAggregateChannel(1, AggregateSum)}
|
||||
noCompareAgg.Channels = []ScoreChannel{teamAggregateChannel(1)}
|
||||
if _, err := ScoreEvent(noCompareAgg); !errors.Is(err, ErrCompareRequired) {
|
||||
t.Errorf("聚合 compare=none 错误 = %v,期望 ErrCompareRequired", err)
|
||||
}
|
||||
@@ -599,3 +629,25 @@ func TestMissingResultOrEntry(t *testing.T) {
|
||||
assertEntryPoints(t, pointsByEntry(es.Ledger), []float64{10, 8, 0})
|
||||
})
|
||||
}
|
||||
|
||||
// TestNoChannelsSkipsRecordBonus 验证「0 个通道 = 只排名不计分」:
|
||||
// 即使项目 scored=true 且破了纪录,也不写任何流水(破纪录加分也跳过)。
|
||||
func TestNoChannelsSkipsRecordBonus(t *testing.T) {
|
||||
entries, results := fixture(lineup(2))
|
||||
cfg := bonusConfig(RecordBonusMax)
|
||||
es := mustScore(t, EventScoreInput{
|
||||
Event: individualEvent(1), Category: testCategory(),
|
||||
Entries: entries, Results: results,
|
||||
RecordBonus: &cfg,
|
||||
RecordBreaks: []RecordBreak{{
|
||||
ID: 1, EventID: 1, EntryID: 1, AthleteID: 101, TeamID: 1001,
|
||||
Level: "meet", State: RecordBreakApproved,
|
||||
}},
|
||||
})
|
||||
if len(es.Ledger) != 0 {
|
||||
t.Fatalf("没有通道时不应产生流水,实际 %d 条: %+v", len(es.Ledger), es.Ledger)
|
||||
}
|
||||
if len(es.Ranked) != 2 {
|
||||
t.Fatalf("名次条数 = %d,期望 2", len(es.Ranked))
|
||||
}
|
||||
}
|
||||
@@ -245,3 +245,43 @@ func TestSumByTeamIgnoresVoided(t *testing.T) {
|
||||
t.Fatalf("总分 = %v,期望只算未作废的 10", got[1].Total)
|
||||
}
|
||||
}
|
||||
|
||||
// TestTeamStandingsTieBreakConfig 验证 StandingsInput.TieBreak 真正参与排序:
|
||||
// 配置 [teamCode] 时即使总分落后也按单位编码在前;未配置时仍是总分优先。
|
||||
func TestTeamStandingsTieBreakConfig(t *testing.T) {
|
||||
ledger := []LedgerEntry{ledgerFor(1, 10), ledgerFor(2, 20)}
|
||||
|
||||
def := BuildTeamStandings(StandingsInput{Teams: testTeams(1, 2), Ledger: ledger})
|
||||
if def[0].TeamID != 2 {
|
||||
t.Fatalf("默认口径榜首 = 单位 %d,期望 2(总分 20 优先)", def[0].TeamID)
|
||||
}
|
||||
|
||||
cfg := BuildTeamStandings(StandingsInput{
|
||||
Teams: testTeams(1, 2), Ledger: ledger, TieBreak: []string{TieBreakTeamCode},
|
||||
})
|
||||
if cfg[0].TeamID != 1 {
|
||||
t.Fatalf("配置 [teamCode] 榜首 = 单位 %d,期望 1(编码 A < B)", cfg[0].TeamID)
|
||||
}
|
||||
if cfg[0].Rank != 1 || cfg[1].Rank != 2 {
|
||||
t.Fatalf("名次 = %d/%d,期望 1/2", cfg[0].Rank, cfg[1].Rank)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAthleteStandingsTieBreakConfig 验证个人榜同样按配置排序。
|
||||
func TestAthleteStandingsTieBreakConfig(t *testing.T) {
|
||||
ledger := []LedgerEntry{ledgerForAthlete(1001, 1, 10), ledgerForAthlete(1002, 2, 10)}
|
||||
athletes := []Athlete{{ID: 1, Name: "张三", TeamID: 1001}, {ID: 2, Name: "李四", TeamID: 1002}}
|
||||
teams := []Team{{ID: 1001, Code: "B", Name: "二单位"}, {ID: 1002, Code: "A", Name: "一单位"}}
|
||||
|
||||
def := BuildAthleteStandings(StandingsInput{Teams: teams, Athletes: athletes, Ledger: ledger})
|
||||
if def[0].AthleteID != 1 {
|
||||
t.Fatalf("默认口径榜首 = 运动员 %d,期望 1(姓名 张三 < 李四)", def[0].AthleteID)
|
||||
}
|
||||
|
||||
cfg := BuildAthleteStandings(StandingsInput{
|
||||
Teams: teams, Athletes: athletes, Ledger: ledger, TieBreak: []string{TieBreakTeamCode},
|
||||
})
|
||||
if cfg[0].AthleteID != 2 {
|
||||
t.Fatalf("配置 [teamCode] 榜首 = 运动员 %d,期望 2(单位编码 A < B)", cfg[0].AthleteID)
|
||||
}
|
||||
}
|
||||
@@ -225,12 +225,12 @@ type EventCategory struct {
|
||||
// basis=aggregate 时固定按「同一主体成绩之和」汇总:汇总口径不可配置,
|
||||
// 库里也没有对应的列,因此不再保留 avg/max/min/count 这类配置项。
|
||||
type ScoreChannel struct {
|
||||
ID int64 `json:"id"`
|
||||
EventID int64 `json:"eventId"`
|
||||
Scope Scope `json:"scope"`
|
||||
Basis Basis `json:"basis"`
|
||||
ProfileID int64 `json:"profileId"`
|
||||
SortOrder int `json:"sortOrder,omitempty"`
|
||||
ID int64 `json:"id"`
|
||||
EventID int64 `json:"eventId"`
|
||||
Scope Scope `json:"scope"`
|
||||
Basis Basis `json:"basis"`
|
||||
ProfileID int64 `json:"profileId"`
|
||||
SortOrder int `json:"sortOrder,omitempty"`
|
||||
}
|
||||
|
||||
// ScoringRule 积分模板的一条明细:名次 → 分值。分值一律来自配置。
|
||||
|
||||
Reference in new issue
Block a user