Skip to content

Commit 78769b7

Browse files
Created choose package
1 parent d5b17c2 commit 78769b7

File tree

4 files changed

+101
-40
lines changed

4 files changed

+101
-40
lines changed

choose/game.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package choose
2+
3+
import (
4+
"notion-igdb-autocomplete/igdb"
5+
"sort"
6+
7+
"github.com/agnivade/levenshtein"
8+
)
9+
10+
type gameComparisons []gameComparison
11+
type gameComparison struct {
12+
index int
13+
game *igdb.Game
14+
}
15+
16+
// Implements interface sort.Interface
17+
func (gc gameComparisons) Len() int {
18+
return len(gc)
19+
}
20+
21+
// Implements interface sort.Interface
22+
func (gc gameComparisons) Swap(i, j int) {
23+
gc[i], gc[j] = gc[j], gc[i]
24+
}
25+
26+
// Implements interface sort.Interface
27+
func (gc gameComparisons) Less(i, j int) bool {
28+
return gc[i].index < gc[j].index
29+
}
30+
31+
// Game returns the best choice in the list for the given search
32+
func Game(search string, list igdb.Games) *igdb.Game {
33+
var comparisons gameComparisons
34+
35+
for _, game := range list {
36+
comparisons = append(comparisons, gameComparison{
37+
game: game,
38+
index: levenshtein.ComputeDistance(search, game.Name),
39+
})
40+
}
41+
42+
sort.Sort(comparisons)
43+
44+
return comparisons[0].game
45+
}

choose/game_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package choose
2+
3+
import (
4+
"notion-igdb-autocomplete/igdb"
5+
"sort"
6+
"testing"
7+
)
8+
9+
func TestGameComparisonsSorting(t *testing.T) {
10+
comparisons := gameComparisons{
11+
gameComparison{index: 89},
12+
gameComparison{index: 4},
13+
gameComparison{index: 64},
14+
gameComparison{index: 5},
15+
}
16+
17+
expected := []gameComparison{
18+
{index: 4},
19+
{index: 5},
20+
{index: 64},
21+
{index: 89},
22+
}
23+
24+
sort.Sort(comparisons)
25+
26+
for i, v := range comparisons {
27+
if v != expected[i] {
28+
t.Errorf("Unexpected comparison[%d]: expected=%v, got=%v", i, expected[i], v)
29+
}
30+
}
31+
}
32+
33+
func TestGameChoice(t *testing.T) {
34+
search := "world oF warcraft"
35+
choices := igdb.Games{
36+
&igdb.Game{Name: "World of Warcraft: Battle for Azeroth"},
37+
&igdb.Game{Name: "World of Warcraft: Wrath of the Lich King"},
38+
&igdb.Game{Name: "World of Warcraft: Warlords of Draenor"},
39+
&igdb.Game{Name: "World of Warcraft: Legion"},
40+
&igdb.Game{Name: "World of Warcraft"},
41+
&igdb.Game{Name: "World of Warcraft: Dragonflight"},
42+
&igdb.Game{Name: "World of Warcraft: Cataclysm"},
43+
&igdb.Game{Name: "World of Warcraft: The Burning Crusade"},
44+
&igdb.Game{Name: "World of Warcraft: Mists of Pandaria"},
45+
&igdb.Game{Name: "World of Warcraft: Shadowlands"},
46+
}
47+
expectedName := "World of Warcraft"
48+
result := Game(search, choices)
49+
50+
if result.Name != expectedName {
51+
t.Errorf("Unexpected choice: expected:%s, got:%s", expectedName, result.Name)
52+
}
53+
}

cmd/updater/main.go

Lines changed: 2 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,11 @@ import (
44
"fmt"
55
"log"
66
"net/http"
7+
"notion-igdb-autocomplete/choose"
78
"notion-igdb-autocomplete/config"
89
"notion-igdb-autocomplete/igdb"
910
"notion-igdb-autocomplete/notion"
10-
"sort"
1111

12-
"github.com/agnivade/levenshtein"
1312
"github.com/gin-gonic/gin"
1413
)
1514

@@ -76,41 +75,5 @@ func searchIgdbGame(gameName string, client *igdb.Client) (*igdb.Game, error) {
7675
return nil, fmt.Errorf("cannot find game '%s'", gameName)
7776
}
7877

79-
return findBestGame(gameName, results), nil
80-
}
81-
82-
type ComparedGames []ComparedGame
83-
type ComparedGame struct {
84-
Game igdb.Game
85-
Index int
86-
}
87-
88-
// Implements interface sort.Interface
89-
func (cg ComparedGames) Len() int {
90-
return len(cg)
91-
}
92-
93-
// Implements interface sort.Interface
94-
func (cg ComparedGames) Swap(i, j int) {
95-
cg[i], cg[j] = cg[j], cg[i]
96-
}
97-
98-
// Implements interface sort.Interface
99-
func (cg ComparedGames) Less(i, j int) bool {
100-
return cg[i].Index < cg[j].Index
101-
}
102-
103-
func findBestGame(search string, games igdb.Games) *igdb.Game {
104-
var comparisons ComparedGames
105-
106-
for _, game := range games {
107-
comparisons = append(comparisons, ComparedGame{
108-
Game: game,
109-
Index: levenshtein.ComputeDistance(search, game.Name),
110-
})
111-
}
112-
113-
sort.Sort(comparisons)
114-
115-
return &comparisons[0].Game
78+
return choose.Game(gameName, results), nil
11679
}

igdb/game.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ type Cover struct {
3636
ImageID string `json:"image_id,required"`
3737
}
3838

39-
type Games []Game
39+
type Games []*Game
4040
type Platforms []Platform
4141
type Franchises []Franchise
4242
type Genres []Genre

0 commit comments

Comments
 (0)