Commits

Alexander Schremmer committed a0ee5d6

Implemented options, added option to play money cards only when desired, made TreasureCards optional.

Comments (0)

Files changed (8)

domination/cards/__init__.py

     abstract = True    # abstract template?
     trash_after_playing = False  # does it go to trash after playing?
     durationaction_activated = False # Seaside duration cards
+    wants_money_selection = False
     __slots__ = ()
 
     def __init__(self):
         gen = game.fire_hook("on_gain_card", game, player, card)
         generator_forward(gen)
 
+    @classmethod
+    def on_setup_card(cls, game):
+        if [c for c in game.selected_cards if c.wants_money_selection]:
+            game.player_options["automatic_money_selection"] = _("Play all available treasure cards")
+
 
 class ActionCard(Card):
     optional = True
     abstract = True
     worth = None
     classname = _("Treasure card")
+    wants_money_selection = True
+    optional = True
 
 
 class CurseCard(Card):

domination/cards/base.py

     edition = BaseGame
     cost = 0
     worth = 1
+    wants_money_selection = False
+    optional = False
 
 class Silver(TreasureCard):
     name = _("Silver")
     edition = BaseGame
     cost = 3
     worth = 2
+    wants_money_selection = False
+    optional = False
 
 class Gold(TreasureCard):
     name = _("Gold")
     edition = BaseGame
     cost = 6
     worth = 3
+    wants_money_selection = False
+    optional = False
 
 class Estate(VictoryCard):
     name = _("Estate")

domination/cards/hinterlands.py

             for val in game.check_empty_pile(card_cls.__name__):
                 yield val
 
-class FoolsGold(TreasureCard,ReactionCard):
+class FoolsGold(TreasureCard, ReactionCard):
     name = _("Fool's Gold")
     edition = Hinterlands
     implemented = False #FIXME Second half of the action should be triggered when card is gained.

domination/cards/prosperity.py

     edition = Prosperity
     cost = 9
     worth = 5
+    wants_money_selection = False
+    optional = False
 
 class Colony(VictoryCard):
     name = _("Colony")

domination/gameengine.py

 from random import SystemRandom
 from threading import Thread, local
 from threading import _Condition as PristineCondition
+from collections import defaultdict
 
 from domination.tools import _, taint_filename
 from domination.macros.__macros__ import generator_forward, generator_forward_ex
 TLS = local()
 
 
+def make_false():
+    return False
+
 class Condition(PristineCondition):
     def __getstate__(self):
         return True
 
 class SelectHandCards(MultipleChoicesRequestMixin, Request):
     def __init__(self, game, player, msg, cls=None, count_lower=0, count_upper=None,
-                 not_selectable=()):
+                 not_selectable=(), preselect_all=False):
         Request.__init__(self, game, player, msg)
         self.cls = cls
         self.count_lower = count_lower
         self.count_upper = count_upper
         self.not_selectable = not_selectable
+        self.preselect_all = preselect_all
 
         count = self.count_lower
         if self.count_upper is not None:
         self.cards_to_draw = 5
         self._hooks = {}
         self.prepare_hooks(selected_cards)
+        self.player_options = {}
 
     def __hash__(self):
         return hash(self.name)
         self.supply.setdefault(cls.__name__, []).extend(cls() for _ in xrange(no))
 
     def play_action_card(self, player, card):
+        player.activated_cards.append(card)
+        try:
+            gen = card.activate_action(self, player)
+        except NotImplementedError:
+            return
         for other_player in self.participants:
             if other_player is not player:
                 yield InfoRequest(self, other_player,
                         _("%s plays:", (player.name, )), [card])
 
-        player.activated_cards.append(card)
-        gen = card.activate_action(self, player)
         generator_forward(gen)
 
     def kick(self, kicker, kickee):
         del self.players[self.players.index(kickee)]
 
     def play_round(self):
+        from domination.cards import TreasureCard
+
         self.pending_round_players = players = self.players[:]
         while players:
             player = players.pop(0)
                         gen = self.play_action_card(player, card)
                         generator_forward(gen)
 
+                    # select money cards
+                    if not player.options["automatic_money_selection"]:
+                        cards = (yield SelectHandCards(self, player, _("Which money cards do you want to play?"), TreasureCard,
+                            preselect_all=True))
+                    else:
+                        cards = [c for c in player.hand if isinstance(c, TreasureCard)]
+                    player.aux_cards.extend(cards)
+                    for card in cards:
+                        player.hand.remove(card)
+                    for card in cards:
+                        gen = self.play_action_card(player, card)
+                        generator_forward(gen)
+                    player.activated_treasure_cards = cards
+
                     # deal
                     break_selection = False
                     while player.remaining_deals and not break_selection:
         self.kicked_by = None
         self.turn_cleanups = []
         self.duration_cards = [] # duration_cards from seaside
+        self.options = defaultdict(make_false)
+        self.activated_treasure_cards = None
 
         self.request_queue = []
         self.info_queue = []
         self.activated_cards = []
         self.current = False
         self.turn_cleanups = []
+        self.activated_treasure_cards = None
         if exc_type is PlayerKickedException:
             return True
 
 
     @property
     def remaining_money(self):
-        return self.virtual_money + sum(card.get_worth(self) for card in self.hand)\
+        return self.virtual_money + sum(card.get_worth(self) for card in self.activated_treasure_cards)\
                 - self.used_money
 
     @property

domination/main.py

     return jsonify()
 
 
+@app.route("/game/toggle_option/<name>")
+@needs_login
+@gets_game
+def toggle_option(game_runner):
+    key = request.args["optionkey"]
+    value = request.args["optionvalue"].lower() == "true"
+    player = get_store()["games"][game_runner.game]
+    player.options[key] = value
+    return jsonify()
+
+
 @app.before_request
 def before_request():
     if request.authorization and app.auth_enabled:

domination/static/game.js

       }
   });
 }
+
+function register_handle_option_click() {
+  $(".playeroption").click(function () {
+    $.getJSON($SCRIPT_ROOT + '/game/toggle_option/' + game_name, {optionkey: this.name, optionvalue: this.checked});
+  });
+}
+

domination/templates/game.html

 {% block title %}{% trans %}Game: {% endtrans %}{{ game.name }}{% endblock %}
 {% block head %}
   {{ super() }}
+  <script type="text/javascript">
+    var game_name = {{ runner.game.name | tojson | safe }};
+  </script>
   {% if not req %}
   <script type="text/javascript">
     var initial_seqno = {{ seqno | tojson | safe }};
-    var game_name = {{ runner.game.name | tojson | safe }};
     $(handle_page_refresh);
   </script>
   {% endif %}
     {% if req %}
       your_turn_reminder();
     {% endif %}
+    $(register_handle_option_click);
     $(function() {
         {% if req %}
         var offset = 2;
               {% endif %}
               </form>
             {% else %}
-              <input type="checkbox" name="card" value="{{ loop.index0 }}">
+              <input type="checkbox" name="card" value="{{ loop.index0 }}" {{ "checked" if req.preselect_all else "" }}>
             {% endif %}
           {% endif %}
         {% endcall %}
       {% endif %}
     {% else %}
       {% if player.activated_cards %}
-      <h3>{% trans %}Activated action cards{% endtrans %}</h3>
+      <h3>{% trans %}Played cards{% endtrans %}</h3>
         {% for card in player.activated_cards %}
           {{ macros.render_card(card, game, player) }}
         {% endfor %}
         {% endfor %}
       </div>
     </div>
-  {% endif%}
+  {% endif %}
+  <div id="options"><form>
+    {% for key, optiontext in game.player_options.items() %}
+      <input type="checkbox" {{ ["", "checked"][player.options[key]] }} class="playeroption" name="{{ key }}"> {{ optiontext }}    
+    {% endfor %}
+  </form></div>
 {% endblock %}