I'm in the middle of a very long project developing a league site for the National Collegiate Wrestling Association in my spare time (once finished, could be a great showcase for Craft).
Currently, I'm trying to implement a points system, where athletes accumulate points based on the matches they win & how they win them.
Here's a screenshot of an athletes stat page (wip) http://cl.ly/image/3L2A0e2x1q3v
All the results are tied to an event, so the points can be recalculated easily if an event has to be removed for whatever reason, or a coach inputs the wrong kid by accident.
There is quite a bit of logic being done here, but manageable, and can be cached- but teams also accumulate points based on the points of the top 11 athletes in their roster!
This means the points will have to be calculated and displayed on
- The Team's page (11x the queries)
- The Index page of all Teams
Currently, the only way I know how to do this, will be some heavy nesting of conditionals in loops- repeating the process of each athlete.
I know doing it this way, all in the templates, is extremely unorthodox. I'm 99.9% this would be much easier to implement with plugins somehow, but unfortunately I don't know PHP nor do I have the time to invest in the trial/error learning process of how to write plugins in the time needed. So I'm kinda up against a wall here and looking for solutions to smooth out this process, regardless of how hacky it may be.
Maybe there's a way to only perform these massive queries in the middle of the night, and the points total only updates once per day, no matter how many results are inputted in the 24-hour period? I don't know. Please help me, very kind Craft community :)
Update: Added requested template logic... Removed extra unnecessary code for readability & sake of space. Team pages & Teams Index page won't require all the information displayed in screenshot (date, opponent, score/time, pt diff)
{% set athlete = craft.users.id( craft.request.lastSegment ).first %}
{% set results = craft.entries.section('results').relatedTo({
targetElement: athlete,
field: 'matches',
}) %}
{% set points = 0 %}
{% if results|length %}
{% for result in results %}
{% for block in result.matches %}
{% if (athlete.id == block.winner.first.id) or (athlete.id == block.loser.first.id) %}
{% if block.winner|length and (athlete.id == block.winner.first.id) %}
{% if (block.result == "Maj") or (block.result == "Dec") %}
{% set points = (points + 5) %}
{% set difference = "+5" %}
{% else %}
{% set points = (points + 6.1) %}
{% set difference = "+6.1" %}
{% endif %}
{% endif %}
{% if block.loser|length and (athlete.id == block.loser.first.id) %}
{% if athlete.id == block.loser.first.id %}
Lost by {{ block.result.label }}
{% set points = points - 1.75 %}
{% set difference = "-1.75" %}
{% set negative %}{% endset %}
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
{% endfor %}
{% endif %}
{% set tournaments = craft.entries.section('results').relatedTo({
targetElement: athlete,
}) %}
{% if tournaments|length %}
{% for tournament in tournaments %}
{% if tournament.type.handle == 'minorTournament' %}
{% for w in 1..3 %}
{% for p in 1..3 %}
{% if (tournament['w'~w~'p'~p]|length) and (tournament['w'~w~'p'~p].first.id == athlete.id) %}
{% switch loop.index %}
{% case "1" %}
{% set points = (points + 6) %}
{% case "2" %}
{% set points = (points + 4) %}
{% case "3" %}
{% set points = (points + 2) %}
{% endswitch %}
{% endif %}
{% endfor %}
{% endfor %}
{% endif %}
{% endfor %}
{% endif %}
Points:>{{points}}