]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/games/uno.rb
uno plugin: fix check for card presence
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / games / uno.rb
index 13f39c9b5992d3445c3484d0e554419215cdb129..fdedc1447f7153cbb28762e983ec968a0b3b5a1d 100644 (file)
@@ -86,6 +86,16 @@ class UnoGame
     def special?
       SPECIALS.include?(@value)
     end
+
+    def <=>(other)
+      cc = self.color <=> other.color
+      if cc == 0
+        return self.value.to_s <=> other.value.to_s
+      else
+        return cc
+      end
+    end
+    include Comparable
   end
 
   # Wild, Wild +4 cards
@@ -116,14 +126,14 @@ class UnoGame
       @cards = []
     end
     def has_card?(short)
-      cards = []
+      has = []
       @cards.each { |c|
-        cards << c if c.shortform == short
+        has << c if c.shortform == short
       }
-      if cards.empty?
+      if has.empty?
         return false
       else
-        return cards
+        return has
       end
     end
     def to_s
@@ -253,8 +263,7 @@ class UnoGame
     end
     if card.picker > 0
       @picker += card.picker
-    else
-      @picker = 0
+      @last_picker = @discard.picker
     end
     if card.special?
       @special = true
@@ -271,13 +280,11 @@ class UnoGame
 
   def can_play(card)
     # When a +something is online, you can only play
-    # a +something of same or higher something, or a Reverse
+    # a +something of same or higher something, or a Reverse of
+    # the correct color
     # TODO make optional
     if @picker > 0
-      if card.color and card.color != @color
-        return false
-      end
-      if card.value == 'Reverse' or card.picker >= @discard.picker
+      if (card.value == 'Reverse' and card.color == @color) or card.picker >= @last_picker
         return true
       else
         return false
@@ -345,8 +352,10 @@ class UnoGame
           announce _("%{p}, choose a color with: co r|b|g|y") % { :p => p }
         end
       else
-        announce _("you don't have that card")
+        announce _("you don't have two cards of that kind")
       end
+    else
+      announce _("you don't have that card")
     end
   end
 
@@ -383,7 +392,9 @@ class UnoGame
       announce _('what color is that?')
       return
     end
-    announce _('color is now %{c}') % { :c => @color.downcase }
+    announce _('color is now %{c}') % {
+      :c => UnoGame.irc_color_bg(@color)+" #{@color} "
+    }
     next_turn
   end
 
@@ -467,8 +478,10 @@ class UnoGame
         end
       end
     end
+    picked.sort!
     notify player, _("You picked %{picked}") % { :picked => picked.join(' ') }
     player.cards += picked
+    player.cards.sort!
   end
 
   def add_player(user)
@@ -491,11 +504,11 @@ class UnoGame
       :uno => UNO, :p => @players.first
     }
     if @picker > 0
-      p = @player[1]
+      p = @players[1]
       announce _("%{p} has to pick %{b}%{n}%{b} cards!") % {
         :p => p, :n => @picker, :b => Bold
       }
-      deal(@player[1], @picker)
+      deal(p, @picker)
       @picker = 0
     end
     score = @players.inject(0) do |sum, p|
@@ -546,8 +559,10 @@ class UnoPlugin < Plugin
     g = @games[m.channel]
     case m.plugin.intern
     when :jo # join game
+      return if m.params
       g.add_player(m.source)
     when :pe # pick card
+      return if m.params
       if g.has_turn?(m.source)
         if g.player_has_picked
           m.reply _("you already picked a card")
@@ -560,6 +575,7 @@ class UnoPlugin < Plugin
         m.reply _("It's not your turn")
       end
     when :pa # pass turn
+      return if m.params
       if g.has_turn?(m.source)
         g.pass(m.source)
       else
@@ -567,7 +583,7 @@ class UnoPlugin < Plugin
       end
     when :pl # play card
       if g.has_turn?(m.source)
-        g.play_card(m.source, m.params)
+        g.play_card(m.source, m.params.downcase)
       else
         m.reply _("It's not your turn")
       end
@@ -578,17 +594,22 @@ class UnoPlugin < Plugin
         m.reply _("It's not your turn")
       end
     when :ca # show current cards
+      return if m.params
       g.show_all_cards(m.source)
     when :cd # show current discard
+      return if m.params
       g.show_discard
     # TODO
     # when :ch
     #   g.challenge
     when :od # show playing order
+      return if m.params
       g.show_order
     when :ti # show play time
+      return if m.params
       g.show_time
     when :tu # show whose turn is it
+      return if m.params
       if g.has_turn?(m.source)
         m.nickreply _("it's your turn, sleepyhead")
       else