Last active
May 17, 2023 20:06
-
-
Save lyuz1n/70d901acb4271c5f240595de17555566 to your computer and use it in GitHub Desktop.
Countdown Helper (extension for forgottenserver)
This file contains hidden or 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
| --[[ | |
| Description: This file is part of Countdown Helper | |
| Author: Lyu | |
| Discord: Lyµ#8767 | |
| ]] | |
| local EVENT_STATE_CREATED = 0 | |
| local EVENT_STATE_STARTED = 1 | |
| local EVENT_STATE_PAUSED = 2 | |
| local EVENT_STATE_FINISHED = 3 | |
| local EVENT_STATE_CANCELED = 4 | |
| local MT = { | |
| _VERSION = 'countdown_helper.lua v2023.05.07', | |
| _URL = 'https://gist.github.com/lyuz1n/70d901acb4271c5f240595de17555566' | |
| } | |
| MT.__index = MT | |
| function Countdown(object) | |
| if object.seconds == nil or type(object.seconds) ~= 'number' or object.seconds <= 0 then | |
| error 'argument seconds must be a number greater than 0' | |
| end | |
| setmetatable(object, {__index = MT}) | |
| object.state = EVENT_STATE_CREATED | |
| if object.autoStart then | |
| if type(object.autoStart) ~= 'boolean' then | |
| error 'argument autoStart must be a boolean' | |
| end | |
| object:start() | |
| end | |
| return object | |
| end | |
| local function isEventMethod(name, method) | |
| if method then | |
| if type(method) ~= 'function' then | |
| error(('argument %s must be a function'):format(name)) | |
| end | |
| return true | |
| end | |
| return false | |
| end | |
| function MT:start() | |
| if self.state ~= EVENT_STATE_CREATED then | |
| return | |
| end | |
| if isEventMethod('onStart', self.onStart) then | |
| self.onStart(self) | |
| self.state = EVENT_STATE_STARTED | |
| end | |
| self.initialSeconds = self.seconds | |
| local function decrement() | |
| if self.state == EVENT_STATE_PAUSED then | |
| self.eventId = addEvent(decrement, 1000) | |
| return | |
| end | |
| if self.seconds > 0 then | |
| if isEventMethod('onTick', self.onTick) then | |
| self.onTick(self) | |
| end | |
| self.seconds = self.seconds - 1 | |
| self.eventId = addEvent(decrement, 1000) | |
| else | |
| if isEventMethod('onFinish', self.onFinish) then | |
| self.eventId = nil | |
| self.onFinish(self) | |
| self.state = EVENT_STATE_FINISHED | |
| end | |
| end | |
| end | |
| decrement() | |
| end | |
| function MT:cancel() | |
| if self.eventId then | |
| stopEvent(self.eventId) | |
| self.eventId = nil | |
| if isEventMethod('onCancel', self.onCancel) then | |
| self.onCancel(self) | |
| self.state = EVENT_STATE_CANCELED | |
| end | |
| end | |
| end | |
| function MT:restart() | |
| if (self.state ~= EVENT_STATE_STARTED and self.state ~= EVENT_STATE_PAUSED) or self.seconds <= 0 then | |
| return | |
| end | |
| self.seconds = self.initialSeconds | |
| if isEventMethod('onRestart', self.onRestart) then | |
| self.onRestart(self) | |
| end | |
| end | |
| function MT:pause() | |
| if self.state == EVENT_STATE_STARTED then | |
| self.state = EVENT_STATE_PAUSED | |
| if isEventMethod('onPause', self.onPause) then | |
| self.onPause(self) | |
| end | |
| end | |
| end | |
| function MT:resume() | |
| if self.state == EVENT_STATE_PAUSED then | |
| self.state = EVENT_STATE_STARTED | |
| if isEventMethod('onResume', self.onResume) then | |
| self.onResume(self) | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ok, nice!