]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/games/uno.rb
plugin(hangman): fixes word generator closes #9
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / games / uno.rb
index 4eab72c2db2ca8b3ceee4935f7b477cd9acface3..df6a1eee1bf040b6f6f7c921ae298c05a56d07c1 100644 (file)
@@ -331,10 +331,10 @@ class UnoGame
     else
       @special = false
     end
-    @must_play = nil
   end
 
   def next_turn(opts={})
+    @must_play = nil
     @players << @players.shift
     @player_has_picked = false
     show_turn unless opts[:silent]
@@ -382,7 +382,7 @@ class UnoGame
   def play_card(source, cards)
     debug "Playing card #{cards}"
     p = get_player(source)
-    shorts = cards.gsub(/\s+/,'').match(/^(?:([rbgy]\+?\d){1,2}|([rbgy][rs])|(w(?:\+4)?)([rbgy])?)$/).to_a
+    shorts = cards.gsub(/\s+/,'').match(/^(?:([rbgy]\+?\d)\1?|([rbgy][rs])|(w(?:\+4)?)([rbgy])?)$/).to_a
     debug shorts.inspect
     if shorts.empty?
       announce _("what cards were that again?")
@@ -498,6 +498,7 @@ class UnoGame
     lp.cards.each do |c|
       if c.color == @last_color and not c.special?
         legal = false
+        break
       end
     end
     if legal
@@ -514,7 +515,7 @@ class UnoGame
       announce _("%{lp}'s move was %{b}not%{b} legal, %{lp} must pick %{b}%{n}%{b} cards and play again!") % {
         :cp => cp, :lp => lp, :b => Bold, :n => @picker
       }
-      lp.cards << @discard # put the W+4 back in place
+      played = @discard # store the misplayed W+4
 
       # reset the discard
       @color = @last_color.dup
@@ -526,7 +527,10 @@ class UnoGame
 
       # force the player to play the current cards
       @must_play = lp.cards.dup
+      # but not the same (type of) card he misplayed, though
+      @must_play.delete(played)
 
+      lp.cards << played # reinstate the W+4 in the list of player cards
       # give him the penalty cards
       deal(lp, @picker)
       @picker = 0
@@ -604,6 +608,12 @@ class UnoGame
   end
 
   def show_turn(opts={})
+    if @players.empty?
+      announce _("nobody is playing %{uno} yet!") % {
+        :uno => UNO
+      }
+      return false
+    end
     cards = true
     cards = opts[:cards] if opts.key?(:cards)
     player = @players.first
@@ -688,6 +698,13 @@ class UnoGame
         return
       end
     end
+    if @last_discard
+      announce _("you can't join now, %{p}, a %{card} was just played, wait until next turn") % {
+        :card => @discard,
+        :p => user
+      }
+      return
+    end
     cards = 7
     if @start_time
       cards = (@players.inject(0) do |s, pl|
@@ -752,11 +769,17 @@ class UnoGame
   def replace_player(old, new)
     # The new user
     user = channel.get_user(new)
-    if p = get_player(user)
+    if not user
+      announce _("there is no '%{nick}' here") % {
+        :nick => new
+      }
+      return false
+    end
+    if pl = get_player(user)
       announce _("%{p} is already playing %{uno} here") % {
-        :p => p, :uno => UNO
+        :p => pl, :uno => UNO
       }
-      return
+      return false
     end
     # We scan the player list of the player with the old nick, instead
     # of using get_player, in case of IRC drops etc
@@ -766,12 +789,13 @@ class UnoGame
         announce _("%{p} takes %{b}%{old}%{b}'s place at %{uno}") % {
           :p => p, :b => Bold, :old => old, :uno => UNO
         }
-        return
+        return true
       end
     end
     announce _("%{b}%{old}%{b} isn't playing %{uno} here") % {
       :uno => UNO, :b => Bold, :old => old
     }
+    return false
   end
 
   def end_game(halted = false)
@@ -810,12 +834,12 @@ class UnoGame
       deal(p, @picker)
       @picker = 0
     end
-    score = @players.inject(0) do |sum, p|
-      if p.cards.length > 0
+    score = @players.inject(0) do |sum, pl|
+      if pl.cards.length > 0
         announce _("%{p} still had %{cards}") % {
-          :p => p, :cards => p.cards.join(' ')
+          :p => pl, :cards => pl.cards.join(' ')
         }
-        sum += p.cards.inject(0) do |cs, c|
+        sum += pl.cards.inject(0) do |cs, c|
           cs += c.score
         end
       end