consistent menus

This commit is contained in:
stryan 2022-05-25 17:52:42 -04:00
parent afc589380b
commit c4ba6b83e6
3 changed files with 47 additions and 6 deletions

36
entrylist.go Normal file
View File

@ -0,0 +1,36 @@
package main
import sim "git.saintnet.tech/stryan/spacetea/simulator"
type entrylist []sim.ItemEntry
// Len is the number of elements in the collection.
func (e entrylist) Len() int {
return len(e)
}
// Less reports whether the element with index i
// must sort before the element with index j.
//
// If both Less(i, j) and Less(j, i) are false,
// then the elements at index i and j are considered equal.
// Sort may place equal elements in any order in the final result,
// while Stable preserves the original input order of equal elements.
//
// Less must describe a transitive ordering:
// - if both Less(i, j) and Less(j, k) are true, then Less(i, k) must be true as well.
// - if both Less(i, j) and Less(j, k) are false, then Less(i, k) must be false as well.
//
// Note that floating-point comparison (the < operator on float32 or float64 values)
// is not a transitive ordering when not-a-number (NaN) values are involved.
// See Float64Slice.Less for a correct implementation for floating-point values.
func (e entrylist) Less(i int, j int) bool {
return e[i].ID() < e[j].ID()
}
// Swap swaps the elements with indexes i and j.
func (e entrylist) Swap(i int, j int) {
tmp := e[i]
e[i] = e[j]
e[j] = tmp
}

View File

@ -2,6 +2,7 @@ package main
import ( import (
"fmt" "fmt"
"sort"
"strconv" "strconv"
"time" "time"
@ -103,23 +104,24 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case "g": case "g":
m.s.Input("get") m.s.Input("get")
case "p": case "p":
var res []sim.ItemEntry var res entrylist
for _, k := range sim.GlobalItems { for _, k := range sim.GlobalItems {
if m.s.Player.Resources[k.ID()] != 0 { if m.s.Player.Resources[k.ID()] != 0 {
res = append(res, k.(sim.ItemEntry)) res = append(res, k.(sim.ItemEntry))
} }
} }
sort.Sort(res)
return newMenuModel(res, placeMenu), nil return newMenuModel(res, placeMenu), nil
case ",": case ",":
m.s.Input("pickup") m.s.Input("pickup")
case "x": case "x":
m.s.Input("destroy") m.s.Input("destroy")
case "c": case "c":
var res []sim.ItemEntry var res entrylist
for k := range m.s.Player.Craftables { for k := range m.s.Player.Craftables {
res = append(res, sim.GlobalItems[k].(sim.ItemEntry)) res = append(res, sim.GlobalItems[k].(sim.ItemEntry))
} }
sort.Sort(res)
return newMenuModel(res, craftMenu), nil return newMenuModel(res, craftMenu), nil
} }
return m, nil return m, nil

View File

@ -104,7 +104,7 @@ func (p *Player) research() {
func (p *Player) Announce(msg string) { func (p *Player) Announce(msg string) {
p.logIndex++ p.logIndex++
p.log = append(p.log, strconv.Itoa(p.logIndex)+" "+msg) p.log = append(p.log, strconv.Itoa(p.logIndex)+" "+msg)
if len(p.log) > 3 { if len(p.log) > 4 {
p.log = p.log[1:] p.log = p.log[1:]
} }
} }
@ -112,8 +112,11 @@ func (p *Player) Announce(msg string) {
//Log returns the player log //Log returns the player log
func (p *Player) Log() string { func (p *Player) Log() string {
res := "Log:\n" res := "Log:\n"
for _, v := range p.log { for i, v := range p.log {
res += v + "\n" res += v
if i < 3 {
res += "\n"
}
} }
return res return res
} }