X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=data%2Frbot%2Fplugins%2Fgames%2Funo.rb;h=df6a1eee1bf040b6f6f7c921ae298c05a56d07c1;hb=c4e7896a87d97988926d3b57f62599384c5c7189;hp=131f0673c09f56219df19a4d25b86508eca3cf52;hpb=5a6c7f42cd079fe52d0927004148081fa8e939b1;p=user%2Fhenk%2Fcode%2Fruby%2Frbot.git diff --git a/data/rbot/plugins/games/uno.rb b/data/rbot/plugins/games/uno.rb index 131f0673..df6a1eee 100644 --- a/data/rbot/plugins/games/uno.rb +++ b/data/rbot/plugins/games/uno.rb @@ -108,7 +108,7 @@ class UnoGame @color = 'Wild' raise if value and not value == '+4' if value - @value = value.dup + @value = value.dup @shortform = 'w'+value else @value = nil @@ -160,6 +160,9 @@ class UnoGame # number of cards to be picked if the player can't play an appropriate card attr_reader :picker + # game start time + attr :start_time + # the IRC user that created the game attr_accessor :manager @@ -208,6 +211,13 @@ class UnoGame @bot.notice player.user, msg, opts end + def notify_error(player, msg, opts={}) + announce _("you can't do that, %{p}") % { + :p => player.user + } + notify player, msg, opts + end + def make_base_stock @base_stock = COLORS.inject([]) do |list, clr| VALUES.each do |n| @@ -234,6 +244,7 @@ class UnoGame end def start_game + @join_timer = nil debug "Starting game" @players.shuffle! show_order @@ -320,27 +331,41 @@ 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 + show_turn unless opts[:silent] end def can_play(card) # if play is forced, check against the only allowed cards return false if @must_play and not @must_play.include?(card) - # When a +something is online, you can only play a +something of same or - # higher something, or a Reverse of the correct color, or a Reverse on - # a Reverse - # TODO make optional if @picker > 0 - return true if card.picker >= @last_picker - return true if card.value == 'Reverse' and (card.color == @color or @discard.value == card.value) - return false + # During a picker run (i.e. after a +something was played and before a + # player is forced to pick) you can only play pickers (+2, +4) and + # Reverse. Reverse can be played if the previous card matches by color or + # value (as usual), a +4 can always be played, a +2 can be played on a +2 + # of any color or on a Reverse of the correct color unless a +4 was + # played on it + # TODO make optional + case card.value + when 'Reverse' + # Reverse can be played if it matches color or value + return (card.color == @color) || (@discard.value == card.value) + when '+2' + return false if @last_picker > 2 + return true if @discard.value == card.value + return true if @discard.value == 'Reverse' and @color == card.color + return false + when '+4' + return true + else + return false + end else # You can always play a Wild return true if Wild === card @@ -357,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?") @@ -373,14 +398,28 @@ class UnoGame toplay = (full == short) ? 1 : 2 end debug [full, short, jolly, jcolor, toplay].inspect - # r7r7 -> r7r7, r7, nil, nil - # r7 -> r7, r7, nil, nil - # w -> w, nil, w, nil - # wg -> wg, nil, w, g + # r7r7 -> r7r7, r7, nil, nil, 2 + # r7 -> r7, r7, nil, nil, 1 + # w -> w, nil, w, nil, 1 + # wg -> wg, nil, w, g, 1 + + # if @color is nil, the player just played a wild without specifying + # a color. (s)he should now use "co ", but we allow him to + # replay the wild _and_ specify the color, without actually replaying + # the card (which would otherwise happen if the player has another wild) + if @color.nil? + if jcolor + choose_color(p.user, jcolor) + else + announce _("you already played your card, ") + _("%{p}, choose a color with: co r|b|g|y") % { :p => p } + end + return + end + if cards = p.has_card?(short) debug cards unless can_play(cards.first) - announce _("you can't play that card") + notify_error p, _("you can't play that card") return end if cards.length >= toplay @@ -394,7 +433,7 @@ class UnoGame # only be possible if the first W+4 was illegal, so it wouldn't # apply for a W+4 played on a +2 anyway. # - if @picker == 0 and Wild === cards.first and cards.first.value + if @picker == 0 and Wild === cards.first and cards.first.value # save the previous discard in case of challenge @last_discard = @discard.dup # save the color too, in case it was a Wild @@ -434,10 +473,10 @@ class UnoGame announce _("%{p}, choose a color with: co r|b|g|y") % { :p => p } end else - announce _("you don't have two cards of that kind") + notify_error p, _("you don't have two cards of that kind") end else - announce _("you don't have that card") + notify_error p, _("you don't have that card") end end @@ -459,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 @@ -475,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 @@ -487,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 @@ -505,6 +548,10 @@ class UnoGame } deal(p, @picker) @picker = 0 + # make sure that if this is the "pick and pass" after a W+4, + # then the following player cannot do a challenge: + @last_discard = nil + @last_color = nil else if @player_has_picked announce _("%{p} passes turn") % { :p => p } @@ -517,6 +564,13 @@ class UnoGame end def choose_color(user, color) + # you can only pick a color if the current color is unset + if @color + announce _("you can't pick a color now, %{p}") % { + :p => get_player(user) + } + return + end case color when 'r' @color = 'Red' @@ -554,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 @@ -562,7 +622,7 @@ class UnoGame end def has_turn?(source) - @players.first.user == source + @start_time && (@players.first.user == source) end def show_picker @@ -582,6 +642,7 @@ class UnoGame def show_user_cards(player) p = Player === player ? player : get_player(player) + return unless p notify p, _('Your cards: %{cards}') % { :cards => p.cards.join(' ') } @@ -637,11 +698,18 @@ 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| + cards = (@players.inject(0) do |s, pl| s +=pl.cards.length - end/@players.length + end*1.0/@players.length).ceil end p = Player.new(user) @players << p @@ -674,11 +742,18 @@ class UnoGame } case @players.length when 2 + if @join_timer + @bot.timer.remove(@join_timer) + announce _("game start countdown stopped") + @join_timer = nil + end if p == @players.first - next_turn + next_turn :silent => @start_time.nil? + end + if @start_time + end_game + return end - end_game - return when 1 end_game(true) return @@ -694,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 @@ -708,16 +789,22 @@ 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) runtime = @start_time ? Time.now - @start_time : 0 + if @join_timer + @bot.timer.remove(@join_timer) + announce _("game start countdown stopped") + @join_timer = nil + end if halted if @start_time announce _("%{uno} game halted after %{time}") % { @@ -736,19 +823,23 @@ class UnoGame } end if @picker > 0 and not halted - p = @players[1] + if @discard.value == 'Reverse' + p = @players.last + else + p = @players[1] + end announce _("%{p} has to pick %{b}%{n}%{b} cards!") % { :p => p, :n => @picker, :b => Bold } 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 @@ -816,8 +907,14 @@ class UnoPlugin < Plugin _("The points won with a game of %{uno} are totalled from the cards remaining in the hands of the other players."), _("Each normal (not special) card is worth its face value (from 0 to 9 points)."), _("Each colored special card (+2, Reverse, Skip) is worth 20 points."), - _("Each Wild and Wild +4 is worth 50 points.") + _("Each Wild and Wild +4 is worth 50 points."), + help(plugin, 'top'), + help(plugin, 'topwin'), ].join(" ") % { :uno => UnoGame::UNO } + when 'top' + _("You can see the scoring table with 'uno top N' where N is the number of top scores to show.") + when 'topwin' + _("You can see the winners table with 'uno topwin N' where N is the number of top winners to show.") when /cards?/ [ _("There are 108 cards in a standard %{uno} deck."), @@ -834,7 +931,7 @@ class UnoPlugin < Plugin _("'uno end' to end the game before its natural completion") ].join("; ") else - _("%{uno} game. !uno to start a game. see 'help uno rules' for the rules, 'help uno admin' for admin commands. In-game commands: %{cmds}.") % { + _("%{uno} game. !uno to start a game. see 'help uno rules' for the rules, 'help uno admin' for admin commands, 'help uno score' for scoring rules. In-game commands: %{cmds}.") % { :uno => UnoGame::UNO, :cmds => help(plugin, 'commands') } @@ -843,7 +940,9 @@ class UnoPlugin < Plugin def message(m) return unless @games.key?(m.channel) + return unless m.plugin # skip messages such as: botname, g = @games[m.channel] + replied = true case m.plugin.intern when :jo # join game return if m.params @@ -862,7 +961,7 @@ class UnoPlugin < Plugin m.reply _("It's not your turn") end when :pa # pass turn - return if m.params + return if m.params or not g.start_time if g.has_turn?(m.source) g.pass(m.source) else @@ -884,7 +983,7 @@ class UnoPlugin < Plugin return if m.params g.show_all_cards(m.source) when :cd # show current discard - return if m.params + return if m.params or not g.start_time g.show_discard when :ch if g.has_turn?(m.source) @@ -905,11 +1004,14 @@ class UnoPlugin < Plugin when :tu # show whose turn is it return if m.params if g.has_turn?(m.source) - m.nickreply _("it's your turn, sleepyhead") + m.reply _("it's your turn, sleepyhead"), :nick => true else g.show_turn(:cards => false) end + else + replied=false end + m.replied=true if replied end def create_game(m, p) @@ -1121,6 +1223,14 @@ class UnoPlugin < Plugin scores << [v.won.inject(0) { |s, w| s+=w.score }, k] end + if wins.empty? + m.reply(_("no %{uno} games were completed here") % { + :uno => UnoGame::UNO + }) + return + end + + if n = p[:scorenum] msg = _("%{uno} %{num} highest scores: ") % { :uno => UnoGame::UNO, :num => p[:scorenum]