Skip to content

Commit

Permalink
More fixes after web API addition (#45)
Browse files Browse the repository at this point in the history
* change down version

* update mrext ver

* increase search result limit

* match insert and tap mode ttp and pbp

* aggressively remove bad ws connections

* link game and system updates in tracker

* don't omit empty ini settings

* fix nfc.csv bugs

* workaround to ignore preset loading
  • Loading branch information
wizzomafizzo authored Apr 15, 2024
1 parent 409ae3e commit 56983f5
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 64 deletions.
2 changes: 1 addition & 1 deletion cmd/nfc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ import (

const (
appName = "tapto"
appVersion = "2.0"
appVersion = "1.3"
)

func addToStartup() error {
Expand Down
16 changes: 8 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require github.com/magefile/mage v1.15.0

require github.com/rthornton128/goncurses v0.0.0-20220628231859-fd57939296e5

require golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8
require golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8

require gopkg.in/ini.v1 v1.67.0

Expand All @@ -17,10 +17,10 @@ require (
github.com/libp2p/zeroconf/v2 v2.2.0 // indirect
github.com/miekg/dns v1.1.55 // indirect
github.com/txn2/txeh v1.4.0 // indirect
golang.org/x/mod v0.16.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/tools v0.19.0 // indirect
golang.org/x/mod v0.17.0 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/tools v0.20.0 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0
)

Expand All @@ -29,8 +29,7 @@ require (
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/stretchr/testify v1.9.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/term v0.18.0 // indirect
golang.org/x/term v0.19.0 // indirect
)

require (
Expand All @@ -43,6 +42,7 @@ require (
github.com/hsanjuan/go-ndef v0.0.1
github.com/joho/godotenv v1.5.1
github.com/rs/zerolog v1.31.0
github.com/wizzomafizzo/mrext v0.0.0-20240404130445-f6d1818d9208
github.com/wizzomafizzo/mrext v0.0.0-20240413092624-7a1a9463038c
go.etcd.io/bbolt v1.3.9
golang.org/x/sync v0.7.0
)
22 changes: 18 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,33 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/txn2/txeh v1.4.0 h1:0tdvpA4HGJrj8X3kmrU6o/JFStI009nKxwDpMK5CnRU=
github.com/txn2/txeh v1.4.0/go.mod h1:Mgq0hY184zCrDBLgvkIp+9NYGHoYbJcu4xKqUcx1shc=
github.com/wizzomafizzo/mrext v0.0.0-20240315225527-30b49ae77201 h1:uCchJEYhUNYpIZ/5KlBuq0BJuRmu3IBu/+oCcLFuIoM=
github.com/wizzomafizzo/mrext v0.0.0-20240315225527-30b49ae77201/go.mod h1:pWjoPIzJIXlDfEmdf++eUqsZKrEsYVkOHy39s/H7WLA=
github.com/wizzomafizzo/mrext v0.0.0-20240404130445-f6d1818d9208 h1:igaavvxDxdTBx7dlHWudSspOl7fUC04f25H8BlzFeic=
github.com/wizzomafizzo/mrext v0.0.0-20240404130445-f6d1818d9208/go.mod h1:pWjoPIzJIXlDfEmdf++eUqsZKrEsYVkOHy39s/H7WLA=
github.com/wizzomafizzo/mrext v0.0.0-20240412021227-ac362d93c5f3 h1:pQCGFUPQraWfVHT3YHNAi1vMlmekSV1CIVyevoxbbns=
github.com/wizzomafizzo/mrext v0.0.0-20240412021227-ac362d93c5f3/go.mod h1:pWjoPIzJIXlDfEmdf++eUqsZKrEsYVkOHy39s/H7WLA=
github.com/wizzomafizzo/mrext v0.0.0-20240413092624-7a1a9463038c h1:BWJ1gPXMvKOIDF/mlS+aYRWbBvwrV7yOyGvapqaoX1U=
github.com/wizzomafizzo/mrext v0.0.0-20240413092624-7a1a9463038c/go.mod h1:pWjoPIzJIXlDfEmdf++eUqsZKrEsYVkOHy39s/H7WLA=
go.etcd.io/bbolt v1.3.9 h1:8x7aARPEXiXbHmtUwAIv7eV2fQFHrLLavdiJ3uzJXoI=
go.etcd.io/bbolt v1.3.9/go.mod h1:zaO32+Ti0PK1ivdPtgMESzuzL2VPoIG1PCQNvOdo/dE=
golang.org/x/exp v0.0.0-20240318143956-a85f2c67cd81 h1:6R2FC06FonbXQ8pK11/PDFY6N6LWlf9KlzibaCapmqc=
golang.org/x/exp v0.0.0-20240318143956-a85f2c67cd81/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ=
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8 h1:aAcj0Da7eBAtrTp03QXWvm88pSyOt+UgdZw2BFZ+lEw=
golang.org/x/exp v0.0.0-20240325151524-a685a6edb6d8/go.mod h1:CQ1k9gNrJ50XIzaKCRR2hssIjF07kZFEiieALBM/ARQ=
golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8 h1:ESSUROHIBHg7USnszlcdmjBEwdMj9VUvU+OPk4yl2mc=
golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8/go.mod h1:/lliqkxwWAhPjf5oSOIJup2XcqJaw8RGS6k3TGEc7GI=
golang.org/x/mod v0.16.0 h1:QX4fJ0Rr5cPQCF7O9lh9Se4pmwfwskqZfq5moyldzic=
golang.org/x/mod v0.16.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/mod v0.17.0 h1:zY54UmvipHiNd+pm+m0x9KhZ9hl1/7QNMyxXbc6ICqA=
golang.org/x/mod v0.17.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20210423184538-5f58ad60dda6/go.mod h1:OJAsFXCWl8Ukc7SiCT/9KSuxbyM7479/AVlXFRxuMCk=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w=
golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210303074136-134d130e1a04/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
Expand All @@ -77,14 +85,20 @@ golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o=
golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
golang.org/x/term v0.19.0 h1:+ThwsDv+tYfnJFhF4L8jITxu1tdTWRTZpdsWgEgjL6Q=
golang.org/x/term v0.19.0/go.mod h1:2CuTdWZ7KHSQwUzKva0cbMg6q2DMI3Mmxp+gKJbskEk=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.19.0 h1:tfGCXNR1OsFG+sVdLAitlpjAvD/I6dHDKnYrpEZUHkw=
golang.org/x/tools v0.19.0/go.mod h1:qoJWxmGSIBmAeriMx19ogtrEPrGtDbPK634QFIcLAhc=
golang.org/x/tools v0.20.0 h1:hz/CVckiOxybQvFw6h7b/q80NTr9IUQb4s1IIzW7KNY=
golang.org/x/tools v0.20.0/go.mod h1:WvitBU7JJf6A4jOdg4S1tviW9bhUxkgeCui/0JHctQg=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/natefinch/lumberjack.v2 v2.0.0 h1:1Lc07Kr7qY4U2YPouBjpCLxpiyxIVoxqXgkXLknAOE8=
Expand Down
16 changes: 8 additions & 8 deletions pkg/config/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@ const UserConfigEnv = "TAPTO_CONFIG"
const UserAppPathEnv = "TAPTO_APP_PATH"

type TapToConfig struct {
ConnectionString string `ini:"connection_string,omitempty"`
AllowCommands bool `ini:"allow_commands,omitempty"`
DisableSounds bool `ini:"disable_sounds,omitempty"`
ProbeDevice bool `ini:"probe_device,omitempty"`
ExitGame bool `ini:"exit_game,omitempty"`
ExitGameBlocklist []string `ini:"exit_game_blocklist,omitempty"`
ConnectionString string `ini:"connection_string"`
AllowCommands bool `ini:"allow_commands"`
DisableSounds bool `ini:"disable_sounds"`
ProbeDevice bool `ini:"probe_device"`
ExitGame bool `ini:"exit_game"`
ExitGameBlocklist []string `ini:"exit_game_blocklist"`
ExitGameDelay int8 `ini:"exit_game_delay"`
Debug bool `ini:"debug,omitempty"`
Debug bool `ini:"debug"`
}

type SystemsConfig struct {
Expand All @@ -53,7 +53,7 @@ type UserConfig struct {
mu sync.RWMutex
AppPath string `ini:"-"`
IniPath string `ini:"-"`
TapTo TapToConfig `ini:"tapto,omitempty"`
TapTo TapToConfig `ini:"tapto"`
Systems SystemsConfig `ini:"systems,omitempty"`
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/daemon/api/games.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/wizzomafizzo/tapto/pkg/platforms/mister"
)

const maxResults = 100
const maxResults = 250

type Index struct {
mu sync.Mutex
Expand Down Expand Up @@ -146,6 +146,7 @@ func handleGames(cfg *config.UserConfig) http.HandlerFunc {
}
search, err = gamesdb.SearchNamesWords([]games.System{*system}, query)
}

if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Error().Err(err).Msg("error searching games")
Expand Down
4 changes: 3 additions & 1 deletion pkg/daemon/api/websocket/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,12 @@ func (cg *connGroup) Broadcast(msg string) {
cg.Clean()
cg.mu.Lock()
defer cg.mu.Unlock()
for _, c := range cg.conns {
for i, c := range cg.conns {
err := send(c, msg)
if err != nil {
cg.logger.Error().Err(err).Msg("failed to write to websocket")
c.Close()
cg.conns[i] = nil
}
}
}
Expand Down
33 changes: 20 additions & 13 deletions pkg/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,33 +64,38 @@ func inExitGameBlocklist(cfg *config.UserConfig) bool {
return slices.Contains(blocklist, strings.ToLower(mister.GetActiveCoreName()))
}

func launchCard(cfg *config.UserConfig, state *state.State, db *database.Database, kbd input.Keyboard) error {
card := state.GetActiveCard()
func launchToken(
cfg *config.UserConfig,
token state.Token,
state *state.State,
db *database.Database,
kbd input.Keyboard,
) error {
uidMap, textMap := state.GetDB()

text := card.Text
override := false
text := token.Text
mapped := false

if v, ok := uidMap[card.UID]; ok {
if v, ok := uidMap[token.UID]; ok {
log.Info().Msg("launching with csv uid match override")
text = v
override = true
mapped = true
}

if v, err := db.GetUidMapping(card.UID); err == nil {
if v, err := db.GetUidMapping(token.UID); err == nil {
if err != nil {
log.Error().Err(err).Msgf("error getting db uid mapping")
} else if v != "" {
log.Info().Msg("launching with db uid match override")
text = v
override = true
mapped = true
}
}

if v, ok := textMap[card.Text]; ok {
if v, ok := textMap[token.Text]; ok {
log.Info().Msg("launching with csv text match override")
text = v
override = true
mapped = true
}

if text == "" {
Expand All @@ -101,7 +106,7 @@ func launchCard(cfg *config.UserConfig, state *state.State, db *database.Databas
cmds := strings.Split(text, "||")

for i, cmd := range cmds {
err := launcher.LaunchToken(cfg, cfg.GetAllowCommands() || override, kbd, cmd, len(cmds), i)
err := launcher.LaunchToken(cfg, cfg.GetAllowCommands() || mapped, kbd, cmd, len(cmds), i)
if err != nil {
return err
}
Expand All @@ -120,6 +125,8 @@ func processLaunchQueue(
for {
select {
case t := <-tq.Tokens:
log.Info().Msgf("processing token: %v", t)

st.SetActiveCard(t)

err := writeScanResult(t)
Expand All @@ -141,9 +148,9 @@ func processLaunchQueue(
continue
}

err = launchCard(cfg, st, db, kbd)
err = launchToken(cfg, t, st, db, kbd)
if err != nil {
log.Error().Err(err).Msgf("error launching card")
log.Error().Err(err).Msgf("error launching token")
}

he.Success = err == nil
Expand Down
19 changes: 5 additions & 14 deletions pkg/daemon/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (
const (
timeToForgetCard = 500 * time.Millisecond
connectMaxTries = 10
TimesToPoll = 20
PeriodBetweenPolls = 300 * time.Millisecond
periodBetweenLoop = 300 * time.Millisecond
TimesToPoll = 1
PeriodBetweenPolls = 250 * time.Millisecond
periodBetweenLoop = 250 * time.Millisecond
)

func pollDevice(
Expand Down Expand Up @@ -211,14 +211,6 @@ func readerPollLoop(
}
}

if cfg.GetExitGame() {
// FIXME: this method makes the activity indicator flicker, is there another way?
ttp = 1
// TODO: value requires investigation, originally set to 150 which worked for pn532
// but not for acr122u (read once then never again). 200 seems to work ok
pbp = 200 * time.Millisecond
}

log.Debug().Msgf("polling for %d times with %s delay", ttp, pbp)

for {
Expand Down Expand Up @@ -261,7 +253,7 @@ func readerPollLoop(

var count int
var target nfc.Target
tries := 6 // ~30 seconds
tries := 4 * 30 // ~30 seconds

for tries > 0 {
count, target, err = pnd.InitiatorPollTarget(tokens.SupportedCardTypes, ttp, pbp)
Expand Down Expand Up @@ -355,9 +347,8 @@ func readerPollLoop(

// if there is no card (newScanned.UID == "")
// if the card is the same as the one we have scanned before ( activeCard.UID == newScanned.UID)
// if the card has no text a real card but empty (newScanned.Text == "")
// if the card is the same that has been loaded last time (newScanned.UID == currentlyLoadedCard.UID)
if newScanned.UID == "" || activeCard.UID == newScanned.UID || newScanned.Text == "" || newScanned.UID == currentlyLoadedCard.UID {
if newScanned.UID == "" || activeCard.UID == newScanned.UID || newScanned.UID == currentlyLoadedCard.UID {
continue
}

Expand Down
35 changes: 21 additions & 14 deletions pkg/platforms/mister/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ func (tr *Tracker) ReloadNameMap() {
tr.NameMap = nameMap
}

func (tr *Tracker) LookupCoreName(name string, game string) NameMapping {
func (tr *Tracker) LookupCoreName(name string, path string) NameMapping {
log.Debug().Msgf("looking up name: %s", name)
log.Debug().Msgf("game: %s", game)
log.Debug().Msgf("file path: %s", path)

for _, mapping := range tr.NameMap {
if len(mapping.CoreName) != len(name) {
Expand All @@ -134,14 +134,14 @@ func (tr *Tracker) LookupCoreName(name string, game string) NameMapping {
return mapping
}

sys, err := games.BestSystemMatch(tr.Config, game)
sys, err := games.BestSystemMatch(tr.Config, path)
if err != nil {
log.Debug().Msgf("error finding system for game %s, %s: %s", name, game, err)
log.Debug().Msgf("error finding system for game %s, %s: %s", name, path, err)
continue
}

if sys.Id != mapping.System {
log.Debug().Msgf("system mismatch: %s != %s", sys.Id, mapping.System)
if sys.Id == "" {
log.Debug().Msgf("no system found for game: %s, %s", name, path)
continue
}

Expand All @@ -158,11 +158,11 @@ func (tr *Tracker) stopCore() bool {
tr.ActiveGameId = ""
tr.ActiveGamePath = ""
tr.ActiveGameName = ""
tr.ActiveSystem = ""
tr.ActiveSystemName = ""
}

tr.ActiveCore = ""
tr.ActiveSystem = ""
tr.ActiveSystemName = ""

return true
} else {
Expand Down Expand Up @@ -204,17 +204,13 @@ func (tr *Tracker) LoadCore() {

result := tr.LookupCoreName(coreName, tr.ActiveGamePath)
if result != (NameMapping{}) {
tr.ActiveSystem = result.System
tr.ActiveSystemName = result.Name

if result.ArcadeName != "" {
tr.ActiveGameId = coreName
tr.ActiveGameName = result.ArcadeName
tr.ActiveGamePath = "" // TODO: any way to find this?
tr.ActiveSystem = ArcadeSystem
tr.ActiveSystemName = ArcadeSystem
}
} else {
tr.ActiveSystem = ""
tr.ActiveSystemName = ""
}

tr.runEventHook()
Expand All @@ -226,6 +222,8 @@ func (tr *Tracker) stopGame() bool {
tr.ActiveGameId = ""
tr.ActiveGamePath = ""
tr.ActiveGameName = ""
tr.ActiveSystem = ""
tr.ActiveSystemName = ""
return true
} else {
return false
Expand Down Expand Up @@ -264,6 +262,12 @@ func (tr *Tracker) loadGame() {
system, err := games.BestSystemMatch(tr.Config, path)
if err != nil {
log.Error().Msgf("error finding system for game: %s", err)

// temporary(?) workaround to ignore bug where presets loaded from
// OSD are written to the recents file as a loaded game
if strings.HasSuffix(strings.ToLower(filename), ".ini") {
return
}
}

id := fmt.Sprintf("%s/%s", system.Id, filename)
Expand All @@ -275,6 +279,9 @@ func (tr *Tracker) loadGame() {
tr.ActiveGameName = name
tr.ActiveGamePath = path

tr.ActiveSystem = system.Id
tr.ActiveSystemName = system.Name

tr.runEventHook()
}
}
Expand Down

0 comments on commit 56983f5

Please sign in to comment.