Source

domination / domination / templates / create_game.html

Full commit
{% extends "base.html" %}
{% block title %}{% trans %}Create new game{% endtrans %}{% endblock %}
{% block head %}
  {{ super() }}
  <script type="text/javascript">
    $(function() {
      $("input[name=edition]").change(update_sets);
      update_sets();
    });
    var update_sets = function() {
      $("input[name=edition]").each(function(i) {
        var key = $(this).attr("value");
        if (! $(this).attr("checked")) {
          $(".edition-" + key).parent().hide()
                              .find("input").attr("checked", false);
          $(".needs-" + key).hide();
        } else {
          $(".edition-" + $(this).attr("value")).parent().show();
          // XXX convoluted loop to make sure only valid sets are visible
          $(".needs-" + key).each(function(i) {
            var enable = true;
            var classes = $(this).attr("class").split(/\s+/);
            for (cls in classes) {
              if (classes[cls] == "") continue;
              if (! $("input[name=edition][value=" +
                      classes[cls].substr(6) + "]").attr("checked")) {
                enable = false;
                break;
              }
            }
            if (enable)
              $(this).show();
          });
        }
      });
      update_button();
    };
    var update_button = function() {
      var button = $("input[type='submit']")[0];
      var checkmarks = $("input[name=card_key]:checked").size();
      button.disabled = (checkmarks != 10);
      $("#noofcards").text(checkmarks);
    };
    $(function() {
      $("input[name=card_key]").change(update_button);
      update_button();
    });
    function select_set(cards) {
      $("input[name=card_key]:checked").attr("checked", false);
      $.each(cards, function(index, value) {
        var elems = $("input[name=card_key][value='" + value + "']");
        if (elems.attr("disabled"))
          return;
        elems.attr("checked", true);
      });
      update_button();
    }
    function select_random_set() {
      $("input[name=card_key]:checked").attr("checked", false);
      $("div.card:visible").children("input[name=card_key][disabled!='disabled']")
                           .random(10).each(function() {
          $(this).attr("checked", true);
      });
      update_button();
    }
  </script>
  {{ macros.init_render_card() }}
{% endblock %}
{% block content %}
  <h2>{% trans %}Create new game{% endtrans %}</h2>
  <form action="" method="post">
    <p>{% trans %}Name of Game: {% endtrans %}<input type="text" name="name" value="{{ name }}"></p>
    <p><input type="checkbox" name="ai" value="yes">{% trans %}Add
      <input type="text" name="numai" value="1" maxlength="1" size="3"> AI player(s){% endtrans %}</p>
    <p>{% trans %}Editions:{% endtrans %}
      {% for edition in editions %}
      <input type="checkbox" name="edition" value="{{ edition.key }}"
      {% if not edition.optional %}checked="checked" disabled="disabled"{% else %}{% if edition.name in enabled_editions %}checked="checked"{% endif %}{% endif %}>
      {{ edition.name }}
      {% endfor %}
    </p>
    <p>{% trans %}Sets:{% endtrans %}
      <a href="javascript:;" onclick='select_set([])'>{% trans %}Empty{% endtrans %}</a>
      <a href="javascript:;" onclick='select_random_set()'>{% trans %}Random{% endtrans %}</a>
      {% for set, cards in card_sets %}
        <a href="javascript:;" class="{% for ed in set.editions %}needs-{{ ed.key }} {% endfor %}"
           onclick='select_set({{cards | tojson | safe}})'>{{ set.name }}</a>
      {% endfor %}

    <p>{% trans %}Which 10 cards? You selected <span id="noofcards"></span> card(s).{% endtrans %}</p>
      {% for edition, cards in app.card_classes() | groupby("edition") %}
        <h3>{{ edition.name }}<span class="edition-{{ edition.key }}"></span></h3>
        {% for cls in cards %}
          {% call macros.render_card(cls) %}
            <span id="card-{{ cls.__name__ }}"></span>
            <span class="edition-{{ cls.edition.key }}"></span>
            <input type="checkbox" name="card_key" value="{{ cls.__name__ }}"
            {% if cls.__name__ in enabled_cards %}checked="checked"{% endif %}>
          {% endcall %}
        {% endfor %}
      {% endfor %}
    <p><input type="submit" value="{% trans %}Create game{% endtrans %}"></p>
  </form>
{% endblock %}