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)
171 @players.each { |p| return p if p.user == user }
175 def announce(msg, opts={})
176 @bot.say channel, msg, opts
179 def notify(player, msg, opts={})
180 @bot.notice player.user, msg, opts
184 @base_stock = COLORS.inject([]) do |list, clr|
186 list << Card.new(clr, n)
187 list << Card.new(clr, n) unless n == 0
192 @base_stock << Wild.new
193 @base_stock << Wild.new('+4')
198 @stock.replace @base_stock
199 # remove the cards in the players hand
200 @players.each { |p| p.cards.each { |c| @stock.delete_one c } }
201 # remove current top discarded card if present
203 @stock.delete_one(discard)
209 debug "Starting game"
212 announce _("%{p} deals the first card from the stock") % {
218 while Wild === card do
219 @stock.insert(rand(@stock.length), card)
228 @start_time = Time.now
232 if @players.length > 2
234 # put the current player back in its place
235 @players.unshift @players.pop
236 announce _("Playing order was reversed!")
243 @players << @players.shift
244 announce _("%{p} skips a turn!") % {
245 # this is first and not last because the actual
246 # turn change will be done by the following next_turn
262 def set_discard(card)
264 @value = card.value.dup rescue card.value
268 @color = card.color.dup
271 @picker += card.picker
272 @last_picker = @discard.picker
281 def next_turn(opts={})
282 @players << @players.shift
283 @player_has_picked = false
288 # When a +something is online, you can only play
289 # a +something of same or higher something, or a Reverse of
293 if (card.value == 'Reverse' and card.color == @color) or card.picker >= @last_picker
299 # You can always play a Wild
300 # FIXME W+4 can only be played if you don't have a proper card
301 # TODO make it playable anyway, and allow players to challenge
302 return true if Wild === card
303 # On a Wild, you must match the color
305 return card.color == @color
307 # Otherwise, you can match either the value or the color
308 return (card.value == @value) || (card.color == @color)
313 def play_card(source, cards)
314 debug "Playing card #{cards}"
315 p = get_player(source)
316 shorts = cards.gsub(/\s+/,'').match(/^(?:([rbgy]\d){1,2}|([rbgy](?:\+\d|[rs]))|(w(?:\+4)?)([rbgy])?)$/).to_a
319 announce _("what cards were that again?")
323 short = shorts[1] || shorts[2] || shorts[3]
329 toplay = (full == short) ? 1 : 2
331 debug [full, short, jolly, jcolor, toplay].inspect
332 # r7r7 -> r7r7, r7, nil, nil
333 # r7 -> r7, r7, nil, nil
334 # w -> w, nil, w, nil
335 # wg -> wg, nil, w, g
336 if cards = p.has_card?(short)
338 unless can_play(cards.first)
339 announce _("you can't play that card")
342 if cards.length >= toplay
343 set_discard(p.cards.delete_one(cards.shift))
345 set_discard(p.cards.delete_one(cards.shift))
346 announce _("%{p} plays %{card} twice!") % {
351 announce _("%{p} plays %{card}") % { :p => p, :card => @discard }
353 if p.cards.length == 1
354 announce _("%{p} has %{uno}!") % {
357 elsif p.cards.length == 0
368 choose_color(p.user, jcolor)
370 announce _("%{p}, choose a color with: co r|b|g|y") % { :p => p }
373 announce _("you don't have two cards of that kind")
376 announce _("you don't have that card")
383 announce _("%{p} passes turn, and has to pick %{b}%{n}%{b} cards!") % {
384 :p => p, :b => Bold, :n => @picker
389 if @player_has_picked
390 announce _("%{p} passes turn") % { :p => p }
392 announce _("you need to pick a card first")
399 def choose_color(user, color)
410 announce _('what color is that?')
413 announce _('color is now %{c}') % {
414 :c => UnoGame.irc_color_bg(@color)+" #{@color} "
421 announce _("This %{uno} game has been going on for %{time}") % {
423 :time => Utils.secs_to_string(Time.now - @start_time)
426 announce _("The game hasn't started yet")
431 announce _("%{uno} playing turn: %{players}") % {
432 :uno => UNO, :players => players.join(' ')
436 def show_turn(opts={})
438 cards = opts[:cards] if opts.key?(:cards)
439 player = @players.first
440 announce _("it's %{player}'s turn") % { :player => player }
441 show_user_cards(player) if cards
444 def has_turn?(source)
445 @players.first.user == source
450 announce _("next player must respond correctly or pick %{b}%{n}%{b} cards") % {
451 :b => Bold, :n => @picker
457 announce _("Current discard: %{card} %{c}") % { :card => @discard,
458 :c => (Wild === @discard) ? UnoGame.irc_color_bg(@color) + " #{@color} " : nil
463 def show_user_cards(player)
464 p = Player === player ? player : get_player(player)
465 notify p, _('Your cards: %{cards}') % {
466 :cards => p.cards.join(' ')
470 def show_all_cards(u=nil)
471 announce(@players.inject([]) { |list, p|
472 list << [p, p.cards.length].join(': ')
481 announce _("%{player} picks a card") % { :player => p }
483 @player_has_picked = true
486 def deal(player, num=1)
489 picked << @stock.delete_one
490 if @stock.length == 0
491 announce _("Shuffling discarded cards")
493 if @stock.length == 0
494 announce _("No more cards!")
495 end_game # FIXME nope!
500 notify player, _("You picked %{picked}") % { :picked => picked.join(' ') }
501 player.cards += picked
506 if p = get_player(user)
507 announce _("you're already in the game, %{p}") % {
514 cards = @players.inject(0) do |s, pl|
520 announce _("%{p} joins this game of %{uno}") % {
524 return if @start_time
526 @bot.timer.reschedule(@join_timer, 10)
527 elsif @players.length > 1
528 announce _("game will start in 20 seconds")
529 @join_timer = @bot.timer.add_once(20) {
536 announce _("%{uno} game finished after %{time}! The winner is %{p}") % {
537 :time => Utils.secs_to_string(Time.now-@start_time),
538 :uno => UNO, :p => @players.first
542 announce _("%{p} has to pick %{b}%{n}%{b} cards!") % {
543 :p => p, :n => @picker, :b => Bold
548 score = @players.inject(0) do |sum, p|
549 if p.cards.length > 0
550 announce _("%{p} still had %{cards}") % {
551 :p => p, :cards => p.cards.join(' ')
553 sum += p.cards.inject(0) do |cs, c|
559 announce _("%{p} wins with %{b}%{score}%{b} points!") % {
560 :p => @players.first, :score => score, :b => Bold
562 @plugin.end_game(@channel)
567 class UnoPlugin < Plugin
574 def help(plugin, topic="")
578 _("'jo' to join in"),
579 _("'pl <card>' to play <card>: e.g. 'pl g7' to play Green 7, or 'pl rr' to play Red Reverse"),
580 _("'pe' to pick a card"),
581 _("'pa' to pass your turn"),
582 _("'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)"),
583 _("'ca' to show current cards"),
584 _("'cd' to show the current discard"),
585 _("'od' to show the playing order"),
586 _("'ti' to show play time"),
587 _("'tu' to show whose turn it is")
590 _("play all your cards, one at a time, by matching either the color or the value of the currently discarded card. ") +
591 _("cards with special effects: Skip (next player skips a turn), Reverse (reverses the playing order), +2 (next player has to take 2 cards). ") +
592 _("Wilds can be played on any card, and you must specify the color for the next card. ") +
593 _("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. ") +
594 _("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. ") +
595 _("you can also play a Reverse on a +2 or +4, bouncing the effect back to the previous player (that now comes next). ")
597 (_("%{uno} game. !uno to start a game. see help uno rules for the rules. commands: %{cmds}") % {
598 :uno => UnoGame::UNO,
599 :cmds => help(plugin, 'commands')
605 return unless @games.key?(m.channel)
606 g = @games[m.channel]
610 g.add_player(m.source)
613 if g.has_turn?(m.source)
614 if g.player_has_picked
615 m.reply _("you already picked a card")
617 m.reply _("you can't pick a card")
619 g.pick_card(m.source)
622 m.reply _("It's not your turn")
626 if g.has_turn?(m.source)
629 m.reply _("It's not your turn")
632 if g.has_turn?(m.source)
633 g.play_card(m.source, m.params.downcase)
635 m.reply _("It's not your turn")
637 when :co # pick color
638 if g.has_turn?(m.source)
639 g.choose_color(m.source, m.params.downcase)
641 m.reply _("It's not your turn")
643 when :ca # show current cards
645 g.show_all_cards(m.source)
646 when :cd # show current discard
652 when :od # show playing order
655 when :ti # show play time
658 when :tu # show whose turn is it
660 if g.has_turn?(m.source)
661 m.nickreply _("it's your turn, sleepyhead")
663 g.show_turn(:cards => false)
668 def create_game(m, p)
669 if @games.key?(m.channel)
670 m.reply _("There is already an %{uno} game running here, say 'jo' to join in") % { :uno => UnoGame::UNO }
673 @games[m.channel] = UnoGame.new(self, m.channel)
674 m.reply _("Ok, created %{uno} game on %{channel}, say 'jo' to join in") % {
675 :uno => UnoGame::UNO,
676 :channel => m.channel
680 def end_game(channel)
681 @games.delete(channel)
684 def print_stock(m, p)
685 unless @games.key?(m.channel)
686 m.reply _("There is no %{uno} game running here") % { :uno => UnoGame::UNO }
689 stock = @games[m.channel].stock
690 m.reply(_("%{num} cards in stock: %{stock}") % {
691 :num => stock.length,
692 :stock => stock.join(' ')
693 }, :split_at => /#{NormalText}\s*/)
699 pg.map 'uno', :private => false, :action => :create_game
700 pg.map 'uno stock', :private => false, :action => :print_stock
701 pg.default_auth('stock', false)