Compare commits

...

3 Commits

Author SHA1 Message Date
The Magician 4f14040747 Add "locate from file" feature 2024-09-08 00:25:38 +01:00
The Magician 0d4d171edf Replace search function with generic version 2024-09-07 19:10:11 +01:00
The Magician 1d9c614d16 Remove unused code 2024-09-05 12:22:02 +01:00
10 changed files with 191 additions and 70 deletions

View File

@ -12,21 +12,32 @@ import (
var output string
func showOutput() {
if output != "" {
fmt.Println(output)
}
}
func MainCliLoop(db *sql.DB) {
var command string
var selectedStorageArea database.StorageArea
var cardLocation database.CardLocation
var insertSearchCriteria logic.SearchCriteria = logic.SearchCriteria{
SetCode: "",
Foil: logic.Either,
Promo: logic.Either,
Language: "en",
}
var insertSearchOptions logic.InsertSearchOptions
// TODO: Make these do something and add the ability to modify them
var locateSearchCriteria logic.SearchCriteria = logic.SearchCriteria{
SetCode: "",
Foil: logic.True,
Promo: logic.Either,
Language: "en",
}
for {
ShowSplashScreen()
showStorageInfo(os.Stdout, selectedStorageArea)
showSearchCriteria()
showInsertSearchCriteria(insertSearchCriteria)
showSelectedCard()
showCopiesInserted()
@ -51,14 +62,18 @@ func MainCliLoop(db *sql.DB) {
cardLocation.StorageAreaId = area.Id
break
case "c", "criteria":
getSearchCriteria()
insertSearchCriteria = getSearchCriteria()
break
case "s", "search":
getSearchOptions(db)
if shouldRefreshSearch {
insertSearchOptions = getSearchOptions(db, insertSearchCriteria)
}
var previousCardPrintingId = cardLocation.CardPrintingId
cardLocation.CardPrintingId, selectedCardPrintingSearchLine, err = logic.Search(searchOptions)
pk, err := logic.GenericSearch(insertSearchOptions)
cardLocation.CardPrintingId = pk
var exitError *exec.ExitError
if errors.As(err, &exitError) {
break
@ -74,22 +89,15 @@ func MainCliLoop(db *sql.DB) {
insertSelectedCard(db, selectedStorageArea, cardLocation)
break
case "l", "locate":
cardName := GetStringResponse("Card name:")
locations, err := logic.LocateCards(db, cardName)
filename := GetStringResponse("Filename:")
cardNames, err := logic.GetCardNamesFromFile(filename)
logic.Check(err)
locations, err := logic.LocateCards(db, cardNames, locateSearchCriteria)
logic.Check(err)
for _, location := range locations {
fmt.Printf("%s (%s %s) [%s]", location.CardName, location.SetCode, location.CollectorNumber, location.Language)
if location.IsFoil {
fmt.Printf(" FOIL")
}
if location.IsPromo {
fmt.Printf(" PROMO")
}
fmt.Printf(" in %s \"%s\" at position %d\n", location.StorageAreaType, location.StorageAreaName, location.Position)
fmt.Println(location)
}
fmt.Scanln()

View File

@ -6,23 +6,18 @@ import (
"sevenkeys/logic"
)
var (
searchCriteria logic.SearchCriteria = logic.SearchCriteria{
SetCode: "",
Foil: logic.Either,
Promo: logic.Either,
Language: "",
}
shouldRefreshSearch bool = true
searchOptions logic.SearchOptions
)
var shouldRefreshSearch bool = true
func getSearchCriteria() logic.SearchCriteria {
var searchCriteria logic.SearchCriteria
func getSearchCriteria() {
searchCriteria.SetCode = GetStringResponse("Set code:")
searchCriteria.Foil = GetTriadicResponse("Foil (y/n/E):")
searchCriteria.Promo = GetTriadicResponse("Promo (y/n/E):")
searchCriteria.Language = GetStringResponse("Language:")
shouldRefreshSearch = true
return searchCriteria
}
func getTriadicDisplay(triadic logic.Triadic) string {
@ -37,13 +32,13 @@ func getTriadicDisplay(triadic logic.Triadic) string {
return "Either"
}
func showSearchCriteria() {
func showInsertSearchCriteria(insertSearchCriteria logic.SearchCriteria) {
fmt.Println("SEARCH CRITERIA")
setCodeDisplay := getInfoDisplay(searchCriteria.SetCode)
foilDisplay := getTriadicDisplay(searchCriteria.Foil)
promoDisplay := getTriadicDisplay(searchCriteria.Promo)
languageDisplay := getInfoDisplay(searchCriteria.Language)
setCodeDisplay := getInfoDisplay(insertSearchCriteria.SetCode)
foilDisplay := getTriadicDisplay(insertSearchCriteria.Foil)
promoDisplay := getTriadicDisplay(insertSearchCriteria.Promo)
languageDisplay := getInfoDisplay(insertSearchCriteria.Language)
fmt.Println("Set code:", setCodeDisplay)
fmt.Println("Foil:", foilDisplay)
@ -58,14 +53,9 @@ func showSelectedCard() {
fmt.Println("Selected card:", selectedCardDisplay)
}
func getSearchOptions(db *sql.DB) {
if shouldRefreshSearch {
fmt.Println("LOADING")
options, err := logic.GetAllSearchOptions(db, searchCriteria)
func getSearchOptions(db *sql.DB, insertSearchCriteria logic.SearchCriteria) logic.InsertSearchOptions {
options, err := logic.GetAllSearchOptions(db, insertSearchCriteria)
logic.Check(err)
searchOptions = options
shouldRefreshSearch = false
fmt.Println("READY")
fmt.Println("RUN")
}
return options
}

View File

@ -1,6 +1,10 @@
package database
import "database/sql"
import (
"database/sql"
"github.com/jmoiron/sqlx"
)
type LocateCardResult struct {
CardName string
@ -14,11 +18,16 @@ type LocateCardResult struct {
Position int
}
func GetLocateCardResultsByCardName(db *sql.DB, cardName string) ([]LocateCardResult, error) {
func GetLocateResults(db *sql.DB, cardNames []string) ([]LocateCardResult, error) {
var results []LocateCardResult
query := "SELECT CardPrinting.Name, CardPrinting.SetCode, CardPrinting.IsFoil, CardPrinting.IsPromo, CardPrinting.CollectorNumber, CardPrinting.Language, StorageArea.StorageType, StorageArea.Name, CardLocation.Position FROM CardLocation JOIN CardPrinting ON CardLocation.CardPrintingId = CardPrinting.Id JOIN StorageArea ON CardLocation.StorageAreaId = StorageArea.Id WHERE CardPrinting.Name = ?;"
rows, err := db.Query(query, cardName)
query := "SELECT CardPrinting.Name, CardPrinting.SetCode, CardPrinting.IsFoil, CardPrinting.IsPromo, CardPrinting.CollectorNumber, CardPrinting.Language, StorageArea.StorageType, StorageArea.Name, CardLocation.Position FROM CardLocation JOIN CardPrinting ON CardLocation.CardPrintingId = CardPrinting.Id JOIN StorageArea ON CardLocation.StorageAreaId = StorageArea.Id WHERE CardPrinting.Name IN (?);"
query, args, err := sqlx.In(query, cardNames)
if err != nil {
return results, err
}
rows, err := db.Query(query, args...)
defer rows.Close()
if err != nil {
return results, err

View File

@ -17,6 +17,7 @@ require (
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inancgumus/screen v0.0.0-20190314163918-06e984b86ed3 // indirect
github.com/jmoiron/sqlx v1.4.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/lukesampson/figlet v0.0.0-20190211215653-8a3ef4a6ac42 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect

View File

@ -20,6 +20,9 @@ github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpv
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/inancgumus/screen v0.0.0-20190314163918-06e984b86ed3 h1:fO9A67/izFYFYky7l1pDP5Dr0BTCRkaQJUG6Jm5ehsk=
github.com/inancgumus/screen v0.0.0-20190314163918-06e984b86ed3/go.mod h1:Ey4uAp+LvIl+s5jRbOHLcZpUDnkjLBROl15fZLwPlTM=
github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o=
github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/lukesampson/figlet v0.0.0-20190211215653-8a3ef4a6ac42 h1:UtyD+eBVdLYSj5/pjfSR6mtnzMgIiOVcFT024G2l4CY=
@ -31,6 +34,7 @@ github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+Ei
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34=
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=

25
sevenkeys/logic/file.go Normal file
View File

@ -0,0 +1,25 @@
package logic
import (
"bufio"
"os"
)
func GetCardNamesFromFile(filename string) ([]string, error) {
var cardNames []string
file, err := os.Open(filename)
defer file.Close()
if err != nil {
return cardNames, err
}
scanner := bufio.NewScanner(file)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
cardNames = append(cardNames, scanner.Text())
}
return cardNames, nil
}

View File

@ -2,10 +2,67 @@ package logic
import (
"database/sql"
"fmt"
"sevenkeys/database"
)
func LocateCards(db *sql.DB, cardName string) ([]database.LocateCardResult, error) {
results, err := database.GetLocateCardResultsByCardName(db, cardName)
return results, err
const SLOTS_PER_BINDER_PAGE = 18 // TODO: Make this configurable
func GetBinderLocationDescription(position int) string {
var page int = (position / SLOTS_PER_BINDER_PAGE) + 1
var pagePosition int = position % SLOTS_PER_BINDER_PAGE
var slot int
var frontOrBack string
if pagePosition <= (SLOTS_PER_BINDER_PAGE / 2) {
slot = pagePosition
frontOrBack = "front"
} else {
slot = pagePosition - (SLOTS_PER_BINDER_PAGE / 2)
frontOrBack = "back"
}
return fmt.Sprintf(" on page %d in %s slot %d", page, frontOrBack, slot)
}
func LocateCards(db *sql.DB, cardNames []string, criteria SearchCriteria) ([]string, error) {
var locations []string
results, err := database.GetLocateResults(db, cardNames)
if err != nil {
return locations, err
}
// TODO: Filter by search criteria
var location string
for _, result := range results {
location = fmt.Sprintf("%s (%s %s) [%s]",
result.CardName,
result.SetCode,
result.CollectorNumber,
result.Language)
if result.IsFoil {
location += " FOIL"
}
if result.IsPromo {
location += " PROMO"
}
location += fmt.Sprintf(" in %s \"%s\"",
result.StorageAreaType,
result.StorageAreaName)
if result.StorageAreaType == "Binder" {
location += GetBinderLocationDescription(result.Position)
} else if result.StorageAreaType == "Box" {
location += fmt.Sprintf(" at position %d", result.Position)
}
locations = append(locations, location)
}
return locations, nil
}

View File

@ -0,0 +1,25 @@
package logic
import "testing"
func Test_GetBinderLocationDescription_ReturnsCorrectFormat_ForFrontSlots(t *testing.T) {
var position int = 24
var expected string = "on page 2 in front slot 6"
description := GetBinderLocationDescription(position)
if description != expected {
t.Errorf("expected %s, got %s\n", expected, description)
}
}
func Test_GetBinderLocationDescription_ReturnsCorrectFormat_ForBackSlots(t *testing.T) {
var position int = 17
var expected string = "on page 1 in back slot 8"
description := GetBinderLocationDescription(position)
if description != expected {
t.Errorf("expected %s, got %s\n", expected, description)
}
}

View File

@ -25,9 +25,9 @@ type SearchCriteria struct {
Language string
}
// The SearchOptions type is a map of `string` keys (which can be searched using fzf)
// to `string` values (which represent a primary key in the CardPrintings table)
type SearchOptions map[string]string
// The InsertSearchOptions type is a map of `string` keys (which can be searched using
// fzf) to `string` values (which represent a primary key in the CardPrintings table)
type InsertSearchOptions map[string]string
func filterPrinting(printing database.CardPrinting, searchCriteria SearchCriteria) bool {
if searchCriteria.SetCode != "" && printing.SetCode != searchCriteria.SetCode {
@ -55,8 +55,8 @@ func filterPrinting(printing database.CardPrinting, searchCriteria SearchCriteri
return false
}
func GetAllSearchOptions(db *sql.DB, searchCriteria SearchCriteria) (SearchOptions, error) {
var searchOptions SearchOptions = make(map[string]string)
func GetAllSearchOptions(db *sql.DB, searchCriteria SearchCriteria) (InsertSearchOptions, error) {
var searchOptions InsertSearchOptions = make(map[string]string)
cardPrintings, err := database.GetAllCardPrintings(db)
if err != nil {
@ -87,28 +87,30 @@ func GetAllSearchOptions(db *sql.DB, searchCriteria SearchCriteria) (SearchOptio
return searchOptions, err
}
func Search(searchOptions SearchOptions) (string, string, error) {
func GenericSearch[pk string | int](options map[string]pk) (pk, error) {
var value pk
cmd := exec.Command("fzf")
cmd.Stderr = os.Stderr
fzfStdin, err := cmd.StdinPipe()
if err != nil {
return "", "", err
return value, err
}
go func() {
defer fzfStdin.Close()
for searchString, _ := range searchOptions {
io.WriteString(fzfStdin, searchString+"\n")
for option, _ := range options {
io.WriteString(fzfStdin, option+"\n")
}
}()
fzfOutput, err := cmd.Output()
if err != nil {
return "", "", err
return value, err
}
key := strings.TrimSuffix(string(fzfOutput), "\n")
return searchOptions[key], key, nil
searchResult := strings.TrimSuffix(string(fzfOutput), "\n")
value = options[searchResult]
return value, nil
}

View File

@ -8,8 +8,8 @@ import (
func main() {
db := database.GetDatabaseFromConfig("config.json")
figlet.ReadFigletFonts()
figlet.ReadFigletFonts()
cli.ShowSplashScreen()
cli.RunUpdateCheck(db)