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 announce _("%{p} joins this game of %{uno}") % {
519 @bot.timer.reschedule(@join_timer, 10)
520 elsif @players.length > 1
521 announce _("game will start in 20 seconds")
522 @join_timer = @bot.timer.add_once(20) {
529 announce _("%{uno} game finished after %{time}! The winner is %{p}") % {
530 :time => Utils.secs_to_string(Time.now-@start_time),
531 :uno => UNO, :p => @players.first
535 announce _("%{p} has to pick %{b}%{n}%{b} cards!") % {
536 :p => p, :n => @picker, :b => Bold
541 score = @players.inject(0) do |sum, p|
542 if p.cards.length > 0
543 announce _("%{p} still had %{cards}") % {
544 :p => p, :cards => p.cards.join(' ')
546 sum += p.cards.inject(0) do |cs, c|
552 announce _("%{p} wins with %{b}%{score}%{b} points!") % {
553 :p => @players.first, :score => score, :b => Bold
555 @plugin.end_game(@channel)
560 class UnoPlugin < Plugin
567 def help(plugin, topic="")
568 (_("%{uno} game. !uno to start a game. in-game commands (no prefix): ") % {
571 _("'jo' to join in"),
572 _("'pl <card>' to play <card>"),
573 _("'pe' to pick a card"),
574 _("'pa' to pass your turn"),
575 _("'co <color>' to pick a color"),
576 _("'ca' to show current cards"),
577 _("'cd' to show the current discard"),
578 _("'od' to show the playing order"),
579 _("'ti' to show play time"),
580 _("'tu' to show whose turn it is")
585 return unless @games.key?(m.channel)
586 g = @games[m.channel]
590 g.add_player(m.source)
593 if g.has_turn?(m.source)
594 if g.player_has_picked
595 m.reply _("you already picked a card")
597 m.reply _("you can't pick a card")
599 g.pick_card(m.source)
602 m.reply _("It's not your turn")
606 if g.has_turn?(m.source)
609 m.reply _("It's not your turn")
612 if g.has_turn?(m.source)
613 g.play_card(m.source, m.params.downcase)
615 m.reply _("It's not your turn")
617 when :co # pick color
618 if g.has_turn?(m.source)
619 g.choose_color(m.source, m.params.downcase)
621 m.reply _("It's not your turn")
623 when :ca # show current cards
625 g.show_all_cards(m.source)
626 when :cd # show current discard
632 when :od # show playing order
635 when :ti # show play time
638 when :tu # show whose turn is it
640 if g.has_turn?(m.source)
641 m.nickreply _("it's your turn, sleepyhead")
643 g.show_turn(:cards => false)
648 def create_game(m, p)
649 if @games.key?(m.channel)
650 m.reply _("There is already an %{uno} game running here, say 'jo' to join in") % { :uno => UnoGame::UNO }
653 @games[m.channel] = UnoGame.new(self, m.channel)
654 m.reply _("Ok, created %{uno} game on %{channel}, say 'jo' to join in") % {
655 :uno => UnoGame::UNO,
656 :channel => m.channel
660 def end_game(channel)
661 @games.delete(channel)
664 def print_stock(m, p)
665 unless @games.key?(m.channel)
666 m.reply _("There is no %{uno} game running here") % { :uno => UnoGame::UNO }
669 stock = @games[m.channel].stock
670 m.reply(_("%{num} cards in stock: %{stock}") % {
671 :num => stock.length,
672 :stock => stock.join(' ')
673 }, :split_at => /#{NormalText}\s*/)
679 pg.map 'uno', :private => false, :action => :create_game
680 pg.map 'uno stock', :private => false, :action => :print_stock
681 pg.default_auth('stock', false)