-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplayer.rb
773 lines (661 loc) · 26.8 KB
/
player.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
module Klank
require_relative "deck.rb"
require_relative "utils.rb"
class Player
FULL_HEALTH = 10
attr_reader :name
attr_reader :index
attr_reader :deck
attr_reader :hand
attr_accessor :played
attr_accessor :item
attr_accessor :mastery
attr_accessor :room_num
attr_accessor :skill
attr_accessor :attack
attr_accessor :move
attr_accessor :coins
attr_accessor :teleport
attr_accessor :shrine_mermaid_teleport
attr_accessor :door_before_teleport
attr_accessor :visited_rooms
attr_accessor :frozen
attr_accessor :artifact
attr_accessor :health
attr_accessor :air
attr_accessor :clank_remove
attr_accessor :num_turns
attr_accessor :num_cards_played
attr_accessor :num_times_shuffled
attr_accessor :num_caves_visited
attr_accessor :num_flooded_rooms_visited
attr_accessor :num_rooms_visited
attr_accessor :num_distance_moved
attr_accessor :num_times_teleported
attr_accessor :num_monsters_killed
attr_accessor :num_damage_dealt
attr_accessor :num_damage_taken
attr_accessor :num_times_out_of_air
attr_accessor :num_damage_healed
attr_accessor :num_coins_collected
attr_accessor :num_clank_added
attr_accessor :num_clank_removed
attr_accessor :num_major_secrets_collected
attr_accessor :num_minor_secrets_collected
attr_accessor :time
attr_accessor :time_per_turn
def initialize(client, games)
@client = client
loop do
@name = input("Name")
if games.find { |g| g.player.find { |p| p.name == @name } != nil } != nil
output("#{name} is already taken! Choose a different name!")
else
break
end
end
output("Welcome to Klank, #{@name}!")
end
def input(msg)
@client.write "#{msg }: "
resp = ""
loop do
char = @client.recv(1)
if (char == "\n") or (char == "\r")
if resp.length > 0
break
end
elsif char =~ /(\w|\s)/
resp += char
resp.strip!
end
end
puts resp.strip
resp.strip || ""
end
def input_num(msg, range)
n = 0
loop do
n = input("#{msg} [#{range.min}, #{range.max}]")
break if n =~ /^\d+/ and range.include?(n.to_i)
output("Oops!")
end
return n.to_i
end
def output(msg)
@client.puts "#{msg.gsub("\n", "\r\n")}\r"
end
def menu(title, options, none = false, all = false)
choice = none ? "N" : options[0][0]
if (options.count > 1) or ((options.count > 0) and none)
table = []
options.each do |o|
row = {"#" => o[0]}
if o[1].is_a?(Hash)
row.merge!(o[1])
else
row["DESC"] = o[1]
end
table << row
end
output("\n#{title}\n#{Klank.table(table)}")
valid = options.map { |o| o[0] }
valid << "N" if none
valid << "A" if all
loop do
choice = input("Choose an option#{none ? " (N: None)" : ""}#{all ? " (A: All)" : ""}").upcase
break if valid.any? { |v| v.to_s.upcase == choice }
output("Oops!")
end
end
choice
end
def start(game, index)
@game = game
@deck = Deck.new(@game, "player.yml")
@mastery = false
@coins = 0
@cubes = 30
@health = FULL_HEALTH
@index = index
@artifact = []
@item = []
@played = []
@room_num = 1
@skill = 0
@num_turns = 0
@num_times_shuffled = 0
@num_cards_played = 0
@num_caves_visited = 0
@num_flooded_rooms_visited = 0
@num_rooms_visited = 0
@num_distance_moved = 0
@num_times_teleported = 0
@num_monsters_killed = 0
@num_damage_dealt = 0
@num_damage_taken = 0
@num_times_out_of_air = 0
@num_damage_healed = 0
@num_coins_collected = 0
@num_clank_added = 0
@num_clank_removed = 0
@num_major_secrets_collected = 0
@num_minor_secrets_collected = 0
@time = 0
end
def score(disp_breakdown = false)
table = []
total = @coins
table << {"POINTS" => @coins, "DESCRIPTION" => "Coins"}
@deck.all.sort_by {|c| c.name }.each do |card|
card_name = ""
if (@deck.trashed.find {|c| c == card} != nil)
card_name = "[" + card.name + "]"
else
card_name = card.name
end
points = card.points(self)
total += points
card_hash = Hash.new
if points > 0
card_hash["POINTS"] = points
end
card_hash["DESCRIPTION"] = card_name
if card.play_count > 0
card_hash["PLAY COUNT"] = card.play_count
end
if card.num_times_discarded > 0
card_hash["DISCARD COUNT"] = card.num_times_discarded
end
table << card_hash
end
@item.sort_by {|i| i.name }.each do |i|
points = i.points()
if points != 0
total += points
table << {"POINTS" => points, "DESCRIPTION" => i.name}
end
end
@artifact.each do |a|
total += a
table << {"POINTS" => a, "DESCRIPTION" => "Artifact"}
end
if @mastery
total += 20
table << {"POINTS" => 20, "DESCRIPTION" => "Mastery"}
end
if [email protected]_over?()
# keep the score for now
elsif @artifact.count <= 0
table << {"POINTS" => -1 * total, "DESCRIPTION" => "No artifact"}
total = 0
elsif @game.map.depths?(self)
table << {"POINTS" => -1 * total, "DESCRIPTION" => "Depths"}
total = 0
end
table << {"POINTS" => total, "DESCRIPTION" => "Total"}
if disp_breakdown
output(Klank.table(table))
end
total
end
def draw(count)
cards = @deck.draw(count, self, @game)
@hand += cards
@new_cards = true
end
def turn()
@hand = []
@played = []
@skill = 0
@move = 0
@attack = 0
@teleport = 0
@shrine_mermaid_teleport = 0
@door_before_teleport = 0
@visited_rooms = [@room_num]
@clank_added = 0
@clank_remove = 0
@frozen = false
@air = [email protected]?(self) || has_item?("Scuba")
@num_turns += 1
start_time = Time.now
output("\a")
if (@game.sunken_treasures)
@game.broadcast("#{@name} #{@air ? "has" : "does NOT have"} air!")
end
draw(5)
loop do
if @new_cards
@new_cards = false
equip()
else
output_abilities()
menu = []
if @hand.count != 0
menu << ["E", {"DESC" => "Equip card(s)"}]
end
if @item.select{ |i| i.playable }.count != 0
menu << ["I", {"DESC" => "Play an item"}]
end
if (@game.reserve[:x].remaining > 0) and (@skill >= @game.reserve[:x].peek.cost)
menu << ["X", {"DESC" => "Buy an explore card", "COST" => 3, "BENEFIT" => "SKILL: 2 | MOVE: 1", "LEFT" => @game.reserve[:x].remaining}]
end
if (@game.reserve[:c].remaining > 0) and (@skill >= @game.reserve[:c].peek.cost)
menu << ["C", {"DESC" => "Buy a mercenary card", "COST" => 2, "BENEFIT" => "SKILL: 1 | ATTACK: 2", "LEFT" => @game.reserve[:c].remaining}]
end
if (@game.reserve[:t].remaining > 0) and (@skill >= @game.reserve[:t].peek.cost)
menu << ["T", {"DESC" => "Buy a tome card", "COST" => 7, "BENEFIT" => "POINTS: 7", "LEFT" => @game.reserve[:t].remaining}]
end
if @game.dungeon.afford?(self)
menu << ["D", {"DESC" => "Go to the dungeon"}]
end
if (@move > 0) and !@frozen
menu << ["M", {"DESC" => "Move"}]
end
if @game.map.market?(self)
menu << ["S", {"DESC" => "Shop in the market"}]
end
if (@teleport > 0) or ((@shrine_mermaid_teleport > 0) and @game.map.flooded?(self)) or ((@door_before_teleport > 0) and (@visited_rooms.count > 1))
menu << ["P", {"DESC" => "Teleport"}]
end
if @attack > 1
if @game.sunken_treasures and (@attack > 2) and @game.map.flooded?(self)
menu << ["GB", {"DESC" => "Kill the goblin", "COST" => 2, "BENEFIT" => "COINS: 1"}]
menu << ["GF", {"DESC" => "Kill the goldfish", "COST" => 3, "BENEFIT" => "COINS: 3"}]
else
menu << ["G", {"DESC" => "Kill the goblin", "COST" => 2, "BENEFIT" => "COINS: 1"}]
end
end
if menu.length > 0
if (@clank_remove < 0) and (@game.dragon.bank.select { |c| c.to_s == @index.to_s }.count > 0)
menu << ["R", {"DESC" => "Remove clank"}]
end
if [email protected]?(self)
menu << ["D", {"DESC" => "View the dungeon"}]
end
menu << ["V", {"DESC" => "View the map"}]
if [email protected]?(self) and (@game.map.market.count > 0)
menu << ["S", {"DESC" => "View the market"}]
end
menu << ["F", {"DESC" => "View the players"}]
if @deck.pile.length > 0
menu << ["B", {"DESC" => "View discard pile"}]
end
end
if @hand.count == 0
menu << ["N", {"DESC" => "End Turn"}]
end
option = menu("ACTION LIST", menu)
case option
when "E"
equip()
when "I"
play()
when "X"
x = @game.reserve[:x].draw(1)
@deck.discard(x)
@skill -= x[0].cost
@game.broadcast("#{@name} bought an Explore! There are #{@game.reserve[:x].remaining} left!")
when "C"
c = @game.reserve[:c].draw(1)
@deck.discard(c)
@skill -= c[0].cost
@game.broadcast("#{@name} bought a Mercenary! There are #{@game.reserve[:c].remaining} left!")
when "T"
tome(1)
@skill -= 7
@game.broadcast("#{@name} bought a Tome! There are #{@game.reserve[:t].remaining} left!")
when "D"
if @game.dungeon.afford?(self)
@game.dungeon.acquire(self)
else
@game.dungeon.view(self)
end
when "M"
@game.map.move(self)
when "S"
if @game.map.market?(self)
@game.map.shop(self)
else
@game.map.view_market(self)
end
when "P"
@game.map.teleport(self)
when "V"
@game.map.view(self)
when "G", "GB"
@game.broadcast("#{@name} killed the Goblin!")
collect_coins(1)
@attack -= 2
@num_damage_dealt += 2
@num_monsters_killed += 1
when "GF"
@game.broadcast("#{@name} killed the Goldfish!")
collect_coins(3)
@attack -= 3
@num_damage_dealt += 3
@num_monsters_killed += 1
when "F"
@game.view_players(self)
when "B"
@deck.view_pile(self)
when "N"
if (menu.count <= 1) or ("Y" == input("Are you sure? (Y: yes)").upcase)
break
end
when "R"
clank_amount = input_num("How much clank do you want to remove from the bank?", 0..[-1 * @clank_remove, @game.dragon.bank.select { |c| c.to_s == @index.to_s }.count].min)
reclaim_clank(clank_amount)
else
output("Hmmm... something went wrong")
end
break if dead?() or @mastery
end
end
reclaim_clank()
if !@air && !has_played?("Mermaid")
@game.broadcast("#{@name} never came up for air on their turn and takes 1 damage!")
@num_times_out_of_air += 1
damage(true)
end
@deck.discard(@played)
@played = []
end_time = Time.now
turn_time = end_time - start_time
@time += turn_time
end
def damage(direct = false)
if @mastery
@game.broadcast("#{@name} can't take damage he has left!")
elsif dead?()
@game.broadcast("#{@name} can't take damage he is already dead!")
else
@health -= 1
@num_damage_taken += 1
if direct
@cubes -= 1
end
if dead?()
@game.broadcast("#{@name} has died!")
@game.trigger_end(self)
end
end
end
def heal(count = 1)
if count > 0
actual = [FULL_HEALTH - @health, count].min
@num_damage_healed += actual
@cubes += actual
@health = [FULL_HEALTH, @health + count].min
@game.broadcast("#{@name} healed #{actual}, their health is #{@health}!")
end
end
def dead?()
@health <= 0
end
def clank(count = 1)
if @mastery
@game.broadcast("#{@name} can't add clank he has left!")
elsif dead?()
@game.broadcast("#{@name} can't add clank he is already dead!")
elsif count > 0
actual = [@cubes, count].min
@cubes -= actual
@game.dragon.add(@index, actual)
@num_clank_added += actual
swagger = @played.select { |c| c.name == "Swagger" }.count * count
if swagger != 0
@game.broadcast("#{@name} gained #{swagger} skill because of their Swagger!")
@skill += swagger
end
elsif count < 0
@clank_remove += count
end
end
def reclaim_clank(request = nil)
if request == nil
request = 30
end
actual = @game.dragon.remove(@index, [-1 * @clank_remove, request].min)
@clank_remove += actual
@cubes += actual
@num_clank_removed += actual
end
def tome(count = 1)
@deck.discard(@game.reserve[:t].draw([count, @game.reserve[:t].remaining].min))
end
def has_artifact?()
@artifact.count > 0
end
def trash_card(card = nil)
if !card
cards = []
(@played + @deck.pile).each_with_index do |c, i|
cards << [i, c.name]
end
c = menu("TRASH A CARD", cards, true)
card = (c != "N") ? cards[c.to_i][1] : ""
end
if card != ""
if @played.find { |c| c.name == card } != nil
@game.broadcast("#{@name} trashed #{card} from their play area!")
@deck.trashed << @played.delete_at(@played.index { |c| c.name == card } || @played.length)
elsif @deck.pile.find { |c| c.name == card } != nil
@game.broadcast("#{@name} trashed #{card} from their discard pile!")
@deck.trashed << @deck.pile.delete_at(@deck.pile.index { |c| c.name == card } || @deck.pile.length)
else
output("Could not trash a #{card}!")
end
end
card != ""
end
def discard_card(count = 1, fishing_pole = false)
d = ""
for j in 1..count
cards = []
@hand.each_with_index do |c, i|
cards << [i, c.play_desc]
end
d = menu("DISCARD", cards, !fishing_pole)
if d != "N"
card = @hand.delete_at(d.to_i)
card.num_times_discarded += 1
@deck.discard([card])
@game.broadcast("#{@name} discarded #{card.name}!")
case card.name
when "Boomerang"
card.equip(self)
when "Coin Purse"
collect_coins(5)
when "Pickpocket"
@game.dungeon.pickpocket(self, 6, false)
when "Short Cut"
@move += 2
end
else
break
end
end
d != "N"
end
def retrieve_card_from_discard()
cards = []
@deck.pile.each_with_index do |c, i|
cards << [i, c.play_desc]
end
c = menu("RETRIEVE A CARD FROM DISCARD PILE", cards, true)
if c != "N"
card = @deck.pile.delete_at(c.to_i)
@hand += [card]
@game.broadcast("#{@name} moved #{card.name} from their discard pile to their hand!")
end
end
def move_card_to_bottom()
@deck.stack.rotate!
end
def has_item?(item)
@item.any? { |i| i.name == item }
end
def item_count(item)
@item.select { |i| i.name == item }.count
end
def has_played?(card)
@played.any? { |c| c.name == card }
end
def collect_coins(count)
if (count > 0)
count = [count, @game.map.bank].min
@game.map.bank -= count
@coins += count
@num_coins_collected += count
search_extra = [@played.select { |c| c.name == "Search" }.count, @game.map.bank].min
expedition_leader_extra = [@played.select { |c| c.name == "Expedition Leader" }.count, @game.map.bank].min
@game.map.bank -= (search_extra + expedition_leader_extra)
@coins += (search_extra + expedition_leader_extra)
@num_coins_collected += (search_extra + expedition_leader_extra)
if count == 0
@game.broadcast("#{@name} attempted to collect coins but the bank is out of coins!")
else
message = "#{@name} collects #{count} coin(s)"
if search_extra != 0
message << " +#{search_extra} for Search"
end
if expedition_leader_extra != 0
message << " +#{expedition_leader_extra} for Expedition Leader"
end
message << " and has #{@coins} coin(s) total! There are #{@game.map.bank} coin(s) in the bank!"
@game.broadcast(message)
end
end
end
def hold_artifact?()
hold = @item.select { |i| i.name == "Backpack" }.count + 1
(@artifact.count < hold)
end
def status()
{
"NAME" => @name,
"HEALTH" => dead?() ? "DEAD" : @mastery ? "OUT" : @health,
"CLANK" => @cubes,
"BANK" => @game.dragon.bank.select { |c| c == @index }.count,
"COINS" => @coins,
"ARTIFACT" => @artifact.join(", "),
"ITEM" => @item.map { |i| i.symbol }.join(""),
"ROOM" => @room_num,
"DECK" => "#{@deck.stack.count}/#{@deck.active_cards.count}",
"SCORE" => score()
}
end
def stats()
stats = []
stats << {"STATISTIC" => "Turns played", @name => @num_turns}
stats << {"STATISTIC" => "Total time spent", @name => Time.at(@time).utc.strftime("%M:%S")}
stats << {"STATISTIC" => "Time spent per turn", @name => Time.at(@time / @num_turns).utc.strftime("%M:%S")}
stats << {"STATISTIC" => "Cards played", @name => @num_cards_played}
stats << {"STATISTIC" => "Cards played per turn", @name => @num_cards_played.fdiv(@num_turns).round(2).to_s}
stats << {"STATISTIC" => "Times reshuffled deck", @name => @num_times_shuffled}
stats << {"STATISTIC" => "Distance moved", @name => @num_distance_moved}
stats << {"STATISTIC" => "Rooms visited", @name => @num_rooms_visited}
stats << {"STATISTIC" => "Caves visited", @name => @num_caves_visited}
if (@game.sunken_treasures)
stats << {"STATISTIC" => "Flooded rooms visited", @name => @num_flooded_rooms_visited}
end
stats << {"STATISTIC" => "Times teleported", @name => @num_times_teleported}
stats << {"STATISTIC" => "Damage dealt", @name => @num_damage_dealt}
stats << {"STATISTIC" => "Monsters killed", @name => @num_monsters_killed}
stats << {"STATISTIC" => "Damage taken", @name => @num_damage_taken}
stats << {"STATISTIC" => "Damage healed", @name => @num_damage_healed}
if (@game.sunken_treasures)
stats << {"STATISTIC" => "Number of times out of air", @name => @num_times_out_of_air}
end
stats << {"STATISTIC" => "Clank added", @name => @num_clank_added}
stats << {"STATISTIC" => "Clank removed", @name => @num_clank_removed}
stats << {"STATISTIC" => "Coins collected", @name => @num_coins_collected}
stats << {"STATISTIC" => "Major secrets collected", @name => @num_major_secrets_collected}
stats << {"STATISTIC" => "Minor secrets collected", @name => @num_minor_secrets_collected}
stats << {"STATISTIC" => "Artifacts collected", @name => @artifact.length()}
stats
end
private
def output_abilities()
ability = {}
if @skill != 0
ability["SKILL"] = @skill
end
if @move != 0
ability["MOVE"] = @move
end
if @attack != 0
ability["ATTACK"] = @attack
end
if @coins != 0
ability["COINS"] = @coins
end
if @teleport != 0
ability["TELEPORT"] = @teleport
end
if @clank_remove < 0
ability["CLANK"] = @clank_remove
end
if ability.keys.count > 0
output("\n" + Klank.table([ability]))
end
end
def equip()
loop do
cards = []
@hand.each_with_index do |c, i|
cards << [i, c.play_desc]
end
c = menu("HAND", cards, true, true)
play = []
if c == "A"
play = @hand
@hand = []
elsif c == "N"
break
else
play = [@hand.delete_at(c.to_i)]
end
@played += play
if play.count > 0
msg = []
play.each do |card|
card.equip(self)
msg << "#{card.name}"
@num_cards_played += 1
end
@game.broadcast("#{@name} played #{msg.join(", ")}!")
end
if play.select { |c| c.name == "Stumble" }.count == 2
@game.broadcast("#{@name} stumbled twice and faceplanted!")
end
break if @hand.count == 0
end
end
def play()
loop do
items = []
lookup = []
@item.each_with_index do |item, i|
if item.playable
items << [lookup.count, item.desc()]
lookup << i
end
end
i = menu("ITEMS", items, true)
if i != "N"
if @item[lookup[i.to_i]].play(self)
@item.delete_at(lookup[i.to_i])
break if (@item.count == 0)
else
output("Item not played!")
end
else
break
end
end
end
end
end