Adopt house golangci-lint config; fix all approved-linter findings
.golangci.yml is the prompts-repo standard verbatim plus one approved
exception (paralleltest, sneak 2026-07-06). Roughly 1,500 findings
fixed:
- autofix sweep (wsl_v5/nlreturn/intrange/modernize formatting), with
the misspell autofix REVERTED where it rewrote authentic C game text
("missle vanishes" stays, with nolint and a comment)
- error handling: errcheck sites get real handling — saveFile now
closes explicitly and removes corrupt saves, scoreboard writes are
documented best-effort, err113 sentinel errors (ErrSaveOutOfDate,
ErrScreenTooSmall); panics allowed for unrecoverable states per
MEMORY.md policy
- gosec: real fixes (ParseInt for SEED, 0600 scorefile) and justified
per-line nolints for provably-bounded conversions (randomMonsterLetter
helper collapses eight rnd(26)+'A' sites)
- API tidying from linters: pointer receivers on flag types
(recvcheck), msg helpers renamed addmsgf/doaddf/Printwf/MvPrintwf
(goprintffuncname), myExit()/findFloor(monst)/mkGameInput(t) drop
always-constant params (unparam), gameEnd carries no status
- gocritic/staticcheck: if-else chains to switches, the pack.c
inventory filter untangled into matchesFilter, main.go split so
defers run before exit (exitAfterDefer, funlen)
- revive doc comments on all exported flag types/consts/methods
Remaining findings are confined to linters pending sneak's exception
decision (mnd, gochecknoglobals, cyclop, nestif, gocognit, exhaustive,
goconst, testpackage); TODO.md records the state. MEMORY.md added at
repo root as the project's agent working notes.
This commit is contained in:
@@ -38,16 +38,22 @@ func (g *RogueGame) rdScore() []ScoreEnt {
|
||||
if g.ScorePath == "" {
|
||||
return topTen
|
||||
}
|
||||
|
||||
f, err := os.Open(g.ScorePath)
|
||||
if err != nil {
|
||||
return topTen
|
||||
}
|
||||
defer f.Close()
|
||||
defer func() { _ = f.Close() }() // read-only handle
|
||||
|
||||
var onDisk []ScoreEnt
|
||||
if err := gob.NewDecoder(f).Decode(&onDisk); err != nil {
|
||||
return topTen
|
||||
|
||||
decErr := gob.NewDecoder(f).Decode(&onDisk)
|
||||
if decErr != nil {
|
||||
return topTen // unreadable scoreboard reads as empty, as in C
|
||||
}
|
||||
|
||||
copy(topTen, onDisk)
|
||||
|
||||
return topTen
|
||||
}
|
||||
|
||||
@@ -57,29 +63,45 @@ func (g *RogueGame) wrScore(topTen []ScoreEnt) {
|
||||
return
|
||||
}
|
||||
// lock_sc/unlock_sc: exclusive-create lock file with stale takeover.
|
||||
// The whole scoreboard write is best effort, as it was in C: a shared
|
||||
// scoreboard must never take the game down.
|
||||
lock := g.ScorePath + ".lck"
|
||||
for range 5 {
|
||||
lf, err := os.OpenFile(lock, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o644)
|
||||
lf, err := os.OpenFile(lock, //nolint:gosec // G304: configured path
|
||||
os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o600)
|
||||
if err == nil {
|
||||
lf.Close()
|
||||
defer os.Remove(lock)
|
||||
_ = lf.Close()
|
||||
|
||||
defer func() { _ = os.Remove(lock) }()
|
||||
|
||||
break
|
||||
}
|
||||
if fi, serr := os.Stat(lock); serr == nil &&
|
||||
time.Since(fi.ModTime()) > 10*time.Second {
|
||||
os.Remove(lock)
|
||||
|
||||
fi, statErr := os.Stat(lock)
|
||||
if statErr == nil && time.Since(fi.ModTime()) > staleLockAge {
|
||||
_ = os.Remove(lock)
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
f, err := os.OpenFile(g.ScorePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o644)
|
||||
|
||||
f, err := os.OpenFile(g.ScorePath,
|
||||
os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0o600)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer f.Close()
|
||||
gob.NewEncoder(f).Encode(topTen)
|
||||
|
||||
_ = gob.NewEncoder(f).Encode(topTen)
|
||||
_ = f.Close()
|
||||
}
|
||||
|
||||
// staleLockAge is how old a scoreboard lock file may be before another
|
||||
// process assumes its owner died and takes it over (mach_dep.c lock_sc
|
||||
// aged its lock the same way).
|
||||
const staleLockAge = 10 * time.Second
|
||||
|
||||
// score figures the score and posts it (rip.c score). flags -1 means just
|
||||
// display the list (the -s command line option).
|
||||
func (g *RogueGame) score(amount, flags int, monst byte) {
|
||||
@@ -92,38 +114,47 @@ func (g *RogueGame) score(amount, flags int, monst byte) {
|
||||
topTen := g.rdScore()
|
||||
// Insert her in list if need be
|
||||
ins := -1
|
||||
|
||||
if !g.NoScore && flags >= 0 {
|
||||
uid := os.Getuid()
|
||||
|
||||
scp := len(topTen)
|
||||
for i := range topTen {
|
||||
if amount > topTen[i].Score {
|
||||
scp = i
|
||||
|
||||
break
|
||||
} else if !g.AllScore && flags != 2 &&
|
||||
topTen[i].UID == uid && topTen[i].Flags != 2 {
|
||||
// only one score per nowin uid
|
||||
scp = len(topTen)
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if scp < len(topTen) {
|
||||
sc2 := len(topTen) - 1
|
||||
if flags != 2 && !g.AllScore {
|
||||
for i := scp; i < len(topTen); i++ {
|
||||
if topTen[i].UID == uid && topTen[i].Flags != 2 {
|
||||
sc2 = i
|
||||
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for sc2 > scp {
|
||||
topTen[sc2] = topTen[sc2-1]
|
||||
sc2--
|
||||
}
|
||||
|
||||
lvl := g.Depth
|
||||
if flags == 2 {
|
||||
lvl = g.MaxDepth
|
||||
}
|
||||
|
||||
topTen[scp] = ScoreEnt{
|
||||
UID: uid,
|
||||
Score: amount,
|
||||
@@ -142,43 +173,53 @@ func (g *RogueGame) score(amount, flags int, monst byte) {
|
||||
if g.AllScore {
|
||||
label = "Scores"
|
||||
}
|
||||
|
||||
lines := []string{
|
||||
fmt.Sprintf("Top Ten %s:", label),
|
||||
" Score Name",
|
||||
}
|
||||
highlight := -1
|
||||
|
||||
for i := range topTen {
|
||||
scp := &topTen[i]
|
||||
if scp.Score == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
line := fmt.Sprintf("%2d %5d %s: %s on level %d", i+1,
|
||||
scp.Score, scp.Name, scoreReasons[scp.Flags], scp.Level)
|
||||
if scp.Flags == 0 || scp.Flags == 3 {
|
||||
line += fmt.Sprintf(" by %s", g.killname(scp.Monster, true))
|
||||
line += " by " + g.killname(scp.Monster, true)
|
||||
}
|
||||
|
||||
line += "."
|
||||
|
||||
if i == ins {
|
||||
highlight = len(lines)
|
||||
}
|
||||
|
||||
lines = append(lines, line)
|
||||
}
|
||||
|
||||
if g.scr != nil && g.scr.term != nil {
|
||||
g.clear()
|
||||
|
||||
for i, line := range lines {
|
||||
if i == highlight {
|
||||
g.standout()
|
||||
}
|
||||
|
||||
g.mvaddstr(i, 0, line)
|
||||
|
||||
if i == highlight {
|
||||
g.standend()
|
||||
}
|
||||
}
|
||||
|
||||
g.refresh()
|
||||
} else {
|
||||
for _, line := range lines {
|
||||
fmt.Println(line)
|
||||
_, _ = fmt.Fprintln(os.Stdout, line) // CLI output
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user