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)
189 return p if p.user == user
193 return p if p.user.irc_downcase == user.irc_downcase(channel.casemap)
196 get_player(user.to_s)
201 def announce(msg, opts={})
202 @bot.say channel, msg, opts
205 def notify(player, msg, opts={})
206 @bot.notice player.user, msg, opts
210 @base_stock = COLORS.inject([]) do |list, clr|
212 list << Card.new(clr, n)
213 list << Card.new(clr, n) unless n == 0
218 @base_stock << Wild.new
219 @base_stock << Wild.new('+4')
224 @stock.replace @base_stock
225 # remove the cards in the players hand
226 @players.each { |p| p.cards.each { |c| @stock.delete_one c } }
227 # remove current top discarded card if present
229 @stock.delete_one(discard)
235 debug "Starting game"
238 announce _("%{p} deals the first card from the stock") % {
244 while Wild === card do
245 @stock.insert(rand(@stock.length), card)
254 @start_time = Time.now
259 Utils.secs_to_string(Time.now-@start_time)
266 if @players.length > 2
268 # put the current player back in its place
269 @players.unshift @players.pop
270 announce _("Playing order was reversed!")
277 @players << @players.shift
278 announce _("%{p} skips a turn!") % {
279 # this is first and not last because the actual
280 # turn change will be done by the following next_turn
296 def set_discard(card)
298 @value = card.value.dup rescue card.value
302 @color = card.color.dup
305 @picker += card.picker
306 @last_picker = @discard.picker
316 def next_turn(opts={})
317 @players << @players.shift
318 @player_has_picked = false
323 # if play is forced, check against the only allowed cards
324 return false if @must_play and not @must_play.include?(card)
326 # When a +something is online, you can only play
327 # a +something of same or higher something, or a Reverse of
331 if (card.value == 'Reverse' and card.color == @color) or card.picker >= @last_picker
337 # You can always play a Wild
338 return true if Wild === card
339 # On a Wild, you must match the color
341 return card.color == @color
343 # Otherwise, you can match either the value or the color
344 return (card.value == @value) || (card.color == @color)
349 def play_card(source, cards)
350 debug "Playing card #{cards}"
351 p = get_player(source)
352 shorts = cards.gsub(/\s+/,'').match(/^(?:([rbgy]\+?\d){1,2}|([rbgy][rs])|(w(?:\+4)?)([rbgy])?)$/).to_a
355 announce _("what cards were that again?")
359 short = shorts[1] || shorts[2] || shorts[3]
365 toplay = (full == short) ? 1 : 2
367 debug [full, short, jolly, jcolor, toplay].inspect
368 # r7r7 -> r7r7, r7, nil, nil
369 # r7 -> r7, r7, nil, nil
370 # w -> w, nil, w, nil
371 # wg -> wg, nil, w, g
372 if cards = p.has_card?(short)
374 unless can_play(cards.first)
375 announce _("you can't play that card")
378 if cards.length >= toplay
379 # if the played card is a W+4 not played during a stacking +x
380 if @picker == 0 and Wild === cards.first and cards.first.value
381 # save the previous discard in case of challenge
382 @last_discard = @discard.dup
383 # save the color too, in case it was a Wild
384 @last_color = @color.dup
388 set_discard(p.cards.delete_one(cards.shift))
390 set_discard(p.cards.delete_one(cards.shift))
391 announce _("%{p} plays %{card} twice!") % {
396 announce _("%{p} plays %{card}") % { :p => p, :card => @discard }
398 if p.cards.length == 1
399 announce _("%{p} has %{uno}!") % {
402 elsif p.cards.length == 0
413 choose_color(p.user, jcolor)
415 announce _("%{p}, choose a color with: co r|b|g|y") % { :p => p }
418 announce _("you don't have two cards of that kind")
421 announce _("you don't have that card")
426 return unless @last_discard
431 announce _("%{cp} challenges %{lp}'s %{card}!") % {
432 :cp => cp, :lp => lp, :card => @discard
434 # show the cards of the previous player to the current player
435 notify cp, _("%{p} has %{cards}") % {
436 :p => lp, :cards => lp.cards.join(' ')
438 # check if the previous player had a non-special card of the correct color
441 if c.color == @last_color and not c.special?
447 announce _("%{lp}'s move was legal, %{cp} must pick %{b}%{n}%{b} cards!") % {
448 :cp => cp, :lp => lp, :b => Bold, :n => @picker
456 announce _("%{lp}'s move was %{b}not%{b} legal, %{lp} must pick %{b}%{n}%{b} cards and play again!") % {
457 :cp => cp, :lp => lp, :b => Bold, :n => @picker
459 lp.cards << @discard # put the W+4 back in place
462 @color = @last_color.dup
463 @discard = @last_discard.dup
465 @value = @discard.value.dup rescue @discard.value
469 # force the player to play the current cards
470 @must_play = lp.cards.dup
472 # give him the penalty cards
476 # and restore the turn
477 @players.unshift @players.pop
484 announce _("%{p} passes turn, and has to pick %{b}%{n}%{b} cards!") % {
485 :p => p, :b => Bold, :n => @picker
490 if @player_has_picked
491 announce _("%{p} passes turn") % { :p => p }
493 announce _("you need to pick a card first")
500 def choose_color(user, color)
511 announce _('what color is that?')
514 announce _('color is now %{c}') % {
515 :c => UnoGame.irc_color_bg(@color)+" #{@color} "
522 announce _("This %{uno} game has been going on for %{time}") % {
524 :time => elapsed_time
527 announce _("The game hasn't started yet")
532 announce _("%{uno} playing turn: %{players}") % {
533 :uno => UNO, :players => players.join(' ')
537 def show_turn(opts={})
539 cards = opts[:cards] if opts.key?(:cards)
540 player = @players.first
541 announce _("it's %{player}'s turn") % { :player => player }
542 show_user_cards(player) if cards
545 def has_turn?(source)
546 @players.first.user == source
551 announce _("next player must respond correctly or pick %{b}%{n}%{b} cards") % {
552 :b => Bold, :n => @picker
558 announce _("Current discard: %{card} %{c}") % { :card => @discard,
559 :c => (Wild === @discard) ? UnoGame.irc_color_bg(@color) + " #{@color} " : nil
564 def show_user_cards(player)
565 p = Player === player ? player : get_player(player)
566 notify p, _('Your cards: %{cards}') % {
567 :cards => p.cards.join(' ')
571 def show_all_cards(u=nil)
572 announce(@players.inject([]) { |list, p|
573 list << [p, p.cards.length].join(': ')
582 announce _("%{player} picks a card") % { :player => p }
584 @player_has_picked = true
587 def deal(player, num=1)
590 picked << @stock.delete_one
591 if @stock.length == 0
592 announce _("Shuffling discarded cards")
594 if @stock.length == 0
595 announce _("No more cards!")
596 end_game # FIXME nope!
601 notify player, _("You picked %{picked}") % { :picked => picked.join(' ') }
602 player.cards += picked
607 if p = get_player(user)
608 announce _("you're already in the game, %{p}") % {
613 @dropouts.each do |dp|
615 announce _("you dropped from the game, %{p}, you can't get back in") % {
623 cards = @players.inject(0) do |s, pl|
629 announce _("%{p} joins this game of %{uno}") % {
633 return if @start_time
635 @bot.timer.reschedule(@join_timer, 10)
636 elsif @players.length > 1
637 announce _("game will start in 20 seconds")
638 @join_timer = @bot.timer.add_once(20) {
644 def drop_player(nick)
645 # A nick is passed because the original player might have left
647 unless p = get_player(nick)
648 announce _("%{p} isn't playing %{uno}") % {
653 announce _("%{p} gives up this game of %{uno}") % {
658 if p == @players.first
668 while p.cards.length > 0
669 @stock.insert(rand(@stock.length), p.cards.shift)
672 @dropouts << @players.delete_one(p)
675 def replace_player(old, new)
677 user = channel.get_user(new)
678 if p = get_player(user)
679 announce _("%{p} is already playing %{uno} here") % {
684 # We scan the player list of the player with the old nick, instead
685 # of using get_player, in case of IRC drops etc
687 if p.user.nick == old
689 announce _("%{p} takes %{b}%{old}%{b}'s place at %{uno}") % {
690 :p => p, :b => Bold, :old => old, :uno => UNO
695 announce _("%{b}%{old}%{b} isn't playing %{uno} here") % {
696 :uno => UNO, :b => Bold, :old => old
700 def end_game(halted = false)
703 announce _("%{uno} game halted after %{time}") % {
704 :time => elapsed_time,
708 announce _("%{uno} game halted before it could start") % {
713 announce _("%{uno} game finished after %{time}! The winner is %{p}") % {
714 :time => elapsed_time,
715 :uno => UNO, :p => @players.first
718 if @picker > 0 and not halted
720 announce _("%{p} has to pick %{b}%{n}%{b} cards!") % {
721 :p => p, :n => @picker, :b => Bold
726 score = @players.inject(0) do |sum, p|
727 if p.cards.length > 0
728 announce _("%{p} still had %{cards}") % {
729 :p => p, :cards => p.cards.join(' ')
731 sum += p.cards.inject(0) do |cs, c|
738 announce _("%{p} wins with %{b}%{score}%{b} points!") % {
739 :p => @players.first, :score => score, :b => Bold
742 @plugin.do_end_game(@channel)
747 class UnoPlugin < Plugin
754 def help(plugin, topic="")
758 _("'jo' to join in"),
759 _("'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"),
760 _("'pe' to pick a card"),
761 _("'pa' to pass your turn"),
762 _("'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)"),
763 _("'ca' to show current cards"),
764 _("'cd' to show the current discard"),
765 _("'ch' to challenge a Wild +4"),
766 _("'od' to show the playing order"),
767 _("'ti' to show play time"),
768 _("'tu' to show whose turn it is")
771 _("A Wild +4 can only be played legally if you don't have normal (not special) cards of the current color. ") +
772 _("The next player can challenge a W+4 by using the 'ch' command. ") +
773 _("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. ") +
774 _("If the W+4 play was legal, the challenger must pick 6 cards instead of 4.")
776 _("play all your cards, one at a time, by matching either the color or the value of the currently discarded card. ") +
777 _("cards with special effects: Skip (next player skips a turn), Reverse (reverses the playing order), +2 (next player has to take 2 cards). ") +
778 _("Wilds can be played on any card, and you must specify the color for the next card. ") +
779 _("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. ") +
780 _("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. ") +
781 _("you can also play a Reverse on a +2 or +4, bouncing the effect back to the previous player (that now comes next). ")
783 (_("%{uno} game. !uno to start a game. see help uno rules for the rules. commands: %{cmds}") % {
784 :uno => UnoGame::UNO,
785 :cmds => help(plugin, 'commands')
791 return unless @games.key?(m.channel)
792 g = @games[m.channel]
796 g.add_player(m.source)
799 if g.has_turn?(m.source)
800 if g.player_has_picked
801 m.reply _("you already picked a card")
805 g.pick_card(m.source)
808 m.reply _("It's not your turn")
812 if g.has_turn?(m.source)
815 m.reply _("It's not your turn")
818 if g.has_turn?(m.source)
819 g.play_card(m.source, m.params.downcase)
821 m.reply _("It's not your turn")
823 when :co # pick color
824 if g.has_turn?(m.source)
825 g.choose_color(m.source, m.params.downcase)
827 m.reply _("It's not your turn")
829 when :ca # show current cards
831 g.show_all_cards(m.source)
832 when :cd # show current discard
836 if g.has_turn?(m.source)
840 m.reply _("previous move cannot be challenged")
843 m.reply _("It's not your turn")
845 when :od # show playing order
848 when :ti # show play time
851 when :tu # show whose turn is it
853 if g.has_turn?(m.source)
854 m.nickreply _("it's your turn, sleepyhead")
856 g.show_turn(:cards => false)
861 def create_game(m, p)
862 if @games.key?(m.channel)
863 m.reply _("There is already an %{uno} game running here, say 'jo' to join in") % { :uno => UnoGame::UNO }
866 @games[m.channel] = UnoGame.new(self, m.channel)
867 m.reply _("Ok, created %{uno} game on %{channel}, say 'jo' to join in") % {
868 :uno => UnoGame::UNO,
869 :channel => m.channel
874 unless @games.key?(m.channel)
875 m.reply _("There is no %{uno} game running here") % { :uno => UnoGame::UNO }
878 @games[m.channel].end_game(true)
881 def do_end_game(channel)
882 @games.delete(channel)
885 def replace_player(m, p)
886 unless @games.key?(m.channel)
887 m.reply _("There is no %{uno} game running here") % { :uno => UnoGame::UNO }
890 @games[m.channel].replace_player(p[:old], p[:new])
893 def drop_player(m, p)
894 unless @games.key?(m.channel)
895 m.reply _("There is no %{uno} game running here") % { :uno => UnoGame::UNO }
898 @games[m.channel].drop_player(p[:nick] || m.source.nick)
901 def print_stock(m, p)
902 unless @games.key?(m.channel)
903 m.reply _("There is no %{uno} game running here") % { :uno => UnoGame::UNO }
906 stock = @games[m.channel].stock
907 m.reply(_("%{num} cards in stock: %{stock}") % {
908 :num => stock.length,
909 :stock => stock.join(' ')
910 }, :split_at => /#{NormalText}\s*/)
916 pg.map 'uno', :private => false, :action => :create_game
917 pg.map 'uno end', :private => false, :action => :end_game
918 pg.map 'uno drop', :private => false, :action => :drop_player
919 pg.map 'uno giveup', :private => false, :action => :drop_player
920 pg.map 'uno drop :nick', :private => false, :action => :drop_player, :auth_path => ':other'
921 pg.map 'uno replace :old [with] :new', :private => false, :action => :replace_player
922 pg.map 'uno stock', :private => false, :action => :print_stock
924 pg.default_auth('stock', false)
925 pg.default_auth('end', false)
926 pg.default_auth('drop::other', false)
927 pg.default_auth('replace', false)