This repository has been archived by the owner on Sep 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loveletter.rb
1040 lines (973 loc) · 31 KB
/
loveletter.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding: utf-8
#
# Title:: Love Letter
# Author:: Jay Thomas <[email protected]>
# Copyright:: (C) 2013 gfax
# License:: GPL
# Version:: 2013-08-18
#
class LoveLetter
Color = Irc.color(:red)
Title = Color + 'Love Letter' + NormalText
Rounds = Struct.new(:played, :total)
Cards = {
:guard => {
:value => 1,
:quantity => 5,
:keyword => /guard/,
:text => 'Name a non-Guard card and choose another player; If ' +
'that player has that card, he or she is out of the round.'
},
:priest => {
:value => 2,
:quantity => 2,
:keyword => /priest/,
:text => 'Look at another player\'s hand.'
},
:baron => {
:value => 3,
:quantity => 2,
:keyword => /baron/,
:text => 'You and another player secretly compare hands. ' +
'The player with the lower value is out of the round. '
},
:handmaid => {
:value => 4,
:quantity => 2,
:keyword => /maid/,
:text => 'Until your next turn, ignore all ' +
'effects from other players\' cards.'
},
:prince => {
:value => 5,
:quantity => 2,
:keyword => /prince$/,
:text => 'Choose any player (including yourself) to ' +
'discard his or her hand and draw a new card.'
},
:king => {
:value => 6,
:quantity => 1,
:keyword => /king/,
:text => 'Trade hands with another player of your choice.'
},
:countess => {
:value => 7,
:quantity => 1,
:keyword => /count/,
:text => 'If you have this card and the King or Prince ' +
'in your hand, you must discard this card.'
},
:princess => {
:value => 8,
:quantity => 1,
:keyword => /princess/,
:text => 'If you discard this card, you are out of the round.'
}
}
class Card
attr_reader :name, :value
def initialize(name)
@name = name
@value = Cards[name][:value]
end
def to_s
Color + '[' + NormalText +
name.to_s.capitalize + ' (' + value.to_s + ')' +
Color + ']' + NormalText
end
end
class Player
attr_accessor :user, :discard, :hand, :moved, :rounds
def initialize(user)
@user = user
@discard = nil
@hand = []
@moved = false
@out = false
@rounds = 0
end
def has?(card)
return false if card.nil?
if card.is_a? Symbol
hand.each { |e| return true if card == e.name }
elsif card.is_a? Card
hand.each { |e| return true if card.name == e.name }
end
return false
end
def to_s
Bold + user.to_s + Bold
end
end
attr_reader :channel, :deck, :dropped, :join_timer,
:manager, :ousted, :players, :rounds, :started
def initialize(plugin, channel, user, rounds)
@bot = plugin.bot
@channel = channel
@plugin = plugin
@registry = plugin.registry
@deck = [] # card stock
@dropped = [] # players booted from game
@join_timer = nil # timer for countdown
@manager = nil # player in control of game
@ousted = [] # players out of the round
@players = [] # players currently in game
@reserve = [] # card reserve for round end
@rounds = Rounds.new(0, rounds) # rounds in game
@started = nil # time the game started
add_player(user)
end
def add_player(user)
ousted.each do |p|
if user == p.user
say "You're out of this round, #{p}."
return
end
end
if player = get_player(user)
say "You're already in the game #{player}."
return
elsif players.size > 3
say "Sorry, this game can only seat 4 players at a time."
return
elsif started and deck.size < 2
say "Round is about to end. Wait until next round to join, #{user}."
return
end
player = Player.new(user)
@players << player
if manager.nil?
@manager = player
say "#{player} creates a game of #{Title}. Type 'j' to join."
else
say "#{player} joins #{Title}."
end
player.hand << @deck.pop if started
if @join_timer
if players.size == 4
@bot.timer.remove(@join_timer)
do_round
else
@bot.timer.reschedule(@join_timer, 10)
end
elsif players.size > 1
countdown = @bot.config['loveletter.countdown']
@join_timer = @bot.timer.add_once(countdown) { do_round }
say "Game will start in #{countdown} seconds."
end
end
def do_baron(player, opponent)
if opponent.nil?
notify player, 'Specify another player.'
return false
end
say "#{player} compares hands with #{opponent}..."
if player.hand.first.value > opponent.hand.first.value
oust_player(opponent)
return false
elsif player.hand.first.value == opponent.hand.first.value
say "It's a draw."
else
oust_player(player)
return false
end
return true
end
def do_discard(player, card, opponent, guard_guess)
if card.nil?
notify player, 'Specify a card name or number.'
return false
elsif not player.has?(card)
notify player, 'Specify one of your hand cards.'
return false
elsif player.has?(:king) or player.has?(:prince)
if player.has?(:countess) and card.name != :countess
notify player, 'You must discard the Countess.'
return false
end
end
if opponent
ousted.each do |p|
if opponent.user == p.user
say "#{opponent} is already out of the round!"
return false
end
end
end
player.discard = card
player.hand.delete_at(player.hand.index { |e| e.name == card.name })
say "#{player} plays #{card}."
case card.name
when :countess
return true
when :guard
if guard_guess and not handmaids?
return do_guard(player, opponent, guard_guess)
elsif not handmaids?
say 'Guess a card.'
end
when :handmaid
say "#{player} is immune until next turn."
return true
when :prince
return do_prince(player, opponent)
when :princess
oust_player(player)
return false
else
if not handmaids?
opponent = players.last if players.size == 2
if opponent
return self.send("do_#{card.name}", player, opponent)
else
say "Pick a player, #{player}."
end
end
end
if handmaids?
say "#{player} discards to no effect."
return true
else
return false
end
end
def do_guard(player, opponent, guard_guess)
opponent = players.last if players.size == 2
if handmaids?
say "#{player} discards to no effect."
return true
elsif guard_guess.nil?
notify player, "Name a card you think #{opponent.user} has."
return false
elsif guard_guess.name == :guard
return false
elsif opponent.nil? or opponent == player
notify player, 'Specify a target player.'
return false
elsif opponent.discard and opponent.discard.name == :handmaid
notify player, "#{opponent.user} is protected by #{opponent.discard}."
return false
end
say "#{player} suspects #{opponent} has the " +
guard_guess.name.to_s.capitalize + '...'
if opponent.has?(guard_guess)
oust_player(opponent)
return false
else
return true
end
end
def do_king(player, opponent)
if opponent.nil? or opponent == player
notify player, 'Specify another player.'
return false
end
say "#{player} swaps hands with #{opponent}."
player.hand, opponent.hand = opponent.hand, player.hand
show_hand([player, opponent])
return true
end
def do_priest(player, opponent)
if opponent.nil? or opponent == player
notify player, 'Specify another player.'
return false
elsif opponent.discard and opponent.discard.name == :handmaid
notify player, "#{opponent} is protected by the Handmaid."
return false
end
notify player, "#{opponent} has: #{opponent.hand.first}"
return true
end
def do_prince(player, opponent)
if opponent.nil?
opponent = player
elsif opponent.discard and opponent.discard.name == :handmaid
notify player, "#{opponent.user} is protected by #{opponent.discard}."
return false
end
if opponent.discard.nil? or opponent.hand.first.name != :handmaid
opponent.discard = opponent.hand.first
end
say "#{opponent} discards #{opponent.hand.first}."
opponent.hand.clear
if deck.size > 0
opponent.hand << @deck.pop
else
opponent.hand << @reserve.pop
end
return true
end
def do_round(last_winner=nil)
@started = Time.now if not started
@players.shuffle!
# re-create deck
@deck.clear
@reserve.clear
Cards.each_pair do |k, v|
v[:quantity].times { @deck << Card.new(k) }
end
@deck.shuffle!
# deal players
players.each { |p| p.hand << @deck.pop }
if players.size == 2
@reserve |= @deck.pop(4)
else
@reserve << @deck.pop
end
if last_winner
players.size.times do
if players[1] == last_winner
break
else
@players << @players.shift
end
end
end
do_turn
end
def do_turn(hold_place=false)
if players.size < 2 or deck.empty?
end_round
return
end
if hold_place
# Show everyone their cards at the start of a new round.
players.each { |p| show_cards(p) unless p == players.first }
else
@players << @players.shift
end
player = players.first
player.discard = nil
players.last.moved = false
while player.hand.size < 2
if deck.empty? and not player.hand.empty?
end_round
return
elsif deck.empty?
player.hand << @reserve.pop
else
player.hand << @deck.pop
end
end
if deck.size == 1
say "#{Color}One card left in the deck.#{NormalText}"
end
say "#{player}, pick a card to discard."
show_hand(player)
end
def drop_player(dropper, a)
case a.first
when nil, 'me' then player = dropper
else player = get_player(a.first, dropper)
end
if player.nil?
say "#{dropper}, there is no one playing named '#{a.first}'."
return
elsif player != dropper and dropper != manager
say "Only the game manager is allowed to drop others, #{dropper}."
return
end
n = 0
n += 1 until players[n] == player
n = next_turn(n)
if player == manager and players.size > 2
unless players[n].user == @bot.nick
@manager = players[n]
else
@manager = players[next_turn(n)]
end
say "#{manager} is now game manager."
end
say "#{player} has been removed from the game."
@dropouts << player
@players.delete(player)
# If the manager drops the only other player, end the game.
if players.size < 2
say "#{player} has been removed from the game. #{Title} stopped."
@plugin.remove_game(channel)
else
do_turn(true) if player == players.first
end
end
def elapsed_time
return Utils.secs_to_string(Time.now-started)
end
def end_game
players.first.moved = true
# Time spent playing the game.
@started = Time.now.to_i - started.to_i
a = []
winners = players.sort { |x, y| y.rounds <=> x.rounds }
winners.each { |p| a << "#{p} - #{p.rounds}" }
say 'Game over. Rounds won: ' + a.join(', ')
winners.reject! { |p| p.rounds < winners.first.rounds }
update_channel_stats
if winners.size > 1
say Utils.comma_list(winners) + ' have won equal affection!'
winners.each { |p| update_user_stats(p, 1) }
else
say "#{winners.first} has won the most affection!"
update_user_stats(winners.first, 1)
end
players.each { |p| update_user_stats(p, 0) unless winners.include?(p) }
@plugin.remove_game(channel)
end
def end_round
@rounds.played += 1
# Sort out the winners.
a = []
winners = players.dup
winners.each { |p| a << "#{p} discards #{p.hand.first}" }
say Utils.comma_list(a) + '.'
winners.sort! { |x, y| y.hand.first.value <=> x.hand.first.value }
winners.reject! { |p| p.hand.first.value < winners.first.hand.first.value }
if winners.size > 1
string = 'It\'s a tie between' + Utils.comma_list(winners) + '!'
winner = nil
else
string = "#{winners.first} wins the round!"
winner = winners.first
end
players.each do |p|
p.discard, p.hand = nil, []
p.moved = false
p.rounds += 1 if winners.include?(p)
end
@players |= @ousted
@ousted.clear
if rounds.played == rounds.total
end_game
else
say string + " Starting round #{rounds.played + 1} of #{rounds.total}..."
do_round(winner)
end
end
def get_card(card)
case card
when Array
ret = nil
card.each { |e| ret = get_card(e) if ret.nil? }
return ret
when Card
return card
when NilClass
return nil
when String
[',', '.', '!', '?'].each { |e| card.gsub!(e, '') }
Cards.each_pair do |k,v|
return Card.new(k) if card =~ v[:keyword]
end
else
get_card(card.to_s)
end
return nil
end
def get_player(user, source='')
case user
when NilClass
return nil
when User
players.each do |p|
return p if p.user == user
end
when String
players.each do |p|
return p if p.user.irc_downcase == user.irc_downcase(channel.casemap)
end
players.each do |p|
if p.user.irc_downcase =~ /^#{user.irc_downcase(channel.casemap)}/
return p unless p.user.irc_downcase == source.downcase
end
end
else
get_player(user.to_s)
end
return nil
end
def handmaids?
# If all other players played Handmaid,
# the card may be discarded to no effect.
players.each do |p|
next if p == players.first
return false if p.discard.nil? or p.discard.name != :handmaid
end
return true
end
def next_turn(num=0)
return 0 if num >= players.length - 1
return num + 1
end
def notify(player, msg, opts={})
@bot.notice player.user, msg, opts
end
def oust_player(player)
say "#{player} is out of the round! Discarding #{player.hand.first}."
player.discard, player.hand = nil, []
hold_place = get_player(player) == players.first
@ousted << player
@players.delete(player)
do_turn(hold_place)
end
def processor(player, a)
return unless player == players.first
return if player.moved or a.empty?
player.moved = true
card = guard_guess = opponent = nil
a.each do |e|
card = player.hand[e.to_i-1] unless card or e.to_i.zero?
card = card || get_card(e)
guard_guess = get_card(e) || guard_guess
opponent = opponent || get_player(e, player.user)
end
player.moved = if player.discard and player.discard.name == :guard
do_guard(player, opponent, guard_guess)
elsif player.discard
if handmaids?
say "#{opponent} discards to no effect."
return true
end
self.send("do_#{player.discard.name}", player, opponent)
else
do_discard(player, card, opponent, guard_guess)
end
do_turn if player.moved
end
def replace_player(replacer, a)
old_player = new_player = nil
a.each do |e|
next if e == @bot.nick.downcase
if old_player.nil?
e = replacer.user.nick if e == 'me'
old_player = channel.get_user(e)
elsif new_player.nil?
new_player = channel.get_user(e)
end
end
unless old_player
notify replacer, "Specify a replacement user, #{replacer.user}."
return
end
# Player only specified one name. Assume that is the new player.
unless new_player
new_player = old_player
old_player = channel.get_user(replacer.user.nick)
end
ousted.each do |p|
if new_player == p.user
notify replacer, "#{new_player.nick} is already playing #{Title}."
return
end
end
if replacer.user == new_player
notify replacer, "You're already playing, #{replacer.user}."
elsif old_player == new_player
notify replacer, 'Replace someone with someone else.'
elsif get_player(new_player.nick)
notify replacer, "#{new_player.nick} is already playing #{Title}."
elsif not player = get_player(old_player) # assign player or return nil
notify replacer, "#{old_player} is not playing #{Title}."
elsif player != replacer and replacer != manager
notify replacer, 'Only game managers can replace other players.'
else
say "#{player} was replaced by #{Bold + new_player.nick + Bold}!"
player.user = new_player
say "#{player} is now game manager." if player == manager
end
end
def say(msg, who=channel, opts={})
return unless msg.is_a? String
return if msg.empty?
@bot.say who, msg, opts
end
def show_help(player, a)
if a.first.to_i.between?(1, player.hand.size)
card = player.hand[a.first.to_i-1]
elsif a.first =~ /^cards?$/
say 'Cards: Princess (1), Countess (1), King (1), Prince ' +
'(2), Handmaid (2), Baron (2), Priest (2), Guard (5).'
else
a.each { |e| card = get_card(e) if card.nil? }
end
return if card.nil?
notify player, "#{card} - #{Cards[card.name][:text]}"
end
def show_hand(p_array=players)
[*p_array].each do |p|
next if p.hand.size < 1
i = 0
string = 'Cards: '
string << p.hand.map { |e| "#{Bold}#{i += 1}.)#{Bold} #{e}" }.join(' ')
notify p, string
end
end
def show_turn
return unless started
a, player = [], players.first
string = "It's #{player}'s turn."
players.each { |p| a << "#{p.user} - #{p.discard || 'none'}" }
string << ' Discard: ' + a.join(', ')
string << " -- Cards left: #{deck.size}"
say string
end
def transfer_management(player, a)
return if a.empty?
unless player == manager
notify player, "You can't transfer ownership. " +
"#{manager} manages this game."
return
end
a.each do |e|
break if new_manager = get_player(e, manager.user)
end
if new_manager.nil?
say "#{player}: Specify another player."
return
elsif manager == new_manager
say "#{player.user}: You are already game manager."
return
end
@manager = new_manager
say "#{new_manager} is now game manager."
end
def update_channel_stats
r = @registry[:chan] || {}
c = channel.name.downcase
r[c] = {} if r[c].nil?
r[c][:games] = r[c][:games].to_i + 1
# display-name for proper caps
r[c][:name] = channel.name
r[c][:rounds] = r[c][:rounds].to_i + rounds.total
r[c][:time] = r[c][:time].to_i + started
@registry[:chan] = r
end
def update_user_stats(player, win)
@registry[:user] = {} if @registry[:user].nil?
c, n = channel.name.downcase, player.user.nick.downcase
h1 = @registry[:chan][c][n] || {}
h2 = @registry[:user][n] || {}
[ h1, h2 ].each do |e|
e[:games] = e[:games].to_i + 1
# Get player's nick in proper caps.
e[:nick] = player.user.to_s
e[:score] = e[:score].to_i + player.rounds
# bonus points for winning
e[:score] += (rounds.total * win)
e[:wins] = e[:wins].to_i + win
end
r1 = @registry[:chan]
r2 = @registry[:user]
r1[c][n], r2[n] = h1, h2
@registry[:chan], @registry[:user] = r1, r2
end
end
class LoveLetterPlugin < Plugin
Title = LoveLetter::Title
Config.register Config::IntegerValue.new 'loveletter.countdown',
:default => 10, :validate => Proc.new{|v| v > 0},
:desc => 'Number of seconds before starting a game of Love Letter.'
attr :games
def initialize
super
@games = {}
end
def help(plugin, topic='')
p = @bot.config['core.address_prefix'].first
case topic.downcase
when /princess/
"#{Bold}Princess Annette #{Bold}- If you discard the " +
"Princess―no matter how or why―she has tossed your " +
"letter into the fire. You are knocked out of the round."
when /countess/
"#{Bold}Countess Wilhelmina #{Bold}- Unlike other cards, which takes " +
"effect when discarded, the text on the Countess applies while she " +
"is in your hand. In fact, she has no effect when you discard her.\n" +
"If you ever have the Countess and either the Princes or King in " +
"your hand, you must discard the Countess. You do not have to reveal " +
"the other card in your hand. Of course, you can also discard the " +
"Countess even if you do not have a royal family member in your " +
"hand. She likes to play mind games...."
when /king/
"#{Bold}King Arnaud IV #{Bold}- When you discard the King, " +
"trade the card in your hand with the card held by another " +
"player of your choice. You cannot trade with a player who " +
"is out of the round, nor with someone protected by the " +
"Handmaid. If all other players still in the round are " +
"protected by the Handmaid, this card does nothing."
when /prince/
"#{Bold}Prince Arnaud #{Bold}- when you discard the Prince, " +
"choose one player still in the round (including yourself) " +
"That player discards his or her hand (do not apply its " +
"effect) and draws a new card. If the deck is empty, that " +
"player draws a card that was removed at the start of the " +
"round.\nIf all other players are protected by the " +
"Handmaid, you must choose yourself."
when /maid/
"#{Bold}Handmaid Susannah #{Bold}- When you discard " +
'the Handmaid, you are immune to the effects of other ' +
'players\' cards until the start of your next turn.'
when /baron/
"#{Bold}Baron Talus #{Bold}- When discarded, choose one other " +
'player still in the round. You and that player secretly compare ' +
'your hands. The player with the lower rank is knocked out of the ' +
'round. In case of a tie, nothing happens. If all other players ' +
'still in the round are protected by the Handmaid, ' +
'this card does nothing.'
when /priest/
"#{Bold}Priest Tomas #{Bold}- When you discard the " +
"Priest, you can look at one other player’s hand. " +
'Do not reveal the hand to all players.'
when /guard/
"#{Bold}Guard Odette #{Bold}- When you discard the Guard, " +
'choose a player and name a card (other than Guard). If ' +
'that player has that card, that player is knocked out ' +
'of the round. If all other players still in the round ' +
'are protected by the Handmaid, this card does nothing.'
when /drop/
"Type 'drop me' to leave the game in progress, or " +
"'drop <another player>' if you are the game manager."
when /card/
'There are 16 cards total in the deck: Princess (1), Countess ' +
'(1), King (1), Prince (2), Handmaid (2), Baron (2), Priest ' +
"(2), Guard (5). Use '#{p}help #{plugin} <card>' for " +
"card-specific information, (or simply use 'help card', " +
"or 'help <card name>' in game for a quick reference)."
when /command/
"In-game commands: 'p <card name or number>' to pick/play " +
"a card, 'p <victim>' to pick a player you wish to play a " +
"card against (e.g.: 'p guard', 'p Frank has the baron!', " +
"or 'p guard fr baron' for short), 'help <card name>' for " +
"quick card info, or preferably 'help <card #>' as to not " +
"give away the card in your hand, 'drop me' to leave a game " +
"in progress, 'replace [me with] user' to have another " +
"player take your spot in the game.\nSee '#{p}help #{plugin} " +
"manage' for commands specific to the game manager."
when /manage/, /transfer/, /xfer/
'The player that starts the game is the game manager. Game ' +
'managers may stop the game at any time, or transfer ownership ' +
"by typing 'transfer [game to] <player>'. Managers may replace " +
'themselves as well as other players in the game by typing ' +
"'replace [me with] <user> / replace <player> [with] <nick>'"
when /object/
"During the game, you hold one secret card in your hand. This is " +
"who currently carries your message of love for the princess. " +
"Make sure that the person closest to the princess holds your " +
"love letter at the end of the day, so it reaches her first!\n" +
"(The higher the card value, the closer the person is to the " +
"princess, but be careful as the higher value cards are more " +
"likely to get you knocked out of the round before it's over!)"
when /rule/, /manual/
"http://www.alderac.com/tempest/files/2012/09/Love_Letter_Rules_Final.pdf"
when /scoring/
'The winner of each round receives a token of affection ' +
'from the princess. The player that received the most affection ' +
'by the end of all the rounds is declared the winner. Players\' ' +
'scores are updated relative to the amount of affection won. ' +
'The winner over all rounds receives a bonus relative ' +
'to the total number of rounds played.'
when /stat/, /scor/
"'#{p}love stats <channel/user>' displays the stats and score for " +
'a channel or user. If no channel or user is specified, this ' +
"command will show you your own stats.\n'#{p}love stats <channel> " +
"<user>' displays a user's stats for a specific channel.\n'#{p}love " +
"top <num> <channel>' shows the top <num> scores for a given channel."
when /cancel/, /end/, /halt/, /stop/
"'#{p}love stop' stops the current game; Only game " +
'managers and bot owners can stop a game in progress.'
when ''
"#{Title}: cards, commands, manual, object, scoring, stats, stop -- " +
"'#{p}love <rounds>' to create a game"
end
end
def create_game(m, p)
if g = @games[m.channel]
if m.source == g.manager.user
m.reply "...you already started #{Title}."
else
m.reply "#{g.manager.user} already started #{Title}."
end
return
end
rounds = p[:rounds].to_i
if rounds > 50 or rounds.zero?
m.reply 'That\'s not a good idea...'
return
end
@games[m.channel] = LoveLetter.new(self, m.channel, m.source, rounds)
end
def message(m)
return unless m.plugin and g = @games[m.channel]
case m.message.downcase
when 'j', 'jo', 'join'
g.add_player(m.source)
when 'ti', 'time'
if g.started
@bot.say m.replyto, Title + " has been in play for #{g.elapsed_time}."
else
m.reply Title + " hasn't started yet."
end
end
# Messages only concerning players:
player = g.get_player(m.source.nick)
return unless player and g.started
a = m.message.downcase.split(' ').uniq[1..-1]
case m.message.downcase
when /^(ca?|cards?)( |\z)/
g.show_hand(player)
when /^drop( |\z)/
g.drop_player(player, a)
when /^h(elp)?( |\z)/
g.show_help(player, a)
when /^(pi?|pl|play)( |\z)/
g.processor(player, a)
when /^(tu?|turn)( |\z)/
g.show_turn
when /^replace( |\z)/
g.replace_player(player, a)
when /^transfer( |\z)/
g.transfer_management(player, a)
end
end
def show_stats(m, params)
if @registry[:chan].nil?
m.reply "No #{Title} stats recorded yet."
return
end
if params[:a] == false
if @registry[:chan][m.channel.name.downcase]
show_stats_chan(m, m.channel.name.downcase, params[:n].to_i)
else
m.reply "No one has played #{Title} in #{m.channel.name}."
end
return
end
a, chan, user, n = params[:a], nil, nil, 0
if a.empty?
user = m.source.nick.downcase
else
a.each do |e|
chan = e.downcase if @registry[:chan][e.downcase]
user = e.downcase if @registry[:user][e.downcase]
n = e.to_i if e.to_i > n
end
end
if chan.nil? and user.nil?
# Check for missing # symbol.
a.each { |e| chan = "##{chan}" if @registry[:chan]["##{chan}"] }
if chan
show_stats_chan(m, chan, n)
else
m.reply "No stats for #{a.join(' or ')}."
end
elsif user
show_stats_user(m, user, chan)
elsif chan
show_stats_chan(m, chan, n)
end
end
def show_stats_chan(m, chan, n)
c = @registry[:chan][chan]
if n.zero?
str = "#{Bold}#{c[:name]}:#{Bold} #{c[:games]} games played, "
str << "rounds played: #{c[:rounds]} "
i = c[:games] > 1
str << "(#{c[:rounds]/c[:games]} rounds average per game), " if i
str << "time accumulated: #{Utils.secs_to_string(c[:time])} "
str << "(#{Utils.secs_to_string(c[:time]/c[:games])} average per game)." if i
@bot.say m.replyto, str
return
end
n = 5 unless n.between?(1,20)
tops = {}
c.each_pair do |k, v|
next unless k.is_a? String
tops[v[:score]] = k
end
n = tops.size if n > tops.size
@bot.say m.replyto, "#{c[:name]}'s top #{n} players:"
i = 1
if n.between?(1,8)
tops.sort.reverse.each do |e|
str = "#{Bold}#{i}.) #{c[e[1]][:nick]}#{Bold} - "
str << "#{e.first} points, "
str << "#{c[e[1]][:wins]}/#{c[e[1]][:games]} wins"
@bot.say m.replyto, str
i += 1
end
else
str = ''
tops.sort.reverse.each do |e|
str << "#{Bold}#{i}.) #{c[e[1]][:nick]}#{Bold} - "
str << "#{e.first} pts."
i += 1
if i > n
break
else
str << ', '
end
end
@bot.say m.replyto, str
end
end
def show_stats_user(m, user, chan=nil)
if chan
u = @registry[:chan][chan][user]
chan = @registry[:chan][chan][:name]
str = "#{Bold}#{u[:nick]}#{Bold} (in #{chan}) -- "
else
u = @registry[:user][user]
str = "#{Bold}#{u[:nick]}#{Bold} -- "
end
str << "score: #{u[:score]}, "
str << "wins: #{u[:wins]}, "
str << "games played: #{u[:games]}"
@bot.say m.replyto, str
end
# Called from within the game.
def remove_game(channel)
if t = @games[channel].join_timer
@bot.timer.remove(t)
end
@games.delete(channel)
end
def reset_everything(m, params)
@registry.clear
m.reply 'Registry cleared.'
end
def stop_game(m, plugin=nil)