Refactor the SAYT code to return better results
This commit is contained in:
@@ -76,21 +76,6 @@ module.exports = class ModPack extends BaseModel
|
||||
result.itemUrl = Url.item result
|
||||
return result
|
||||
|
||||
gatherNames: (options={})->
|
||||
options.includeGatherable ?= false
|
||||
options.includeDisabled ?= false
|
||||
|
||||
nameData = {}
|
||||
for modVersion in @modVersions
|
||||
continue unless modVersion.enabled or options.includeDisabled
|
||||
modVersion.gatherNames nameData, options
|
||||
|
||||
result = []
|
||||
names = _.keys(nameData).sort()
|
||||
for name in names
|
||||
result.push nameData[name]
|
||||
return result
|
||||
|
||||
hasRecipe: (name, options={})->
|
||||
options.includeDisabled ?= false
|
||||
|
||||
|
||||
@@ -55,22 +55,6 @@ module.exports = class ModVersion extends BaseModel
|
||||
findName: (slug)->
|
||||
return @names[slug]
|
||||
|
||||
gatherNames: (result={}, options={})->
|
||||
options.includeGatherable ?= false
|
||||
|
||||
for slug, item of @items
|
||||
continue if result[item.slug]
|
||||
if not item.isCraftable
|
||||
continue unless options.includeGatherable
|
||||
result[item.slug] = value:item.name, label:"#{item.name} (from #{@name} #{@version})"
|
||||
|
||||
if options.includeGatherable
|
||||
for slug, name of @names
|
||||
continue if result[slug]
|
||||
result[slug] = value:name, label:"#{name} (from #{@name} #{@version})"
|
||||
|
||||
return result
|
||||
|
||||
hasRecipe: (name)->
|
||||
item = @findItemByName name
|
||||
return false unless item?
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
###
|
||||
Crafting Guide - name_finder.coffee
|
||||
|
||||
Copyright (c) 2015 by Redwood Labs
|
||||
All rights reserved.
|
||||
###
|
||||
|
||||
########################################################################################################################
|
||||
|
||||
module.exports = class NameFinder
|
||||
|
||||
constructor: (modPack, options={})->
|
||||
if not modPack? then throw new Error 'modPack is required'
|
||||
|
||||
options.includeGatherable ?= false
|
||||
options.includeDisabledMods ?= false
|
||||
options.limit ?= 25
|
||||
|
||||
@includeDisabledMods = options.includeDisabledMods
|
||||
@includeGatherable = options.includeGatherable
|
||||
@limit = options.limit
|
||||
@modPack = modPack
|
||||
@names = []
|
||||
@_nameMap = {}
|
||||
|
||||
# Public Methods ###############################################################################
|
||||
|
||||
search: (nameHint='')->
|
||||
nameHint = null if nameHint.trim() is ''
|
||||
nameHint = nameHint.toLowerCase() if nameHint?
|
||||
names = @_findNames nameHint
|
||||
|
||||
names.sort (a, b)->
|
||||
c = a.modVersion.compareTo b.modVersion
|
||||
if c isnt 0 then return c
|
||||
|
||||
return 0 if a.label is b.label
|
||||
return if a.label < b.label then -1 else +1
|
||||
|
||||
names = names[0...@limit]
|
||||
|
||||
return names
|
||||
|
||||
# Private Methods ##############################################################################
|
||||
|
||||
_isMatch: (name, hint)->
|
||||
nameWords = name.split ' '
|
||||
hintWords = hint.split ' '
|
||||
|
||||
for hintWord in hintWords
|
||||
while true
|
||||
return false if nameWords.length is 0
|
||||
nameWord = nameWords.shift()
|
||||
break if nameWord.indexOf(hintWord) is 0
|
||||
|
||||
return true
|
||||
|
||||
_findNames: (nameHint=null)->
|
||||
names = []
|
||||
nameMap = {}
|
||||
|
||||
for modVersion in @modPack.modVersions
|
||||
continue unless modVersion.enabled or @includeDisabledMods
|
||||
|
||||
for slug, name of modVersion.names
|
||||
continue if nameMap[name]
|
||||
|
||||
item = modVersion.items[slug]
|
||||
if not @includeGatherable
|
||||
continue unless item? and (not item.isGatherable)
|
||||
|
||||
scanName = "#{modVersion.name} : #{name}"
|
||||
if nameHint?
|
||||
continue unless @_isMatch scanName.toLowerCase(), nameHint
|
||||
|
||||
nameMap[name] = name
|
||||
names.push value:name, label:scanName, modVersion:modVersion
|
||||
|
||||
return names
|
||||
Reference in New Issue
Block a user