4 # :title: Uno Game Plugin for rbot
6 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
8 # Copyright:: (C) 2008 Giuseppe Bilotta
12 # Uno Game: get rid of the cards you have
15 # TODO allow full form card names for play
16 # TODO allow color specification with wild
17 # TODO allow choice of rules re stacking + and playing Reverse with them
18 # TODO highscore table
21 COLORS = %w{Red Green Blue Yellow}
22 SPECIALS = %w{+2 Reverse Skip}
23 NUMERICS = (0..9).to_a
24 VALUES = NUMERICS + SPECIALS
26 def UnoGame.color_map(clr)
39 def UnoGame.irc_color_bg(clr)
40 Irc.color([:white,:black][COLORS.index(clr)%2],UnoGame.color_map(clr))
43 def UnoGame.irc_color_fg(clr)
44 Irc.color(UnoGame.color_map(clr))
47 def UnoGame.colorify(str, fg=false)
49 str.length.times do |i|
51 UnoGame.irc_color_fg(COLORS[i%4]) :
52 UnoGame.irc_color_bg(COLORS[i%4]) ) +str[i,1]
57 UNO = UnoGame.colorify('UNO!', true)
63 attr_reader :shortform
67 def initialize(color, value)
68 raise unless COLORS.include? color
70 raise unless VALUES.include? value
71 if NUMERICS.include? value
79 @shortform = (@color[0,1]+@value).downcase
81 @shortform = (@color[0,1]+@value.to_s[0,1]).downcase
83 @to_s = UnoGame.irc_color_bg(@color) +
84 Bold + ['', @color, @value, ''].join(' ') + NormalText
88 return 0 unless @value.to_s[0,1] == '+'
89 return @value[1,1].to_i
93 SPECIALS.include?(@value)
97 cc = self.color <=> other.color
99 return self.value.to_s <=> other.value.to_s
107 # Wild, Wild +4 cards
109 def initialize(value=nil)
111 raise if value and not value == '+4'
114 @shortform = 'w'+value
120 @to_s = UnoGame.colorify(['', @color, @value, ''].compact.join(' '))
137 has << c if c.shortform == short
146 Bold + @user.to_s + Bold
154 attr_reader :player_has_picked
157 def initialize(plugin, channel)
176 @players.each { |p| return p if p.user == user }
180 def announce(msg, opts={})
181 @bot.say channel, msg, opts
184 def notify(player, msg, opts={})
185 @bot.notice player.user, msg, opts
189 @base_stock = COLORS.inject([]) do |list, clr|
191 list << Card.new(clr, n)
192 list << Card.new(clr, n) unless n == 0
197 @base_stock << Wild.new
198 @base_stock << Wild.new('+4')
203 @stock.replace @base_stock
204 # remove the cards in the players hand
205 @players.each { |p| p.cards.each { |c| @stock.delete_one c } }
206 # remove current top discarded card if present
208 @stock.delete_one(discard)
214 debug "Starting game"
217 announce _("%{p} deals the first card from the stock") % {
223 while Wild === card do
224 @stock.insert(rand(@stock.length), card)
233 @start_time = Time.now
238 Utils.secs_to_string(Time.now-@start_time)
245 if @players.length > 2
247 # put the current player back in its place
248 @players.unshift @players.pop
249 announce _("Playing order was reversed!")
256 @players << @players.shift
257 announce _("%{p} skips a turn!") % {
258 # this is first and not last because the actual
259 # turn change will be done by the following next_turn
275 def set_discard(card)
277 @value = card.value.dup rescue card.value
281 @color = card.color.dup
284 @picker += card.picker
285 @last_picker = @discard.picker
294 def next_turn(opts={})
295 @players << @players.shift
296 @player_has_picked = false
301 # When a +something is online, you can only play
302 # a +something of same or higher something, or a Reverse of
306 if (card.value == 'Reverse' and card.color == @color) or card.picker >= @last_picker
312 # You can always play a Wild
313 # FIXME W+4 can only be played if you don't have a proper card
314 # TODO make it playable anyway, and allow players to challenge
315 return true if Wild === card
316 # On a Wild, you must match the color
318 return card.color == @color
320 # Otherwise, you can match either the value or the color
321 return (card.value == @value) || (card.color == @color)
326 def play_card(source, cards)
327 debug "Playing card #{cards}"
328 p = get_player(source)
329 shorts = cards.gsub(/\s+/,'').match(/^(?:([rbgy]\d){1,2}|([rbgy](?:\+\d|[rs]))|(w(?:\+4)?)([rbgy])?)$/).to_a
332 announce _("what cards were that again?")
336 short = shorts[1] || shorts[2] || shorts[3]
342 toplay = (full == short) ? 1 : 2
344 debug [full, short, jolly, jcolor, toplay].inspect
345 # r7r7 -> r7r7, r7, nil, nil
346 # r7 -> r7, r7, nil, nil
347 # w -> w, nil, w, nil
348 # wg -> wg, nil, w, g
349 if cards = p.has_card?(short)
351 unless can_play(cards.first)
352 announce _("you can't play that card")
355 if cards.length >= toplay
356 set_discard(p.cards.delete_one(cards.shift))
358 set_discard(p.cards.delete_one(cards.shift))
359 announce _("%{p} plays %{card} twice!") % {
364 announce _("%{p} plays %{card}") % { :p => p, :card => @discard }
366 if p.cards.length == 1
367 announce _("%{p} has %{uno}!") % {
370 elsif p.cards.length == 0
381 choose_color(p.user, jcolor)
383 announce _("%{p}, choose a color with: co r|b|g|y") % { :p => p }
386 announce _("you don't have two cards of that kind")
389 announce _("you don't have that card")
396 announce _("%{p} passes turn, and has to pick %{b}%{n}%{b} cards!") % {
397 :p => p, :b => Bold, :n => @picker
402 if @player_has_picked
403 announce _("%{p} passes turn") % { :p => p }
405 announce _("you need to pick a card first")
412 def choose_color(user, color)
423 announce _('what color is that?')
426 announce _('color is now %{c}') % {
427 :c => UnoGame.irc_color_bg(@color)+" #{@color} "
434 announce _("This %{uno} game has been going on for %{time}") % {
436 :time => elapsed_time
439 announce _("The game hasn't started yet")
444 announce _("%{uno} playing turn: %{players}") % {
445 :uno => UNO, :players => players.join(' ')
449 def show_turn(opts={})
451 cards = opts[:cards] if opts.key?(:cards)
452 player = @players.first
453 announce _("it's %{player}'s turn") % { :player => player }
454 show_user_cards(player) if cards
457 def has_turn?(source)
458 @players.first.user == source
463 announce _("next player must respond correctly or pick %{b}%{n}%{b} cards") % {
464 :b => Bold, :n => @picker
470 announce _("Current discard: %{card} %{c}") % { :card => @discard,
471 :c => (Wild === @discard) ? UnoGame.irc_color_bg(@color) + " #{@color} " : nil
476 def show_user_cards(player)
477 p = Player === player ? player : get_player(player)
478 notify p, _('Your cards: %{cards}') % {
479 :cards => p.cards.join(' ')
483 def show_all_cards(u=nil)
484 announce(@players.inject([]) { |list, p|
485 list << [p, p.cards.length].join(': ')
494 announce _("%{player} picks a card") % { :player => p }
496 @player_has_picked = true
499 def deal(player, num=1)
502 picked << @stock.delete_one
503 if @stock.length == 0
504 announce _("Shuffling discarded cards")
506 if @stock.length == 0
507 announce _("No more cards!")
508 end_game # FIXME nope!
513 notify player, _("You picked %{picked}") % { :picked => picked.join(' ') }
514 player.cards += picked
519 if p = get_player(user)
520 announce _("you're already in the game, %{p}") % {
525 @dropouts.each do |dp|
527 announce _("you dropped from the game, %{p}, you can't get back in") % {
535 cards = @players.inject(0) do |s, pl|
541 announce _("%{p} joins this game of %{uno}") % {
545 return if @start_time
547 @bot.timer.reschedule(@join_timer, 10)
548 elsif @players.length > 1
549 announce _("game will start in 20 seconds")
550 @join_timer = @bot.timer.add_once(20) {
556 def drop_player(user)
557 unless p = get_player(user)
558 announce _("%{p} isn't playing %{uno}") % {
563 announce _("%{p} gives up this game of %{uno}") % {
568 if p == @players.first
578 while p.cards.length > 0
579 @stock.insert(rand(@stock.length), p.cards.shift)
582 @dropouts << @players.delete_one(p)
585 def replace_player(old, new)
587 user = channel.get_user(new)
588 if p = get_player(user)
589 announce _("%{p} is already playing %{uno} here") % {
594 # We scan the player list of the player with the old nick, instead
595 # of using get_player, in case of IRC drops etc
597 if p.user.nick == old
599 announce _("%{p} takes %{b}%{old}%{b}'s place at %{uno}") % {
600 :p => p, :b => Bold, :old => old, :uno => UNO
605 announce _("%{b}%{old}%{b} isn't playing %{uno} here") % {
606 :uno => UNO, :b => Bold, :old => old
610 def end_game(halted = false)
613 announce _("%{uno} game halted after %{time}") % {
614 :time => elapsed_time,
618 announce _("%{uno} game halted before it could start") % {
623 announce _("%{uno} game finished after %{time}! The winner is %{p}") % {
624 :time => elapsed_time,
625 :uno => UNO, :p => @players.first
628 if @picker > 0 and not halted
630 announce _("%{p} has to pick %{b}%{n}%{b} cards!") % {
631 :p => p, :n => @picker, :b => Bold
636 score = @players.inject(0) do |sum, p|
637 if p.cards.length > 0
638 announce _("%{p} still had %{cards}") % {
639 :p => p, :cards => p.cards.join(' ')
641 sum += p.cards.inject(0) do |cs, c|
648 announce _("%{p} wins with %{b}%{score}%{b} points!") % {
649 :p => @players.first, :score => score, :b => Bold
652 @plugin.do_end_game(@channel)
657 class UnoPlugin < Plugin
664 def help(plugin, topic="")
668 _("'jo' to join in"),
669 _("'pl <card>' to play <card>: e.g. 'pl g7' to play Green 7, or 'pl rr' to play Red Reverse, or 'pl y2y2' to play both Yellow 2 cards"),
670 _("'pe' to pick a card"),
671 _("'pa' to pass your turn"),
672 _("'co <color>' to pick a color after playing a Wild: e.g. 'co g' to select Green (or 'pl w+4 g' to select the color when playing the Wild)"),
673 _("'ca' to show current cards"),
674 _("'cd' to show the current discard"),
675 _("'od' to show the playing order"),
676 _("'ti' to show play time"),
677 _("'tu' to show whose turn it is")
680 _("play all your cards, one at a time, by matching either the color or the value of the currently discarded card. ") +
681 _("cards with special effects: Skip (next player skips a turn), Reverse (reverses the playing order), +2 (next player has to take 2 cards). ") +
682 _("Wilds can be played on any card, and you must specify the color for the next card. ") +
683 _("Wild +4 also forces the next player to take 4 cards, but it can only be played if you can't play a color card. ") +
684 _("you can play another +2 or +4 card on a +2 card, and a +4 on a +4, forcing the first player who can't play one to pick the cumulative sum of all cards. ") +
685 _("you can also play a Reverse on a +2 or +4, bouncing the effect back to the previous player (that now comes next). ")
687 (_("%{uno} game. !uno to start a game. see help uno rules for the rules. commands: %{cmds}") % {
688 :uno => UnoGame::UNO,
689 :cmds => help(plugin, 'commands')
695 return unless @games.key?(m.channel)
696 g = @games[m.channel]
700 g.add_player(m.source)
703 if g.has_turn?(m.source)
704 if g.player_has_picked
705 m.reply _("you already picked a card")
709 g.pick_card(m.source)
712 m.reply _("It's not your turn")
716 if g.has_turn?(m.source)
719 m.reply _("It's not your turn")
722 if g.has_turn?(m.source)
723 g.play_card(m.source, m.params.downcase)
725 m.reply _("It's not your turn")
727 when :co # pick color
728 if g.has_turn?(m.source)
729 g.choose_color(m.source, m.params.downcase)
731 m.reply _("It's not your turn")
733 when :ca # show current cards
735 g.show_all_cards(m.source)
736 when :cd # show current discard
742 when :od # show playing order
745 when :ti # show play time
748 when :tu # show whose turn is it
750 if g.has_turn?(m.source)
751 m.nickreply _("it's your turn, sleepyhead")
753 g.show_turn(:cards => false)
758 def create_game(m, p)
759 if @games.key?(m.channel)
760 m.reply _("There is already an %{uno} game running here, say 'jo' to join in") % { :uno => UnoGame::UNO }
763 @games[m.channel] = UnoGame.new(self, m.channel)
764 m.reply _("Ok, created %{uno} game on %{channel}, say 'jo' to join in") % {
765 :uno => UnoGame::UNO,
766 :channel => m.channel
771 unless @games.key?(m.channel)
772 m.reply _("There is no %{uno} game running here") % { :uno => UnoGame::UNO }
775 @games[m.channel].end_game(true)
778 def do_end_game(channel)
779 @games.delete(channel)
782 def replace_player(m, p)
783 unless @games.key?(m.channel)
784 m.reply _("There is no %{uno} game running here") % { :uno => UnoGame::UNO }
787 @games[m.channel].replace_player(p[:old], p[:new])
790 def drop_player(m, p)
791 unless @games.key?(m.channel)
792 m.reply _("There is no %{uno} game running here") % { :uno => UnoGame::UNO }
795 who = p[:nick] ? m.channel.get_user(p[:nick]) : m.source
796 @games[m.channel].drop_player(who)
799 def print_stock(m, p)
800 unless @games.key?(m.channel)
801 m.reply _("There is no %{uno} game running here") % { :uno => UnoGame::UNO }
804 stock = @games[m.channel].stock
805 m.reply(_("%{num} cards in stock: %{stock}") % {
806 :num => stock.length,
807 :stock => stock.join(' ')
808 }, :split_at => /#{NormalText}\s*/)
814 pg.map 'uno', :private => false, :action => :create_game
815 pg.map 'uno end', :private => false, :action => :end_game
816 pg.map 'uno drop', :private => false, :action => :drop_player
817 pg.map 'uno giveup', :private => false, :action => :drop_player
818 pg.map 'uno drop :nick', :private => false, :action => :drop_player, :auth_path => ':other'
819 pg.map 'uno replace :old [with] :new', :private => false, :action => :replace_player
820 pg.map 'uno stock', :private => false, :action => :print_stock
822 pg.default_auth('stock', false)
823 pg.default_auth('end', false)
824 pg.default_auth('drop::other', false)
825 pg.default_auth('replace', false)