blob: c9b271730380ec7d2bc6bc609fdf6dc74b25f1b6 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
package object
import "strings"
type EquipSlot string
const (
SlotHead EquipSlot = "head"
SlotNeck EquipSlot = "neck"
SlotTorso EquipSlot = "torso"
SlotLegs EquipSlot = "legs"
SlotHands EquipSlot = "hands"
SlotFeet EquipSlot = "feet"
SlotBack EquipSlot = "back"
SlotAmmo EquipSlot = "ammo"
SlotMainHand EquipSlot = "main_hand"
SlotOffHand EquipSlot = "off_hand"
SlotRing EquipSlot = "ring"
)
type WeaponType string
const (
WeaponMelee WeaponType = "melee"
WeaponRanged WeaponType = "ranged"
WeaponTechnology WeaponType = "technology"
)
type ItemDef struct {
ID string `yaml:"id"`
Name string `yaml:"name"`
Aliases []string `yaml:"aliases"`
Description string `yaml:"description"`
Value int `yaml:"value"`
Stackable bool `yaml:"stackable"`
EquipSlot EquipSlot `yaml:"equip_slot"`
WeaponType WeaponType `yaml:"weapon_type"`
Stats ItemStats `yaml:"stats"`
Speed int `yaml:"speed"`
ToolType string `yaml:"tool_type"`
ToolSpeed int `yaml:"tool_speed"`
}
type ItemStats struct {
AttackBonus int `yaml:"attack_bonus"`
StrengthBonus int `yaml:"strength_bonus"`
DefenseBonus int `yaml:"defense_bonus"`
ScienceBonus int `yaml:"science_bonus"`
TechnologyBonus int `yaml:"technology_bonus"`
}
func (d *ItemDef) MatchesName(input string) bool {
lower := strings.ToLower(strings.TrimSpace(input))
if lower == "" {
return false
}
if strings.ToLower(d.Name) == lower {
return true
}
for _, alias := range d.Aliases {
if strings.ToLower(alias) == lower {
return true
}
}
if wordPrefixMatch(lower, d.Name) {
return true
}
for _, alias := range d.Aliases {
if wordPrefixMatch(lower, alias) {
return true
}
}
return false
}
func wordPrefixMatch(input, name string) bool {
inputWords := strings.Fields(input)
if len(inputWords) == 0 {
return false
}
nameWords := strings.Fields(strings.ToLower(name))
for _, iw := range inputWords {
found := false
for _, nw := range nameWords {
if strings.HasPrefix(nw, iw) || strings.HasPrefix(iw, nw) {
found = true
break
}
}
if !found {
return false
}
}
return true
}
|