Skip to content

Implemented has_clip/ammo & has_secondary_fire_clip/ammo to the Weapon class #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,6 @@ def _replace_escaped_sequences(given_string):
# Return the replaced string
return given_string

def get_strings(self, key, **tokens):
"""Return a TranslationStrings object with updated tokens."""
strings = self[key]
strings.tokens.update(tokens)
return strings


class TranslationStrings(dict):
"""Stores and get language strings for a particular string."""
Expand Down Expand Up @@ -250,11 +244,39 @@ def get_string(self, language=None, **tokens):
# Possibly raise an error silently here
return ''

# Update the stored tokens with the given ones
self.tokens.update(tokens)
# Expose all TranslationStrings instances in self.tokens
exposed_tokens = {}
for token_name, token in self.tokens.items():
if isinstance(token, TranslationStrings):

# Pass additional tokens - these will be used to format
# the string
token = token.get_string(language, **tokens)

exposed_tokens[token_name] = token

# Expose all TranslationsStrings instances in **tokens
for token_name, token in tokens.items():
if isinstance(token, TranslationStrings):

# Don't pass any additional tokens, the token should either
# be trivial or rely on itself (self.tokens)
token = token.get_string(language)

exposed_tokens[token_name] = token

# Get the language shortname to be used
language = self.get_language(language)

# Was a valid language found?
if language is None:

# Return an empty string
# Possibly raise an error silently here
return ''

# Return the formatted message
return self[language].format(**self.tokens)
return self[language].format(**exposed_tokens)

def get_language(self, language):
"""Return the language to be used."""
Expand Down Expand Up @@ -291,5 +313,20 @@ def get_language(self, language):
# Return None as the language, as no language has been found
return None

def tokenized(self, **tokens):
"""Create a new TranslationStrings instance and store tokens in it.

:param dict tokens: Tokens to store in the instance.
:return: New TranslationStrings instance with tokens stored in it.
:rtype: TranslationStrings
"""
result = TranslationStrings()
result.tokens.update(tokens)

for key, value in self.items():
result[key] = value

return result

# Get the translations language strings
_translation_strings = LangStrings('_core/translations_strings')
36 changes: 35 additions & 1 deletion addons/source-python/packages/source-python/weapons/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ def _validate_clip(self):
) or self._clip == -1:
raise ValueError('Weapon does not have a clip.')

def has_clip(self):
"""Check if the weapon has a clip."""
try:
self._validate_clip()
except ValueError:
return False
return True

def get_clip(self):
"""Return the amount of ammo in the weapon's clip."""
self._validate_clip()
Expand Down Expand Up @@ -67,6 +75,14 @@ def _validate_ammo(self):

return player

def has_ammo(self):
"""Check if the weapon has a valid ammoprop and an owner."""
try:
self._validate_ammo()
except ValueError:
return False
return True

def get_ammo(self):
"""Return the amount of ammo the player has for the weapon."""
player = self._validate_ammo()
Expand Down Expand Up @@ -98,6 +114,14 @@ def _validate_secondary_fire_clip(self):
if self._secondary_fire_clip == -1:
raise ValueError('Weapon does not have a secondary fire clip.')

def has_secondary_fire_clip(self):
"""Check if the weapon has a secondary fire clip."""
try:
self._validate_secondary_fire_clip()
except ValueError:
return False
return True

def get_secondary_fire_clip(self):
"""Return the amount of ammo in the weapon's secondary fire clip."""
self._validate_secondary_fire_clip()
Expand All @@ -114,7 +138,8 @@ def set_secondary_fire_clip(self, value):
doc="""Property to get/set the weapon's secondary fire clip.""")

def _validate_secondary_fire_ammo(self):
"""Test if the weapon has a valid secondary fire ammoprop and an owner."""
"""Test if the weapon has a valid secondary fire ammoprop
and an owner."""
if self.secondary_fire_ammoprop == -1:
raise ValueError(
'Unable to get secondary fire ammoprop for {0}'.format(
Expand All @@ -126,6 +151,15 @@ def _validate_secondary_fire_ammo(self):

return player

def has_secondary_fire_ammo(self):
"""Check if the weapon has a valid secondary fire ammoprop
and an owner."""
try:
self._validate_secondary_fire_ammo()
except ValueError:
return False
return True

def get_secondary_fire_ammo(self):
"""Return the secondary fire ammo the player has for the weapon."""
player = self._validate_secondary_fire_ammo()
Expand Down