2
2
3
3
]]
4
4
5
+ local Players = game :GetService (" Players" )
6
+
5
7
local Console = require (script .Parent .Parent .Packages .Console )
6
- local Loader = require (script .Parent .Parent .Packages .Loader )
8
+ local Runtime = require (script .Parent .Parent .Packages .Runtime )
7
9
local State = require (script .Parent .Parent .Packages .State )
8
10
local Promise = require (script .Parent .Parent .Packages .Promise )
9
11
12
+ local Api = require (script .Parent .Parent .Enums .Api )
13
+ local ActionType = require (script .Parent .Parent .Enums .ActionType )
14
+ local Error = require (script .Parent .Parent .Enums .Error )
15
+
16
+ local ErrorFormats = require (script .Parent .Parent .Data .ErrorFormats )
17
+
10
18
local Action = require (script .Parent .Parent .API .Action )
11
19
12
- local ActionService = { }
20
+ local ApiService = require (script .Parent .ApiService )
21
+
22
+ local ActionService = {}
13
23
14
24
ActionService .Priority = - 1
15
25
ActionService .Reporter = Console .new (` 🎬 {script.Name}` )
16
26
17
- ActionService .Actions = { } :: { [string ]: Action .Action }
27
+ ActionService .Actions = {} :: { [string ]: Action .Action }
18
28
ActionService .InternalActionsLoaded = State .new (false )
19
29
30
+ function ActionService .DeserialiseArgumentArray (
31
+ self : ActionService ,
32
+ eventArguments : { [number] : { type : string , name : string , value : any } }
33
+ )
34
+ local deserialisedArguments = {}
35
+
36
+ for _ , argumentData in eventArguments do
37
+ local value
38
+
39
+ if argumentData .type == string.upper (ActionType .Number ) then
40
+ value = tonumber (argumentData .value )
41
+ elseif argumentData .type == string.upper (ActionType .String ) then
42
+ value = tostring (argumentData .value )
43
+ elseif argumentData .type == string.upper (ActionType .Boolean ) then
44
+ value = argumentData .value == " true" and true or false
45
+ elseif argumentData .type == string.upper (ActionType .Player ) then
46
+ local playerId = argumentData .value
47
+ local player = Players :GetPlayerByUserId (playerId )
48
+
49
+ value = player
50
+ end
51
+
52
+ table.insert (deserialisedArguments , value )
53
+ end
54
+
55
+ return deserialisedArguments
56
+ end
57
+
20
58
function ActionService .InvokeActionAsync (self : ActionService , actionUuid : string , eventArguments : { [any] : any } )
21
59
local actionObject = Action .fromUuid (actionUuid )
22
60
23
61
return Promise .try (function ()
24
- return actionObject :OnRemoteServerInputRecieved (eventArguments )
62
+ return actionObject :OnRemoteServerInputRecieved (self : DeserialiseArgumentArray ( eventArguments ) )
25
63
end )
26
64
end
27
65
28
66
function ActionService .OnStart (self : ActionService )
29
- local Actions = Loader . LoadDescendants (script .Parent .Parent .Actions )
67
+ local Actions = Runtime : RequireDescendants (script .Parent .Parent .Actions )
30
68
31
69
for actionModuleName , actionConstructorFunction in Actions do
32
70
local actionObject = actionConstructorFunction ()
@@ -38,6 +76,38 @@ function ActionService.OnStart(self: ActionService)
38
76
self .InternalActionsLoaded :Set (true )
39
77
end
40
78
79
+ function ActionService .OnInit (self : ActionService )
80
+ Action .ActionAdded :Connect (function (actionObject : Action.Action )
81
+ local camelCaseActionArguments = {}
82
+
83
+ if actionObject .Arguments then
84
+ for index , actionMetadata in next , actionObject .Arguments do
85
+ self .Reporter :Assert (
86
+ ActionType [actionMetadata .Type ] ~= nil ,
87
+ string.format (ErrorFormats [Error .InvalidActionArgumentType ], actionMetadata .Type , actionObject .Name )
88
+ )
89
+
90
+ camelCaseActionArguments [index ] = {
91
+ [" default" ] = actionMetadata .Default ,
92
+ [" required" ] = actionMetadata .IsRequired ,
93
+ [" name" ] = actionMetadata .Name ,
94
+ [" type" ] = string.upper (actionMetadata .Type ),
95
+ }
96
+ end
97
+ end
98
+
99
+ ApiService :PostAsync (Api .RegisterAction , {
100
+ [" serverId" ] = ApiService .JobId ,
101
+ [" placeVersion" ] = tostring (game .PlaceVersion ),
102
+ [" key" ] = actionObject .Uuid ,
103
+ [" name" ] = actionObject .Name ,
104
+ [" arguments" ] = camelCaseActionArguments ,
105
+ }):andThen (function ()
106
+ self .Reporter :Log (` Registered action '{actionObject.Name}' with metrik backend` )
107
+ end )
108
+ end )
109
+ end
110
+
41
111
export type ActionService = typeof (ActionService )
42
112
43
- return ActionService
113
+ return ActionService
0 commit comments