Created
January 29, 2018 13:04
-
-
Save maritaria/dbfec14638d81bfe411d684f32d85017 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local intercept = {} | |
function intercept.before(func, callback) | |
return function(...) | |
callback(...) | |
return func(...) | |
end | |
end | |
function intercept.after(func, callback) | |
return function(...) | |
local result = func(...) | |
callback(..., result) | |
return result | |
end | |
end | |
function intercept.around(func, callback) | |
return function(...) return callback(func, ...) end | |
end | |
return intercept |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local userClass = require("userclass") | |
require("usercoordination") --installs the coordination aspect | |
local user1 = userClass.create() | |
local user2 = userClass.create() | |
local user3 = userClass.create() | |
local user4 = userClass.create() | |
local user5 = userClass.create() | |
local user6 = userClass.create() | |
local user7 = userClass.create() | |
local user8 = userClass.create() | |
local user9 = userClass.create() | |
print("user1", user1:findSlot()) | |
print("user2", user2:findSlot()) | |
print("user3", user3:findSlot()) | |
print("user4", user4:findSlot()) | |
print("user5", user5:findSlot()) | |
print("user6", user6:findSlot()) | |
print("user7", user7:findSlot()) | |
print("user8", user8:findSlot()) | |
print("user9", user9:findSlot()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local userClass = {} | |
function userClass:findSlot() | |
while true do | |
local slot = math.random(0, 10) | |
if self:canUseSlot(slot) then | |
self:reserveSlot(slot) | |
return slot | |
end | |
end | |
end | |
function userClass:canUseSlot(slot) | |
return not self.slots[slot] | |
end | |
function userClass:reserveSlot(slot) | |
self.slots[slot] = true | |
end | |
function userClass.create() | |
local instance = { slots = {} } | |
setmetatable(instance, { __index = userClass }) | |
return instance | |
end | |
return userClass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local userClass = require("userclass") | |
local intercept = require("intercept") | |
--Create a weak table that allows the users to be garbage collected | |
local users = setmetatable({}, { __mode = "kv" }) | |
local counter = 0 | |
userClass.create = intercept.after(userClass.create, function(_, inst) | |
--Save the users in the table | |
counter = counter + 1 | |
users[inst] = counter | |
print("registered user #" .. counter) | |
end) | |
userClass.canUseSlot = intercept.around(userClass.canUseSlot, function(func, user, slot) | |
--Check with all existing users | |
local result = true | |
print("Organized reservation for slot #" .. slot) | |
for existingUser, index in pairs(users) do | |
print("checking with user #" .. index) | |
if not func(existingUser, slot) then | |
print("Collision with user schedule, returning false") | |
return false | |
end | |
end | |
return true | |
end) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment