Looking for suggestions

Discuss API design here.
arawra
Senior Member
Posts: 185
Joined: Fri Jun 21, 2013 6:51 am

Looking for suggestions

Postby arawra » Wed Oct 07, 2020 6:28 pm

For the D&D plugin I am working on, one of the available features is that players can "cast spells". The way that this works is a player binds a ClientCommand and supplies the necessary arguments.

I am working to improve the current system I have because... well its trash. Here is a proposed way I can see this working. I am looking for pointers into how I can improve this, or at least keep it pythonic.

Syntax: Select all

RPGPlayer...

def cast(self, spell : Spell):
spell.cast(self)


class Spell():

spells = []

def __init__(self, name, source, resource, cost):

self.name = name
self.source = source
self.resource = resource
self.cost = cost
self.kwargs = {}

Spell.spells.append(self)

def cast(self, callback, (player : RPGPlayer, *args, **kwargs)):

if player.getClass() != self.source:
if player.getRace() != self.source:
return

if not hasattr(player,self.resource):
return

if not player.spellCooldown >= time.time() - 1.5:
messagePlayer('Your spells are on cooldown!', player.index)
return

if not getattr(player, resource) >= self.cost:
if 'resource' in ['mana']:
messagePlayer('You do not have enough {0} to cast {1}'.format(resource, self.name.title()), player.index)
else:
messagePlayer('You do not have any more uses of {0}'.format(self.name.title()), player.index)
return

if callback(player, args, kwargs):
setattr(player, self.resource, getattr(player, self.resource) - self.cost)



class TargetedSpell(Spell):

def cast(self, callback, *args, **kwargs):

if player.getClass() != self.source:
if player.getRace() != self.source:
return

if not hasattr(player,self.resource):
return

if not player.spellCooldown >= time.time() - 1.5:
messagePlayer('Your spells are on cooldown!', player.index)
return

if not getattr(player, self.resource) >= self.cost:
if self.resource in ['mana']:
messagePlayer('You do not have enough {0} to cast {1}'.format(resource, self.name.title()), player.index)
else:
messagePlayer('You do not have any more uses of {0}'.format(self.name.title()), player.index)
return

target = player.getViewPlayer()
if not target:
return
if target.is_dead():
return

if callback(player, target, args, kwargs):
setattr(player, self.resource, getattr(player, self.resource) - self.cost)

@ClientCommand('!cast')
def cast(command, index):

player = players.from_userid(userid_from_index(index))
spell = None
for s in Spell.spells:
if command.arg_string.lower() == s.name.lower():
spell = s
player.cast(spell)

Return to “API Design”

Who is online

Users browsing this forum: No registered users and 2 guests