Skip to content

Commit cec2905

Browse files
committed
fix mounts
1 parent ba3ba7e commit cec2905

4 files changed

Lines changed: 52 additions & 122 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: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,6 @@ func GenerateDatabase(items *[]mapping.MappedMultilangItemUnity, sets *[]mapping
512512
itemIndexBatch := make(map[string][]SearchIndexedItem)
513513
itemsTable := fmt.Sprintf("%s-all_items", utils.NextRedBlueVersionStr(version.MemDb))
514514
setsTable := fmt.Sprintf("%s-sets", utils.NextRedBlueVersionStr(version.MemDb))
515-
mountsTable := fmt.Sprintf("%s-mounts", utils.NextRedBlueVersionStr(version.MemDb))
516515
recipesTable := fmt.Sprintf("%s-recipes", utils.NextRedBlueVersionStr(version.MemDb))
517516

518517
for _, recipe := range *recipes {
@@ -639,27 +638,24 @@ func GenerateDatabase(items *[]mapping.MappedMultilangItemUnity, sets *[]mapping
639638
}
640639
}
641640

642-
// mounts
643641
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)
642+
for _, item := range *items {
643+
if !mountEquipmentTypeIds[item.Type.ItemTypeId] {
644+
continue
648645
}
649-
646+
itemCp := item
650647
for _, lang := range config.Languages {
651648
object := SearchIndexedMount{
652-
Name: mountCp.Name[lang],
653-
Id: mountCp.AnkamaId,
649+
Name: itemCp.Name[lang],
650+
Id: itemCp.AnkamaId,
654651
Family: ApiType{
655-
Name: strings.ToLower(mountCp.FamilyName[lang]),
656-
Id: mountCp.FamilyId,
652+
Name: strings.ToLower(itemCp.Type.Name[lang]),
653+
Id: itemCp.Type.ItemTypeId,
657654
},
658655
StuffType: SearchStuffType{
659656
NameId: "mounts",
660657
},
661658
}
662-
663659
mountIndexBatch[lang] = append(mountIndexBatch[lang], object)
664660
if len(mountIndexBatch[lang]) >= maxBatchSize {
665661
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 & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -497,24 +497,24 @@ type APIMount struct {
497497
Effects []ApiEffect `json:"effects,omitempty"`
498498
}
499499

500-
func RenderMount(mount *mapping.MappedMultilangMount, lang string) APIMount {
501-
resMount := APIMount{
502-
Id: mount.AnkamaId,
503-
Name: mount.Name[lang],
500+
func RenderEquipmentAsMountListEntry(item *mapping.MappedMultilangItemUnity, lang string) APIMount {
501+
return APIMount{
502+
Id: item.AnkamaId,
503+
Name: item.Name[lang],
504504
Family: APIMountFamily{
505-
Id: mount.FamilyId,
506-
Name: mount.FamilyName[lang],
505+
Id: item.Type.ItemTypeId,
506+
Name: item.Type.Name[lang],
507507
},
508-
ImageUrls: RenderImageUrls(utils.ImageUrls(mount.AnkamaId, "mount", config.MountImgResolutions, config.ApiScheme, config.MajorVersion, config.ApiHostName, config.IsBeta)),
508+
ImageUrls: RenderImageUrls(utils.ImageUrls(item.IconId, "item", config.ItemImgResolutions, config.ApiScheme, config.MajorVersion, config.ApiHostName, config.IsBeta)),
509509
}
510+
}
510511

511-
effects := RenderEffects(&mount.Effects, lang)
512-
if len(effects) == 0 {
513-
resMount.Effects = nil
514-
} else {
512+
func RenderEquipmentAsMount(item *mapping.MappedMultilangItemUnity, lang string) APIMount {
513+
resMount := RenderEquipmentAsMountListEntry(item, lang)
514+
effects := RenderEffects(&item.Effects, lang)
515+
if len(effects) != 0 {
515516
resMount.Effects = effects
516517
}
517-
518518
return resMount
519519
}
520520

0 commit comments

Comments
 (0)