55 "strings"
66 "unicode"
77 "unicode/utf8"
8+
9+ "github.com/projectbarks/cimap"
810)
911
1012const (
2426 guildNameRegex = regexp .MustCompile (`[^\sa-zA-Z]` )
2527
2628 // validVocations stores all valid tibia vocations
27- validVocations = []string {"none" , "knight" , "knights" , "paladin" , "paladins" , "sorcerer" , "sorcerers" , "druid" , "druids" , "monk" , "monks" , "all" }
29+ validVocations = func () * cimap.CaseInsensitiveMap [bool ] {
30+ m := cimap.New [bool ](12 )
31+ for _ , v := range []string {"none" , "knight" , "knights" , "paladin" , "paladins" , "sorcerer" , "sorcerers" , "druid" , "druids" , "monk" , "monks" , "all" } {
32+ m .Add (v , true )
33+ }
34+ return m
35+ }()
2836)
2937
3038// IsRestrictionMode reports whether the restriction mode is enabled
@@ -51,10 +59,8 @@ func IsNewsIDValid(ID int) error {
5159// IsVocationValid reports wheter the provided string represents a valid vocation
5260// Check if error == nil to see whether the vocation is valid or not
5361func IsVocationValid (vocation string ) error {
54- for _ , voc := range validVocations {
55- if strings .EqualFold (vocation , voc ) {
56- return nil
57- }
62+ if _ , ok := validVocations .Get (vocation ); ok {
63+ return nil
5864 }
5965
6066 return ErrorVocationDoesNotExist
@@ -196,25 +202,12 @@ func IsCreatureNameValid(name string) (string, error) {
196202 return "" , ErrorCreatureNameInvalid
197203 }
198204
199- var (
200- found bool
201- endpoint string
202- )
203-
204205 // Check if creature exists
205- for _ , creature := range val .Creatures {
206- if strings .EqualFold (name , creature .Endpoint ) || strings .EqualFold (name , creature .Name ) || strings .EqualFold (name , creature .PluralName ) {
207- found = true
208- endpoint = creature .Endpoint
209- break
210- }
206+ if endpoint , ok := creatureLookup .Get (name ); ok {
207+ return endpoint , nil
211208 }
212209
213- if ! found {
214- return "" , ErrorCreatureNotFound
215- }
216-
217- return endpoint , nil
210+ return "" , ErrorCreatureNotFound
218211}
219212
220213// IsSpellNameOrFormulaValid reports wheter the provided string represents a valid spell name or formula
@@ -263,25 +256,12 @@ func IsSpellNameOrFormulaValid(name string) (string, error) {
263256 return "" , ErrorSpellNameInvalid
264257 }
265258
266- var (
267- found bool
268- endpoint string
269- )
270-
271259 // Check if spell exists
272- for _ , spell := range val .Spells {
273- if strings .EqualFold (name , spell .Endpoint ) || strings .EqualFold (name , spell .Name ) || strings .EqualFold (name , spell .Formula ) {
274- found = true
275- endpoint = spell .Endpoint
276- break
277- }
260+ if endpoint , ok := spellLookup .Get (name ); ok {
261+ return endpoint , nil
278262 }
279263
280- if ! found {
281- return "" , ErrorSpellNotFound
282- }
283-
284- return endpoint , nil
264+ return "" , ErrorSpellNotFound
285265}
286266
287267// GetWorlds returns a list of all existing worlds
@@ -303,13 +283,8 @@ func WorldExists(world string) (bool, error) {
303283 }
304284
305285 // Try to find the world
306- for _ , w := range val .Worlds {
307- if strings .EqualFold (w , world ) {
308- return true , nil
309- }
310- }
311-
312- return false , nil
286+ _ , found := worldLookup .Get (world )
287+ return found , nil
313288}
314289
315290// GetTowns returns a list of all existing towns
@@ -334,13 +309,8 @@ func TownExists(town string) (bool, error) {
334309 town = strings .ReplaceAll (town , "+" , " " )
335310
336311 // Try to find the town
337- for _ , t := range val .Towns {
338- if strings .EqualFold (t , town ) {
339- return true , nil
340- }
341- }
342-
343- return false , nil
312+ _ , found := townLookup .Get (town )
313+ return found , nil
344314}
345315
346316// GetHouses returns a slice of all houses
0 commit comments