Skip to content

Commit

Permalink
Now displays status of current war.
Browse files Browse the repository at this point in the history
  • Loading branch information
AaronTraas committed Jan 18, 2019
1 parent fb958f2 commit 6468681
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 8 deletions.
2 changes: 1 addition & 1 deletion crtools/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "1.9.4"
__version__ = "1.10.0"
11 changes: 11 additions & 0 deletions crtools/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
import urllib.parse



class ClashRoyaleAPIError(Exception):
""""""
pass

class ClashRoyaleAPIMissingFieldsError(Exception):
""""""
pass

class ClashRoyaleAPIAuthenticationError(Exception):
"""Failed to authenticate with the API. Key is likely bad."""
pass
Expand All @@ -31,6 +36,12 @@ class ClashRoyaleAPI:
debug = False

def __init__(self, api_key, clan_tag, debug=False):
if api_key == False:
raise ClashRoyaleAPIMissingFieldsError('API key not provided.');

if clan_tag == False:
raise ClashRoyaleAPIMissingFieldsError('Clan tag not provided.');

self.api_key = api_key
self.clan_tag = clan_tag
self.debug = debug
Expand Down
49 changes: 43 additions & 6 deletions crtools/crtools.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import shutil
import tempfile

from .api import ClashRoyaleAPI, ClashRoyaleAPIError, ClashRoyaleAPIAuthenticationError, ClashRoyaleAPIClanNotFound
from .api import ClashRoyaleAPI, ClashRoyaleAPIError, ClashRoyaleAPIMissingFieldsError, ClashRoyaleAPIAuthenticationError, ClashRoyaleAPIClanNotFound
from ._version import __version__

ARENA_LEAGUE_LOOKUP = {
Expand Down Expand Up @@ -344,9 +344,40 @@ def process_clan(config, clan, current_war):

return clan_processed

def process_current_war(config, current_war):
current_war_processed = current_war.copy()

print(current_war_processed['state'])
if current_war_processed['state'] == 'notInWar':
current_war_processed['stateLabel'] = 'The clan is not currently engaged in a war.'
else:
cards = 0;
for member in current_war_processed['participants']:
cards += member['cardsEarned']
current_war_processed['cards'] = cards

now = datetime.utcnow()
if current_war_processed['state'] == 'collectionDay':
current_war_processed['stateLabel'] = 'Collection Day'

collection_end_time = datetime.strptime(current_war_processed['collectionEndTime'].split('.')[0], '%Y%m%dT%H%M%S')
collection_end_time_delta = math.floor((collection_end_time - now).seconds / 3600)
current_war_processed['collectionEndTimeLabel'] = '{} hours'.format(collection_end_time_delta)
current_war_processed['endLabel'] = '1 day, {} hours'.format(collection_end_time_delta)
else:
current_war_processed['stateLabel'] = 'War Day'

end_time = datetime.strptime(current_war_processed['warEndTime'].split('.')[0], '%Y%m%dT%H%M%S')
end_time_delta = math.floor((end_time - now).seconds / 3600)
current_war_processed['collectionEndTimeLabel'] = 'Complete'
current_war_processed['endLabel'] = '{} hours'.format(collection_end_time_delta)

return current_war_processed

def build_dashboard(config):
"""Compile and render clan dashboard."""

debug_out(config, 'crtools version v{}'.format(__version__))
debug_out(config, config)

# Putting everything in a `try`...`finally` to ensure `tempdir` is removed
Expand Down Expand Up @@ -407,6 +438,7 @@ def build_dashboard(config):

clan_processed = process_clan(config, clan, current_war)
members_processed = process_members(config, clan, warlog, current_war)
current_war_processed = process_current_war(config, current_war)

# Create environment for template parser
env = Environment(
Expand All @@ -423,6 +455,7 @@ def build_dashboard(config):
war_labels = warlog_labels(warlog, clan['tag']),
clan = clan_processed,
clan_hero = clan_hero_html,
current_war = current_war_processed,
suggestions = get_suggestions(config, members_processed),
scoring_rules = get_scoring_rules(config)
)
Expand All @@ -447,11 +480,12 @@ def build_dashboard(config):
if(config['crtools']['debug'] == True):
log_path = os.path.join(tempdir, 'log')
os.makedirs(log_path)
write_object_to_file(os.path.join(log_path, 'clan.json'), clan)
write_object_to_file(os.path.join(log_path, 'warlog.json'), warlog)
write_object_to_file(os.path.join(log_path, 'currentwar.json'), current_war)
write_object_to_file(os.path.join(log_path, 'clan-processed.json'), clan_processed)
write_object_to_file(os.path.join(log_path, 'members-processed.json'), members_processed)
write_object_to_file(os.path.join(log_path, 'clan.json'), clan)
write_object_to_file(os.path.join(log_path, 'warlog.json'), warlog)
write_object_to_file(os.path.join(log_path, 'currentwar.json'), current_war)
write_object_to_file(os.path.join(log_path, 'clan-processed.json'), clan_processed)
write_object_to_file(os.path.join(log_path, 'members-processed.json'), members_processed)
write_object_to_file(os.path.join(log_path, 'currentwar-processed.json'), current_war_processed)

output_path = os.path.expanduser(config['paths']['out'])
if os.path.exists(output_path):
Expand Down Expand Up @@ -501,6 +535,9 @@ def build_dashboard(config):
except ClashRoyaleAPIError as e:
print('developer.clashroyale.com error: {}'.format(e))

except ClashRoyaleAPIMissingFieldsError as e:
print('error: {}'.format(e))

finally:
# Ensure that temporary directory gets deleted no matter what
shutil.rmtree(tempdir)
54 changes: 53 additions & 1 deletion crtools/static/crtools.css
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ select {
margin: 0;
text-transform: none;

border: 1px solid #ccc;
border-right: 1px solid #E1E1E1;
height: 34px;
}

Expand Down Expand Up @@ -339,6 +339,58 @@ img.war-league {
padding-bottom: 0;
}

/* Current war region
–––––––––––––––––––––––––––––––––––––––––––––––––– */
#members article#current-war {
margin-bottom: 66px;
}

#current-war dl {
display: flex
}

#current-war dl > * {
flex: 1;
border-right: 1px solid #E1E1E1;
}

#current-war dl > *:last-child {
border-right: none;
}

#current-war dl dt,
#current-war dl dd {
display: block;
text-align: center;
}

#current-war .cards dd:before,
#current-war .crowns dd:before,
#current-war .warDay dd:before,
#current-war .collectionDay dd:before {
content: '';
display: inline-block;
height: 1em;
width: 1em;
background-repeat: no-repeat;
background-position: center;
background-size: contain;
margin: 0 .3em -.1em 0;
}

#current-war .crowns dd:before {
background-image: url(images/crown-gold.svg);
}

#current-war .cards dd:before,
#current-war .collectionDay dd:before {
background-image: url(images/cards.svg);
}

#current-war .warDay dd:before {
background-image: url(images/war-battle-win.svg);
}

/* Member list section
–––––––––––––––––––––––––––––––––––––––––––––––––– */
#members header {
Expand Down
15 changes: 15 additions & 0 deletions crtools/templates/page.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,21 @@
</div>
</section>
<section id="members">
<article id="current-war">
<header>
<h2>Current war:</h2>
</header>
<dl>
<div class="{{current_war.state}}"><dt>War State</dt><dd>{{current_war.stateLabel}}</dd></div>
{% if current_war.state in ['collectionDay', 'warDay'] %}
<div><dt>Collection Day End</dt><dd>{{current_war.collectionEndTimeLabel}}</dd></div>
<div><dt>War End</dt><dd>{{current_war.endLabel}}</dd></div>
<div><dt>Participants</dt><dd>{{current_war.clan.participants}}</dd></div>
<div class="cards"><dt>Cards Earned</dt><dd>{{current_war.cards}}</dd></div>
<div class="crowns"><dt>Crowns</dt><dd>{{current_war.clan.crowns}}</dd></div>
{% endif %}
</dl>
</article>
<article>
<header>
<h2>Member list <span class="no-mobile">and stats</span></h2>
Expand Down

0 comments on commit 6468681

Please sign in to comment.