@quenty/scoredactionservice 16.53.0 → 16.53.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,348 @@
1
+ --!strict
2
+ --[[
3
+ @class ScoredActionPicker.spec.lua
4
+ ]]
5
+
6
+ local require = require(script.Parent.loader).load(script)
7
+
8
+ local Jest = require("Jest")
9
+ local Maid = require("Maid")
10
+ local ScoredAction = require("ScoredAction")
11
+ local ScoredActionPicker = require("ScoredActionPicker")
12
+
13
+ local describe = Jest.Globals.describe
14
+ local expect = Jest.Globals.expect
15
+ local it = Jest.Globals.it
16
+
17
+ local function setup(): any
18
+ local maid = Maid.new()
19
+
20
+ local function makeAction(score: number): ScoredAction.ScoredAction
21
+ local action: ScoredAction.ScoredAction = ScoredAction.new()
22
+ action:SetScore(score)
23
+ return action
24
+ end
25
+
26
+ local controller = {
27
+ newPicker = function()
28
+ return maid:Add(ScoredActionPicker.new()) :: any
29
+ end,
30
+
31
+ newLoosePicker = function()
32
+ return ScoredActionPicker.new()
33
+ end,
34
+
35
+ newAction = function(score: number)
36
+ return maid:Add(makeAction(score)) :: any
37
+ end,
38
+
39
+ newLooseAction = makeAction,
40
+
41
+ destroy = function()
42
+ maid:DoCleaning()
43
+ end,
44
+ }
45
+
46
+ return controller
47
+ end
48
+
49
+ describe("ScoredActionPicker.Update", function()
50
+ it("prefers the highest scoring action", function()
51
+ local controller = setup()
52
+ local picker = controller.newPicker()
53
+
54
+ local low = controller.newAction(1)
55
+ local high = controller.newAction(2)
56
+ picker:AddAction(low)
57
+ picker:AddAction(high)
58
+ picker:Update()
59
+
60
+ expect(high:IsPreferred()).toBe(true)
61
+ expect(low:IsPreferred()).toBe(false)
62
+
63
+ controller.destroy()
64
+ end)
65
+
66
+ it("never prefers an action scored at -math.huge", function()
67
+ local controller = setup()
68
+ local picker = controller.newPicker()
69
+
70
+ local action = controller.newAction(-math.huge)
71
+ picker:AddAction(action)
72
+ picker:Update()
73
+
74
+ expect(action:IsPreferred()).toBe(false)
75
+
76
+ controller.destroy()
77
+ end)
78
+
79
+ it("drops the preference when the only action is disabled", function()
80
+ local controller = setup()
81
+ local picker = controller.newPicker()
82
+
83
+ local action = controller.newAction(1)
84
+ picker:AddAction(action)
85
+ expect(action:IsPreferred()).toBe(true)
86
+
87
+ -- The action stays in the set while disabled, so nothing else can take the preference off it.
88
+ -- Left preferred, a control told to disable itself stays bound and keeps its hint on screen.
89
+ action:SetIsEnabled(false)
90
+ picker:Update()
91
+
92
+ expect(action:IsPreferred()).toBe(false)
93
+
94
+ controller.destroy()
95
+ end)
96
+
97
+ it("hands the preference to the best still-enabled action", function()
98
+ local controller = setup()
99
+ local picker = controller.newPicker()
100
+
101
+ local low = controller.newAction(1)
102
+ local high = controller.newAction(2)
103
+ picker:AddAction(low)
104
+ picker:AddAction(high)
105
+ picker:Update()
106
+
107
+ high:SetIsEnabled(false)
108
+ picker:Update()
109
+
110
+ expect(high:IsPreferred()).toBe(false)
111
+ expect(low:IsPreferred()).toBe(true)
112
+
113
+ controller.destroy()
114
+ end)
115
+
116
+ it("gives the preference back when the action is re-enabled", function()
117
+ local controller = setup()
118
+ local picker = controller.newPicker()
119
+
120
+ local action = controller.newAction(1)
121
+ picker:AddAction(action)
122
+
123
+ action:SetIsEnabled(false)
124
+ picker:Update()
125
+ expect(action:IsPreferred()).toBe(false)
126
+
127
+ action:SetIsEnabled(true)
128
+ picker:Update()
129
+ expect(action:IsPreferred()).toBe(true)
130
+
131
+ controller.destroy()
132
+ end)
133
+
134
+ it("drops the preference when the preferred action is removed", function()
135
+ local controller = setup()
136
+ local picker = controller.newPicker()
137
+
138
+ local action = controller.newAction(1)
139
+ picker:AddAction(action)
140
+ expect(action:IsPreferred()).toBe(true)
141
+
142
+ picker:RemoveAction(action)
143
+
144
+ expect(action:IsPreferred()).toBe(false)
145
+ expect(picker:HasActions()).toBe(false)
146
+
147
+ controller.destroy()
148
+ end)
149
+
150
+ it("breaks score ties in favor of the older action", function()
151
+ local controller = setup()
152
+ local picker = controller.newPicker()
153
+
154
+ local older = controller.newAction(1)
155
+ local newer = controller.newAction(1)
156
+ picker:AddAction(newer)
157
+ picker:AddAction(older)
158
+ picker:Update()
159
+
160
+ expect(older:IsPreferred()).toBe(true)
161
+ expect(newer:IsPreferred()).toBe(false)
162
+
163
+ controller.destroy()
164
+ end)
165
+
166
+ it("skips past unscored actions to reach a scored one", function()
167
+ local controller = setup()
168
+ local picker = controller.newPicker()
169
+
170
+ local unscored = controller.newAction(-math.huge)
171
+ local scored = controller.newAction(-1000)
172
+ picker:AddAction(unscored)
173
+ picker:AddAction(scored)
174
+ picker:Update()
175
+
176
+ expect(scored:IsPreferred()).toBe(true)
177
+ expect(unscored:IsPreferred()).toBe(false)
178
+
179
+ controller.destroy()
180
+ end)
181
+
182
+ it("moves the preference when a score change reorders the actions", function()
183
+ local controller = setup()
184
+ local picker = controller.newPicker()
185
+
186
+ local first = controller.newAction(2)
187
+ local second = controller.newAction(1)
188
+ picker:AddAction(first)
189
+ picker:AddAction(second)
190
+ picker:Update()
191
+ expect(first:IsPreferred()).toBe(true)
192
+
193
+ second:SetScore(3)
194
+ picker:Update()
195
+
196
+ expect(first:IsPreferred()).toBe(false)
197
+ expect(second:IsPreferred()).toBe(true)
198
+
199
+ controller.destroy()
200
+ end)
201
+
202
+ it("tolerates an action destroyed without being removed", function()
203
+ local controller = setup()
204
+ local picker = controller.newPicker()
205
+
206
+ local action = controller.newLooseAction(1)
207
+ picker:AddAction(action)
208
+ action:Destroy()
209
+
210
+ expect(function()
211
+ picker:Update()
212
+ end).never.toThrow()
213
+
214
+ controller.destroy()
215
+ end)
216
+ end)
217
+
218
+ describe("ScoredActionPicker.AddAction", function()
219
+ it("prefers the action without an explicit update", function()
220
+ local controller = setup()
221
+ local picker = controller.newPicker()
222
+
223
+ local action = controller.newAction(1)
224
+ picker:AddAction(action)
225
+
226
+ expect(action:IsPreferred()).toBe(true)
227
+ expect(picker:HasActions()).toBe(true)
228
+
229
+ controller.destroy()
230
+ end)
231
+
232
+ it("re-evaluates when an action's enabled state changes", function()
233
+ local controller = setup()
234
+ local picker = controller.newPicker()
235
+
236
+ local low = controller.newAction(1)
237
+ local high = controller.newAction(2)
238
+ picker:AddAction(low)
239
+ picker:AddAction(high)
240
+
241
+ high:SetIsEnabled(false)
242
+
243
+ expect(high:IsPreferred()).toBe(false)
244
+ expect(low:IsPreferred()).toBe(true)
245
+
246
+ controller.destroy()
247
+ end)
248
+
249
+ it("ignores a repeated add of the same action", function()
250
+ local controller = setup()
251
+ local picker = controller.newPicker()
252
+
253
+ local action = controller.newAction(1)
254
+ picker:AddAction(action)
255
+ picker:AddAction(action)
256
+ picker:RemoveAction(action)
257
+
258
+ expect(picker:HasActions()).toBe(false)
259
+
260
+ controller.destroy()
261
+ end)
262
+
263
+ it("throws on a non-table action", function()
264
+ local controller = setup()
265
+ local picker = controller.newPicker()
266
+
267
+ expect(function()
268
+ (picker :: any):AddAction("action")
269
+ end).toThrow()
270
+
271
+ controller.destroy()
272
+ end)
273
+ end)
274
+
275
+ describe("ScoredActionPicker.RemoveAction", function()
276
+ it("hands the preference to the next best action", function()
277
+ local controller = setup()
278
+ local picker = controller.newPicker()
279
+
280
+ local low = controller.newAction(1)
281
+ local high = controller.newAction(2)
282
+ picker:AddAction(low)
283
+ picker:AddAction(high)
284
+
285
+ picker:RemoveAction(high)
286
+
287
+ expect(low:IsPreferred()).toBe(true)
288
+
289
+ controller.destroy()
290
+ end)
291
+
292
+ it("stops tracking the enabled state of a removed action", function()
293
+ local controller = setup()
294
+ local picker = controller.newPicker()
295
+
296
+ local removed = controller.newAction(2)
297
+ local remaining = controller.newAction(1)
298
+ picker:AddAction(removed)
299
+ picker:AddAction(remaining)
300
+ picker:RemoveAction(removed)
301
+
302
+ removed:SetIsEnabled(false)
303
+
304
+ expect(remaining:IsPreferred()).toBe(true)
305
+
306
+ controller.destroy()
307
+ end)
308
+
309
+ it("ignores an action that was never added", function()
310
+ local controller = setup()
311
+ local picker = controller.newPicker()
312
+ local action = controller.newAction(1)
313
+
314
+ expect(function()
315
+ picker:RemoveAction(action)
316
+ end).never.toThrow()
317
+
318
+ controller.destroy()
319
+ end)
320
+
321
+ it("throws on a non-table action", function()
322
+ local controller = setup()
323
+ local picker = controller.newPicker()
324
+
325
+ expect(function()
326
+ (picker :: any):RemoveAction("action")
327
+ end).toThrow()
328
+
329
+ controller.destroy()
330
+ end)
331
+ end)
332
+
333
+ describe("ScoredActionPicker.Destroy", function()
334
+ it("drops the preference it was holding", function()
335
+ local controller = setup()
336
+ local picker = controller.newLoosePicker()
337
+
338
+ local action = controller.newAction(1)
339
+ picker:AddAction(action)
340
+ expect(action:IsPreferred()).toBe(true)
341
+
342
+ picker:Destroy()
343
+
344
+ expect(action:IsPreferred()).toBe(false)
345
+
346
+ controller.destroy()
347
+ end)
348
+ end)
@@ -0,0 +1,271 @@
1
+ --!strict
2
+ --[[
3
+ @class ScoredActionPickerProvider.spec.lua
4
+ ]]
5
+
6
+ local require = require(script.Parent.loader).load(script)
7
+
8
+ local Jest = require("Jest")
9
+ local Maid = require("Maid")
10
+ local ScoredAction = require("ScoredAction")
11
+ local ScoredActionPickerProvider = require("ScoredActionPickerProvider")
12
+ local SlottedTouchButtonUtils = require("SlottedTouchButtonUtils")
13
+
14
+ local describe = Jest.Globals.describe
15
+ local expect = Jest.Globals.expect
16
+ local it = Jest.Globals.it
17
+
18
+ type Controller = {
19
+ newProvider: () -> ScoredActionPickerProvider.ScoredActionPickerProvider,
20
+ newLooseProvider: () -> ScoredActionPickerProvider.ScoredActionPickerProvider,
21
+ newAction: (number) -> ScoredAction.ScoredAction,
22
+ slot: (string) -> any,
23
+ destroy: () -> (),
24
+ }
25
+
26
+ local function setup(): Controller
27
+ local maid = Maid.new()
28
+
29
+ local controller: Controller = {
30
+ newProvider = function()
31
+ return maid:Add(ScoredActionPickerProvider.new()) :: any
32
+ end,
33
+
34
+ newLooseProvider = function()
35
+ return ScoredActionPickerProvider.new()
36
+ end,
37
+
38
+ newAction = function(score: number)
39
+ local action: ScoredAction.ScoredAction = maid:Add(ScoredAction.new()) :: any
40
+ action:SetScore(score)
41
+ return action
42
+ end,
43
+
44
+ slot = function(slotId: string)
45
+ return SlottedTouchButtonUtils.createSlottedTouchButton(slotId)
46
+ end,
47
+
48
+ destroy = function()
49
+ maid:DoCleaning()
50
+ end,
51
+ }
52
+
53
+ return controller
54
+ end
55
+
56
+ describe("ScoredActionPickerProvider.GetOrCreatePicker", function()
57
+ it("reuses the picker for an input type", function()
58
+ local controller = setup()
59
+ local provider = controller.newProvider()
60
+
61
+ local first = provider:GetOrCreatePicker(Enum.KeyCode.E)
62
+ local second = provider:GetOrCreatePicker(Enum.KeyCode.E)
63
+
64
+ expect(second).toBe(first)
65
+
66
+ controller.destroy()
67
+ end)
68
+
69
+ it("creates a separate picker per input type", function()
70
+ local controller = setup()
71
+ local provider = controller.newProvider()
72
+
73
+ local keyboard = provider:GetOrCreatePicker(Enum.KeyCode.E)
74
+ local gamepad = provider:GetOrCreatePicker(Enum.KeyCode.ButtonX)
75
+
76
+ expect(gamepad).never.toBe(keyboard)
77
+
78
+ controller.destroy()
79
+ end)
80
+
81
+ it("creates a competing picker for a key code", function()
82
+ local controller = setup()
83
+ local provider = controller.newProvider()
84
+
85
+ local picker = provider:GetOrCreatePicker(Enum.KeyCode.E)
86
+
87
+ expect((picker :: any).ClassName).toBe("ScoredActionPicker")
88
+
89
+ controller.destroy()
90
+ end)
91
+
92
+ it("creates a non-competing picker for a Roblox touch button", function()
93
+ local controller = setup()
94
+ local provider = controller.newProvider()
95
+
96
+ local picker = provider:GetOrCreatePicker("TouchButton")
97
+
98
+ expect((picker :: any).ClassName).toBe("TouchButtonScoredActionPicker")
99
+
100
+ controller.destroy()
101
+ end)
102
+
103
+ it("reuses the picker for slotted touch buttons sharing a slot", function()
104
+ local controller = setup()
105
+ local provider = controller.newProvider()
106
+
107
+ local first = provider:GetOrCreatePicker(controller.slot("primary1"))
108
+ local second = provider:GetOrCreatePicker(controller.slot("primary1"))
109
+
110
+ expect(second).toBe(first)
111
+
112
+ controller.destroy()
113
+ end)
114
+
115
+ it("creates a separate picker per slot", function()
116
+ local controller = setup()
117
+ local provider = controller.newProvider()
118
+
119
+ local first = provider:GetOrCreatePicker(controller.slot("primary1"))
120
+ local second = provider:GetOrCreatePicker(controller.slot("primary2"))
121
+
122
+ expect(second).never.toBe(first)
123
+
124
+ controller.destroy()
125
+ end)
126
+
127
+ it("throws on a nil input type", function()
128
+ local controller = setup()
129
+ local provider = controller.newProvider()
130
+
131
+ expect(function()
132
+ (provider :: any):GetOrCreatePicker(nil)
133
+ end).toThrow()
134
+
135
+ controller.destroy()
136
+ end)
137
+ end)
138
+
139
+ describe("ScoredActionPickerProvider.FindPicker", function()
140
+ it("returns nil for an input type with no picker", function()
141
+ local controller = setup()
142
+ local provider = controller.newProvider()
143
+
144
+ expect(provider:FindPicker(Enum.KeyCode.E)).toBeNil()
145
+
146
+ controller.destroy()
147
+ end)
148
+
149
+ it("finds a created picker", function()
150
+ local controller = setup()
151
+ local provider = controller.newProvider()
152
+
153
+ local picker = provider:GetOrCreatePicker(Enum.KeyCode.E)
154
+
155
+ expect(provider:FindPicker(Enum.KeyCode.E)).toBe(picker)
156
+
157
+ controller.destroy()
158
+ end)
159
+
160
+ it("finds a slotted touch button picker by an equivalent slot", function()
161
+ local controller = setup()
162
+ local provider = controller.newProvider()
163
+
164
+ local picker = provider:GetOrCreatePicker(controller.slot("inner1"))
165
+
166
+ expect(provider:FindPicker(controller.slot("inner1"))).toBe(picker)
167
+
168
+ controller.destroy()
169
+ end)
170
+ end)
171
+
172
+ describe("ScoredActionPickerProvider.Update", function()
173
+ it("discards a picker that has no actions", function()
174
+ local controller = setup()
175
+ local provider = controller.newProvider()
176
+
177
+ provider:GetOrCreatePicker(Enum.KeyCode.E)
178
+ provider:Update()
179
+
180
+ expect(provider:FindPicker(Enum.KeyCode.E)).toBeNil()
181
+
182
+ controller.destroy()
183
+ end)
184
+
185
+ it("keeps a picker that still has actions", function()
186
+ local controller = setup()
187
+ local provider = controller.newProvider()
188
+
189
+ local picker: any = provider:GetOrCreatePicker(Enum.KeyCode.E)
190
+ picker:AddAction(controller.newAction(1))
191
+ provider:Update()
192
+
193
+ expect(provider:FindPicker(Enum.KeyCode.E)).toBe(picker)
194
+
195
+ controller.destroy()
196
+ end)
197
+
198
+ it("updates the pickers it keeps", function()
199
+ local controller = setup()
200
+ local provider = controller.newProvider()
201
+
202
+ local picker: any = provider:GetOrCreatePicker(Enum.KeyCode.E)
203
+ local action = controller.newAction(-math.huge)
204
+ picker:AddAction(action)
205
+
206
+ action:SetScore(1)
207
+ provider:Update()
208
+
209
+ expect(action:IsPreferred()).toBe(true)
210
+
211
+ controller.destroy()
212
+ end)
213
+
214
+ it("discards a picker once its last action is removed", function()
215
+ local controller = setup()
216
+ local provider = controller.newProvider()
217
+
218
+ local picker: any = provider:GetOrCreatePicker(Enum.KeyCode.E)
219
+ local action = controller.newAction(1)
220
+ picker:AddAction(action)
221
+ provider:Update()
222
+
223
+ picker:RemoveAction(action)
224
+ provider:Update()
225
+
226
+ expect(provider:FindPicker(Enum.KeyCode.E)).toBeNil()
227
+
228
+ controller.destroy()
229
+ end)
230
+
231
+ it("discards a touch button picker that has no actions", function()
232
+ local controller = setup()
233
+ local provider = controller.newProvider()
234
+
235
+ provider:GetOrCreatePicker("TouchButton")
236
+ provider:Update()
237
+
238
+ expect(provider:FindPicker("TouchButton")).toBeNil()
239
+
240
+ controller.destroy()
241
+ end)
242
+
243
+ it("does nothing when there are no pickers", function()
244
+ local controller = setup()
245
+ local provider = controller.newProvider()
246
+
247
+ expect(function()
248
+ provider:Update()
249
+ end).never.toThrow()
250
+
251
+ controller.destroy()
252
+ end)
253
+ end)
254
+
255
+ describe("ScoredActionPickerProvider.Destroy", function()
256
+ it("drops the preferences its pickers were holding", function()
257
+ local controller = setup()
258
+ local provider = controller.newLooseProvider()
259
+
260
+ local picker: any = provider:GetOrCreatePicker(Enum.KeyCode.E)
261
+ local action = controller.newAction(1)
262
+ picker:AddAction(action)
263
+ expect(action:IsPreferred()).toBe(true)
264
+
265
+ provider:Destroy()
266
+
267
+ expect(action:IsPreferred()).toBe(false)
268
+
269
+ controller.destroy()
270
+ end)
271
+ end)
@@ -7,6 +7,7 @@
7
7
  local require = require(script.Parent.loader).load(script)
8
8
 
9
9
  local BaseObject = require("BaseObject")
10
+ local ScoredAction = require("ScoredAction")
10
11
 
11
12
  local TouchButtonScoredActionPicker = setmetatable({}, BaseObject)
12
13
  TouchButtonScoredActionPicker.ClassName = "TouchButtonScoredActionPicker"
@@ -34,14 +35,12 @@ function TouchButtonScoredActionPicker.Update(self: TouchButtonScoredActionPicke
34
35
  if not action.Destroy then
35
36
  warn("[ScoredActionPicker] - Action is destroyed. Should have been removed.")
36
37
  self._maid[action] = nil
37
- else
38
- if action:GetScore() ~= -math.huge then
39
- if not self._maid[action] then
40
- self._maid[action] = action:PushPreferred()
41
- end
42
- else
43
- self._maid[action] = nil
38
+ elseif self:_isPreferable(action) then
39
+ if not self._maid[action] then
40
+ self._maid[action] = action:PushPreferred()
44
41
  end
42
+ else
43
+ self._maid[action] = nil
45
44
  end
46
45
  end
47
46
  end
@@ -54,11 +53,26 @@ function TouchButtonScoredActionPicker.AddAction(self: TouchButtonScoredActionPi
54
53
  -- Always prefer touch buttons
55
54
  self._actionSet[action] = true
56
55
 
57
- if action:GetScore() ~= -math.huge then
56
+ if self:_isPreferable(action) then
58
57
  self._maid[action] = action:PushPreferred()
59
58
  end
60
59
  end
61
60
 
61
+ --[=[
62
+ Touch actions do not compete for a slot the way [ScoredActionPicker] makes keys compete, so being
63
+ disabled is the only thing that keeps a touch button off the screen.
64
+
65
+ @param action ScoredAction
66
+ @return boolean
67
+ @private
68
+ ]=]
69
+ function TouchButtonScoredActionPicker._isPreferable(
70
+ _self: TouchButtonScoredActionPicker,
71
+ action: ScoredAction.ScoredAction
72
+ ): boolean
73
+ return action:IsEnabled() and action:GetScore() ~= -math.huge
74
+ end
75
+
62
76
  function TouchButtonScoredActionPicker.RemoveAction(self: TouchButtonScoredActionPicker, action)
63
77
  self._actionSet[action] = nil
64
78
  self._maid[action] = nil