Paste #8618: Untitled Paste

Date: 2014/08/20 04:45:12 UTC-07:00
Type: Denizen Script

View Raw Paste Download This Paste
Copy Link


# ---------------------------------------------------------------------------- #
#                                                                              |
#                                                                              |
#                         P e r k P o i n t   S y s t e m                      |
#                                                                              |
#             A system for giving players timed access to your server          |
#                                                                              |
#                                                                              |
#   Author: |Anthony|                                                          |
#   Version: 0.1                                                               |
#   dScript Version: 0.9.5-b150X                                               |
#                                                                              |
# ---------------------------------------------------------------------------- #
# 
# 
# ABOUT:
# 
# 
# 
# 
# USAGE:
# 
# 
# 
# 
# TODO - Future Feature Fluff:
# 
# 
#______________________________________________________________________________#


'PerkPointSystem':
  type: world
  debug: false

  events:
    on server start:
    - run locally start delay:2s

    on perk command:
    - inject locally commandHandler instantly

    on perkadmin command:
    - inject locally adminCommandHandler instantly

  start:
    - ^inject locally loadYaml instantly
    - ^inject locally loadData instantly
    - ^inject locally initDatabase
    - ^inject locally sqlConnection
    - ^inject locally initTables
    - ^announce "<&b>PerkPoints<&co><&3> System loaded!" to_console

  loadYaml:
    # Loads the yaml files and generates default files if they don't exist.
    - announce "<&b>PerkPoints<&co><&3> Loading system config files..." to_console
    - if !<server.has_file[PerkPoints/config.yml]>
      || !<server.has_file[PerkPoints/packages.yml]> {
      - if <player> != null {
        - flag <player> ConfigFileGeneratorNotify:true
        - run s@ConfigFileGeneratorHelper def:PerkPoints|Configurations|false|true|false instantly
        - flag <player> ConfigFileGeneratorNotify:!
        }
        else run s@ConfigFileGeneratorHelper def:PerkPoints|Configurations instantly
      }
    - if <yaml.list.contains[PerkPoints_config]> yaml unload 'id:PerkPoints_config'
    - yaml 'load:PerkPoints/config.yml' 'id:PerkPoints_config'
    - if <yaml.list.contains[PerkPoints_packages]> yaml unload 'id:PerkPoints_packages'
    - yaml 'load:PerkPoints/packages.yml' 'id:PerkPoints_packages'
    - announce "<&b>PerkPoints<&co><&3> System config files Loaded!" to_console

  loadData:
    - ^define db '<yaml[PerkPoints_config].read[config.MySQL.database]>'
    - ^define retry '<yaml[PerkPoints_config].read[config.MySQL.connection.retry]>'
    - ^define retryDelay '<yaml[PerkPoints_config].read[config.MySQL.connection.retryDelay]>'
    - ^define host '<yaml[PerkPoints_config].read[config.MySQL.connection.host]>'
    - ^define port '<yaml[PerkPoints_config].read[config.MySQL.connection.port]>'
    - ^define username '<yaml[PerkPoints_config].read[config.MySQL.connection.username]>'
    - ^define password '<yaml[PerkPoints_config].read[config.MySQL.connection.password]>'
    - ^define autoCreateAccounts '<yaml[PerkPoints_config].read[config.General.AutoCreateAccounts]>'
    - ^define points '<yaml[PerkPoints_config].read[config.General.Points]>'
    - ^define transferrable '<yaml[PerkPoints_config].read[config.General.Transferrable]>'
    - ^define expireOnStartup '<yaml[PerkPoints_config].read[config.General.ExpireOnStartup]>'
    - ^define purgeOnStart '<yaml[PerkPoints_config].read[config.General.PurgeOnStart]>'
    - ^define serverName '<yaml[PerkPoints_config].read[config.General.ServerName]>'
    - ^define expireCheck '<yaml[PerkPoints_config].read[config.General.ExpireCheck]>'

  initDatabase:
    # Attempt to connect to the server.
    - ^sql id:%db%_query connect:%host%:%port% username:%username% password:%password%
    - announce "<&b>PerkPoints<&co><&3> attempting to connect to server..." to_console
    - ^repeat %retry% {
      - if !<server.list_sql_connections.contains[%db%_query]> {
        - announce "<&b>PerkPoints<&co><&3> attempting to connect to server..." to_console
        - sql id:%db%_query connect:%host%:%port% username:%username% password:%password%
        - wait <def[retryDelay].as_duration>
        }
        else {
        - announce "<&b>PerkPoints<&co><&a> successfully connected to server..." to_console
        - repeat stop
        }
      }
    # Make sure we're connected before we proceed
    - ^if !<server.list_sql_connections.contains[%db%_query]> {
      - announce "<&b>PerkPoints<&co><&c> Failed to connect to server!" to_console
      - announce "<&b>PerkPoints<&co><&3> please verify that you have configured all the MySQL connections info in the config.yml file!" to_console
      - inject locally unload
      - queue stop
      }
    # Check if the %db% database exists
    - ^sql id:%db%_query "query:SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME ='%db%'" save:dbExists
    - ^define dbExists '<entry[dbExists].result.get[1].split[/].get[1]||null>'
    - ^if <def[dbExists].is[==].to[null]> {
      - announce "<&b>PerkPoints<&co><&c> %db% database does not exist!" to_console
      - inject locally unload
      - queue stop
      }
    - ^if <def[dbExists].is[==].to[%db%]> {
      - announce "<&b>PerkPoints<&co><&3> %db% database found." to_console
      }
      else {
      - announce "<&b>PerkPoints<&co><&c> Something went wrong testing the %db% database!" to_console
      - announce "<&b>PerkPoints<&co><&c> The %db% database check returned %dbExists%" to_console
      - inject locally unload
      - queue stop
      }
    - ^sql id:%db%_query disconnect

  sqlConnection:
    - ^if <server.list_sql_connections.contains[%db%]> {
      - announce "<&b>PerkPoints<&co><&3> closing existing database connection..." to_console
      - sql disconnect id:%db%
      }
    - ^sql id:%db% connect:%host%:%port%/%db% username:%username% password:%password%
    - announce "<&b>PerkPoints<&co><&3> attempting to connect to the %db% database..." to_console
    - ^repeat %retry% {
      - if !<server.list_sql_connections.contains[%db%]> {
        - announce "<&b>PerkPoints<&co><&3> attempting to connect to the %db% database..." to_console
        - sql id:%db% connect:%host%:%port%/%db% username:%username% password:%password%
        - wait <def[retryDelay].as_duration>
        }
        else {
        - repeat stop
        }
      }
    - ^if <server.list_sql_connections.contains[%db%]> {
      - announce "<&b>PerkPoints<&co><&3> connected to the %db% database!" to_console
      }
      else {
      - announce "<&b>PerkPoints<&co><&c> Failed to connect to the %db% database!" to_console
      - announce "<&b>PerkPoints<&co><&3> please verify that you have created the <&b>%db%<&3> database and have configured all the MySQL connections info in the config.yml file!" to_console
      - inject locally unload
      - queue stop
      }

  initTables:
    - ^announce "<&b>PerkPoints<&co><&3> initializing tables..." to_console
    - ^define tables '<yaml[PerkPoints_config].list_keys[config.MySQL.tables]>'
    - ^foreach %tables% {
      - define table %value%
      - define tableName '<yaml[PerkPoints_config].read[config.MySQL.tables.%table%.name]>'
      - inject locally testTable instantly
      }
    - ^announce "<&b>PerkPoints<&co><&3> Tables loaded!" to_console

  testTable:
    - sql id:%db% "query:SELECT COUNT(*) FROM information_schema.tables WHERE table_schema='%db%' AND table_name='%tableName%';" save:%tableName%_tableExists
    - define tableExists '<entry[%tableName%_tableExists].result.get[1].split[/].get[1]||null>'
    - if <def[tableExists].is[==].to[null]> {
      - announce "<&b>PerkPoints<&co><&c> Something went wrong testing the %tableName% table!" to_console
      - inject locally unload
      - queue stop
      }
    - if <def[tableExists].is[==].to[0]> {
      - announce "<&b>PerkPoints<&co><&3> creating %tableName%..." to_console
      - inject locally createTable instantly
      }
      else if <def[tableExists].is[==].to[1]> {
      - announce "<&b>PerkPoints<&co><&3> %tableName% table found." to_console
      }
      else {
      - announce "<&b>PerkPoints<&co><&c> Something went wrong testing the %tableName% table!" to_console
      - announce "<&b>PerkPoints<&co><&c> The %tableName% table check returned %tableExists%" to_console
      - inject locally unload
      - queue stop
      }

  createTable:
    - define columns '<yaml[PerkPoints_config].list_keys[config.MySQL.tables.%table%.columns]>'
    - foreach %columns% {
      - define column %value%
      - define type '<yaml[PerkPoints_config].read[config.MySQL.tables.%table%.columns.%column%]>'
      - define data '%column% %type%'
      - define datalist '<tern[<def[datalist].exists>]:<el@value[%datalist%].as_list.include[%data%]> || <def[data]>>'
      }
    - sql id:%db% "update:CREATE table %tableName%(<def[dataList].comma_separated>);"
    - define datalist null

  unload:
    - ^if <server.list_sql_connections.contains[%db%_query]> {
      - announce "<&b>PerkPoints<&co><&3> closing %db%_query connection..." to_console
      - sql disconnect id:%db%_query
      }
    - ^if <server.list_sql_connections.contains[%db%]> {
      - announce "<&b>PerkPoints<&co><&3> closing %db% connection..." to_console
      - sql disconnect id:%db%
      }
    - ^if <yaml.list.contains[PerkPoints_config]> {
      - announce "<&b>PerkPoints<&co><&3> unloading config.yml file..." to_console
      - yaml unload 'id:PerkPoints_config'
      }
    - ^if <yaml.list.contains[PerkPoints_packages]> {
      - announce "<&b>PerkPoints<&co><&3> unloading packages.yml file..." to_console
      - yaml unload 'id:PerkPoints_packages'
      }
    - ^announce "<&b>PerkPoints<&co><&3> System disabled..." to_console


################################################################################
#
# Command Handler stuff
#

  adminCommandHandler:
    - if !<player.has_permission[perkpoints.admin]> queue stop
    - if <c.args.get[1].is[==].to[--setup]> {
      - determine passively FULFILLED
      - run locally start
      - queue stop
      }



################################################################################
#                                                                              #
# Configuration Files                                                          #
#                                                                              #
#   These are the default config files. They will be used to build the default #
# config file and data storage files.                                          #
#                                                                              #
#______________________________________________________________________________#
#                                                                              #
#______________________________DO_NOT_EDIT_THIS_DATA___________________________#
#______________________________________________________________________________#

'PerkPoints_Configurations':
  type: task
  debug: false

  # Where you save this script file
  scriptPath: 'scripts/PerkPoints.yml'

  config:
    General:
      AutoCreateAccounts: true
      Points: Points
      Transferrable: false
      ExpireOnStartup: true
      PurgeOnStart: true
      ServerName: Minecraft Server
      ExpireCheck: 5m
    MySQL:
      connection:
        retry: 3
        retryDelay: 10s
        host: localhost
        port: 3306
        username: root
        password: password
      database: PerkPoints
      tables:
        VoteLogs:
          name: perks_votelog
          columns:
            id: int
            playeruuid: varchar(255)
            date: date
            service: varchar(255)
        PerkPoints:
          name: perks_points
          columns:
            id: int
            playeruuid: varchar(255)
            pointsearned: int
            pointsspent: int
            pointsbalance: int

  packages:
    ExamplePackage:
      price: 100
      description: This is an example package.
      hasLimit: false
      limit: 3
      activeImmediately: true
      requireOnline: true
      expires: false
      expireTime: 3h
      commands:
      - say <def[player].name> has purchased the example package.
      - give <def[player].name> diamond 10
      expireCommands:
      - say <def[player].name><&sq>s example package has expired.
      requirePrerequisite: false
      prerequisite: ''
      requiredInventorySpace: 1