From 27a2ef644638332d66969295fa180dcb406f5c6a Mon Sep 17 00:00:00 2001 From: FZFalzar Date: Sat, 22 Feb 2014 02:06:36 +0800 Subject: [PATCH 1/3] Initial plugin commit for serverads_plugin. Modified README to display plugin details and added source files. --- README.md | 25 ++++ plugins/serverads_plugin/__init__.py | 1 + plugins/serverads_plugin/serverads_plugin.py | 129 +++++++++++++++++++ 3 files changed, 155 insertions(+) create mode 100644 plugins/serverads_plugin/__init__.py create mode 100644 plugins/serverads_plugin/serverads_plugin.py diff --git a/README.md b/README.md index 9c2ed51..b11cb01 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,31 @@ Please note: /socialspy #Enables/Disables SocialSpy, a feature for admins to receive PMs sent by anyone, for policing purposes > ``` +## [FZFalzar's ServerAds plugin](https://github.com/FZFalzar/StarryPy_plugins/tree/serverads_plugin/plugins/serverads_plugin) +> Broadcasts defined messages(ServerAds) at a preset duration +> Additional messages can be specified in config.json under "serverads_list". The plugin prefix can also be modified to your liking, under "serverads_prefix" +> Adds the following commands and abilities to the server: +> ``` +@permissions(UserLevels.ADMIN) +/ads_reload #Reloads the configuration. Currently does not work as there is no way to reload config.json +/ads_interval [num] #Displays the current interval for message broadcast. If num (in seconds) is specified, the new interval for message broadcast will be set. +> ``` +### Add the following block to your config file: + +>``` +"serverads_plugin": { + "auto_activate": true, + "serverads_interval": 30, + "serverads_list": [ + "Welcome to the server!", + "Have a nice stay!", + "Have fun, don't grief!", + "Check out our site at somewebpage.com!" + ], + "serverads_prefix": "^#FF6600;[SA]" + } +>``` + ## [Maffi's uptime plugin](https://github.com/MrMarvin/StarryPy_plugins/blob/master/plugins/uptime) > Very simple plugin that responds to /uptime with the time StarryPy is running. diff --git a/plugins/serverads_plugin/__init__.py b/plugins/serverads_plugin/__init__.py new file mode 100644 index 0000000..2942f84 --- /dev/null +++ b/plugins/serverads_plugin/__init__.py @@ -0,0 +1 @@ +from serverads_plugin import ServerAds \ No newline at end of file diff --git a/plugins/serverads_plugin/serverads_plugin.py b/plugins/serverads_plugin/serverads_plugin.py new file mode 100644 index 0000000..04c2809 --- /dev/null +++ b/plugins/serverads_plugin/serverads_plugin.py @@ -0,0 +1,129 @@ +#=========================================================== +# ServerAds Plugin +# Author: FZFalzar/Duck of Brutus.SG Starbound (http://steamcommunity.com/groups/BrutusSG) +# Version: v0.1 +# Description: Broadcasts defined messages(ServerAds) at a preset duration +#=========================================================== + +import random +from base_plugin import SimpleCommandPlugin +from plugins.core.player_manager import permissions, UserLevels +from threading import Timer + +class ServerAds(SimpleCommandPlugin): + """ + Broadcasts pre-defined messages at set intervals to all players + """ + name = "serverads_plugin" + depends = ["command_dispatcher"] + commands = ["ads_reload", "ads_interval"] + auto_activate = True + + def activate(self): + super(ServerAds, self).activate() + self.load_config() + self.ads_thread = None + self.prevMsgIdx = 0 + self.rNum = 0 + self.start_broadcasting() + + def load_config(self): + try: + self.serverads_list = self.config.plugin_config["serverads_list"] + self.serverads_prefix = self.config.plugin_config["serverads_prefix"] + self.interval = self.config.plugin_config["serverads_interval"] + except Exception as e: + self.logger.info("Error occured! %s" % e) + if self.protocol is not None: + self.protocol.send_chat_message("Reload failed! Please check config.json! Then do /ads_reload") + self.protocol.send_chat_message("Initiating with default values...") + self.serverads_list = ["Welcome to the server!", "Have a nice stay!"] + self.serverads_prefix = "[SA]" + self.interval = 30 + def save_config(self): + self.config.plugin_config['serverads_list'] = self.serverads_list + self.config.plugin_config['serverads_prefix'] = self.serverads_prefix + self.config.plugin_config['serverads_interval'] = self.interval + self.config.save() + + def start_broadcasting(self): + if self.ads_thread is not None: + self.ads_thread.stop() + self.logger.info("Broadcast thread stopped!") + del self.ads_thread + self.ads_thread = ThreadedTimer(self.interval, self.broadcast) + self.ads_thread.start() + self.logger.info("Broadcast thread started!") + + def broadcast(self): + #make sure we do not re-broadcast the last message, for variety + if len(self.serverads_list) > 1: + while self.rNum == self.prevMsgIdx: + #randomly pick from the array + self.rNum = random.randint(0, len(self.serverads_list) - 1) + #override previous index + self.prevMsgIdx = self.rNum + self.logger.info("%s %s" % (self.serverads_prefix, self.serverads_list[self.rNum])) + self.factory.broadcast("%s ^#00FF00;%s" % (self.serverads_prefix, self.serverads_list[self.rNum]), 0, "", "ServerAds") + elif len(self.serverads_list) <= 1: + self.logger.info("%s %s" % (self.serverads_prefix, self.serverads_list[0])) + self.factory.broadcast("%s ^#00FF00;%s" % (self.serverads_prefix, self.serverads_list[0]), 0, "", "ServerAds") + + @permissions(UserLevels.ADMIN) + def ads_reload(self, data): + """Reloads values from configuration. Syntax: /ads_reload""" + self.protocol.send_chat_message("ServerAds reloading!") + self.load_config() + self.start_broadcasting() + self.protocol.send_chat_message("ServerAds reloaded!") + self.protocol.send_chat_message("A restart is necessary as there is currently no other way to reload config.json. Sorry for the inconvenience!" + + @permissions(UserLevels.ADMIN) + def ads_interval(self, data): + """Sets interval for display of serverads. Syntax: /ads_interval [duration in seconds]""" + if len(data) == 0: + self.protocol.send_chat_message(self.ads_interval.__doc__) + self.protocol.send_chat_message("Current interval: %s seconds" % self.interval) + return + num = data[0] + try: + self.interval = int(num) + self.protocol.send_chat_message("Interval set -> %s seconds" % self.interval) + except: + self.protocol.send_chat_message("Invalid input! %s" % num) + self.protocol.send_chat_message(self.ads_interval.__doc__) + return + + self.save_config() + self.load_config() + self.start_broadcasting() + + +class ThreadedTimer(object): + #original source, stackoverflow + """ + Provides an interface for an interruptible timer thread + """ + def __init__(self, interval, function, *args, **kwargs): + self._timer = None + self.interval = interval + self.function = function + self.args = args + self.kwargs = kwargs + self.is_running = False + self.start() + + def _run(self): + self.is_running = False + self.start() + self.function(*self.args, **self.kwargs) + + def start(self): + if not self.is_running: + self._timer = Timer(self.interval, self._run) + self._timer.start() + self.is_running = True + + def stop(self): + self._timer.cancel() + self.is_running = False \ No newline at end of file From 814d835ee0488b23d2426fe2ca81396bd1d10bd2 Mon Sep 17 00:00:00 2001 From: FZFalzar Date: Sat, 22 Feb 2014 02:28:42 +0800 Subject: [PATCH 2/3] Fixed README formatting --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index b11cb01..f288830 100644 --- a/README.md +++ b/README.md @@ -36,24 +36,24 @@ Please note: ## [FZFalzar's BetterWhisper plugin](https://github.com/FZFalzar/StarryPy_plugins/tree/brutus_whisper/plugins/brutus_whisper) > Brings better functionality for the sending of Private Messages in Starbound > Adds the following commands and abilities to the server: -> ``` +>``` @permissions(UserLevels.GUEST) /w #Sends a PM to target. Overrides default /w functionality. alias is /whisper /r #Replies to the last person who you received a PM from @permissions(UserLevels.ADMIN) /socialspy #Enables/Disables SocialSpy, a feature for admins to receive PMs sent by anyone, for policing purposes -> ``` +>``` ## [FZFalzar's ServerAds plugin](https://github.com/FZFalzar/StarryPy_plugins/tree/serverads_plugin/plugins/serverads_plugin) > Broadcasts defined messages(ServerAds) at a preset duration > Additional messages can be specified in config.json under "serverads_list". The plugin prefix can also be modified to your liking, under "serverads_prefix" > Adds the following commands and abilities to the server: -> ``` +>``` @permissions(UserLevels.ADMIN) /ads_reload #Reloads the configuration. Currently does not work as there is no way to reload config.json /ads_interval [num] #Displays the current interval for message broadcast. If num (in seconds) is specified, the new interval for message broadcast will be set. -> ``` +>``` ### Add the following block to your config file: >``` From 275910cb281443ede97b54e33601934c2ef22576 Mon Sep 17 00:00:00 2001 From: FZFalzar Date: Sat, 22 Feb 2014 12:21:26 +0800 Subject: [PATCH 3/3] Fixed a missing ")" that was causing crashes. I really need to invest in an IDE :| --- plugins/serverads_plugin/serverads_plugin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/serverads_plugin/serverads_plugin.py b/plugins/serverads_plugin/serverads_plugin.py index 04c2809..7f6e0f6 100644 --- a/plugins/serverads_plugin/serverads_plugin.py +++ b/plugins/serverads_plugin/serverads_plugin.py @@ -76,7 +76,7 @@ def ads_reload(self, data): self.load_config() self.start_broadcasting() self.protocol.send_chat_message("ServerAds reloaded!") - self.protocol.send_chat_message("A restart is necessary as there is currently no other way to reload config.json. Sorry for the inconvenience!" + self.protocol.send_chat_message("A restart is necessary as there is currently no other way to reload config.json. Sorry for the inconvenience!") @permissions(UserLevels.ADMIN) def ads_interval(self, data):