Skip to content

Commit b517eb1

Browse files
committed
WIP Added ccEvents
1 parent 970c119 commit b517eb1

File tree

8 files changed

+231
-32
lines changed

8 files changed

+231
-32
lines changed

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ jobs:
1313
- name: get lua
1414
uses: leafo/gh-actions-lua@v10
1515
with:
16-
luaVersion: "5.1"
16+
luaVersion: "5.2"
1717

1818
- name: get luarocks
1919
uses: leafo/gh-actions-luarocks@v4
2020
with:
21-
luaVersion: "5.1"
21+
luaVersion: "5.2"
2222

2323
- name: get busted and luasocket
2424
run: |

events.lua

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
local class = require("ccClass")
2+
3+
---@class Event
4+
---@field eventName string
5+
---@field eventArgs table | nil
6+
---@field receivedBy table
7+
8+
---@class timer
9+
---@field id number
10+
---@field triggerAfter number
11+
12+
---@class timerList
13+
---@field timers timer[]
14+
---@field currentID number
15+
16+
---@class ccEvent
17+
---@field FIFOEventList Event[]
18+
---@field FIFOTimerList timerList
19+
---@field co thread
20+
---@field time number eq. os.time("ingame") from ccTweaked)
21+
---@field epoch number eq. os.epoch("ingame") from ccTweaked)
22+
---@field private tmpObj any
23+
local Events = {}
24+
25+
---@param path string
26+
---@return ccEvent
27+
---@return table loadedModule
28+
function Events:new(path)
29+
30+
local eventObj = class(function(baseClass)
31+
---@cast baseClass ccEvent
32+
baseClass.FIFOEventList = {}
33+
baseClass.FIFOTimerList = {timers = {}, currentID = 1}
34+
baseClass.time = 0
35+
baseClass.epoch = 0
36+
37+
end)()
38+
39+
local env = {}
40+
---@class EventOS: oslib
41+
env.os = setmetatable({
42+
43+
pullEvent = function(name)
44+
local t = {coroutine.yield(name)}
45+
while not t or (name and t[1] ~= name) do
46+
t = {coroutine.yield(name, "does not match")}
47+
end
48+
return table.unpack(t)
49+
end,
50+
queueEvent = function(name, ...)
51+
eventObj:invoke(name, arg)
52+
end,
53+
startTimer = function(time)
54+
return eventObj:addTimer(time)
55+
end,
56+
cancleTimer = function(id)
57+
eventObj:removeTimer(id)
58+
end
59+
60+
}, {__index = os})
61+
setmetatable(env, {__index = _G})
62+
63+
print("stuff", env.os.pullEvent)
64+
local func = assert(loadfile(path, "t", env))
65+
print("func", func)
66+
67+
eventObj.co = coroutine.create(function ()
68+
return func()
69+
end)
70+
local _, loadedModule = coroutine.resume(eventObj.co)
71+
return eventObj, loadedModule
72+
end
73+
74+
75+
76+
---@param ccEvent ccEvent
77+
local run = coroutine.create(function(ccEvent)
78+
while true do
79+
while #ccEvent.FIFOEventList > 0 and coroutine.status(ccEvent.co) == "suspended" do
80+
coroutine.resume(ccEvent.co, table.remove(ccEvent.FIFOEventList, 1))
81+
-- just empty the list until an event was valid OR no Events are left
82+
end
83+
coroutine.yield("tick")
84+
end
85+
end)
86+
87+
function Events:addTimer(time)
88+
local triggerAt = time * 1000 + self.epoch
89+
local id = self.FIFOTimerList.currentID
90+
self.FIFOTimerList.currentID = self.FIFOTimerList.currentID + 1
91+
table.insert(self.FIFOTimerList.timers, {triggerAt = triggerAt, id = id})
92+
return id
93+
end
94+
95+
function Events:removeTimer(id)
96+
for k,v in pairs(self.FIFOTimerList.timers) do
97+
if v.id == id then
98+
self.FIFOTimerList.currentID[id] = nil
99+
end
100+
end
101+
end
102+
103+
---Passes time (in Seconds)
104+
---Required for timers
105+
---@param time number seconds
106+
function Events:passTime(time)
107+
self.time = (self.time + (time / 60 / 24)) % 24
108+
self.epoch = self.epoch + time
109+
110+
for key, value in pairs(self.FIFOTimerList.timers) do
111+
if value.triggerAfter <= self.epoch then
112+
self:invoke("timer", value.id)
113+
end
114+
end
115+
116+
end
117+
118+
function Events:invoke(eventName, ...)
119+
---@type Event
120+
local event = {eventName = eventName, receivedBy = {}, eventArgs = arg}
121+
table.insert(self.FIFOEventList, event)
122+
self.newEventAdded = true
123+
coroutine.resume(run, "tick")
124+
end
125+
126+
return Events

files.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ fs.lua
44
json.lua
55
scm.lua
66
textutils.lua
7-
settings.lua
7+
settings.lua
8+
events.lua

t.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
local co = coroutine.create(function (a,b)
2+
local t = coroutine.yield("a")
3+
print("t", t)
4+
end)
5+
print(coroutine.resume(co, "b"))
6+
print(coroutine.resume(co, "b"))

tests/event_spec.lua

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---@class are
2+
---@field same string
3+
---@field equal string
4+
---@field equals string
5+
6+
---@class is
7+
---@field truthy string
8+
---@field falsy string
9+
---@field not_true string
10+
---@field not_false string
11+
12+
---@class has
13+
---@field error string
14+
---@field errors string
15+
16+
---@class assert
17+
---@field are are
18+
---@field is is
19+
---@field are_not are
20+
---@field is_not is
21+
---@field has has
22+
---@field has_no has
23+
---@field True string
24+
---@field False string
25+
---@field has_error string
26+
---@field is_false string
27+
---@field is_true string
28+
---@field equal string
29+
assert = assert
30+
31+
package.path = package.path .. ";"
32+
.."libs/?.lua;"
33+
34+
local EventEmulator = require("events")
35+
36+
describe("Event-Tests", function ()
37+
---@type ccEvent, TestFile
38+
local eventManager, testModule
39+
before_each(function()
40+
eventManager, testModule = EventEmulator:new("tests/testFiles/event_testFile.lua")
41+
end)
42+
describe("Basics", function()
43+
it("pull And Invoke", function()
44+
assert(type(testModule)=="table", type(testModule))
45+
testModule:event1()
46+
assert.is.falsy(testModule.status.event1)
47+
eventManager:invoke("TestEvent")
48+
assert.is.truthy(testModule.status.event1)
49+
end)
50+
end)
51+
end)

tests/settings_spec.lua

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
---@class are
2-
---@field same function
3-
---@field equal function
4-
---@field equals function
2+
---@field same string
3+
---@field equal string
4+
---@field equals string
55

66
---@class is
7-
---@field truthy function
8-
---@field falsy function
9-
---@field not_true function
10-
---@field not_false function
7+
---@field truthy string
8+
---@field falsy string
9+
---@field not_true string
10+
---@field not_false string
1111

1212
---@class has
13-
---@field error function
14-
---@field errors function
13+
---@field error string
14+
---@field errors string
1515

1616
---@class assert
1717
---@field are are
@@ -20,12 +20,12 @@
2020
---@field is_not is
2121
---@field has has
2222
---@field has_no has
23-
---@field True function
24-
---@field False function
25-
---@field has_error function
26-
---@field is_false function
27-
---@field is_true function
28-
---@field equal function
23+
---@field True string
24+
---@field False string
25+
---@field has_error string
26+
---@field is_false string
27+
---@field is_true string
28+
---@field equal string
2929
assert = assert
3030

3131

tests/testFiles/event_testFile.lua

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---@class TestFile
2+
local TestFile = {
3+
status = {}
4+
}
5+
6+
function TestFile:event1()
7+
print("os.execute", os.execute)
8+
print("os.pullEvent", os.pullEvent)
9+
os.pullEvent()
10+
self.status.event1 = "Fired"
11+
end
12+
13+
14+
15+
return TestFile

vector.lua

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
1-
---@type function
1+
---@type string
22
local class = require("ccClass")
33

44
---@class Vector
55
---@field x number
66
---@field y number
77
---@field z number
8-
---@field new function @Create a new Vector
9-
---@field add function @Add two vectors
10-
---@field sub function @Subtract two vectors
11-
---@field mul function @Multiply by value
12-
---@field div function @Divide by value
13-
---@field unm function @negate the vector
14-
---@field dot function @dot product of two vectors
15-
---@field cross function @cross product of two vectors
16-
---@field length function @magnitude / length of the vector
17-
---@field norm function @normalize the vector
18-
---@field round function @round the vector
19-
---@field toString function @Returns a String of the vector
20-
---@field equals function @check if two vectors are equal
8+
---@field new string @Create a new Vector
9+
---@field add string @Add two vectors
10+
---@field sub string @Subtract two vectors
11+
---@field mul string @Multiply by value
12+
---@field div string @Divide by value
13+
---@field unm string @negate the vector
14+
---@field dot string @dot product of two vectors
15+
---@field cross string @cross product of two vectors
16+
---@field length string @magnitude / length of the vector
17+
---@field norm string @normalize the vector
18+
---@field round string @round the vector
19+
---@field toString string @Returns a String of the vector
20+
---@field equals string @check if two vectors are equal
2121

2222
---@type Vector
2323
---@diagnostic disable-next-line: missing-fields

0 commit comments

Comments
 (0)