SpamProofCommands

Custom Packages that plugins can require for common usages.
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

SpamProofCommands

Postby iPlayer » Thu May 19, 2016 8:26 am

SpamProofCommands (custom package)
GitHub repo: https://github.com/KirillMysnik/SP-SpamProofCommands
Download latest release: https://github.com/KirillMysnik/SP-SpamProofCommands/releases/latest

WHAT'S THIS?
Ever been worried of binding some resource-consuming callbacks to server, client and say (chat) commands?
Possibility of bad Russian guys coming to spam your server won't let you sleep?

Fear no more! SpamProofCommands custom package restricts your callbacks to be executed by each player only after some cooldown delay.

WORKING EXAMPLE

Syntax: Select all

from core import echo_console
from messages import SayText2

from spam_proof_commands.client import ClientCommand
from spam_proof_commands.say import SayCommand
from spam_proof_commands.server import ServerCommand


TEST_TEXT = "You've just executed a command"

test_message = SayText2(message=TEST_TEXT)


# This command will only be executed once every 3 seconds
@SayCommand(3, "!spamtest")
def say_spamtest(command, index, team_only):
test_message.send(index)


# Same with client command
@ClientCommand(3, "spamtest_client")
def client_spamtest_client(command, index):
test_message.send(index)


# And server command
@ServerCommand(3, "spamtest_server")
def server_spamtest_server(command):
echo_console(TEST_TEXT)


Image

Hope somebody finds this useful.
Last edited by iPlayer on Tue May 24, 2016 3:59 pm, edited 1 time in total.
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
Mahi
Senior Member
Posts: 236
Joined: Wed Aug 29, 2012 8:39 pm
Location: Finland

Re: SpamProofCommands

Postby Mahi » Thu May 19, 2016 9:31 am

Good idea and package iPlayer, nice job! There are a few improvements I'd make though.

First of all, you seem to define a common base class in the __init__.py which is then used in the other files of your package. This is rather bad practice, as __init__.py should contain as little as possible. It's often used for stuff like forward importing the "important" classes (important for the target group of your package) from your package. __init__.py will get executed everytime someone imports something from your package. So, I would suggest you to move the stuff in your __init__.py into a "base.py" or similar, and import that in your client, server, and say files.

Next up you could forward import your "important" classes in the __init__.py:

Code: Select all

from .client import ClientCommand
from .say import SayCommand
from .server import ServerCommand
Now when someone wants to use your package, they can still use the "from spam_proof_commands.client import ClientCommand", but they can also shorten it down to "from spam_proof_commands import ClientCommand". You could also leave the __init__.py empty if you don't want forward importing. Most __init__.py files in general are empty, and it's considered really good practice to keep it that way.

An other thing that caught my eye is ANTI_SPAM_TEXT. SP's translations use dictionaries, so the lookup operations take insignificant time and processing power. We're talking about micro seconds here, this is nothing but premature optimization to get rid of an useful feature.

Other than that, it looks really nice :)!
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: SpamProofCommands

Postby iPlayer » Thu May 19, 2016 9:37 am

Thanks for the feedback, Mahi.

I should probably move base class to command.py.
I did not forward any classes from say.py, client.py and server.py to look as close as possible to original SP commands package. It doesn't forward them.

As for translations, I didn't use them not because of retrieving a translated string by language, but because of every time you use translations, SP first things first tries to determine client's language - and it does it by retrieving cl_language cvar from client - thus network overhead.
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image
User avatar
Mahi
Senior Member
Posts: 236
Joined: Wed Aug 29, 2012 8:39 pm
Location: Finland

Re: SpamProofCommands

Postby Mahi » Thu May 19, 2016 9:53 am

Yeah command.py seems like a good idea since you're mimicing SP's commands package. And thus leave the __init__.py empty.

As far as I understand (without super in-depth familiarization to the translations package), the LangStrings class uses dict's normal __getitem__ so there's nothing done with cl_language out there. Messages on the other hand seem to categorize players by language anyways, so there's no noticeable difference between using translations or not. Also, seems like cl_language is cached, so the networking is done only once anyways! :) Translations should be fine as far as I can see.
User avatar
iPlayer
Developer
Posts: 590
Joined: Sat Nov 14, 2015 8:37 am
Location: Moscow
Contact:

Re: SpamProofCommands

Postby iPlayer » Thu May 19, 2016 11:48 am

I've made a new release. Moved contents of __init__.py to command.py, removed __init__.py and added English and Russian translations for the anti-spam message.

Thanks again, Mahi, for your feedback.
Image /id/its_iPlayer
My plugins: Map Cycle • Killstreaker • DeadChat • Infinite Jumping • TripMines • AdPurge • Bot Damage • PLRBots • Entity AntiSpam

Hail, Companion. [...] Hands to yourself, sneak thief. Image

Return to “Custom Packages”

Who is online

Users browsing this forum: No registered users and 6 guests