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.scan(/[rbgy]\s*(?:\+?\d|[rs])|w\s*(?:\+4)?/)
318 if shorts.length > 2 or shorts.length < 1
319 announce _("you can only play one or two cards")
322 if shorts.length == 2 and shorts.first != shorts.last
323 announce _("you can only play two cards if they are the same")
326 if cards = p.has_card?(shorts.first)
328 unless can_play(cards.first)
329 announce _("you can't play that card")
332 if cards.length >= shorts.length
333 set_discard(p.cards.delete_one(cards.shift))
335 set_discard(p.cards.delete_one(cards.shift))
336 announce _("%{p} plays %{card} twice!") % {
341 announce _("%{p} plays %{card}") % { :p => p, :card => @discard }
343 if p.cards.length == 1
344 announce _("%{p} has %{uno}!") % {
347 elsif p.cards.length == 0
358 announce _("%{p}, choose a color with: co r|b|g|y") % { :p => p }
361 announce _("you don't have two cards of that kind")
364 announce _("you don't have that card")
371 announce _("%{p} passes turn, and has to pick %{b}%{n}%{b} cards!") % {
372 :p => p, :b => Bold, :n => @picker
377 if @player_has_picked
378 announce _("%{p} passes turn") % { :p => p }
380 announce _("you need to pick a card first")
387 def choose_color(user, color)
398 announce _('what color is that?')
401 announce _('color is now %{c}') % {
402 :c => UnoGame.irc_color_bg(@color)+" #{@color} "
409 announce _("This %{uno} game has been going on for %{time}") % {
411 :time => Utils.secs_to_string(Time.now - @start_time)
414 announce _("The game hasn't started yet")
419 announce _("%{uno} playing turn: %{players}") % {
420 :uno => UNO, :players => players.join(' ')
424 def show_turn(opts={})
426 cards = opts[:cards] if opts.key?(:cards)
427 player = @players.first
428 announce _("it's %{player}'s turn") % { :player => player }
429 show_user_cards(player) if cards
432 def has_turn?(source)
433 @players.first.user == source
438 announce _("next player must respond correctly or pick %{b}%{n}%{b} cards") % {
439 :b => Bold, :n => @picker
445 announce _("Current discard: %{card} %{c}") % { :card => @discard,
446 :c => (Wild === @discard) ? UnoGame.irc_color_bg(@color) + " #{@color} " : nil
451 def show_user_cards(player)
452 p = Player === player ? player : get_player(player)
453 notify p, _('Your cards: %{cards}') % {
454 :cards => p.cards.join(' ')
458 def show_all_cards(u=nil)
459 announce(@players.inject([]) { |list, p|
460 list << [p, p.cards.length].join(': ')
469 announce _("%{player} picks a card") % { :player => p }
471 @player_has_picked = true
474 def deal(player, num=1)
477 picked << @stock.delete_one
478 if @stock.length == 0
479 announce _("Shuffling discarded cards")
481 if @stock.length == 0
482 announce _("No more cards!")
483 end_game # FIXME nope!
488 notify player, _("You picked %{picked}") % { :picked => picked.join(' ') }
489 player.cards += picked
494 if p = get_player(user)
495 announce _("you're already in the game, %{p}") % {
502 announce _("%{p} joins this game of %{uno}") % {
507 @bot.timer.reschedule(@join_timer, 10)
508 elsif @players.length > 1
509 announce _("game will start in 20 seconds")
510 @join_timer = @bot.timer.add_once(20) {
517 announce _("%{uno} game finished after %{time}! The winner is %{p}") % {
518 :time => Utils.secs_to_string(Time.now-@start_time),
519 :uno => UNO, :p => @players.first
523 announce _("%{p} has to pick %{b}%{n}%{b} cards!") % {
524 :p => p, :n => @picker, :b => Bold
529 score = @players.inject(0) do |sum, p|
530 if p.cards.length > 0
531 announce _("%{p} still had %{cards}") % {
532 :p => p, :cards => p.cards.join(' ')
534 sum += p.cards.inject(0) do |cs, c|
540 announce _("%{p} wins with %{b}%{score}%{b} points!") % {
541 :p => @players.first, :score => score, :b => Bold
543 @plugin.end_game(@channel)
548 class UnoPlugin < Plugin
555 def help(plugin, topic="")
556 (_("%{uno} game. !uno to start a game. in-game commands (no prefix): ") % {
559 _("'jo' to join in"),
560 _("'pl <card>' to play <card>"),
561 _("'pe' to pick a card"),
562 _("'pa' to pass your turn"),
563 _("'co <color>' to pick a color"),
564 _("'ca' to show current cards"),
565 _("'cd' to show the current discard"),
566 _("'od' to show the playing order"),
567 _("'ti' to show play time"),
568 _("'tu' to show whose turn it is")
573 return unless @games.key?(m.channel)
574 g = @games[m.channel]
578 g.add_player(m.source)
581 if g.has_turn?(m.source)
582 if g.player_has_picked
583 m.reply _("you already picked a card")
585 m.reply _("you can't pick a card")
587 g.pick_card(m.source)
590 m.reply _("It's not your turn")
594 if g.has_turn?(m.source)
597 m.reply _("It's not your turn")
600 if g.has_turn?(m.source)
601 g.play_card(m.source, m.params.downcase)
603 m.reply _("It's not your turn")
605 when :co # pick color
606 if g.has_turn?(m.source)
607 g.choose_color(m.source, m.params.downcase)
609 m.reply _("It's not your turn")
611 when :ca # show current cards
613 g.show_all_cards(m.source)
614 when :cd # show current discard
620 when :od # show playing order
623 when :ti # show play time
626 when :tu # show whose turn is it
628 if g.has_turn?(m.source)
629 m.nickreply _("it's your turn, sleepyhead")
631 g.show_turn(:cards => false)
636 def create_game(m, p)
637 if @games.key?(m.channel)
638 m.reply _("There is already an %{uno} game running here, say 'jo' to join in") % { :uno => UnoGame::UNO }
641 @games[m.channel] = UnoGame.new(self, m.channel)
642 m.reply _("Ok, created %{uno} game on %{channel}, say 'jo' to join in") % {
643 :uno => UnoGame::UNO,
644 :channel => m.channel
648 def end_game(channel)
649 @games.delete(channel)
652 def print_stock(m, p)
653 unless @games.key?(m.channel)
654 m.reply _("There is no %{uno} game running here") % { :uno => UnoGame::UNO }
657 stock = @games[m.channel].stock
658 m.reply(_("%{num} cards in stock: %{stock}") % {
659 :num => stock.length,
660 :stock => stock.join(' ')
661 }, :split_at => /#{NormalText}\s*/)
667 pg.map 'uno', :private => false, :action => :create_game
668 pg.map 'uno stock', :private => false, :action => :print_stock
669 pg.default_auth('stock', false)