Customize Decorator Class

Please post any questions about developing your plugin here. Please use the search function before posting!
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Customize Decorator Class

Postby decompile » Fri Oct 20, 2017 12:04 am

Hey,

I'm currently building a project and which has different classes and I want to add "class" shortcuts for SayCommands and so more.

Instead of adding 10 SayCommands with each shortcut, and splitting the command[0] to "guess" the meant class, I thought theres probably an easier and nicer way to do that.

How can I modify or make a better decorator class?

For Example I can give 3 classes:
Knight = kt
Magician = mn
Acrobat = at

Usually I would do stuff like:

Syntax: Select all

@SayCommand('top10')
@SayCommand('kttop10')
@SayCommand('mntop10')
@SayCommand('attop10')
def command_top10(command, index, team_only):
split_command = command[0].split('top10')
if len(split_command) > 1:
# Used a class as prefix
class = get_class_from_prefix(split_command[0])

else:
# Default / Current Class
class = get_class_from_index(index)
print('Used class:' + class)


So something nicer would be:

Syntax: Select all

@ClassSayCommand('top10')
def command_top10(command, index, team_only, class)
print('Used class:' + class)
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: Customize Decorator Class

Postby L'In20Cible » Fri Oct 20, 2017 1:06 am

Why not delegating to an handler function?

Syntax: Select all

# ============================================================================
# >> IMPORTS
# ============================================================================
# Python Imports
# Enum
from enum import Enum

# Source.Python Imports
# Commands
from commands.say import SayCommand
# Players
from players.entity import Player


# ============================================================================
# >> ENUMERATORS
# ============================================================================
class PlayerClass(Enum):
KNIGHT = 0
MAGICIAN = 1
ACROBAT = 2


# ============================================================================
# >> COMMANDS
# ============================================================================
@SayCommand('top10')
def _top10(command, index, say_team):
handle_top10_command(Player(index), get_class_from_index(index))


@SayCommand('kttop10')
def _kttop10(command, index, say_team):
handle_top10_command(Player(index), PlayerClass.KNIGHT)


@SayCommand('mntop10')
def _mntop10(command, index, say_team):
handle_top10_command(Player(index), PlayerClass.MAGICIAN)


@SayCommand('attop10')
def _attop10(command, index, say_team):
handle_top10_command(Player(index), PlayerClass.ACROBAT)


# ============================================================================
# >> FUNCTIONS
# ============================================================================
def handle_top10_command(player, player_class):
print(f'Handling top10 command of class {player_class} for {player.name}')

It might seems redundant, but this is common practice and I believe it is the most readable approach. Complex decorator with nested callback only to save few lines of codes doesn't make it more clean in my opinion. I would personally go with that approach, in your situation but if you absolutely want a decorator I will see to post a working code tomorrow.
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Re: Customize Decorator Class

Postby decompile » Fri Oct 20, 2017 2:50 pm

Hey,

I was thinking about this too last night, but if you say this is the most readable approach, where I agree, I'll go with it.


Thank you for the help!

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 29 guests