Paste #8111: Untitled Paste

Date: 2014/08/08 18:49:24 UTC-07:00
Type: Denizen Script

View Raw Paste Download This Paste
Copy Link


# ---------------------------------------------------------------------------- #
#                                                                              |
#                                                                              |
#                     S u b s c r i p t i o n   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:
# 
#    - The .parsed tags are not a real thing. They will be replaced with
#       <parsed:<string>>
# 
#______________________________________________________________________________#


'SubscriptionSystem':
  type: world
  debug: true

  events:

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

    on player joins:
    - run locally join instantly


  load:
    # Loads the yaml files and generates default files if they don't exist.
    - announce "<&b>Loading Subscription System config files..." to_console
#
# This is the way i /should/ be doing it but the ConfigFileGenerator needs
# to be updated to support creating a single file from a script container.
# 
#    - define files '<yaml[Subscriptions_config].read[config.categories]>'
#    - foreach files {
#      - if !<server.has_file[Subscriptions/%value%.yml]> {
#        - if <player> != null {
#          - flag <player> ConfigFileGeneratorNotify:true
#          - run s@ConfigFileGeneratorHelper def:SubscriptionSystem|Configurations|false|true|false instantly
#          - flag <player> ConfigFileGeneratorNotify:!
#          }
#          else run s@ConfigFileGeneratorHelper def:SubscriptionSystem|Configurations|%value% instantly
#        }
#
#   The ConfigFileGenerator will attempt to build every file if it's missing one of the files.
#
    - if !<server.has_file[SubscriptionSystem/config.yml]>
      || !<server.has_file[SubscriptionSystem/trial.yml]>
      || !<server.has_file[SubscriptionSystem/members.yml]>
      || !<server.has_file[SubscriptionSystem/expired.yml]> {
      - if <player> != null {
        - flag <player> ConfigFileGeneratorNotify:true
        - run s@ConfigFileGeneratorHelper def:SubscriptionSystem|Configurations|false|true|false instantly
        - flag <player> ConfigFileGeneratorNotify:!
        }
        else run s@ConfigFileGeneratorHelper def:SubscriptionSystem|Configurations instantly
      }
    - if <yaml.list.contains[Subscriptions_config]> yaml unload 'id:Subscriptions_config'
    - yaml 'load:SubscriptionSystem/config.yml' 'id:Subscriptions_config'
    - if <yaml.list.contains[Subscriptions_expired]> yaml unload 'id:Subscriptions_expired'
    - yaml 'load:SubscriptionSystem/expired.yml' 'id:Subscriptions_expired'
    - define categories '<yaml[Subscriptions_config].read[config.categories]>'
    - foreach %categories% {
      - if <yaml.list.contains[Subscriptions_%value%]> yaml unload 'id:Subscriptions_%value%'
      - yaml 'load:SubscriptionSystem/%value%.yml' 'id:Subscriptions_%value%'
      }
    - announce "<&b>Subscriptions System config files Loaded!" to_console

  start:
    - inject locally load instantly
    - define categories '<yaml[Subscriptions_config].read[config.categories]>'
    - foreach %categories% {
      - if <queue.exists[Subscriptions_%value%_timer]> {
        - queue queue:Subscriptions_%value%_timer stop
        }
      - run locally checkExpiredLoop 'def:%value%' delay:2s 'id:Subscriptions_%value%_timer'
      }

  checkExpiredLoop:
    - ^define wait '<yaml[Subscriptions_%1%].read[%1%.config.checkInterval].as_duration>'
    - ^wait %wait%
    # do NOT run checkAllExpired instantly! we want to prevent cpu lockup
    - ^run locally checkAllExpired def:%1%
    - ^inject locally checkExpiredLoop

  checkAllExpired:
    - define players '<yaml[Subscriptions_%1%].list_keys[%1%.players]>'
    - if <def[players].is[==].to[null]> queue stop
    - define now '<server.current_time_millis>'
    - foreach %players% {
      - if !<def[value].as_player].is_online> {
        - narrate "Need to handle online/offline"
        }
        else {
        - define expireDate '<yaml[Subscriptions_%1%].read[%1%.players.%value%]>'
        - if <def[expireDate].is[OR_LESS].than[%now%]> {
          - run locally setExpired as:<def[value].as_player>
          }
        }
      }

  checkExpired:
    - define now '<server.current_time_millis>'
    - define player '<player.uuid>'
    - define categories '<yaml[Subscriptions_config].read[config.categories]>'
    - foreach %categories% {
      - define category %value%
      - if <yaml[Subscriptions_%category%].list_keys[%category%.players].contains[%player%]> {
        - define expireDate '<yaml[Subscriptions_%category%].read[%category%.players.%player%]>'
        - if <def[expireDate].is[OR_LESS].than[%now%]> {
          - run locally setExpired 'def:%category%' player:<player>
          }
        }
      }

  setExpired:
    - define player <player.name>
    - define expireAction '<yaml[Subscriptions_config].read[config.expireAction]>'
    - define externalCommands '<parse:<yaml[Subscriptions_config].read[config.expireCommandsExt]>>'
    - define internalCommands '<parse:<yaml[Subscriptions_config].read[config.expireCommandsInt]>>'
    - inject locally runCmds

    - if <def[expireAction].is[==].to[kick]> {
      - adjust <player> 'kick:<parse:<yaml[Subscriptions_config].read[config.kickMsg]>>'
      }
      else if <def[expireAction].is[==].to[ban]> {
      - execute as_server '<parse:<yaml[Subscriptions_config].read[config.banCmd]>>'
      }
      else if <def[expireAction].is[==].to[tp]> {
      - teleport <player> '<yaml[Subscriptions_config].read[config.tpLocation].as_location>'
      }
      else {
      - adjust <player> 'kick:<parse:<yaml[Subscriptions_config].read[config.kickMsg]>>'
      }
    - yaml set '%1%.players.<player.uuid>:!' 'id:Subscriptions_%1%'
    - yaml 'write:expired.players.<player.uuid>' 'value:<server.current_time_millis>' 'id:Subscriptions_expired'
    - yaml 'savefile:SubscriptionSystem/%1%.yml' 'id:Subscriptions_%1%'
    - yaml 'savefile:SubscriptionSystem/expired.yml' 'id:Subscriptions_expired'

  join:
    - if <player.has_permission[firstjoin]> {
      - inject locally checkExpired
      }
      else {
      - inject locally firstJoin
      }
    - inject locally timeLeft

  firstJoin:
    - define expireDate '<server.current_time_millis.add_int[<yaml[Subscriptions_trial].read[trial.config.duration].as_duration.in_milliseconds.as_int>]>'
    - yaml 'write:trial.players.<player.uuid>' 'value:%expireDate%' id:Subscriptions_trial

    - define externalCommands '<parse:<yaml[Subscriptions_config].read[config.firstJoinCommandsExt]>>'
    - define internalCommands '<parse:<yaml[Subscriptions_config].read[config.firstJoinCommandsInt]>>'
    - inject locally runCmds

  runCmds:
    - foreach %externalCommands% {
      - execute as_server '%value%' player:<player>
      }
    - foreach %internalCommands% {
      - execute as_server "ex %value%" player:<player>
      }

  timeLeft:
    - define now '<server.current_time_millis>'
    - define expireDate '<yaml[Subscriptions_%category%].read[%category%.players.%player%]>'
    - define timeLeft '<def[expireDate].sub_int[%now%].as_duration.formatted>'
    - narrate "<&c>You have <&f>%timeLeft%<&c> left before your subscription expires."


################################################################################
#                                                                              #
# 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___________________________#
#______________________________________________________________________________#

'SubscriptionSystem_Configurations':
  type: task
  debug: false

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

  config:
    categories:
      - trial
      - members
    expireAction: kick
    kickMsg: 'Your subscription has expired'
    banCmd: ''
    tpLocation: ''
    expireCommandsExt:
      - 'perm player <def[player]> setgroup expired'
    expireCommandsInt:
      - 'narrate "<def[player]> your time has expired"'
    firstJoinCommandsExt:
      - 'perm player <def[player]> setgroup trial'
    firstJoinCommandsInt:
      - 'narrate "Welcome <def[player]>!"'
      - 'narrate "Welcome <def[player]>!"'
      - 'narrate "Welcome <def[player]>!"'


  trial:
    config:
      checkInterval: 5m
      duration: 10m
    players: []

  members:
    config:
      checkInterval: 24h
      duration: 30d
      gracePeriod: 1d
    players: []

  expired:
    config:
      checkInterval: 1h
    players: []