Code Monkey home page Code Monkey logo

as_cooldowns's Introduction

as_cooldowns

Simple cooldown system for FiveM using ESX and mysql-async

Requirements

ES Extended

Installation

  • Put the resource in your resources folder.
  • Import as_cooldowns.sql in your database.
  • I recommend to start this resource in your server.cfg before the ones that will use the cooldowns.
  • Setup your scripts, and you are ready to go.

Configuration

  • Config.Debug: false by default.
    • true: it will show messages in your console when a event is called and when changes are made in the database, it also adds commands to test some functions, you can easily delete them if you want.
    • false: messages will not appear in your console and commands will not be created.
  • Config.InexistentCooldownStartsAt0: false by default.
    • true: inexistent cooldowns will return 0.
    • false: inexistent cooldowns will return the current time.
  • Config.RegisterSetCooldown: false by default, I strongly recommend that this stays in false, use the export from server side.
    • true: it will mean that the event as_cooldowns:setCooldown can be triggered by clients.
    • false: the only way to set cooldowns from other resources is to use the export.
  • Config.DateFormat: format the date in the function TimeToString(time): '%d': day; '%m': month; '%Y': year; '%y': short year.

Usage

GetCooldown

Returns the cooldown (time - current time) of a registered cooldown. Parameters after the callback:

  • type: name of the registered cooldown.
  • format: optional
    • 'string': returns the cooldown in string format.
    • 'table': returns the cooldown as a table.

Export - Client and server side

function myFunction()
    exports.as_cooldowns:GetCooldown(function(cooldown)
        if cooldown > 0 then
            print(string.format('You need to wait %s seconds before robbing this shop again', cooldown))
        else
            -- Do stuff
        end
    end, 'robbery_shop')
end

Event - Client and server side

function myFunction()
    TriggerEvent('as_cooldowns:getCooldown', function(cooldown)
        if cooldown > 0 then
            print(string.format('You need to wait %s seconds before robbing this shop again', cooldown))
        else
            -- Do stuff
        end
    end, 'robbery_shop')
end

ESX Server Callback - Client side

function myFunction()
    ESX.TriggerServerCallback('as_cooldowns:getCooldown', function(cooldown)
        if cooldown > 0 then
            print(string.format('You need to wait %s seconds before robbing this shop again', cooldown))
        else
            -- Do stuff
        end
    end, 'robbery_shop')
end

GetTime

Returns the time in Epoch of a registered cooldown. Parameters after the callback:

  • type: name of the registered cooldown.
  • format: optional

Export - Client and server side

function myFunction()
    exports.as_cooldowns:GetTime(function(time)
        print(string.format('Cooldown ends at %s', time))
    end, 'robbery_shop', 'string')
end

Event - Client and server side

function myFunction()
    TriggerEvent('as_cooldowns:getTime', function(time)
        print(string.format('Cooldown ends at %s', time))
    end, 'robbery_shop', 'string')
end

ESX Server Callback - Client side

function myFunction()
    ESX.TriggerServerCallback('as_cooldowns:getTime', function(cooldown)
        print(string.format('Cooldown ends at %s', time))
    end, 'robbery_shop', 'string')
end

SetCooldown

Sets or registers the cooldown. Parameters:

  • type: name to register for the cooldown.
  • cooldown: time of the cooldown (in seconds).

Export - Server side

function myFunction()
    exports.as_cooldows:SetCooldown('robbery_shop', os.time() + 3600)
end
function myFunction()
    TriggerServerEvent('as_cooldowns:setCooldown', 'robbery_shop', os.time() + 3600)
end
function myFunction()
    TriggerEvent('as_cooldowns:setCooldown', 'robbery_shop', os.time() + 3600)
end

CooldownToTable

Returns a table with the remaining time of the cooldown, the table returns this values:

  • isNegative: true/false self explanatory.
  • days: remaining days.
  • hours: remaining hours.
  • minutes: remaining minutes.
  • seconds: remaining seconds.

Export - Client and server side

function myFunction(cooldown)
    local myTable = exports.as_cooldowns:CooldownToTable(cooldown)
    if myTable.isNegative then
        print(string.format('Cooldown has passed current time'))
    end
end

CooldownToString

Returns a string with format, example output: 1:12:55:42.

Export - Client and server side

function myFunction(cooldown)
    local myString = exports.as_cooldowns:CooldownToString(cooldown)
    print(myString)
end

TimeToString

Returns a string with format, example output: 24/12/2020 23:59:50.

Export - Server side

function myFunction(myTime)
    local myString = exports.as_cooldowns:TimeToString(myTime)
    print(myString)
end

TimeToTable

Returns a table with the format of os.date("*t", time).

Export - Server side

function myFunction(myTime)
    local myTable = exports.as_cooldowns:TimeToTable(myTime)
    print(myTable.day)
end

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.