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 # previous discard, in case of challenge
155 attr_reader :last_discard
156 # channel the game is played in
160 # true if the player picked a card (and can thus pass turn)
161 attr_reader :player_has_picked
162 # number of cards to be picked if the player can't play an appropriate card
165 def initialize(plugin, channel)
186 @players.each { |p| return p if p.user == user }
190 def announce(msg, opts={})
191 @bot.say channel, msg, opts
194 def notify(player, msg, opts={})
195 @bot.notice player.user, msg, opts
199 @base_stock = COLORS.inject([]) do |list, clr|
201 list << Card.new(clr, n)
202 list << Card.new(clr, n) unless n == 0
207 @base_stock << Wild.new
208 @base_stock << Wild.new('+4')
213 @stock.replace @base_stock
214 # remove the cards in the players hand
215 @players.each { |p| p.cards.each { |c| @stock.delete_one c } }
216 # remove current top discarded card if present
218 @stock.delete_one(discard)
224 debug "Starting game"
227 announce _("%{p} deals the first card from the stock") % {
233 while Wild === card do
234 @stock.insert(rand(@stock.length), card)
243 @start_time = Time.now
248 Utils.secs_to_string(Time.now-@start_time)
255 if @players.length > 2
257 # put the current player back in its place
258 @players.unshift @players.pop
259 announce _("Playing order was reversed!")
266 @players << @players.shift
267 announce _("%{p} skips a turn!") % {
268 # this is first and not last because the actual
269 # turn change will be done by the following next_turn
285 def set_discard(card)
287 @value = card.value.dup rescue card.value
291 @color = card.color.dup
294 @picker += card.picker
295 @last_picker = @discard.picker
305 def next_turn(opts={})
306 @players << @players.shift
307 @player_has_picked = false
312 # if play is forced, check against the only allowed cards
313 return false if @must_play and not @must_play.include?(card)
315 # When a +something is online, you can only play
316 # a +something of same or higher something, or a Reverse of
320 if (card.value == 'Reverse' and card.color == @color) or card.picker >= @last_picker
326 # You can always play a Wild
327 return true if Wild === card
328 # On a Wild, you must match the color
330 return card.color == @color
332 # Otherwise, you can match either the value or the color
333 return (card.value == @value) || (card.color == @color)
338 def play_card(source, cards)
339 debug "Playing card #{cards}"
340 p = get_player(source)
341 shorts = cards.gsub(/\s+/,'').match(/^(?:([rbgy]\+?\d){1,2}|([rbgy][rs])|(w(?:\+4)?)([rbgy])?)$/).to_a
344 announce _("what cards were that again?")
348 short = shorts[1] || shorts[2] || shorts[3]
354 toplay = (full == short) ? 1 : 2
356 debug [full, short, jolly, jcolor, toplay].inspect
357 # r7r7 -> r7r7, r7, nil, nil
358 # r7 -> r7, r7, nil, nil
359 # w -> w, nil, w, nil
360 # wg -> wg, nil, w, g
361 if cards = p.has_card?(short)
363 unless can_play(cards.first)
364 announce _("you can't play that card")
367 if cards.length >= toplay
368 # if the played card is a W+4 not played during a stacking +x
369 if @picker == 0 and Wild === cards.first and cards.first.value
370 # save the previous discard in case of challenge
371 @last_discard = @discard.dup
372 # save the color too, in case it was a Wild
373 @last_color = @color.dup
377 set_discard(p.cards.delete_one(cards.shift))
379 set_discard(p.cards.delete_one(cards.shift))
380 announce _("%{p} plays %{card} twice!") % {
385 announce _("%{p} plays %{card}") % { :p => p, :card => @discard }
387 if p.cards.length == 1
388 announce _("%{p} has %{uno}!") % {
391 elsif p.cards.length == 0
402 choose_color(p.user, jcolor)
404 announce _("%{p}, choose a color with: co r|b|g|y") % { :p => p }
407 announce _("you don't have two cards of that kind")
410 announce _("you don't have that card")
415 return unless @last_discard
420 announce _("%{cp} challenges %{lp}'s %{card}!") % {
421 :cp => cp, :lp => lp, :card => @discard
423 # show the cards of the previous player to the current player
424 notify cp, _("%{p} has %{cards}") % {
425 :p => lp, :cards => lp.cards.join(' ')
427 # check if the previous player had a non-special card of the correct color
430 if c.color == @last_color and not c.special?
436 announce _("%{lp}'s move was legal, %{cp} must pick %{b}%{n}%{b} cards!") % {
437 :cp => cp, :lp => lp, :b => Bold, :n => @picker
445 announce _("%{lp}'s move was %{b}not%{b} legal, %{lp} must pick %{b}%{n}%{b} cards and play again!") % {
446 :cp => cp, :lp => lp, :b => Bold, :n => @picker
448 lp.cards << @discard # put the W+4 back in place
451 @color = @last_color.dup
452 @discard = @last_discard.dup
454 @value = @discard.value.dup rescue @discard.value
458 # force the player to play the current cards
459 @must_play = lp.cards.dup
461 # give him the penalty cards
465 # and restore the turn
466 @players.unshift @players.pop
473 announce _("%{p} passes turn, and has to pick %{b}%{n}%{b} cards!") % {
474 :p => p, :b => Bold, :n => @picker
479 if @player_has_picked
480 announce _("%{p} passes turn") % { :p => p }
482 announce _("you need to pick a card first")
489 def choose_color(user, color)
500 announce _('what color is that?')
503 announce _('color is now %{c}') % {
504 :c => UnoGame.irc_color_bg(@color)+" #{@color} "
511 announce _("This %{uno} game has been going on for %{time}") % {
513 :time => elapsed_time
516 announce _("The game hasn't started yet")
521 announce _("%{uno} playing turn: %{players}") % {
522 :uno => UNO, :players => players.join(' ')
526 def show_turn(opts={})
528 cards = opts[:cards] if opts.key?(:cards)
529 player = @players.first
530 announce _("it's %{player}'s turn") % { :player => player }
531 show_user_cards(player) if cards
534 def has_turn?(source)
535 @players.first.user == source
540 announce _("next player must respond correctly or pick %{b}%{n}%{b} cards") % {
541 :b => Bold, :n => @picker
547 announce _("Current discard: %{card} %{c}") % { :card => @discard,
548 :c => (Wild === @discard) ? UnoGame.irc_color_bg(@color) + " #{@color} " : nil
553 def show_user_cards(player)
554 p = Player === player ? player : get_player(player)
555 notify p, _('Your cards: %{cards}') % {
556 :cards => p.cards.join(' ')
560 def show_all_cards(u=nil)
561 announce(@players.inject([]) { |list, p|
562 list << [p, p.cards.length].join(': ')
571 announce _("%{player} picks a card") % { :player => p }
573 @player_has_picked = true
576 def deal(player, num=1)
579 picked << @stock.delete_one
580 if @stock.length == 0
581 announce _("Shuffling discarded cards")
583 if @stock.length == 0
584 announce _("No more cards!")
585 end_game # FIXME nope!
590 notify player, _("You picked %{picked}") % { :picked => picked.join(' ') }
591 player.cards += picked
596 if p = get_player(user)
597 announce _("you're already in the game, %{p}") % {
602 @dropouts.each do |dp|
604 announce _("you dropped from the game, %{p}, you can't get back in") % {
612 cards = @players.inject(0) do |s, pl|
618 announce _("%{p} joins this game of %{uno}") % {
622 return if @start_time
624 @bot.timer.reschedule(@join_timer, 10)
625 elsif @players.length > 1
626 announce _("game will start in 20 seconds")
627 @join_timer = @bot.timer.add_once(20) {
633 def drop_player(user)
634 unless p = get_player(user)
635 announce _("%{p} isn't playing %{uno}") % {
640 announce _("%{p} gives up this game of %{uno}") % {
645 if p == @players.first
655 while p.cards.length > 0
656 @stock.insert(rand(@stock.length), p.cards.shift)
659 @dropouts << @players.delete_one(p)
662 def replace_player(old, new)
664 user = channel.get_user(new)
665 if p = get_player(user)
666 announce _("%{p} is already playing %{uno} here") % {
671 # We scan the player list of the player with the old nick, instead
672 # of using get_player, in case of IRC drops etc
674 if p.user.nick == old
676 announce _("%{p} takes %{b}%{old}%{b}'s place at %{uno}") % {
677 :p => p, :b => Bold, :old => old, :uno => UNO
682 announce _("%{b}%{old}%{b} isn't playing %{uno} here") % {
683 :uno => UNO, :b => Bold, :old => old
687 def end_game(halted = false)
690 announce _("%{uno} game halted after %{time}") % {
691 :time => elapsed_time,
695 announce _("%{uno} game halted before it could start") % {
700 announce _("%{uno} game finished after %{time}! The winner is %{p}") % {
701 :time => elapsed_time,
702 :uno => UNO, :p => @players.first
705 if @picker > 0 and not halted
707 announce _("%{p} has to pick %{b}%{n}%{b} cards!") % {
708 :p => p, :n => @picker, :b => Bold
713 score = @players.inject(0) do |sum, p|
714 if p.cards.length > 0
715 announce _("%{p} still had %{cards}") % {
716 :p => p, :cards => p.cards.join(' ')
718 sum += p.cards.inject(0) do |cs, c|
725 announce _("%{p} wins with %{b}%{score}%{b} points!") % {
726 :p => @players.first, :score => score, :b => Bold
729 @plugin.do_end_game(@channel)
734 class UnoPlugin < Plugin
741 def help(plugin, topic="")
745 _("'jo' to join in"),
746 _("'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"),
747 _("'pe' to pick a card"),
748 _("'pa' to pass your turn"),
749 _("'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)"),
750 _("'ca' to show current cards"),
751 _("'cd' to show the current discard"),
752 _("'ch' to challenge a Wild +4"),
753 _("'od' to show the playing order"),
754 _("'ti' to show play time"),
755 _("'tu' to show whose turn it is")
758 _("A Wild +4 can only be played legally if you don't have normal (not special) cards of the current color. ") +
759 _("The next player can challenge a W+4 by using the 'ch' command. ") +
760 _("If the W+4 play was illegal, the player who played it must pick the W+4, pick 4 cards from the stock, and play a legal card. ") +
761 _("If the W+4 play was legal, the challenger must pick 6 cards instead of 4.")
763 _("play all your cards, one at a time, by matching either the color or the value of the currently discarded card. ") +
764 _("cards with special effects: Skip (next player skips a turn), Reverse (reverses the playing order), +2 (next player has to take 2 cards). ") +
765 _("Wilds can be played on any card, and you must specify the color for the next card. ") +
766 _("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. ") +
767 _("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. ") +
768 _("you can also play a Reverse on a +2 or +4, bouncing the effect back to the previous player (that now comes next). ")
770 (_("%{uno} game. !uno to start a game. see help uno rules for the rules. commands: %{cmds}") % {
771 :uno => UnoGame::UNO,
772 :cmds => help(plugin, 'commands')
778 return unless @games.key?(m.channel)
779 g = @games[m.channel]
783 g.add_player(m.source)
786 if g.has_turn?(m.source)
787 if g.player_has_picked
788 m.reply _("you already picked a card")
792 g.pick_card(m.source)
795 m.reply _("It's not your turn")
799 if g.has_turn?(m.source)
802 m.reply _("It's not your turn")
805 if g.has_turn?(m.source)
806 g.play_card(m.source, m.params.downcase)
808 m.reply _("It's not your turn")
810 when :co # pick color
811 if g.has_turn?(m.source)
812 g.choose_color(m.source, m.params.downcase)
814 m.reply _("It's not your turn")
816 when :ca # show current cards
818 g.show_all_cards(m.source)
819 when :cd # show current discard
823 if g.has_turn?(m.source)
827 m.reply _("previous move cannot be challenged")
830 m.reply _("It's not your turn")
832 when :od # show playing order
835 when :ti # show play time
838 when :tu # show whose turn is it
840 if g.has_turn?(m.source)
841 m.nickreply _("it's your turn, sleepyhead")
843 g.show_turn(:cards => false)
848 def create_game(m, p)
849 if @games.key?(m.channel)
850 m.reply _("There is already an %{uno} game running here, say 'jo' to join in") % { :uno => UnoGame::UNO }
853 @games[m.channel] = UnoGame.new(self, m.channel)
854 m.reply _("Ok, created %{uno} game on %{channel}, say 'jo' to join in") % {
855 :uno => UnoGame::UNO,
856 :channel => m.channel
861 unless @games.key?(m.channel)
862 m.reply _("There is no %{uno} game running here") % { :uno => UnoGame::UNO }
865 @games[m.channel].end_game(true)
868 def do_end_game(channel)
869 @games.delete(channel)
872 def replace_player(m, p)
873 unless @games.key?(m.channel)
874 m.reply _("There is no %{uno} game running here") % { :uno => UnoGame::UNO }
877 @games[m.channel].replace_player(p[:old], p[:new])
880 def drop_player(m, p)
881 unless @games.key?(m.channel)
882 m.reply _("There is no %{uno} game running here") % { :uno => UnoGame::UNO }
885 who = p[:nick] ? m.channel.get_user(p[:nick]) : m.source
886 @games[m.channel].drop_player(who)
889 def print_stock(m, p)
890 unless @games.key?(m.channel)
891 m.reply _("There is no %{uno} game running here") % { :uno => UnoGame::UNO }
894 stock = @games[m.channel].stock
895 m.reply(_("%{num} cards in stock: %{stock}") % {
896 :num => stock.length,
897 :stock => stock.join(' ')
898 }, :split_at => /#{NormalText}\s*/)
904 pg.map 'uno', :private => false, :action => :create_game
905 pg.map 'uno end', :private => false, :action => :end_game
906 pg.map 'uno drop', :private => false, :action => :drop_player
907 pg.map 'uno giveup', :private => false, :action => :drop_player
908 pg.map 'uno drop :nick', :private => false, :action => :drop_player, :auth_path => ':other'
909 pg.map 'uno replace :old [with] :new', :private => false, :action => :replace_player
910 pg.map 'uno stock', :private => false, :action => :print_stock
912 pg.default_auth('stock', false)
913 pg.default_auth('end', false)
914 pg.default_auth('drop::other', false)
915 pg.default_auth('replace', false)