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 choice of rules re stacking + and playing Reverse with them
19 COLORS = %w{Red Green Blue Yellow}
20 SPECIALS = %w{+2 Reverse Skip}
21 NUMERICS = (0..9).to_a
22 VALUES = NUMERICS + SPECIALS
24 def UnoGame.color_map(clr)
37 def UnoGame.irc_color_bg(clr)
38 Irc.color([:white,:black][COLORS.index(clr)%2],UnoGame.color_map(clr))
41 def UnoGame.irc_color_fg(clr)
42 Irc.color(UnoGame.color_map(clr))
45 def UnoGame.colorify(str, fg=false)
47 str.length.times do |i|
49 UnoGame.irc_color_fg(COLORS[i%4]) :
50 UnoGame.irc_color_bg(COLORS[i%4]) ) +str[i,1]
55 UNO = UnoGame.colorify('UNO!', true)
61 attr_reader :shortform
65 def initialize(color, value)
66 raise unless COLORS.include? color
68 raise unless VALUES.include? value
69 if NUMERICS.include? value
77 @shortform = (@color[0,1]+@value).downcase
79 @shortform = (@color[0,1]+@value.to_s[0,1]).downcase
81 @to_s = UnoGame.irc_color_bg(@color) +
82 Bold + ['', @color, @value, ''].join(' ') + NormalText
86 return 0 unless @value.to_s[0,1] == '+'
87 return @value[1,1].to_i
91 SPECIALS.include?(@value)
95 cc = self.color <=> other.color
97 return self.value.to_s <=> other.value.to_s
105 # Wild, Wild +4 cards
107 def initialize(value=nil)
109 raise if value and not value == '+4'
112 @shortform = 'w'+value
118 @to_s = UnoGame.colorify(['', @color, @value, ''].compact.join(' '))
135 has << c if c.shortform == short
144 Bold + @user.to_s + Bold
152 # previous discard, in case of challenge
153 attr_reader :last_discard
154 # channel the game is played in
158 # true if the player picked a card (and can thus pass turn)
159 attr_reader :player_has_picked
160 # number of cards to be picked if the player can't play an appropriate card
166 # the IRC user that created the game
167 attr_accessor :manager
169 def initialize(plugin, channel, manager)
194 return p if p.user == user
198 return p if p.user.irc_downcase == user.irc_downcase(channel.casemap)
201 get_player(user.to_s)
206 def announce(msg, opts={})
207 @bot.say channel, msg, opts
210 def notify(player, msg, opts={})
211 @bot.notice player.user, msg, opts
214 def notify_error(player, msg, opts={})
215 announce _("you can't do that, %{p}") % {
218 notify player, msg, opts
222 @base_stock = COLORS.inject([]) do |list, clr|
224 list << Card.new(clr, n)
225 list << Card.new(clr, n) unless n == 0
230 @base_stock << Wild.new
231 @base_stock << Wild.new('+4')
236 @stock.replace @base_stock
237 # remove the cards in the players hand
238 @players.each { |p| p.cards.each { |c| @stock.delete_one c } }
239 # remove current top discarded card if present
241 @stock.delete_one(discard)
248 debug "Starting game"
251 announce _("%{p} deals the first card from the stock") % {
257 while Wild === card do
258 @stock.insert(rand(@stock.length), card)
267 @start_time = Time.now
272 Utils.secs_to_string(Time.now-@start_time)
279 # if there are two players, the Reverse acts like a Skip, unless
280 # there's a @picker running, in which case the Reverse should bounce the
281 # pick on the other player
282 if @players.length > 2
284 # put the current player back in its place
285 @players.unshift @players.pop
286 announce _("Playing order was reversed!")
288 announce _("%{cp} bounces the pick to %{np}") % {
289 :cp => @players.first,
298 @players << @players.shift
299 announce _("%{p} skips a turn!") % {
300 # this is first and not last because the actual
301 # turn change will be done by the following next_turn
317 def set_discard(card)
319 @value = card.value.dup rescue card.value
323 @color = card.color.dup
326 @picker += card.picker
327 @last_picker = @discard.picker
336 def next_turn(opts={})
338 @players << @players.shift
339 @player_has_picked = false
340 show_turn unless opts[:silent]
344 # if play is forced, check against the only allowed cards
345 return false if @must_play and not @must_play.include?(card)
348 # During a picker run (i.e. after a +something was played and before a
349 # player is forced to pick) you can only play pickers (+2, +4) and
350 # Reverse. Reverse can be played if the previous card matches by color or
351 # value (as usual), a +4 can always be played, a +2 can be played on a +2
352 # of any color or on a Reverse of the correct color unless a +4 was
357 # Reverse can be played if it matches color or value
358 return (card.color == @color) || (@discard.value == card.value)
360 return false if @last_picker > 2
361 return true if @discard.value == card.value
362 return true if @discard.value == 'Reverse' and @color == card.color
370 # You can always play a Wild
371 return true if Wild === card
372 # On a Wild, you must match the color
374 return card.color == @color
376 # Otherwise, you can match either the value or the color
377 return (card.value == @value) || (card.color == @color)
382 def play_card(source, cards)
383 debug "Playing card #{cards}"
384 p = get_player(source)
385 shorts = cards.gsub(/\s+/,'').match(/^(?:([rbgy]\+?\d)\1?|([rbgy][rs])|(w(?:\+4)?)([rbgy])?)$/).to_a
388 announce _("what cards were that again?")
392 short = shorts[1] || shorts[2] || shorts[3]
398 toplay = (full == short) ? 1 : 2
400 debug [full, short, jolly, jcolor, toplay].inspect
401 # r7r7 -> r7r7, r7, nil, nil, 2
402 # r7 -> r7, r7, nil, nil, 1
403 # w -> w, nil, w, nil, 1
404 # wg -> wg, nil, w, g, 1
406 # if @color is nil, the player just played a wild without specifying
407 # a color. (s)he should now use "co <colorname>", but we allow him to
408 # replay the wild _and_ specify the color, without actually replaying
409 # the card (which would otherwise happen if the player has another wild)
412 choose_color(p.user, jcolor)
414 announce _("you already played your card, ") + _("%{p}, choose a color with: co r|b|g|y") % { :p => p }
419 if cards = p.has_card?(short)
421 unless can_play(cards.first)
422 notify_error p, _("you can't play that card")
425 if cards.length >= toplay
426 # if the played card is a W+4 not played during a stacking +x
427 # TODO if A plays an illegal W+4, B plays a W+4, should the next
428 # player be able to challenge A? For the time being we say no,
429 # but I think he should, and in case A's move was illegal
430 # game would have to go back, A would get the penalty and replay,
431 # while if it was legal the challenger would get 50% more cards,
432 # i.e. 12 cards (or more if the stacked +4 were more). This would
433 # only be possible if the first W+4 was illegal, so it wouldn't
434 # apply for a W+4 played on a +2 anyway.
436 if @picker == 0 and Wild === cards.first and cards.first.value
437 # save the previous discard in case of challenge
438 @last_discard = @discard.dup
439 # save the color too, in case it was a Wild
440 @last_color = @color.dup
442 # mark the move as not challengeable
446 set_discard(p.cards.delete_one(cards.shift))
448 set_discard(p.cards.delete_one(cards.shift))
449 announce _("%{p} plays %{card} twice!") % {
454 announce _("%{p} plays %{card}") % { :p => p, :card => @discard }
456 if p.cards.length == 1
457 announce _("%{p} has %{uno}!") % {
460 elsif p.cards.length == 0
471 choose_color(p.user, jcolor)
473 announce _("%{p}, choose a color with: co r|b|g|y") % { :p => p }
476 notify_error p, _("you don't have two cards of that kind")
479 notify_error p, _("you don't have that card")
484 return unless @last_discard
489 announce _("%{cp} challenges %{lp}'s %{card}!") % {
490 :cp => cp, :lp => lp, :card => @discard
492 # show the cards of the previous player to the current player
493 notify cp, _("%{p} has %{cards}") % {
494 :p => lp, :cards => lp.cards.join(' ')
496 # check if the previous player had a non-special card of the correct color
499 if c.color == @last_color and not c.special?
506 announce _("%{lp}'s move was legal, %{cp} must pick %{b}%{n}%{b} cards!") % {
507 :cp => cp, :lp => lp, :b => Bold, :n => @picker
515 announce _("%{lp}'s move was %{b}not%{b} legal, %{lp} must pick %{b}%{n}%{b} cards and play again!") % {
516 :cp => cp, :lp => lp, :b => Bold, :n => @picker
518 played = @discard # store the misplayed W+4
521 @color = @last_color.dup
522 @discard = @last_discard.dup
524 @value = @discard.value.dup rescue @discard.value
528 # force the player to play the current cards
529 @must_play = lp.cards.dup
530 # but not the same (type of) card he misplayed, though
531 @must_play.delete(played)
533 lp.cards << played # reinstate the W+4 in the list of player cards
534 # give him the penalty cards
538 # and restore the turn
539 @players.unshift @players.pop
546 announce _("%{p} passes turn, and has to pick %{b}%{n}%{b} cards!") % {
547 :p => p, :b => Bold, :n => @picker
551 # make sure that if this is the "pick and pass" after a W+4,
552 # then the following player cannot do a challenge:
556 if @player_has_picked
557 announce _("%{p} passes turn") % { :p => p }
559 announce _("you need to pick a card first")
566 def choose_color(user, color)
567 # you can only pick a color if the current color is unset
569 announce _("you can't pick a color now, %{p}") % {
570 :p => get_player(user)
584 announce _('what color is that?')
587 announce _('color is now %{c}') % {
588 :c => UnoGame.irc_color_bg(@color)+" #{@color} "
595 announce _("This %{uno} game has been going on for %{time}") % {
597 :time => elapsed_time
600 announce _("The game hasn't started yet")
605 announce _("%{uno} playing turn: %{players}") % {
606 :uno => UNO, :players => players.join(' ')
610 def show_turn(opts={})
612 announce _("nobody is playing %{uno} yet!") % {
618 cards = opts[:cards] if opts.key?(:cards)
619 player = @players.first
620 announce _("it's %{player}'s turn") % { :player => player }
621 show_user_cards(player) if cards
624 def has_turn?(source)
625 @start_time && (@players.first.user == source)
630 announce _("next player must respond correctly or pick %{b}%{n}%{b} cards") % {
631 :b => Bold, :n => @picker
637 announce _("Current discard: %{card} %{c}") % { :card => @discard,
638 :c => (Wild === @discard) ? UnoGame.irc_color_bg(@color) + " #{@color} " : nil
643 def show_user_cards(player)
644 p = Player === player ? player : get_player(player)
646 notify p, _('Your cards: %{cards}') % {
647 :cards => p.cards.join(' ')
651 def show_all_cards(u=nil)
652 announce(@players.inject([]) { |list, p|
653 list << [p, p.cards.length].join(': ')
662 announce _("%{player} picks a card") % { :player => p }
664 @player_has_picked = true
667 def deal(player, num=1)
670 picked << @stock.delete_one
671 if @stock.length == 0
672 announce _("Shuffling discarded cards")
674 if @stock.length == 0
675 announce _("No more cards!")
676 end_game # FIXME nope!
681 notify player, _("You picked %{picked}") % { :picked => picked.join(' ') }
682 player.cards += picked
687 if p = get_player(user)
688 announce _("you're already in the game, %{p}") % {
693 @dropouts.each do |dp|
695 announce _("you dropped from the game, %{p}, you can't get back in") % {
702 announce _("you can't join now, %{p}, a %{card} was just played, wait until next turn") % {
710 cards = (@players.inject(0) do |s, pl|
712 end*1.0/@players.length).ceil
716 announce _("%{p} joins this game of %{uno}") % {
720 return if @start_time
722 @bot.timer.reschedule(@join_timer, 10)
723 elsif @players.length > 1
724 announce _("game will start in 20 seconds")
725 @join_timer = @bot.timer.add_once(20) {
731 def drop_player(nick)
732 # A nick is passed because the original player might have left
734 unless p = get_player(nick)
735 announce _("%{p} isn't playing %{uno}") % {
740 announce _("%{p} gives up this game of %{uno}") % {
746 @bot.timer.remove(@join_timer)
747 announce _("game start countdown stopped")
750 if p == @players.first
751 next_turn :silent => @start_time.nil?
762 while p.cards.length > 0
763 @stock.insert(rand(@stock.length), p.cards.shift)
766 @dropouts << @players.delete_one(p)
769 def replace_player(old, new)
771 user = channel.get_user(new)
773 announce _("there is no '%{nick}' here") % {
778 if pl = get_player(user)
779 announce _("%{p} is already playing %{uno} here") % {
780 :p => pl, :uno => UNO
784 # We scan the player list of the player with the old nick, instead
785 # of using get_player, in case of IRC drops etc
787 if p.user.nick == old
789 announce _("%{p} takes %{b}%{old}%{b}'s place at %{uno}") % {
790 :p => p, :b => Bold, :old => old, :uno => UNO
795 announce _("%{b}%{old}%{b} isn't playing %{uno} here") % {
796 :uno => UNO, :b => Bold, :old => old
801 def end_game(halted = false)
802 runtime = @start_time ? Time.now - @start_time : 0
804 @bot.timer.remove(@join_timer)
805 announce _("game start countdown stopped")
810 announce _("%{uno} game halted after %{time}") % {
811 :time => elapsed_time,
815 announce _("%{uno} game halted before it could start") % {
820 announce _("%{uno} game finished after %{time}! The winner is %{p}") % {
821 :time => elapsed_time,
822 :uno => UNO, :p => @players.first
825 if @picker > 0 and not halted
826 if @discard.value == 'Reverse'
831 announce _("%{p} has to pick %{b}%{n}%{b} cards!") % {
832 :p => p, :n => @picker, :b => Bold
837 score = @players.inject(0) do |sum, pl|
838 if pl.cards.length > 0
839 announce _("%{p} still had %{cards}") % {
840 :p => pl, :cards => pl.cards.join(' ')
842 sum += pl.cards.inject(0) do |cs, c|
849 closure = { :dropouts => @dropouts, :players => @players, :runtime => runtime }
851 announce _("%{p} wins with %{b}%{score}%{b} points!") % {
852 :p => @players.first, :score => score, :b => Bold
854 closure.merge!(:winner => @players.first, :score => score,
855 :opponents => @players.length - 1)
858 @plugin.do_end_game(@channel, closure)
863 # A won game: store score and number of opponents, so we can calculate
864 # an average score per opponent (requested by Squiddhartha)
865 define_structure :UnoGameWon, :score, :opponents
866 # For each player we store the number of games played, the number of
867 # games forfeited, and an UnoGameWon for each won game
868 define_structure :UnoPlayerStats, :played, :forfeits, :won
870 class UnoPlugin < Plugin
877 def help(plugin, topic="")
881 _("'jo' to join in"),
882 _("'pl <card>' to play <card>: e.g. 'pl g7' to play Green 7, or 'pl rr' to play Red Reverse, or 'pl y2y2' to play both Yellow 2 cards"),
883 _("'pe' to pick a card"),
884 _("'pa' to pass your turn"),
885 _("'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)"),
886 _("'ca' to show current cards"),
887 _("'cd' to show the current discard"),
888 _("'ch' to challenge a Wild +4"),
889 _("'od' to show the playing order"),
890 _("'ti' to show play time"),
891 _("'tu' to show whose turn it is")
894 _("A Wild +4 can only be played legally if you don't have normal (not special) cards of the current color. ") +
895 _("The next player can challenge a W+4 by using the 'ch' command. ") +
896 _("If the W+4 play was illegal, the player who played it must pick the W+4, pick 4 cards from the stock, and play a legal card. ") +
897 _("If the W+4 play was legal, the challenger must pick 6 cards instead of 4.")
899 _("play all your cards, one at a time, by matching either the color or the value of the currently discarded card. ") +
900 _("cards with special effects: Skip (next player skips a turn), Reverse (reverses the playing order), +2 (next player has to take 2 cards). ") +
901 _("Wilds can be played on any card, and you must specify the color for the next card. ") +
902 _("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. ") +
903 _("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. ") +
904 _("you can also play a Reverse on a +2 or +4, bouncing the effect back to the previous player (that now comes next). ")
905 when /scor(?:e|ing)/, /points?/
907 _("The points won with a game of %{uno} are totalled from the cards remaining in the hands of the other players."),
908 _("Each normal (not special) card is worth its face value (from 0 to 9 points)."),
909 _("Each colored special card (+2, Reverse, Skip) is worth 20 points."),
910 _("Each Wild and Wild +4 is worth 50 points."),
912 help(plugin, 'topwin'),
913 ].join(" ") % { :uno => UnoGame::UNO }
915 _("You can see the scoring table with 'uno top N' where N is the number of top scores to show.")
917 _("You can see the winners table with 'uno topwin N' where N is the number of top winners to show.")
920 _("There are 108 cards in a standard %{uno} deck."),
921 _("For each color (Blue, Green, Red, Yellow) there are 19 numbered cards (from 0 to 9), with two of each number except for 0."),
922 _("There are also 6 special cards for each color, two each of +2, Reverse, Skip."),
923 _("Finally, there are 4 Wild and 4 Wild +4 cards.")
924 ].join(" ") % { :uno => UnoGame::UNO }
926 _("The game manager (the user that started the game) can execute the following commands to manage it: ") +
928 _("'uno drop <user>' to drop a user from the game (any user can drop itself using 'uno drop')"),
929 _("'uno replace <old> [with] <new>' to replace a player with someone else (useful in case of disconnects)"),
930 _("'uno transfer [to] <nick>' to transfer game ownership to someone else"),
931 _("'uno end' to end the game before its natural completion")
934 _("%{uno} game. !uno to start a game. see 'help uno rules' for the rules, 'help uno admin' for admin commands, 'help uno score' for scoring rules. In-game commands: %{cmds}.") % {
935 :uno => UnoGame::UNO,
936 :cmds => help(plugin, 'commands')
942 return unless @games.key?(m.channel)
943 return unless m.plugin # skip messages such as: <someuser> botname,
944 g = @games[m.channel]
949 g.add_player(m.source)
952 if g.has_turn?(m.source)
953 if g.player_has_picked
954 m.reply _("you already picked a card")
958 g.pick_card(m.source)
961 m.reply _("It's not your turn")
964 return if m.params or not g.start_time
965 if g.has_turn?(m.source)
968 m.reply _("It's not your turn")
971 if g.has_turn?(m.source)
972 g.play_card(m.source, m.params.downcase)
974 m.reply _("It's not your turn")
976 when :co # pick color
977 if g.has_turn?(m.source)
978 g.choose_color(m.source, m.params.downcase)
980 m.reply _("It's not your turn")
982 when :ca # show current cards
984 g.show_all_cards(m.source)
985 when :cd # show current discard
986 return if m.params or not g.start_time
989 if g.has_turn?(m.source)
993 m.reply _("previous move cannot be challenged")
996 m.reply _("It's not your turn")
998 when :od # show playing order
1001 when :ti # show play time
1004 when :tu # show whose turn is it
1006 if g.has_turn?(m.source)
1007 m.reply _("it's your turn, sleepyhead"), :nick => true
1009 g.show_turn(:cards => false)
1014 m.replied=true if replied
1017 def create_game(m, p)
1018 if @games.key?(m.channel)
1019 m.reply _("There is already an %{uno} game running here, managed by %{who}. say 'jo' to join in") % {
1020 :who => @games[m.channel].manager,
1021 :uno => UnoGame::UNO
1025 @games[m.channel] = UnoGame.new(self, m.channel, m.source)
1026 @bot.auth.irc_to_botuser(m.source).set_temp_permission('uno::manage', true, m.channel)
1027 m.reply _("Ok, created %{uno} game on %{channel}, say 'jo' to join in") % {
1028 :uno => UnoGame::UNO,
1029 :channel => m.channel
1033 def transfer_ownership(m, p)
1034 unless @games.key?(m.channel)
1035 m.reply _("There is no %{uno} game running here") % { :uno => UnoGame::UNO }
1038 g = @games[m.channel]
1040 new = m.channel.get_user(p[:nick])
1043 @bot.auth.irc_to_botuser(old).reset_temp_permission('uno::manage', m.channel)
1044 @bot.auth.irc_to_botuser(new).set_temp_permission('uno::manage', true, m.channel)
1045 m.reply _("%{uno} game ownership transferred from %{old} to %{nick}") % {
1046 :uno => UnoGame::UNO, :old => old, :nick => p[:nick]
1049 m.reply _("who is this %{nick} you want me to transfer game ownership to?") % p
1054 unless @games.key?(m.channel)
1055 m.reply _("There is no %{uno} game running here") % { :uno => UnoGame::UNO }
1058 @games[m.channel].end_game(true)
1062 @games.each { |k, g| g.end_game(true) }
1066 def chan_reg(channel)
1067 @registry.sub_registry(channel.downcase)
1070 def chan_stats(channel)
1071 stats = chan_reg(channel).sub_registry('stats')
1080 stats.set_default(0)
1084 def chan_pstats(channel)
1085 pstats = chan_reg(channel).sub_registry('players')
1086 pstats.set_default(UnoPlayerStats.new(0,0,[]))
1090 def do_end_game(channel, closure)
1091 reg = chan_reg(channel)
1092 stats = chan_stats(channel)
1093 stats['played'] += 1
1094 stats['played_runtime'] += closure[:runtime]
1096 stats['finished'] += 1
1097 stats['finished_runtime'] += closure[:runtime]
1099 pstats = chan_pstats(channel)
1101 closure[:players].each do |pl|
1102 k = pl.user.downcase
1108 closure[:dropouts].each do |pl|
1109 k = pl.user.downcase
1116 winner = closure[:winner]
1117 won = UnoGameWon.new(closure[:score], closure[:opponents])
1118 k = winner.user.downcase
1119 pls = pstats[k] # already marked played +1 above
1124 @bot.auth.irc_to_botuser(@games[channel].manager).reset_temp_permission('uno::manage', channel)
1125 @games.delete(channel)
1128 def do_chanstats(m, p)
1129 stats = chan_stats(m.channel)
1130 np = stats['played']
1131 nf = stats['finished']
1133 str = _("%{nf} %{uno} games completed over %{np} games played. ") % {
1134 :np => np, :uno => UnoGame::UNO, :nf => nf
1136 cgt = stats['finished_runtime']
1137 tgt = stats['played_runtime']
1138 str << _("%{cgt} game time for completed games") % {
1139 :cgt => Utils.secs_to_string(cgt)
1142 str << _(" on %{tgt} total game time. ") % {
1143 :tgt => Utils.secs_to_string(tgt)
1148 str << _("%{avg} average game time for completed games") % {
1149 :avg => Utils.secs_to_string(cgt/nf)
1151 str << _(", %{tavg} for all games") % {
1152 :tavg => Utils.secs_to_string(tgt/np)
1156 m.reply _("nobody has played %{uno} on %{chan} yet") % {
1157 :uno => UnoGame::UNO, :chan => m.channel
1163 dnick = p[:nick] || m.source # display-nick, don't later case
1164 nick = dnick.downcase
1165 ps = chan_pstats(m.channel)[nick]
1167 m.reply _("%{nick} never played %{uno} here") % {
1168 :uno => UnoGame::UNO, :nick => dnick
1175 score = ps.won.inject(0) { |sum, w| sum += w.score }
1176 str = _("%{nick} played %{np} %{uno} games here, ") % {
1177 :nick => dnick, :np => np, :uno => UnoGame::UNO
1179 str << _("forfeited %{nf} games, ") % { :nf => nf } if nf > 0
1180 str << _("won %{nw} games") % { :nw => nw}
1182 str << _(" with %{score} total points") % { :score => score }
1183 avg = ps.won.inject(0) { |sum, w| sum += w.score/w.opponents }/nw
1184 str << _(" and an average of %{avg} points per opponent") % { :avg => avg }
1189 def replace_player(m, p)
1190 unless @games.key?(m.channel)
1191 m.reply _("There is no %{uno} game running here") % { :uno => UnoGame::UNO }
1194 @games[m.channel].replace_player(p[:old], p[:new])
1197 def drop_player(m, p)
1198 unless @games.key?(m.channel)
1199 m.reply _("There is no %{uno} game running here") % { :uno => UnoGame::UNO }
1202 @games[m.channel].drop_player(p[:nick] || m.source.nick)
1205 def print_stock(m, p)
1206 unless @games.key?(m.channel)
1207 m.reply _("There is no %{uno} game running here") % { :uno => UnoGame::UNO }
1210 stock = @games[m.channel].stock
1211 m.reply(_("%{num} cards in stock: %{stock}") % {
1212 :num => stock.length,
1213 :stock => stock.join(' ')
1214 }, :split_at => /#{NormalText}\s*/)
1218 pstats = chan_pstats(m.channel)
1221 pstats.each do |k, v|
1222 wins << [v.won.length, k]
1223 scores << [v.won.inject(0) { |s, w| s+=w.score }, k]
1227 m.reply(_("no %{uno} games were completed here") % {
1228 :uno => UnoGame::UNO
1235 msg = _("%{uno} %{num} highest scores: ") % {
1236 :uno => UnoGame::UNO, :num => p[:scorenum]
1238 scores.sort! { |a1, a2| -(a1.first <=> a2.first) }
1239 scores = scores[0, n.to_i].compact
1241 if scores.length <= 5
1242 list = "\n" + scores.map { |a|
1244 _("%{i}. %{b}%{nick}%{b} with %{b}%{score}%{b} points") % {
1245 :i => i, :b => Bold, :nick => a.last, :score => a.first
1249 list = scores.map { |a|
1251 _("%{i}. %{nick} ( %{score} )") % {
1252 :i => i, :nick => a.last, :score => a.first
1256 elsif n = p[:winnum]
1257 msg = _("%{uno} %{num} most wins: ") % {
1258 :uno => UnoGame::UNO, :num => p[:winnum]
1260 wins.sort! { |a1, a2| -(a1.first <=> a2.first) }
1261 wins = wins[0, n.to_i].compact
1264 list = "\n" + wins.map { |a|
1266 _("%{i}. %{b}%{nick}%{b} with %{b}%{score}%{b} wins") % {
1267 :i => i, :b => Bold, :nick => a.last, :score => a.first
1271 list = wins.map { |a|
1273 _("%{i}. %{nick} ( %{score} )") % {
1274 :i => i, :nick => a.last, :score => a.first
1279 msg = _("uh, what kind of score list did you want, again?")
1280 list = _(" I can only show the top scores (with top) and the most wins (with topwin)")
1282 m.reply msg + list, :max_lines => (msg+list).count("\n")+1
1288 pg.map 'uno', :private => false, :action => :create_game
1289 pg.map 'uno end', :private => false, :action => :end_game, :auth_path => 'manage'
1290 pg.map 'uno drop', :private => false, :action => :drop_player, :auth_path => 'manage::drop::self!'
1291 pg.map 'uno giveup', :private => false, :action => :drop_player, :auth_path => 'manage::drop::self!'
1292 pg.map 'uno drop :nick', :private => false, :action => :drop_player, :auth_path => 'manage::drop::other!'
1293 pg.map 'uno replace :old [with] :new', :private => false, :action => :replace_player, :auth_path => 'manage'
1294 pg.map 'uno transfer [game [ownership]] [to] :nick', :private => false, :action => :transfer_ownership, :auth_path => 'manage'
1295 pg.map 'uno stock', :private => false, :action => :print_stock
1296 pg.map 'uno chanstats', :private => false, :action => :do_chanstats
1297 pg.map 'uno stats [:nick]', :private => false, :action => :do_pstats
1298 pg.map 'uno top :scorenum', :private => false, :action => :do_top, :defaults => { :scorenum => 5 }
1299 pg.map 'uno topwin :winnum', :private => false, :action => :do_top, :defaults => { :winnum => 5 }
1301 pg.default_auth('stock', false)
1302 pg.default_auth('manage', false)
1303 pg.default_auth('manage::drop::self', true)