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 COLORS = %w{Red Green Blue Yellow}
16 SPECIALS = %w{+2 Reverse Skip}
17 NUMERICS = (0..9).to_a
18 VALUES = NUMERICS + SPECIALS
20 def UnoGame.color_map(clr)
33 def UnoGame.irc_color_bg(clr)
34 Irc.color([:white,:black][COLORS.index(clr)%2],UnoGame.color_map(clr))
37 def UnoGame.irc_color_fg(clr)
38 Irc.color(UnoGame.color_map(clr))
41 def UnoGame.colorify(str, fg=false)
43 str.length.times do |i|
45 UnoGame.irc_color_fg(COLORS[i%4]) :
46 UnoGame.irc_color_bg(COLORS[i%4]) ) +str[i,1]
51 UNO = UnoGame.colorify('UNO!', true)
57 attr_reader :shortform
61 def initialize(color, value)
62 raise unless COLORS.include? color
64 raise unless VALUES.include? value
65 if NUMERICS.include? value
73 @shortform = (@color[0,1]+@value).downcase
75 @shortform = (@color[0,1]+@value.to_s[0,1]).downcase
77 @to_s = UnoGame.irc_color_bg(@color) +
78 Bold + ['', @color, @value, ''].join(' ') + NormalText
82 return 0 unless @value.to_s[0,1] == '+'
83 return @value[1,1].to_i
87 SPECIALS.include?(@value)
91 cc = self.color <=> other.color
93 return self.value.to_s <=> other.value.to_s
101 # Wild, Wild +4 cards
103 def initialize(value=nil)
105 raise if value and not value == '+4'
108 @shortform = 'w'+value
114 @to_s = UnoGame.colorify(['', @color, @value, ''].compact.join(' '))
131 cards << c if c.shortform == short
140 Bold + @user.to_s + Bold
148 attr_reader :player_has_picked
151 def initialize(plugin, channel)
165 @players.each { |p| return p if p.user == user }
169 def announce(msg, opts={})
170 @bot.say channel, msg, opts
173 def notify(player, msg, opts={})
174 @bot.notice player.user, msg, opts
178 @base_stock = COLORS.inject([]) do |list, clr|
180 list << Card.new(clr, n)
181 list << Card.new(clr, n) unless n == 0
186 @base_stock << Wild.new
187 @base_stock << Wild.new('+4')
192 @stock.replace @base_stock
193 # remove the cards in the players hand
194 @players.each { |p| p.cards.each { |c| @stock.delete_one c } }
195 # remove current top discarded card if present
197 @stock.delete_one(discard)
203 debug "Starting game"
206 announce _("%{p} deals the first card from the stock") % {
212 while Wild === card do
213 @stock.insert(rand(@stock.length), card)
222 @start_time = Time.now
226 if @players.length > 2
228 # put the current player back in its place
229 @players.unshift @players.pop
230 announce _("Playing order was reversed!")
237 @players << @players.shift
238 announce _("%{p} skips a turn!") % {
239 # this is first and not last because the actual
240 # turn change will be done by the following next_turn
256 def set_discard(card)
258 @value = card.value.dup rescue card.value
262 @color = card.color.dup
265 @picker += card.picker
274 def next_turn(opts={})
275 @players << @players.shift
276 @player_has_picked = false
281 # When a +something is online, you can only play
282 # a +something of same or higher something, or a Reverse of
286 if (card.value == 'Reverse' and card.color == @color) or card.picker >= @discard.picker
292 # You can always play a Wild
293 # FIXME W+4 can only be played if you don't have a proper card
294 # TODO make it playable anyway, and allow players to challenge
295 return true if Wild === card
296 # On a Wild, you must match the color
298 return card.color == @color
300 # Otherwise, you can match either the value or the color
301 return (card.value == @value) || (card.color == @color)
306 def play_card(source, cards)
307 debug "Playing card #{cards}"
308 p = get_player(source)
309 shorts = cards.scan(/[rbgy]\s*(?:\+?\d|[rs])|w\s*(?:\+4)?/)
311 if shorts.length > 2 or shorts.length < 1
312 announce _("you can only play one or two cards")
315 if shorts.length == 2 and shorts.first != shorts.last
316 announce _("you can only play two cards if they are the same")
319 if cards = p.has_card?(shorts.first)
321 unless can_play(cards.first)
322 announce _("you can't play that card")
325 if cards.length >= shorts.length
326 set_discard(p.cards.delete_one(cards.shift))
328 set_discard(p.cards.delete_one(cards.shift))
329 announce _("%{p} plays %{card} twice!") % {
334 announce _("%{p} plays %{card}") % { :p => source, :card => @discard }
336 if p.cards.length == 1
337 announce _("%{p} has %{uno}!") % {
338 :p => source, :uno => UNO
340 elsif p.cards.length == 0
351 announce _("%{p}, choose a color with: co r|b|g|y") % { :p => p }
354 announce _("you don't have that card")
362 announce _("%{p} passes turn, and has to pick %{b}%{n}%{b} cards!") % {
363 :p => user, :b => Bold, :n => @picker
368 if @player_has_picked
369 announce _("%{p} passes turn") % { :p => user }
371 announce _("you need to pick a card first")
378 def choose_color(user, color)
389 announce _('what color is that?')
392 announce _('color is now %{c}') % { :c => @color.downcase }
398 announce _("This %{uno} game has been going on for %{time}") % {
400 :time => Utils.secs_to_string(Time.now - @start_time)
403 announce _("The game hasn't started yet")
408 announce _("%{uno} playing turn: %{players}") % {
409 :uno => UNO, :players => players.join(' ')
413 def show_turn(opts={})
415 cards = opts[:cards] if opts.key?(:cards)
416 player = @players.first
417 announce _("it's %{player}'s turn") % { :player => player }
418 show_user_cards(player) if cards
421 def has_turn?(source)
422 @players.first.user == source
427 announce _("next player must respond correctly or pick %{b}%{n}%{b} cards") % {
428 :b => Bold, :n => @picker
434 announce _("Current discard: %{card} %{c}") % { :card => @discard,
435 :c => (Wild === @discard) ? UnoGame.irc_color_bg(@color) + " #{@color} " : nil
440 def show_user_cards(player)
441 p = Player === player ? player : get_player(player)
442 notify p, _('Your cards: %{cards}') % {
443 :cards => p.cards.join(' ')
447 def show_all_cards(u=nil)
448 announce(@players.inject([]) { |list, p|
449 list << [p, p.cards.length].join(': ')
458 announce _("%{player} picks a card") % { :player => p }
460 @player_has_picked = true
463 def deal(player, num=1)
466 picked << @stock.delete_one
467 if @stock.length == 0
468 announce _("Shuffling discarded cards")
470 if @stock.length == 0
471 announce _("No more cards!")
472 end_game # FIXME nope!
476 notify player, _("You picked %{picked}") % { :picked => picked.join(' ') }
477 player.cards += picked
482 return if get_player(user)
487 @bot.timer.reschedule(@join_timer, 10)
488 elsif @players.length > 1
489 announce _("game will start in 20 seconds")
490 @join_timer = @bot.timer.add_once(20) {
497 announce _("%{uno} game finished! The winner is %{p}") % {
498 :uno => UNO, :p => @players.first
502 announce _("%{p} has to pick %{b}%{n}%{b} cards!") % {
503 :p => p, :n => @picker, :b => Bold
508 score = @players.inject(0) do |sum, p|
509 if p.cards.length > 0
510 announce _("%{p} still had %{cards}") % {
511 :p => p, :cards => p.cards.join(' ')
513 sum += p.cards.inject(0) do |cs, c|
519 announce _("%{p} wins with %{b}%{score}%{b} points!") % {
520 :p => @players.first, :score => score, :b => Bold
522 @plugin.end_game(@channel)
527 class UnoPlugin < Plugin
534 def help(plugin, topic="")
535 (_("%{uno} game. !uno to start a game. in-game commands (no prefix): ") % {
538 _("'jo' to join in"),
539 _("'pl <card>' to play <card>"),
540 _("'pe' to pick a card"),
541 _("'pa' to pass your turn"),
542 _("'co <color>' to pick a color"),
543 _("'ca' to show current cards"),
544 _("'cd' to show the current discard"),
545 _("'od' to show the playing order"),
546 _("'ti' to show play time"),
547 _("'tu' to show whose turn it is")
552 return unless @games.key?(m.channel)
553 g = @games[m.channel]
556 g.add_player(m.source)
558 if g.has_turn?(m.source)
559 if g.player_has_picked
560 m.reply _("you already picked a card")
562 m.reply _("you can't pick a card")
564 g.pick_card(m.source)
567 m.reply _("It's not your turn")
570 if g.has_turn?(m.source)
573 m.reply _("It's not your turn")
576 if g.has_turn?(m.source)
577 g.play_card(m.source, m.params.downcase)
579 m.reply _("It's not your turn")
581 when :co # pick color
582 if g.has_turn?(m.source)
583 g.choose_color(m.source, m.params.downcase)
585 m.reply _("It's not your turn")
587 when :ca # show current cards
588 g.show_all_cards(m.source)
589 when :cd # show current discard
594 when :od # show playing order
596 when :ti # show play time
598 when :tu # show whose turn is it
599 if g.has_turn?(m.source)
600 m.nickreply _("it's your turn, sleepyhead")
602 g.show_turn(:cards => false)
607 def create_game(m, p)
608 if @games.key?(m.channel)
609 m.reply _("There is already an %{uno} game running here, say 'jo' to join in") % { :uno => UnoGame::UNO }
612 @games[m.channel] = UnoGame.new(self, m.channel)
613 m.reply _("Ok, created %{uno} game on %{channel}, say 'jo' to join in") % {
614 :uno => UnoGame::UNO,
615 :channel => m.channel
619 def end_game(channel)
620 @games.delete(channel)
623 def print_stock(m, p)
624 unless @games.key?(m.channel)
625 m.reply _("There is no %{uno} game running here") % { :uno => UnoGame::UNO }
628 stock = @games[m.channel].stock
629 m.reply(_("%{num} cards in stock: %{stock}") % {
630 :num => stock.length,
631 :stock => stock.join(' ')
632 }, :split_at => /#{NormalText}\s*/)
638 pg.map 'uno', :private => false, :action => :create_game
639 pg.map 'uno stock', :private => false, :action => :print_stock
640 pg.default_auth('stock', false)