TheMathemagicians/sevenkeys/logic/locate.go

99 lines
2.2 KiB
Go

package logic
import (
"database/sql"
"fmt"
"sevenkeys/database"
)
const SLOTS_PER_BINDER_PAGE = 18 // TODO: Make this configurable
func GetBinderLocationDescription(position int) string {
var page int = ((position - 1) / SLOTS_PER_BINDER_PAGE) + 1
var pagePosition int = position % SLOTS_PER_BINDER_PAGE
if pagePosition == 0 {
pagePosition = 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 GetCardAtLocation(db *sql.DB, cardLocationId int) (database.LocateCardResult, error) {
var result database.LocateCardResult
result, err := database.GetLocateResultByCardLocationId(db, cardLocationId)
if err != nil {
return result, err
}
return result, nil
}
func LocateCards(db *sql.DB, cardNames []string, criteria SearchCriteria) ([]database.LocateCardResult, error) {
results, err := database.GetLocateResults(db, cardNames)
if err != nil {
return results, err
}
var filteredResults []database.LocateCardResult
for _, result := range results {
printing := database.CardPrinting{
SetCode: result.SetCode,
IsFoil: result.IsFoil,
IsPromo: result.IsPromo,
Language: result.Language,
}
filter := filterPrinting(printing, criteria)
if filter {
continue
}
filteredResults = append(filteredResults, result)
}
return filteredResults, nil
}
func GetLocationDescription(location database.LocateCardResult) string {
var description string
description = fmt.Sprintf("%s (%s %s) [%s]",
location.CardName,
location.SetCode,
location.CollectorNumber,
location.Language)
if location.IsFoil {
description += " FOIL"
}
if location.IsPromo {
description += " PROMO"
}
description += fmt.Sprintf(" in %s \"%s\"",
location.StorageAreaType,
location.StorageAreaName)
if location.StorageAreaType == "Binder" {
description += GetBinderLocationDescription(location.Position)
} else if location.StorageAreaType == "Box" {
description += fmt.Sprintf(" at position %d", location.Position)
}
return description
}