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)
93 def initialize(value=nil)
95 raise if value and not value == '+4'
98 @shortform = 'w'+value
104 @to_s = UnoGame.colorify(['', @color, @value, ''].compact.join(' '))
121 cards << c if c.shortform == short
138 attr_reader :player_has_picked
141 def initialize(plugin, channel)
155 @players.each { |p| return p if p.user == user }
159 def announce(msg, opts={})
160 @bot.say channel, msg, opts
163 def notify(player, msg, opts={})
164 @bot.notice player.user, msg, opts
168 @base_stock = COLORS.inject([]) do |list, clr|
170 list << Card.new(clr, n)
171 list << Card.new(clr, n) unless n == 0
176 @base_stock << Wild.new
177 @base_stock << Wild.new('+4')
182 @stock.replace @base_stock
183 # remove the cards in the players hand
184 @players.each { |p| p.cards.each { |c| @stock.delete_one c } }
185 # remove current top discarded card if present
187 @stock.delete_one(discard)
193 debug "Starting game"
196 announce _("%{p} deals the first card from the stock") % {
197 :p => @players.first.user
202 while Wild === card do
203 @stock.insert(rand(@stock.length), card)
212 @start_time = Time.now
216 if @players.length > 2
218 # put the current player back in its place
219 @players.unshift @players.pop
220 announce _("Playing order was reversed!")
227 @players << @players.shift
228 announce _("%{p} skips a turn!") % {
229 # this is first and not last because the actual
230 # turn change will be done by the following next_turn
231 :p => @players.first.user
246 def set_discard(card)
248 @value = card.value.dup rescue card.value
252 @color = card.color.dup
255 @picker += card.picker
266 def next_turn(opts={})
267 @players << @players.shift
268 @player_has_picked = false
273 # When a +something is online, you can only play
274 # a +something of same or higher something, or a Reverse
277 if card.value == 'Reverse' or card.picker >= @discard.picker
283 # You can always play a Wild
284 # FIXME W+4 can only be played if you don't have a proper card
285 # TODO make it playable anyway, and allow players to challenge
286 return true if Wild === card
287 # On a Wild, you must match the color
289 return card.color == @color
291 # Otherwise, you can match either the value or the color
292 return (card.value == @value) || (card.color == @color)
297 def play_card(source, cards)
298 debug "Playing card #{cards}"
299 p = get_player(source)
300 shorts = cards.scan(/[rbgy]\s*(?:\+?\d|[rs])|w\s*(?:\+4)?/)
302 if shorts.length > 2 or shorts.length < 1
303 announce _("you can only play one or two cards")
306 if shorts.length == 2 and shorts.first != shorts.last
307 announce _("you can only play two cards if they are the same")
310 if cards = p.has_card?(shorts.first)
312 unless can_play(cards.first)
313 announce _("you can't play that card")
316 if cards.length >= shorts.length
317 set_discard(p.cards.delete_one(cards.shift))
319 set_discard(p.cards.delete_one(cards.shift))
320 announce _("%{p} plays %{card} twice!") % {
325 announce _("%{p} plays %{card}") % { :p => source, :card => @discard }
327 if p.cards.length == 1
328 announce _("%{p} has %{uno}!") % {
329 :p => source, :uno => UNO
331 elsif p.cards.length == 0
342 announce _("%{p}, choose a color with: co r|b|g|y") % {
347 announce _("you don't have that card")
355 announce _("%{p} passes turn, and has to pick %{b}%{n}%{b} cards!") % {
356 :p => user, :b => Bold, :n => @picker
361 if @player_has_picked
362 announce _("%{p} passes turn") % { :p => user }
364 announce _("you need to pick a card first")
371 def choose_color(user, color)
382 announce _('what color is that?')
385 announce _('color is now %{c}') % { :c => @color.downcase }
391 announce _("This %{uno} game has been going on for %{time}") % {
393 :time => Utils.secs_to_string(Time.now - @start_time)
396 announce _("The game hasn't started yet")
401 announce _("%{uno} playing turn: %{players}") % {
402 :uno => UNO, :players => players.join(' ')
406 def show_turn(opts={})
408 cards = opts[:cards] if opts.key?(:cards)
409 player = @players.first
410 announce _("it's %{player}'s turn") % { :player => player.user }
411 show_user_cards(player) if cards
414 def has_turn?(source)
415 @players.first.user == source
420 announce _("next player must respond correctly or pick %{b}%{n}%{b} cards") % {
421 :b => Bold, :n => @picker
427 announce _("Current discard: %{card} %{c}") % { :card => @discard,
428 :c => (Wild === @discard) ? UnoGame.irc_color_bg(@color) + " #{@color} " : nil
433 def show_user_cards(player)
434 p = Player === player ? player : get_player(player)
435 notify p, _('Your cards: %{cards}') % {
436 :cards => p.cards.join(' ')
440 def show_all_cards(u=nil)
441 announce(@players.inject([]) { |list, p|
442 list << [p.user, p.cards.length].join(': ')
451 announce _("%{player} picks a card") % { :player => p }
453 @player_has_picked = true
456 def deal(player, num=1)
459 picked << @stock.delete_one
460 if @stock.length == 0
461 announce _("Shuffling discarded cards")
463 if @stock.length == 0
464 announce _("No more cards!")
465 end_game # FIXME nope!
469 notify player, _("You picked %{picked}") % { :picked => picked.join(' ') }
470 player.cards += picked
474 return if get_player(user)
479 @bot.timer.reschedule(@join_timer, 10)
480 elsif @players.length > 1
481 announce _("game will start in 20 seconds")
482 @join_timer = @bot.timer.add_once(20) {
489 announce _('TODO end game')
490 @plugin.end_game(@channel)
495 class UnoPlugin < Plugin
502 def help(plugin, topic="")
503 (_("%{uno} game. !uno to start a game. in-game commands (no prefix): ") % {
506 _("'jo' to join in"),
507 _("'pl <card>' to play <card>"),
508 _("'pe' to pick a card"),
509 _("'pa' to pass your turn"),
510 _("'co <color>' to pick a color"),
511 _("'ca' to show current cards"),
512 _("'cd' to show the current discard"),
513 _("'od' to show the playing order"),
514 _("'ti' to show play time"),
515 _("'tu' to show whose turn it is")
520 return unless @games.key?(m.channel)
521 g = @games[m.channel]
524 g.add_player(m.source)
526 if g.has_turn?(m.source)
527 if g.player_has_picked
528 m.reply _("you already picked a card")
530 m.reply _("you can't pick a card")
532 g.pick_card(m.source)
535 m.reply _("It's not your turn")
538 if g.has_turn?(m.source)
541 m.reply _("It's not your turn")
544 if g.has_turn?(m.source)
545 g.play_card(m.source, m.params)
547 m.reply _("It's not your turn")
549 when :co # pick color
550 if g.has_turn?(m.source)
551 g.choose_color(m.source, m.params.downcase)
553 m.reply _("It's not your turn")
555 when :ca # show current cards
556 g.show_all_cards(m.source)
557 when :cd # show current discard
562 when :od # show playing order
564 when :ti # show play time
566 when :tu # show whose turn is it
567 if g.has_turn?(m.source)
568 m.nickreply _("it's your turn, sleepyhead")
570 g.show_turn(:cards => false)
575 def create_game(m, p)
576 if @games.key?(m.channel)
577 m.reply _("There is already an %{uno} game running here, say 'jo' to join in") % { :uno => UnoGame::UNO }
580 @games[m.channel] = UnoGame.new(self, m.channel)
581 m.reply _("Ok, created %{uno} game on %{channel}, say 'jo' to join in") % {
582 :uno => UnoGame::UNO,
583 :channel => m.channel
587 def end_game(channel)
588 @games.delete(channel)
591 def print_stock(m, p)
592 unless @games.key?(m.channel)
593 m.reply _("There is no %{uno} game running here") % { :uno => UnoGame::UNO }
596 stock = @games[m.channel].stock
597 m.reply(_("%{num} cards in stock: %{stock}") % {
598 :num => stock.length,
599 :stock => stock.join(' ')
600 }, :split_at => /#{NormalText}\s*/)
606 pg.map 'uno', :private => false, :action => :create_game
607 pg.map 'uno stock', :private => false, :action => :print_stock
608 pg.default_auth('stock', false)