]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/games/uno.rb
uno plugin: mark all legit moves as not challengeable
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / games / uno.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: Uno Game Plugin for rbot
5 #
6 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
7 #
8 # Copyright:: (C) 2008 Giuseppe Bilotta
9 #
10 # License:: GPL v2
11 #
12 # Uno Game: get rid of the cards you have
13 #
14 # TODO documentation
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
19
20 class UnoGame
21   COLORS = %w{Red Green Blue Yellow}
22   SPECIALS = %w{+2 Reverse Skip}
23   NUMERICS = (0..9).to_a
24   VALUES = NUMERICS + SPECIALS
25
26   def UnoGame.color_map(clr)
27     case clr
28     when 'Red'
29       :red
30     when 'Blue'
31       :royal_blue
32     when 'Green'
33       :limegreen
34     when 'Yellow'
35       :yellow
36     end
37   end
38
39   def UnoGame.irc_color_bg(clr)
40     Irc.color([:white,:black][COLORS.index(clr)%2],UnoGame.color_map(clr))
41   end
42
43   def UnoGame.irc_color_fg(clr)
44     Irc.color(UnoGame.color_map(clr))
45   end
46
47   def UnoGame.colorify(str, fg=false)
48     ret = Bold.dup
49     str.length.times do |i|
50       ret << (fg ?
51               UnoGame.irc_color_fg(COLORS[i%4]) :
52               UnoGame.irc_color_bg(COLORS[i%4]) ) +str[i,1]
53     end
54     ret << NormalText
55   end
56
57   UNO = UnoGame.colorify('UNO!', true)
58
59   # Colored play cards
60   class Card
61     attr_reader :color
62     attr_reader :value
63     attr_reader :shortform
64     attr_reader :to_s
65     attr_reader :score
66
67     def initialize(color, value)
68       raise unless COLORS.include? color
69       @color = color.dup
70       raise unless VALUES.include? value
71       if NUMERICS.include? value
72         @value = value
73         @score = value
74       else
75         @value = value.dup
76         @score = 20
77       end
78       if @value == '+2'
79         @shortform = (@color[0,1]+@value).downcase
80       else
81         @shortform = (@color[0,1]+@value.to_s[0,1]).downcase
82       end
83       @to_s = UnoGame.irc_color_bg(@color) +
84         Bold + ['', @color, @value, ''].join(' ') + NormalText
85     end
86
87     def picker
88       return 0 unless @value.to_s[0,1] == '+'
89       return @value[1,1].to_i
90     end
91
92     def special?
93       SPECIALS.include?(@value)
94     end
95
96     def <=>(other)
97       cc = self.color <=> other.color
98       if cc == 0
99         return self.value.to_s <=> other.value.to_s
100       else
101         return cc
102       end
103     end
104     include Comparable
105   end
106
107   # Wild, Wild +4 cards
108   class Wild < Card
109     def initialize(value=nil)
110       @color = 'Wild'
111       raise if value and not value == '+4'
112       if value
113         @value = value.dup 
114         @shortform = 'w'+value
115       else
116         @value = nil
117         @shortform = 'w'
118       end
119       @score = 50
120       @to_s = UnoGame.colorify(['', @color, @value, ''].compact.join(' '))
121     end
122     def special?
123       @value
124     end
125   end
126
127   class Player
128     attr_accessor :cards
129     attr_accessor :user
130     def initialize(user)
131       @user = user
132       @cards = []
133     end
134     def has_card?(short)
135       has = []
136       @cards.each { |c|
137         has << c if c.shortform == short
138       }
139       if has.empty?
140         return false
141       else
142         return has
143       end
144     end
145     def to_s
146       Bold + @user.to_s + Bold
147     end
148   end
149
150   # cards in stock
151   attr_reader :stock
152   # current discard
153   attr_reader :discard
154   # previous discard, in case of challenge
155   attr_reader :last_discard
156   # channel the game is played in
157   attr_reader :channel
158   # list of players
159   attr :players
160   # true if the player picked a card (and can thus pass turn)
161   attr_reader :player_has_picked
162   # number of cards to be picked if the player can't play an appropriate card
163   attr_reader :picker
164
165   def initialize(plugin, channel)
166     @channel = channel
167     @plugin = plugin
168     @bot = plugin.bot
169     @players = []
170     @dropouts = []
171     @discard = nil
172     @last_discard = nil
173     @value = nil
174     @color = nil
175     make_base_stock
176     @stock = []
177     make_stock
178     @start_time = nil
179     @join_timer = nil
180     @picker = 0
181     @last_picker = 0
182     @must_play = nil
183   end
184
185   def get_player(user)
186     case user
187     when User
188       @players.each do |p|
189         return p if p.user == user
190       end
191     when String
192       @players.each do |p|
193         return p if p.user.irc_downcase == user.irc_downcase(channel.casemap)
194       end
195     else
196       get_player(user.to_s)
197     end
198     return nil
199   end
200
201   def announce(msg, opts={})
202     @bot.say channel, msg, opts
203   end
204
205   def notify(player, msg, opts={})
206     @bot.notice player.user, msg, opts
207   end
208
209   def make_base_stock
210     @base_stock = COLORS.inject([]) do |list, clr|
211       VALUES.each do |n|
212         list << Card.new(clr, n)
213         list << Card.new(clr, n) unless n == 0
214       end
215       list
216     end
217     4.times do
218       @base_stock << Wild.new
219       @base_stock << Wild.new('+4')
220     end
221   end
222
223   def make_stock
224     @stock.replace @base_stock
225     # remove the cards in the players hand
226     @players.each { |p| p.cards.each { |c| @stock.delete_one c } }
227     # remove current top discarded card if present
228     if @discard
229       @stock.delete_one(discard)
230     end
231     @stock.shuffle!
232   end
233
234   def start_game
235     debug "Starting game"
236     @players.shuffle!
237     show_order
238     announce _("%{p} deals the first card from the stock") % {
239       :p => @players.first
240     }
241     card = @stock.shift
242     @picker = 0
243     @special = false
244     while Wild === card do
245       @stock.insert(rand(@stock.length), card)
246       card = @stock.shift
247     end
248     set_discard(card)
249     show_discard
250     if @special
251       do_special
252     end
253     next_turn
254     @start_time = Time.now
255   end
256
257   def elapsed_time
258     if @start_time
259       Utils.secs_to_string(Time.now-@start_time)
260     else
261       _("no time")
262     end
263   end
264
265   def reverse_turn
266     if @players.length > 2
267       @players.reverse!
268       # put the current player back in its place
269       @players.unshift @players.pop
270       announce _("Playing order was reversed!")
271     else
272       skip_turn
273     end
274   end
275
276   def skip_turn
277     @players << @players.shift
278     announce _("%{p} skips a turn!") % {
279       # this is first and not last because the actual
280       # turn change will be done by the following next_turn
281       :p => @players.first
282     }
283   end
284
285   def do_special
286     case @discard.value
287     when 'Reverse'
288       reverse_turn
289       @special = false
290     when 'Skip'
291       skip_turn
292       @special = false
293     end
294   end
295
296   def set_discard(card)
297     @discard = card
298     @value = card.value.dup rescue card.value
299     if Wild === card
300       @color = nil
301     else
302       @color = card.color.dup
303     end
304     if card.picker > 0
305       @picker += card.picker
306       @last_picker = @discard.picker
307     end
308     if card.special?
309       @special = true
310     else
311       @special = false
312     end
313     @must_play = nil
314   end
315
316   def next_turn(opts={})
317     @players << @players.shift
318     @player_has_picked = false
319     show_turn
320   end
321
322   def can_play(card)
323     # if play is forced, check against the only allowed cards
324     return false if @must_play and not @must_play.include?(card)
325
326     # When a +something is online, you can only play
327     # a +something of same or higher something, or a Reverse of
328     # the correct color
329     # TODO make optional
330     if @picker > 0
331       if (card.value == 'Reverse' and card.color == @color) or card.picker >= @last_picker
332         return true
333       else
334         return false
335       end
336     else
337       # You can always play a Wild
338       return true if Wild === card
339       # On a Wild, you must match the color
340       if Wild === @discard
341         return card.color == @color
342       else
343         # Otherwise, you can match either the value or the color
344         return (card.value == @value) || (card.color == @color)
345       end
346     end
347   end
348
349   def play_card(source, cards)
350     debug "Playing card #{cards}"
351     p = get_player(source)
352     shorts = cards.gsub(/\s+/,'').match(/^(?:([rbgy]\+?\d){1,2}|([rbgy][rs])|(w(?:\+4)?)([rbgy])?)$/).to_a
353     debug shorts.inspect
354     if shorts.empty?
355       announce _("what cards were that again?")
356       return
357     end
358     full = shorts[0]
359     short = shorts[1] || shorts[2] || shorts[3]
360     jolly = shorts[3]
361     jcolor = shorts[4]
362     if jolly
363       toplay = 1
364     else
365       toplay = (full == short) ? 1 : 2
366     end
367     debug [full, short, jolly, jcolor, toplay].inspect
368     # r7r7 -> r7r7, r7, nil, nil
369     # r7 -> r7, r7, nil, nil
370     # w -> w, nil, w, nil
371     # wg -> wg, nil, w, g
372     if cards = p.has_card?(short)
373       debug cards
374       unless can_play(cards.first)
375         announce _("you can't play that card")
376         return
377       end
378       if cards.length >= toplay
379         # if the played card is a W+4 not played during a stacking +x
380         # TODO if A plays an illegal W+4, B plays a W+4, should the next
381         # player be able to challenge A? For the time being we say no,
382         # but I think he should, and in case A's move was illegal
383         # game would have to go back, A would get the penalty and replay,
384         # while if it was legal the challenger would get 50% more cards,
385         # i.e. 12 cards (or more if the stacked +4 were more). This would
386         # only be possible if the first W+4 was illegal, so it wouldn't
387         # apply for a W+4 played on a +2 anyway.
388         #
389         if @picker == 0 and Wild === cards.first and cards.first.value 
390           # save the previous discard in case of challenge
391           @last_discard = @discard.dup
392           # save the color too, in case it was a Wild
393           @last_color = @color.dup
394         else
395           # mark the move as not challengeable
396           @last_discard = nil
397           @last_color = nil
398         end
399         set_discard(p.cards.delete_one(cards.shift))
400         if toplay > 1
401           set_discard(p.cards.delete_one(cards.shift))
402           announce _("%{p} plays %{card} twice!") % {
403             :p => p,
404             :card => @discard
405           }
406         else
407           announce _("%{p} plays %{card}") % { :p => p, :card => @discard }
408         end
409         if p.cards.length == 1
410           announce _("%{p} has %{uno}!") % {
411             :p => p, :uno => UNO
412           }
413         elsif p.cards.length == 0
414           end_game
415           return
416         end
417         show_picker
418         if @color
419           if @special
420             do_special
421           end
422           next_turn
423         elsif jcolor
424           choose_color(p.user, jcolor)
425         else
426           announce _("%{p}, choose a color with: co r|b|g|y") % { :p => p }
427         end
428       else
429         announce _("you don't have two cards of that kind")
430       end
431     else
432       announce _("you don't have that card")
433     end
434   end
435
436   def challenge
437     return unless @last_discard
438     # current player
439     cp = @players.first
440     # previous player
441     lp = @players.last
442     announce _("%{cp} challenges %{lp}'s %{card}!") % {
443       :cp => cp, :lp => lp, :card => @discard
444     }
445     # show the cards of the previous player to the current player
446     notify cp, _("%{p} has %{cards}") % {
447       :p => lp, :cards => lp.cards.join(' ')
448     }
449     # check if the previous player had a non-special card of the correct color
450     legal = true
451     lp.cards.each do |c|
452       if c.color == @last_color and not c.special?
453         legal = false
454       end
455     end
456     if legal
457       @picker += 2
458       announce _("%{lp}'s move was legal, %{cp} must pick %{b}%{n}%{b} cards!") % {
459         :cp => cp, :lp => lp, :b => Bold, :n => @picker
460       }
461       @last_color = nil
462       @last_discard = nil
463       deal(cp, @picker)
464       @picker = 0
465       next_turn
466     else
467       announce _("%{lp}'s move was %{b}not%{b} legal, %{lp} must pick %{b}%{n}%{b} cards and play again!") % {
468         :cp => cp, :lp => lp, :b => Bold, :n => @picker
469       }
470       lp.cards << @discard # put the W+4 back in place
471
472       # reset the discard
473       @color = @last_color.dup
474       @discard = @last_discard.dup
475       @special = false
476       @value = @discard.value.dup rescue @discard.value
477       @last_color = nil
478       @last_discard = nil
479
480       # force the player to play the current cards
481       @must_play = lp.cards.dup
482
483       # give him the penalty cards
484       deal(lp, @picker)
485       @picker = 0
486
487       # and restore the turn
488       @players.unshift @players.pop
489     end
490   end
491
492   def pass(user)
493     p = get_player(user)
494     if @picker > 0
495       announce _("%{p} passes turn, and has to pick %{b}%{n}%{b} cards!") % {
496         :p => p, :b => Bold, :n => @picker
497       }
498       deal(p, @picker)
499       @picker = 0
500     else
501       if @player_has_picked
502         announce _("%{p} passes turn") % { :p => p }
503       else
504         announce _("you need to pick a card first")
505         return
506       end
507     end
508     next_turn
509   end
510
511   def choose_color(user, color)
512     case color
513     when 'r'
514       @color = 'Red'
515     when 'b'
516       @color = 'Blue'
517     when 'g'
518       @color = 'Green'
519     when 'y'
520       @color = 'Yellow'
521     else
522       announce _('what color is that?')
523       return
524     end
525     announce _('color is now %{c}') % {
526       :c => UnoGame.irc_color_bg(@color)+" #{@color} "
527     }
528     next_turn
529   end
530
531   def show_time
532     if @start_time
533       announce _("This %{uno} game has been going on for %{time}") % {
534         :uno => UNO,
535         :time => elapsed_time
536       }
537     else
538       announce _("The game hasn't started yet")
539     end
540   end
541
542   def show_order
543     announce _("%{uno} playing turn: %{players}") % {
544       :uno => UNO, :players => players.join(' ')
545     }
546   end
547
548   def show_turn(opts={})
549     cards = true
550     cards = opts[:cards] if opts.key?(:cards)
551     player = @players.first
552     announce _("it's %{player}'s turn") % { :player => player }
553     show_user_cards(player) if cards
554   end
555
556   def has_turn?(source)
557     @players.first.user == source
558   end
559
560   def show_picker
561     if @picker > 0
562       announce _("next player must respond correctly or pick %{b}%{n}%{b} cards") % {
563         :b => Bold, :n => @picker
564       }
565     end
566   end
567
568   def show_discard
569     announce _("Current discard: %{card} %{c}") % { :card => @discard,
570       :c => (Wild === @discard) ? UnoGame.irc_color_bg(@color) + " #{@color} " : nil
571     }
572     show_picker
573   end
574
575   def show_user_cards(player)
576     p = Player === player ? player : get_player(player)
577     notify p, _('Your cards: %{cards}') % {
578       :cards => p.cards.join(' ')
579     }
580   end
581
582   def show_all_cards(u=nil)
583     announce(@players.inject([]) { |list, p|
584       list << [p, p.cards.length].join(': ')
585     }.join(', '))
586     if u
587       show_user_cards(u)
588     end
589   end
590
591   def pick_card(user)
592     p = get_player(user)
593     announce _("%{player} picks a card") % { :player => p }
594     deal(p, 1)
595     @player_has_picked = true
596   end
597
598   def deal(player, num=1)
599     picked = []
600     num.times do
601       picked << @stock.delete_one
602       if @stock.length == 0
603         announce _("Shuffling discarded cards")
604         make_stock
605         if @stock.length == 0
606           announce _("No more cards!")
607           end_game # FIXME nope!
608         end
609       end
610     end
611     picked.sort!
612     notify player, _("You picked %{picked}") % { :picked => picked.join(' ') }
613     player.cards += picked
614     player.cards.sort!
615   end
616
617   def add_player(user)
618     if p = get_player(user)
619       announce _("you're already in the game, %{p}") % {
620         :p => p
621       }
622       return
623     end
624     @dropouts.each do |dp|
625       if dp.user == user
626         announce _("you dropped from the game, %{p}, you can't get back in") % {
627           :p => dp
628         }
629         return
630       end
631     end
632     cards = 7
633     if @start_time
634       cards = @players.inject(0) do |s, pl|
635         s +=pl.cards.length
636       end/@players.length
637     end
638     p = Player.new(user)
639     @players << p
640     announce _("%{p} joins this game of %{uno}") % {
641       :p => p, :uno => UNO
642     }
643     deal(p, cards)
644     return if @start_time
645     if @join_timer
646       @bot.timer.reschedule(@join_timer, 10)
647     elsif @players.length > 1
648       announce _("game will start in 20 seconds")
649       @join_timer = @bot.timer.add_once(20) {
650         start_game
651       }
652     end
653   end
654
655   def drop_player(nick)
656     # A nick is passed because the original player might have left
657     # the channel or IRC
658     unless p = get_player(nick)
659       announce _("%{p} isn't playing %{uno}") % {
660         :p => p, :uno => UNO
661       }
662       return
663     end
664     announce _("%{p} gives up this game of %{uno}") % {
665       :p => p, :uno => UNO
666     }
667     case @players.length
668     when 2
669       if p == @players.first
670         next_turn
671       end
672       end_game
673       return
674     when 1
675       end_game(true)
676       return
677     end
678     debug @stock.length
679     while p.cards.length > 0
680       @stock.insert(rand(@stock.length), p.cards.shift)
681     end
682     debug @stock.length
683     @dropouts << @players.delete_one(p)
684   end
685
686   def replace_player(old, new)
687     # The new user
688     user = channel.get_user(new)
689     if p = get_player(user)
690       announce _("%{p} is already playing %{uno} here") % {
691         :p => p, :uno => UNO
692       }
693       return
694     end
695     # We scan the player list of the player with the old nick, instead
696     # of using get_player, in case of IRC drops etc
697     @players.each do |p|
698       if p.user.nick == old
699         p.user = user
700         announce _("%{p} takes %{b}%{old}%{b}'s place at %{uno}") % {
701           :p => p, :b => Bold, :old => old, :uno => UNO
702         }
703         return
704       end
705     end
706     announce _("%{b}%{old}%{b} isn't playing %{uno} here") % {
707       :uno => UNO, :b => Bold, :old => old
708     }
709   end
710
711   def end_game(halted = false)
712     if halted
713       if @start_time
714         announce _("%{uno} game halted after %{time}") % {
715           :time => elapsed_time,
716           :uno => UNO
717         }
718       else
719         announce _("%{uno} game halted before it could start") % {
720           :uno => UNO
721         }
722       end
723     else
724       announce _("%{uno} game finished after %{time}! The winner is %{p}") % {
725         :time => elapsed_time,
726         :uno => UNO, :p => @players.first
727       }
728     end
729     if @picker > 0 and not halted
730       p = @players[1]
731       announce _("%{p} has to pick %{b}%{n}%{b} cards!") % {
732         :p => p, :n => @picker, :b => Bold
733       }
734       deal(p, @picker)
735       @picker = 0
736     end
737     score = @players.inject(0) do |sum, p|
738       if p.cards.length > 0
739         announce _("%{p} still had %{cards}") % {
740           :p => p, :cards => p.cards.join(' ')
741         }
742         sum += p.cards.inject(0) do |cs, c|
743           cs += c.score
744         end
745       end
746       sum
747     end
748     if not halted
749       announce _("%{p} wins with %{b}%{score}%{b} points!") % {
750         :p => @players.first, :score => score, :b => Bold
751       }
752     end
753     @plugin.do_end_game(@channel)
754   end
755
756 end
757
758 class UnoPlugin < Plugin
759   attr :games
760   def initialize
761     super
762     @games = {}
763   end
764
765   def help(plugin, topic="")
766     case topic
767     when 'commands'
768       [
769       _("'jo' to join in"),
770       _("'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"),
771       _("'pe' to pick a card"),
772       _("'pa' to pass your turn"),
773       _("'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)"),
774       _("'ca' to show current cards"),
775       _("'cd' to show the current discard"),
776       _("'ch' to challenge a Wild +4"),
777       _("'od' to show the playing order"),
778       _("'ti' to show play time"),
779       _("'tu' to show whose turn it is")
780     ].join(" ; ")
781     when 'challenge'
782       _("A Wild +4 can only be played legally if you don't have normal (not special) cards of the current color. ") +
783       _("The next player can challenge a W+4 by using the 'ch' command. ") +
784       _("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. ") +
785       _("If the W+4 play was legal, the challenger must pick 6 cards instead of 4.")
786     when 'rules'
787       _("play all your cards, one at a time, by matching either the color or the value of the currently discarded card. ") +
788       _("cards with special effects: Skip (next player skips a turn), Reverse (reverses the playing order), +2 (next player has to take 2 cards). ") +
789       _("Wilds can be played on any card, and you must specify the color for the next card. ") +
790       _("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. ") +
791       _("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. ") +
792       _("you can also play a Reverse on a +2 or +4, bouncing the effect back to the previous player (that now comes next). ")
793     else
794       (_("%{uno} game. !uno to start a game. see help uno rules for the rules. commands: %{cmds}") % {
795         :uno => UnoGame::UNO,
796         :cmds => help(plugin, 'commands')
797       })
798     end
799   end
800
801   def message(m)
802     return unless @games.key?(m.channel)
803     g = @games[m.channel]
804     case m.plugin.intern
805     when :jo # join game
806       return if m.params
807       g.add_player(m.source)
808     when :pe # pick card
809       return if m.params
810       if g.has_turn?(m.source)
811         if g.player_has_picked
812           m.reply _("you already picked a card")
813         elsif g.picker > 0
814           g.pass(m.source)
815         else
816           g.pick_card(m.source)
817         end
818       else
819         m.reply _("It's not your turn")
820       end
821     when :pa # pass turn
822       return if m.params
823       if g.has_turn?(m.source)
824         g.pass(m.source)
825       else
826         m.reply _("It's not your turn")
827       end
828     when :pl # play card
829       if g.has_turn?(m.source)
830         g.play_card(m.source, m.params.downcase)
831       else
832         m.reply _("It's not your turn")
833       end
834     when :co # pick color
835       if g.has_turn?(m.source)
836         g.choose_color(m.source, m.params.downcase)
837       else
838         m.reply _("It's not your turn")
839       end
840     when :ca # show current cards
841       return if m.params
842       g.show_all_cards(m.source)
843     when :cd # show current discard
844       return if m.params
845       g.show_discard
846     when :ch
847       if g.has_turn?(m.source)
848         if g.last_discard
849           g.challenge
850         else
851           m.reply _("previous move cannot be challenged")
852         end
853       else
854         m.reply _("It's not your turn")
855       end
856     when :od # show playing order
857       return if m.params
858       g.show_order
859     when :ti # show play time
860       return if m.params
861       g.show_time
862     when :tu # show whose turn is it
863       return if m.params
864       if g.has_turn?(m.source)
865         m.nickreply _("it's your turn, sleepyhead")
866       else
867         g.show_turn(:cards => false)
868       end
869     end
870   end
871
872   def create_game(m, p)
873     if @games.key?(m.channel)
874       m.reply _("There is already an %{uno} game running here, say 'jo' to join in") % { :uno => UnoGame::UNO }
875       return
876     end
877     @games[m.channel] = UnoGame.new(self, m.channel)
878     m.reply _("Ok, created %{uno} game on %{channel}, say 'jo' to join in") % {
879       :uno => UnoGame::UNO,
880       :channel => m.channel
881     }
882   end
883
884   def end_game(m, p)
885     unless @games.key?(m.channel)
886       m.reply _("There is no %{uno} game running here") % { :uno => UnoGame::UNO }
887       return
888     end
889     @games[m.channel].end_game(true)
890   end
891
892   def do_end_game(channel)
893     @games.delete(channel)
894   end
895
896   def replace_player(m, p)
897     unless @games.key?(m.channel)
898       m.reply _("There is no %{uno} game running here") % { :uno => UnoGame::UNO }
899       return
900     end
901     @games[m.channel].replace_player(p[:old], p[:new])
902   end
903
904   def drop_player(m, p)
905     unless @games.key?(m.channel)
906       m.reply _("There is no %{uno} game running here") % { :uno => UnoGame::UNO }
907       return
908     end
909     @games[m.channel].drop_player(p[:nick] || m.source.nick)
910   end
911
912   def print_stock(m, p)
913     unless @games.key?(m.channel)
914       m.reply _("There is no %{uno} game running here") % { :uno => UnoGame::UNO }
915       return
916     end
917     stock = @games[m.channel].stock
918     m.reply(_("%{num} cards in stock: %{stock}") % {
919       :num => stock.length,
920       :stock => stock.join(' ')
921     }, :split_at => /#{NormalText}\s*/)
922   end
923 end
924
925 pg = UnoPlugin.new
926
927 pg.map 'uno', :private => false, :action => :create_game
928 pg.map 'uno end', :private => false, :action => :end_game
929 pg.map 'uno drop', :private => false, :action => :drop_player
930 pg.map 'uno giveup', :private => false, :action => :drop_player
931 pg.map 'uno drop :nick', :private => false, :action => :drop_player, :auth_path => ':other'
932 pg.map 'uno replace :old [with] :new', :private => false, :action => :replace_player
933 pg.map 'uno stock', :private => false, :action => :print_stock
934
935 pg.default_auth('stock', false)
936 pg.default_auth('end', false)
937 pg.default_auth('drop::other', false)
938 pg.default_auth('replace', false)