# Module `ExpGamingCore.Server`

Adds a thread system and event listening and a admin bypass (recommend to disable /c and use optional /interface)

### Info:

- License: https://github.com/explosivegaming/scenario/blob/master/LICENSE
- Author: Cooldude2606

## Functions

new_thread (obj) | Generates a new thread object
get_thread (mixed) | Used to get a thread via uuid or name (if one is assied)
queue_thread (thread_to_queue) | Adds a thread into the resolve queue, can be used to lower lag
close_all_threads (with_force) | Closes all active threads, can use force if it causes errors
run_tick_threads () | Runs all the theads which have opened with an on_tick event
check_timeouts () | Checks the timeout on all active timeout threads
_thread_debuger (player, event[, state=toggle]) | Used to print event info to a player
interface (callback[, use_thread[, env[, ...]]]) | Acts as a bypass for running functions, can accept a string

## Tables

global | Global Table

## Fields

uuid | Used to generate a new uuid for the thread system
threads | Redirect to the thread index

## Class Thread

Server._thread:create ([obj={}]) | Returns a new thread object
Server._thread:queue () | Opens and queses a thread
Server._thread:valid ([skip_location_check=false]) | Test if the thread has all requied parts
Server._thread:open () | Opens the thread; indexs this thread in the global index
Server._thread:close () | Inverse of thread:open() - Removes all indexs to this thread, most cases this will cause it to become inassible
Server._thread:resolve ([...]) | Trigger the on_resolve function and closes the thread - error and success called based on result of pcall (useful for async)
Server._thread:check_timeout () | Checks the timeout on a thread - if timed out then it calles on_timeout and closes
Server._thread:error (err) | Used to check and raise the error handler of the thread, if not present it raises an error
Server._thread:on_event (event, callback) | Set function to run then an event is triggered, none of them are 'needed' but you are advised to have atleast one

## modules.expgamingcore.server.src.commands Functions

_comment () | This file will be loaded when ExpGamingCore.Command is present
interface | Runs the given input from the script

## Functions

new_thread (obj)
  Generates a new thread object

  ### Parameters:

  - obj table the atributes to give to the thread

  ### Returns:

  Server._thread the new thread created

  ### Usage:

      Server.new_thread{name='foo',data={}}

get_thread (mixed)
  Used to get a thread via uuid or name (if one is assied)

  ### Parameters:

  - mixed either a uuid or the name given to a thread

  ### Returns:

  Server._thread the thread by that name or uuid

  ### Or

  boolean if false is returned then no thread existes

  ### Usage:

      Server.get_thread('decon') -- return thread

queue_thread (thread_to_queue)
  Adds a thread into the resolve queue, can be used to lower lag

  ### Parameters:

  - thread_to_queue Server._thread the thread to be added to the queue, must be open and have a on_resolve function

  ### Returns:

  boolean was it added successfuly

  ### Usage:

      Server.queue_thread(thread) -- return true/false

close_all_threads (with_force)
  Closes all active threads, can use force if it causes errors

  ### Parameters:

  - with_force bolean use force when closing

  ### Usage:

  -     Server.close_all_threads() -- asks all threads to close

  -     Server.close_all_threads(true) -- forcefuly close all threads

run_tick_threads ()
  Runs all the theads which have opened with an on_tick event

  ### Usage:

      Server.run_tick_threads()

check_timeouts ()
  Checks the timeout on all active timeout threads

  ### Usage:

      Server.check_timeouts()

_thread_debuger (player, event[, state=toggle])
  Used to print event info to a player

  ### Parameters:

  - player name, index or LuaPlayer the player that the info will be returned to
  - event name or index the event that info will be returned fo
  - state boolean will info be returned, nil to toggle current state (default toggle)

  ### Usage:

      Server._thread_debuger('Cooldude2606','on_player_died',true) -- will output event info to 'Cooldude2606' for 'on_player_died'

interface (callback[, use_thread[, env[, ...]]])
  Acts as a bypass for running functions, can accept a string

  ### Parameters:

  - callback string or function function to be ran
  - use_thread Server._thread or true run the command on a premade thread or let it make its own (optional)
  - env table run the env to run the command in must have _env key as true to be (optional)
  - ... any args you want to pass to the function (optional)

  ### Returns:

  1. boolean if no thread then it will return a true for the success
  2. if no thread then it will return the value(s) returned by the callback

  ### Or

  1. boolean if no thread then it will return a false for the success
  2. string if no thread then it will return a an err value

  ### Usage:

  -     Server.interface('local x = 1+1 print(x) return x') -- return 2

  -     Server.interface('local x = 1+1 print(x)',true) -- will creat a thread to run as root (this is the bypass)

## Tables

global
  Global Table

  ### Fields:

  - all a list of every thread (indexed by uuid)
  - queue an index for threads which will be resolved (contains uuids)
  - tick an index for threads which will run every tick (contains uuids)
  - timeout an index for threads which will timeout (contains uuids)
  - events an index of threads based on event ids (contains uuids)
  - paused an index of pasued threads (contains uuids)
  - named a name index for thread uuids
  - print_to contains players that event details will be printed to
  - uuid contains the random number generator for the uuid system

## Fields

uuid
  Used to generate a new uuid for the thread system

  ### Usage:

      local uuid = tostring(Server.uuid) -- calling tostring locks the value

threads
  Redirect to the thread index

  ### Usage:

  -     Server.threads -- return #global.all

  -     Server.threads -- return global.all

## Class Thread

The class for the server threads, allows abbilty to run async function

Server._thread:create ([obj={}])
  Returns a new thread object

  ### Parameters:

  - obj table all values are opt {timeout=int,name=str,data=any} (default {})

  ### Returns:

  Server._thread the new thread object

  ### Usage:

      new_thread = thread:create()

Server._thread:queue ()
  Opens and queses a thread

  ### Returns:

  boolean was the thread queued successfuly

  ### See also:

  Server.queue_thread

  ### Usage:

      Server._thread:queue() -- returns true/false

Server._thread:valid ([skip_location_check=false])
  Test if the thread has all requied parts

  ### Parameters:

  - skip_location_check bolean true to skip the location checking (default false)

  ### Returns:

  boolean is the thread valid

  ### Usage:

      if thread:valid() then end -- basic test for valid

Server._thread:open ()
  Opens the thread; indexs this thread in the global index

  ### Returns:

  bolean if the thread was opened successfuly

  ### Usage:

      thread:open() -- return true

Server._thread:close ()
  Inverse of thread:open() - Removes all indexs to this thread, most cases this will cause it to become inassible

  ### Returns:

  boolean if the thread had a on_close function

  ### Usage:

      thread:close() -- return true

Server._thread:resolve ([...])
  Trigger the on_resolve function and closes the thread - error and success called based on result of pcall (useful for async)

  ### Parameters:

  - ... any arguments you want to pass to the resolve function (optional)

  ### Returns:

  bolean true if the thread called on_success or on_error

  ### Usage:

      thread:resolve(x,y,z) -- return true

Server._thread:check_timeout ()
  Checks the timeout on a thread - if timed out then it calles on_timeout and closes

  ### Returns:

  bolean if the thread timed out

  ### Usage:

      thread:check_timeout() -- return true

Server._thread:error (err)
  Used to check and raise the error handler of the thread, if not present it raises an error

  ### Parameters:

  - err string the err to be rasied

  ### Returns:

  boolean did the thread have an error handler

  ### Usage:

      thread:error(err) -- return true

Server._thread:on_event (event, callback)
  Set function to run then an event is triggered, none of them are 'needed' but you are advised to have atleast one

  ### Parameters:

  - event string or index the name of the event that the function should be called on
  - callback function the function which is called by the event trigger

  ### Returns:

  table returns self so that they can be chained together

  ### Usage:

  -     thread:on_event('close',function) -- if event is not one below then a game event is used

  -     thread_only_events = ['close','timeout','tick','resolve','success','error']

## modules.expgamingcore.server.src.commands Functions

_comment ()
  This file will be loaded when ExpGamingCore.Command is present
interface
  Runs the given input from the script

  ### param:

  - code The code that will be ran

generated by LDoc 1.4.6 Last updated 2018-06-07 12:58:23