Skip to content

Commit c9f6bd5

Browse files
committed
fix mounts
1 parent ba3ba7e commit c9f6bd5

4 files changed

Lines changed: 55 additions & 153 deletions

File tree

handler.go

Lines changed: 31 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@ var (
3535

3636
searchAllItemAllowedExpandFields = []string{"type", "image_urls", "level"}
3737

38-
mountAllowedExpandFields = []string{"effects"}
38+
mountAllowedExpandFields = []string{"effects"}
39+
40+
// Equipment item type IDs that represent mounts
41+
mountEquipmentTypeIds = map[int]bool{
42+
242: true,
43+
245: true,
44+
247: true,
45+
}
3946
setAllowedExpandFields = utils.Concat(mountAllowedExpandFields, []string{"equipment_ids"})
4047
itemAllowedExpandFields = utils.Concat(mountAllowedExpandFields, []string{"recipe", "description", "conditions", "pods"})
4148
equipmentAllowedExpandFields = utils.Concat(itemAllowedExpandFields, []string{"range", "parent_set", "is_weapon", "critical_hit_probability", "critical_hit_bonus", "max_cast_per_turn", "ap_cost"})
@@ -211,8 +218,8 @@ func ListMounts(w http.ResponseWriter, r *http.Request) {
211218
txn := database.Db.Txn(false)
212219
defer txn.Abort()
213220

214-
it, err := txn.Get(fmt.Sprintf("%s-%s", utils.CurrentRedBlueVersionStr(database.Version.MemDb), "mounts"), "id")
215-
if err != nil || it == nil {
221+
equipIt, err := txn.Get(fmt.Sprintf("%s-%s", utils.CurrentRedBlueVersionStr(database.Version.MemDb), "equipment"), "id")
222+
if err != nil || equipIt == nil {
216223
e.WriteNotFoundResponse(w, "No mounts found.")
217224
return
218225
}
@@ -221,10 +228,13 @@ func ListMounts(w http.ResponseWriter, r *http.Request) {
221228
utils.RequestsMountsList.Inc()
222229

223230
var mounts []APIMount
224-
for obj := it.Next(); obj != nil; obj = it.Next() {
225-
p := obj.(*mapping.MappedMultilangMount)
231+
for obj := equipIt.Next(); obj != nil; obj = equipIt.Next() {
232+
p := obj.(*mapping.MappedMultilangItemUnity)
233+
if !mountEquipmentTypeIds[p.Type.ItemTypeId] {
234+
continue
235+
}
226236
if filterFamilyName != "" {
227-
if !strings.EqualFold(p.FamilyName[lang], filterFamilyName) {
237+
if !strings.EqualFold(p.Type.Name[lang], filterFamilyName) {
228238
continue
229239
}
230240
}
@@ -234,12 +244,11 @@ func ListMounts(w http.ResponseWriter, r *http.Request) {
234244
e.WriteInvalidFilterResponse(w, "filter[family.id] is not a number.")
235245
return
236246
}
237-
if p.FamilyId != filterFamilyId {
247+
if p.Type.ItemTypeId != filterFamilyId {
238248
continue
239249
}
240250
}
241-
mount := RenderMountListEntry(p, lang)
242-
251+
mount := RenderEquipmentAsMountListEntry(p, lang)
243252
if expansions.Has("effects") {
244253
effects := RenderEffects(&p.Effects, lang)
245254
if len(effects) != 0 {
@@ -837,19 +846,17 @@ func SearchMounts(w http.ResponseWriter, r *http.Request) {
837846
}
838847
itemId := int(hit.Id)
839848

840-
raw, err := txn.First(fmt.Sprintf("%s-%s", utils.CurrentRedBlueVersionStr(database.Version.MemDb), "mounts"), "id", itemId)
849+
raw, err := txn.First(fmt.Sprintf("%s-%s", utils.CurrentRedBlueVersionStr(database.Version.MemDb), "equipment"), "id", itemId)
841850
if err != nil {
842851
e.WriteServerErrorResponse(w, "Could not read database: "+err.Error())
843852
return
844853
}
845-
846854
if raw == nil {
847855
e.WriteNotFoundResponse(w, fmt.Sprintf("Could not find %s with ID %s in database", "mount", strconv.Itoa(itemId)))
848856
return
849857
}
850-
851-
item := raw.(*mapping.MappedMultilangMount)
852-
mounts = append(mounts, RenderMountListEntry(item, lang))
858+
item := raw.(*mapping.MappedMultilangItemUnity)
859+
mounts = append(mounts, RenderEquipmentAsMountListEntry(item, lang))
853860
}
854861

855862
utils.WriteCacheHeader(&w)
@@ -1061,7 +1068,7 @@ func SearchAllIndices(w http.ResponseWriter, r *http.Request) {
10611068

10621069
searchChans := make([]chan []ApiAllSearchResultScore, 0)
10631070

1064-
indicesHasItem := parsedIndices.Has("items-equipment") || parsedIndices.Has("items-consumables") || parsedIndices.Has("items-resources") || parsedIndices.Has("items-quest_items") || parsedIndices.Has("items-cosmetics")
1071+
indicesHasItem := parsedIndices.Has("items-equipment") || parsedIndices.Has("items-consumables") || parsedIndices.Has("items-resources") || parsedIndices.Has("items-quest_items") || parsedIndices.Has("items-cosmetics") || parsedIndices.Has("mounts")
10651072
needItemSearch := parsedIndices.Size() == 0 || indicesHasItem
10661073
if needItemSearch {
10671074
itemRetChan := make(chan []ApiAllSearchResultScore)
@@ -1129,7 +1136,8 @@ func SearchAllIndices(w http.ResponseWriter, r *http.Request) {
11291136
}
11301137

11311138
// search_index filter
1132-
if !parsedIndices.Has(itemType) {
1139+
isMountItem := mountEquipmentTypeIds[item.Type.ItemTypeId]
1140+
if !parsedIndices.Has(itemType) && !(parsedIndices.Has("mounts") && isMountItem) {
11331141
continue
11341142
}
11351143

@@ -1252,84 +1260,6 @@ func SearchAllIndices(w http.ResponseWriter, r *http.Request) {
12521260
}()
12531261
}
12541262

1255-
needMountSearch := false
1256-
mountIncluded := additiveTypesExceptions.Has("mount")
1257-
notExcluded = !removedTypesExceptions.Has("mount")
1258-
noIndexGiven = parsedIndices.Size() == 0 && notExcluded
1259-
if noIndexGiven {
1260-
needMountSearch = true
1261-
} else if parsedIndices.Size() != 0 && !parsedIndices.Has("mounts") { // index given but no set
1262-
needMountSearch = false
1263-
} else {
1264-
needMountSearch = mountIncluded && notExcluded
1265-
}
1266-
1267-
if needMountSearch {
1268-
mountIndexUid := fmt.Sprintf("%s-mounts-%s", utils.CurrentRedBlueVersionStr(database.Version.Search), lang)
1269-
mountIndex := client.Index(mountIndexUid)
1270-
mountRetChan := make(chan []ApiAllSearchResultScore)
1271-
searchChans = append(searchChans, mountRetChan)
1272-
go func() {
1273-
request := &meilisearch.SearchRequest{
1274-
Limit: searchLimit * 3,
1275-
ShowRankingScoreDetails: true,
1276-
}
1277-
1278-
var searchResp *meilisearch.SearchResponse
1279-
if searchResp, err = mountIndex.Search(query, request); err != nil {
1280-
e.WriteServerErrorResponse(w, "Failed to search for query: "+err.Error())
1281-
mountRetChan <- nil
1282-
return
1283-
}
1284-
1285-
mounts := make([]ApiAllSearchResultScore, 0)
1286-
for _, hitRaw := range searchResp.Hits {
1287-
indexed := Hit{}
1288-
err = hitRaw.DecodeInto(&indexed)
1289-
if err != nil {
1290-
e.WriteServerErrorResponse(w, "Could not decode hit: "+err.Error())
1291-
return
1292-
}
1293-
1294-
score := indexed.ScoreDetails.Words.Score*wordScoreWeight + indexed.ScoreDetails.Typo.Score*typoScoreWeight
1295-
1296-
mountId := int(indexed.Id)
1297-
1298-
txn := database.Db.Txn(false)
1299-
raw, err := txn.First(fmt.Sprintf("%s-%s", utils.CurrentRedBlueVersionStr(database.Version.MemDb), "mounts"), "id", mountId)
1300-
if err != nil {
1301-
e.WriteServerErrorResponse(w, "Could not read database: "+err.Error())
1302-
mountRetChan <- nil
1303-
return
1304-
}
1305-
1306-
if raw == nil {
1307-
e.WriteServerErrorResponse(w, fmt.Sprintf("Could not find %s with ID %s in database", "mount", strconv.Itoa(mountId)))
1308-
mountRetChan <- nil
1309-
return
1310-
}
1311-
1312-
item := raw.(*mapping.MappedMultilangMount)
1313-
1314-
result := ApiAllSearchResult{
1315-
Name: item.Name[lang],
1316-
Id: item.AnkamaId,
1317-
Type: ApiAllSearchResultType{
1318-
NameId: "mounts",
1319-
},
1320-
ItemFields: nil,
1321-
}
1322-
1323-
mounts = append(mounts, ApiAllSearchResultScore{
1324-
Result: result,
1325-
Score: score,
1326-
})
1327-
}
1328-
1329-
mountRetChan <- mounts
1330-
}()
1331-
}
1332-
13331263
// wait for all search results and merge them into one slice
13341264
var merged []ApiAllSearchResultScore
13351265

@@ -1580,21 +1510,25 @@ func GetSingleMountHandler(w http.ResponseWriter, r *http.Request) {
15801510
txn := database.Db.Txn(false)
15811511
defer txn.Abort()
15821512

1583-
raw, err := txn.First(fmt.Sprintf("%s-%s", utils.CurrentRedBlueVersionStr(database.Version.MemDb), "mounts"), "id", ankamaId)
1513+
raw, err := txn.First(fmt.Sprintf("%s-%s", utils.CurrentRedBlueVersionStr(database.Version.MemDb), "equipment"), "id", ankamaId)
15841514
if err != nil {
15851515
e.WriteServerErrorResponse(w, "Could not read database: "+err.Error())
15861516
return
15871517
}
1588-
15891518
if raw == nil {
15901519
e.WriteNotFoundResponse(w, fmt.Sprintf("Could not find %s with ID %s in database", "mount", strconv.Itoa(ankamaId)))
15911520
return
15921521
}
1522+
item := raw.(*mapping.MappedMultilangItemUnity)
1523+
if !mountEquipmentTypeIds[item.Type.ItemTypeId] {
1524+
e.WriteNotFoundResponse(w, fmt.Sprintf("Could not find %s with ID %s in database", "mount", strconv.Itoa(ankamaId)))
1525+
return
1526+
}
15931527

15941528
utils.RequestsTotal.Inc()
15951529
utils.RequestsMountsSingle.Inc()
15961530

1597-
mount := RenderMount(raw.(*mapping.MappedMultilangMount), lang)
1531+
mount := RenderEquipmentAsMount(item, lang)
15981532
utils.WriteCacheHeader(&w)
15991533
err = json.NewEncoder(w).Encode(mount)
16001534
if err != nil {

indexing.go

Lines changed: 11 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ func IndexApiData(version *database.VersionT) (*memdb.MemDB, map[string]database
7171
var items []mapping.MappedMultilangItemUnity
7272
var sets []mapping.MappedMultilangSetUnity
7373
var recipes []mapping.MappedMultilangRecipe
74-
var mounts []mapping.MappedMultilangMount
7574

7675
// --
7776
itemsResponse, err := http.Get(config.ReleaseUrl + "/MAPPED_ITEMS.json")
@@ -121,24 +120,9 @@ func IndexApiData(version *database.VersionT) (*memdb.MemDB, map[string]database
121120
log.Fatal(err)
122121
}
123122

124-
// --
125-
mountsResponse, err := http.Get(config.ReleaseUrl + "/MAPPED_MOUNTS.json")
126-
if err != nil {
127-
log.Fatal(err)
128-
}
129-
130-
mountsBody, err := io.ReadAll(mountsResponse.Body)
131-
if err != nil {
132-
log.Fatal(err)
133-
}
134-
135-
err = json.Unmarshal(mountsBody, &mounts)
136-
if err != nil {
137-
log.Fatal(err)
138-
}
139-
log.Debug("loaded", "mounts", len(mounts), "items", len(items), "sets", len(sets), "recipes", len(recipes))
123+
log.Debug("loaded", "items", len(items), "sets", len(sets), "recipes", len(recipes))
140124

141-
db, indexes := GenerateDatabase(&items, &sets, &recipes, &mounts, version)
125+
db, indexes := GenerateDatabase(&items, &sets, &recipes, version)
142126

143127
return db, indexes
144128
}
@@ -375,7 +359,7 @@ type AlmanaxBonusListingMeili struct {
375359
Name string `json:"name"` // translated text
376360
}
377361

378-
func GenerateDatabase(items *[]mapping.MappedMultilangItemUnity, sets *[]mapping.MappedMultilangSetUnity, recipes *[]mapping.MappedMultilangRecipe, mounts *[]mapping.MappedMultilangMount, version *database.VersionT) (*memdb.MemDB, map[string]database.SearchIndexes) {
362+
func GenerateDatabase(items *[]mapping.MappedMultilangItemUnity, sets *[]mapping.MappedMultilangSetUnity, recipes *[]mapping.MappedMultilangRecipe, version *database.VersionT) (*memdb.MemDB, map[string]database.SearchIndexes) {
379363
/*
380364
item_category_mapping := hashbidimap.New()
381365
item_category_Put(0, 862817) // Ausrüstung
@@ -512,7 +496,6 @@ func GenerateDatabase(items *[]mapping.MappedMultilangItemUnity, sets *[]mapping
512496
itemIndexBatch := make(map[string][]SearchIndexedItem)
513497
itemsTable := fmt.Sprintf("%s-all_items", utils.NextRedBlueVersionStr(version.MemDb))
514498
setsTable := fmt.Sprintf("%s-sets", utils.NextRedBlueVersionStr(version.MemDb))
515-
mountsTable := fmt.Sprintf("%s-mounts", utils.NextRedBlueVersionStr(version.MemDb))
516499
recipesTable := fmt.Sprintf("%s-recipes", utils.NextRedBlueVersionStr(version.MemDb))
517500

518501
for _, recipe := range *recipes {
@@ -639,27 +622,24 @@ func GenerateDatabase(items *[]mapping.MappedMultilangItemUnity, sets *[]mapping
639622
}
640623
}
641624

642-
// mounts
643625
mountIndexBatch := make(map[string][]SearchIndexedMount)
644-
for _, mount := range *mounts {
645-
mountCp := mount
646-
if err := txn.Insert(mountsTable, &mountCp); err != nil {
647-
log.Fatal(err)
626+
for _, item := range *items {
627+
if !mountEquipmentTypeIds[item.Type.ItemTypeId] {
628+
continue
648629
}
649-
630+
itemCp := item
650631
for _, lang := range config.Languages {
651632
object := SearchIndexedMount{
652-
Name: mountCp.Name[lang],
653-
Id: mountCp.AnkamaId,
633+
Name: itemCp.Name[lang],
634+
Id: itemCp.AnkamaId,
654635
Family: ApiType{
655-
Name: strings.ToLower(mountCp.FamilyName[lang]),
656-
Id: mountCp.FamilyId,
636+
Name: strings.ToLower(itemCp.Type.Name[lang]),
637+
Id: itemCp.Type.ItemTypeId,
657638
},
658639
StuffType: SearchStuffType{
659640
NameId: "mounts",
660641
},
661642
}
662-
663643
mountIndexBatch[lang] = append(mountIndexBatch[lang], object)
664644
if len(mountIndexBatch[lang]) >= maxBatchSize {
665645
taskInfo, err := multilangSearchIndexes[lang].Mounts.AddDocuments(mountIndexBatch[lang], nil)

main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232

3333
var (
3434
DoduapiMajor = 1 // Major version also used for prefixing API routes.
35-
DoduapiVersion = fmt.Sprintf("v%d.0.4", DoduapiMajor) // change with every release
35+
DoduapiVersion = fmt.Sprintf("v%d.0.5", DoduapiMajor) // change with every release
3636
DoduapiShort = "doduapi - Open Dofus Encyclopedia API"
3737
DoduapiLong = ""
3838
DoduapiVersionHelp = DoduapiShort + "\n" + DoduapiVersion + "\nhttps://github.com/dofusdude/doduapi"

types.go

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -425,18 +425,6 @@ func RenderTypedItemListEntry(item *mapping.MappedMultilangItemUnity, lang strin
425425
}
426426
}
427427

428-
func RenderMountListEntry(mount *mapping.MappedMultilangMount, lang string) APIMount {
429-
return APIMount{
430-
Id: mount.AnkamaId,
431-
Name: mount.Name[lang],
432-
ImageUrls: RenderImageUrls(utils.ImageUrls(mount.AnkamaId, "mount", config.MountImgResolutions, config.ApiScheme, config.MajorVersion, config.ApiHostName, config.IsBeta)),
433-
Family: APIMountFamily{
434-
Id: mount.FamilyId,
435-
Name: mount.FamilyName[lang],
436-
},
437-
}
438-
}
439-
440428
type APIRecipe struct {
441429
AnkamaId int `json:"item_ankama_id"`
442430
ItemType string `json:"item_subtype"`
@@ -497,24 +485,24 @@ type APIMount struct {
497485
Effects []ApiEffect `json:"effects,omitempty"`
498486
}
499487

500-
func RenderMount(mount *mapping.MappedMultilangMount, lang string) APIMount {
501-
resMount := APIMount{
502-
Id: mount.AnkamaId,
503-
Name: mount.Name[lang],
488+
func RenderEquipmentAsMountListEntry(item *mapping.MappedMultilangItemUnity, lang string) APIMount {
489+
return APIMount{
490+
Id: item.AnkamaId,
491+
Name: item.Name[lang],
504492
Family: APIMountFamily{
505-
Id: mount.FamilyId,
506-
Name: mount.FamilyName[lang],
493+
Id: item.Type.ItemTypeId,
494+
Name: item.Type.Name[lang],
507495
},
508-
ImageUrls: RenderImageUrls(utils.ImageUrls(mount.AnkamaId, "mount", config.MountImgResolutions, config.ApiScheme, config.MajorVersion, config.ApiHostName, config.IsBeta)),
496+
ImageUrls: RenderImageUrls(utils.ImageUrls(item.IconId, "item", config.ItemImgResolutions, config.ApiScheme, config.MajorVersion, config.ApiHostName, config.IsBeta)),
509497
}
498+
}
510499

511-
effects := RenderEffects(&mount.Effects, lang)
512-
if len(effects) == 0 {
513-
resMount.Effects = nil
514-
} else {
500+
func RenderEquipmentAsMount(item *mapping.MappedMultilangItemUnity, lang string) APIMount {
501+
resMount := RenderEquipmentAsMountListEntry(item, lang)
502+
effects := RenderEffects(&item.Effects, lang)
503+
if len(effects) != 0 {
515504
resMount.Effects = effects
516505
}
517-
518506
return resMount
519507
}
520508

0 commit comments

Comments
 (0)