@quenty/randomutils 2.2.0 → 2.3.0
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.
- package/CHANGELOG.md +16 -0
- package/LICENSE.md +1 -1
- package/package.json +5 -2
- package/src/Shared/RandomSampler.lua +62 -0
- package/src/Shared/RandomUtils.lua +1 -4
- package/src/node_modules.project.json +7 -0
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,22 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
# [2.3.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/randomutils@2.2.0...@quenty/randomutils@2.3.0) (2023-08-23)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* Use table.clone() ([28f311b](https://github.com/Quenty/NevermoreEngine/commit/28f311b9c2534fa7b2056265db05c1447dc43a8a))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
### Features
|
|
15
|
+
|
|
16
|
+
* Add RandomSampler object to RandomUtils ([5ca48d9](https://github.com/Quenty/NevermoreEngine/commit/5ca48d91697ee0b20ec1df8dda91a0e9f9ceb0a3))
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
6
22
|
# [2.2.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/randomutils@2.1.1...@quenty/randomutils@2.2.0) (2022-03-27)
|
|
7
23
|
|
|
8
24
|
**Note:** Version bump only for package @quenty/randomutils
|
package/LICENSE.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quenty/randomutils",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"description": "Quenty's RandomUtils, utility functions for Roblox",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Roblox",
|
|
@@ -28,5 +28,8 @@
|
|
|
28
28
|
"publishConfig": {
|
|
29
29
|
"access": "public"
|
|
30
30
|
},
|
|
31
|
-
"
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@quenty/loader": "^6.3.0"
|
|
33
|
+
},
|
|
34
|
+
"gitHead": "2547e660a0333034a5b20ddd6b23e343bc01f7c6"
|
|
32
35
|
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
--[=[
|
|
2
|
+
@class RandomSampler
|
|
3
|
+
]=]
|
|
4
|
+
|
|
5
|
+
local require = require(script.Parent.loader).load(script)
|
|
6
|
+
|
|
7
|
+
local RandomUtils = require("RandomUtils")
|
|
8
|
+
|
|
9
|
+
local RandomSampler = {}
|
|
10
|
+
RandomSampler.ClassName = "RandomSampler"
|
|
11
|
+
RandomSampler.__index = RandomSampler
|
|
12
|
+
|
|
13
|
+
function RandomSampler.new(samples)
|
|
14
|
+
local self = setmetatable({}, RandomSampler)
|
|
15
|
+
|
|
16
|
+
self._optionsList = {}
|
|
17
|
+
self._shuffledAvailableList = {}
|
|
18
|
+
|
|
19
|
+
if samples then
|
|
20
|
+
self:SetSamples(samples)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
return self
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
function RandomSampler:SetSamples(samples)
|
|
27
|
+
assert(type(samples) == "table", "Bad samples")
|
|
28
|
+
|
|
29
|
+
if self._optionsList ~= samples then
|
|
30
|
+
self._optionsList = samples
|
|
31
|
+
|
|
32
|
+
-- TODO: Smarter refill
|
|
33
|
+
self:Refill()
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
function RandomSampler:Sample()
|
|
38
|
+
if #self._shuffledAvailableList == 0 then
|
|
39
|
+
self:Refill()
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
local selection = table.remove(self._shuffledAvailableList)
|
|
43
|
+
self._lastSelection = selection
|
|
44
|
+
|
|
45
|
+
return selection
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
function RandomSampler:Refill()
|
|
49
|
+
local newList = RandomUtils.shuffledCopy(self._optionsList)
|
|
50
|
+
|
|
51
|
+
if #newList > 1 then
|
|
52
|
+
-- prevent repeat on restart
|
|
53
|
+
if newList[#newList] == self._lastSelection then
|
|
54
|
+
newList[1], newList[#newList] = newList[#newList], newList[1]
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
self._shuffledAvailableList = newList
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
return RandomSampler
|