MCPcopy Create free account
hub / github.com/GoMudEngine/GoMud / TryItemCommand

Function TryItemCommand

internal/scripting/item.go:76–136  ·  view source on GitHub ↗
(cmd string, item items.Item, userId int)

Source from the content-addressed store, hash-verified

74}
75
76func TryItemCommand(cmd string, item items.Item, userId int) (bool, error) {
77
78 sItem := GetItem(item)
79
80 timestart := time.Now()
81 defer func() {
82 mudlog.Debug("TryItemCommand()", "cmd", cmd, "itemId", item.ItemId, "userId", userId, "time", time.Since(timestart))
83 }()
84
85 vmw, err := getItemVM(sItem)
86 if err != nil {
87 return false, err
88 }
89
90 if onCommandFunc, ok := vmw.GetFunction(`onCommand_` + cmd); ok {
91
92 sUser := GetActor(userId, 0)
93 sRoom := GetRoom(sUser.GetRoomId())
94
95 res, err := runCallable(vmw, scriptItemTimeout, onCommandFunc,
96 vmw.VM.ToValue(sUser),
97 vmw.VM.ToValue(sItem),
98 vmw.VM.ToValue(sRoom),
99 )
100 if err != nil {
101 return false, fmt.Errorf("onCommand_%s(): %w", cmd, err)
102 }
103
104 // Save any changes that might have happened to the item
105 sUser.characterRecord.UpdateItem(item, *sItem.itemRecord)
106
107 if boolVal, ok := res.Export().(bool); ok {
108 return boolVal, nil
109 }
110
111 } else if onCommandFunc, ok := vmw.GetFunction(`onCommand`); ok {
112
113 sUser := GetActor(userId, 0)
114 sRoom := GetRoom(sUser.GetRoomId())
115
116 res, err := runCallable(vmw, scriptItemTimeout, onCommandFunc,
117 vmw.VM.ToValue(cmd),
118 vmw.VM.ToValue(sUser),
119 vmw.VM.ToValue(sItem),
120 vmw.VM.ToValue(sRoom),
121 )
122 if err != nil {
123 return false, fmt.Errorf("onCommand(): %w", err)
124 }
125
126 // Save any changes that might have happened to the item
127 sUser.characterRecord.UpdateItem(item, *sItem.itemRecord)
128
129 if boolVal, ok := res.Export().(bool); ok {
130 return boolVal, nil
131 }
132
133 }

Callers 1

TryCommandFunction · 0.92

Calls 9

DebugFunction · 0.92
GetItemFunction · 0.85
getItemVMFunction · 0.85
GetActorFunction · 0.85
GetRoomFunction · 0.85
runCallableFunction · 0.85
GetFunctionMethod · 0.80
GetRoomIdMethod · 0.45
UpdateItemMethod · 0.45

Tested by

no test coverage detected