forked from kevinkey/klank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.rb
246 lines (210 loc) · 6.78 KB
/
game.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
module Klank
require_relative "deck.rb"
require_relative "dragon.rb"
require_relative "dungeon.rb"
require_relative "map.rb"
require_relative "utils.rb"
class Game
attr_reader :name
attr_reader :num
attr_reader :map
attr_reader :promo
attr_reader :started
attr_reader :shutdown
attr_reader :game_over
attr_reader :reserve
attr_reader :dungeon
attr_reader :dragon
attr_reader :mummy
attr_reader :player
attr_reader :escalation
attr_accessor :game
def initialize(name, num, map, game, promo)
@name = name
@num = num
@map_num = map
@game = game
@promo = promo
@map = Map.new(self, map)
@mummy = :camel
@player = []
@spectator = []
@shutdown = false
@game_over = false
@started = false
end
def status
{
"Name" => @name,
"Game" => @game,
"Map" => @map_num,
"Players" => "#{@player.count} / #{@num}"
}
end
def sunken_treasures?()
@game == "Sunken Treasures"
end
def mummys_curse?()
@game == "The Mummy's Curse"
end
def spectate(player)
@spectator << player
loop do
sleep(1.0)
break if @shutdown
end
end
def join(player)
@player.each_with_index do |p, i|
p.output("#{@player.count}: #{player.name}")
player.output("#{i}: #{p.name}")
end
player.output("#{@player.count}: #{player.name}")
@player << player
if @player.count == @num
start()
else
loop do
sleep(1.0)
break if @shutdown
end
end
end
def broadcast(msg)
@player.each do |p|
p.output(msg)
end
@spectator.each do |p|
begin
p.output(msg)
rescue => exception
@spectator.remove(p)
end
end
end
def roll_pyramid_die()
region = %i[axe camel snake sun].sample
broadcast("\nThe pyramid was rolled, and it landed on the #{region} region!")
sleep(1)
if @mummy != region
broadcast("The mummy moved from the #{@mummy} region to the #{region} region!")
sleep(1)
@mummy = region
@player.each do |p|
if @map.region(p) == @mummy
p.curse()
sleep(1)
end
end
else
broadcast("The mummy stayed at the #{@mummy} region!")
sleep(1)
end
end
def trigger_end(player)
if @trigger < 0
@trigger = player.index
broadcast("#{player.name} has triggered the end of game!")
end
end
def view_players(player = nil)
status = []
@player.each do |p|
status << p.status
end
if player == nil
broadcast("\nPLAYERS\n#{Klank.table(status)}")
else
player.output("\nPLAYERS\n#{Klank.table(status)}")
end
end
def game_over?()
all_done = true
@player.each do |p|
if !p.dead?() and !p.mastery
all_done = false
break
end
end
@game_over or all_done
end
private
def start()
@dragon = Dragon.new(self)
@dungeon = Dungeon.new(self)
@player = Klank.randomize(@player)
@escalation = 0
@trigger = -1
@started = true
msg = "\nGAME STARTING\nRandomizing play order...\n"
@player.each_with_index do |p, i|
msg += "#{i}: #{p.name}\n"
p.start(self, i)
end
broadcast("#{msg}\n")
@player.each_with_index do |p, i|
p.clank(3 - i)
end
@reserve = {
x: Deck.new(self, "explore.yml"),
c: Deck.new(self, "mercenary.yml"),
t: Deck.new(self, "tome.yml"),
}
loop do
@player.each do |p|
begin
@dungeon.replenish()
break if game_over?()
view_players()
if @trigger == p.index
@escalation += 1
if @escalation > 3
@game_over = true
else
broadcast("\n#{p.name} moves to escalation level #{@escalation}!")
@dragon.attack()
end
elsif p.dead?()
broadcast("\n#{p.name} is dead!")
elsif p.mastery
broadcast("\n#{p.name} has left with an artifact!")
else
broadcast("\nStarting #{p.name}'s turn...")
p.turn()
end
rescue => exception
puts exception.full_message
broadcast("An error occurred so #{p.name}'s turn is ended prematurely, sorry!")
end
break if game_over?()
end
break if game_over?()
sleep(0.1)
end
broadcast("GAME OVER!")
scores()
stats()
@dragon.view_bag
@shutdown = true
end
def scores()
msg = "Scores (Map ##{@map.map_num})\n"
@player.sort_by { |p| p.score() }.reverse.each_with_index do |p, i|
msg += "#{p.name}: #{p.score(true)} (Room ##{p.room_num})\n"
end
broadcast(msg)
end
def stats()
stats = []
@player.each do |p|
stats += p.stats
end
stats.each do |s|
stats.select { |stat| stat.first == s.first }.each do |t|
s.merge!(t)
end
end
broadcast("\nSTATISTICS\n#{Klank.table(stats[0..((stats.length() / @player.length()) - 1)])}")
end
end
end