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

Function TryItemScriptEvent

internal/scripting/item.go:34–74  ·  view source on GitHub ↗
(eventName string, item items.Item, userId int)

Source from the content-addressed store, hash-verified

32}
33
34func TryItemScriptEvent(eventName string, item items.Item, userId int) (bool, error) {
35
36 sItem := GetItem(item)
37
38 timestart := time.Now()
39 defer func() {
40 mudlog.Debug("TryItemScriptEvent()", "eventName", eventName, "item", item, "time", time.Since(timestart))
41 }()
42
43 vmw, err := getItemVM(sItem)
44 if err != nil {
45 return false, err
46 }
47
48 if onCommandFunc, ok := vmw.GetFunction(eventName); ok {
49
50 sUser := GetActor(userId, 0)
51 sRoom := GetRoom(sUser.GetRoomId())
52
53 res, err := runCallable(vmw, scriptItemTimeout, onCommandFunc,
54 vmw.VM.ToValue(sUser),
55 vmw.VM.ToValue(sItem),
56 vmw.VM.ToValue(sRoom),
57 )
58 if err != nil {
59 return false, fmt.Errorf("%s(): %w", eventName, err)
60 }
61
62 if eventName != `onLost` {
63 // Save any changes that might have happened to the item
64 sUser.characterRecord.UpdateItem(item, *sItem.itemRecord)
65 }
66
67 if boolVal, ok := res.Export().(bool); ok {
68 return boolVal, nil
69 }
70
71 }
72
73 return false, ErrEventNotFound
74}
75
76func TryItemCommand(cmd string, item items.Item, userId int) (bool, error) {
77

Callers 2

CheckItemQuestsFunction · 0.92
tryPurchaseFunction · 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