-
Notifications
You must be signed in to change notification settings - Fork 0
/
krage.rb
174 lines (155 loc) · 5.24 KB
/
krage.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
# frozen_string_literal: true
SOUND_ON = system("ps x | grep -q '[p]aplay.*echo.ogg'")
STTY_STATE = `stty -g`.freeze
KRAGE_DIR = File.expand_path(__dir__)
unless RUBY_VERSION.to_f > 2.4
STDERR.puts 'Require ruby > 2.4'
sleep 2
exit 1
end
unless system("ps x | grep -q '[/]usr/local/bin/krage'")
abort('Run command `krage` if the game is installed')
end
trap('SIGINT') { `pkill -9 -f 'paplay.*krage'`; exit }
print "\e[?25l\e[?7l\e[?1000h"
`stty -echo`
$LOAD_PATH.unshift("#{KRAGE_DIR}/lib")
require 'krage_class'
CROW = YAML.load(File.read("#{KRAGE_DIR}/ext/crow.yaml"))
POINTER = "\e[33m⮕\e[0m "
FINAL_OPTIONS = ["#{POINTER}Courageous enough for one more challenge ",
'I have had enough of this silly game']
game_options = [+"#{POINTER}Krage For Softies 🔰 ", 'Krage Under Pressure ']
MUSIC = ['krage_slavic.ogg', 'krage_western.ogg',
'krage_viking.ogg', 'krage_indigenous.ogg'].freeze
krage_profile_id = File.read("#{KRAGE_DIR}/ext/.current_krage_id")
font = `dconf read /org/gnome/terminal/legacy/profiles:/:#{krage_profile_id}/font\
| grep -Eo "[0-9]+\.?[0-9]*"`.to_f
curr_rows_cols = File.read("#{KRAGE_DIR}/ext/.current_rows_columns") rescue ''
if curr_rows_cols != `stty size`
begin
sleep 0.05
rows, columns = `stty size`.split(' ').map(&:to_i)
if rows < 44 || columns < 159
font -= rows + columns < 170 ? 0.5 : 0.1
status_change = 'reducing'
else
font += rows + columns > 235 ? 0.5 : 0.1 if status_change == 'adding'
status_change = 'adding'
end
font_status ||= status_change
`dconf write /org/gnome/terminal/legacy/profiles:/:#{krage_profile_id}/font\
"'DejaVu Sans Mono Bold #{font}'"`
end while font_status == status_change
sleep 0.05
`stty size > #{KRAGE_DIR}/ext/.current_rows_columns`
end
rows, columns = `stty size`.split(' ').map(&:to_i)
NS = (rows-44) / 2
WE = (columns-159) / 2
if SOUND_ON
spawn("paplay #{KRAGE_DIR}/data/welcome.ogg")
spawn("paplay #{KRAGE_DIR}/data/#{MUSIC[rand(4)]}")
end
Displayable.intro
loop do
print `tput cup #{32+NS} #{60+WE}`, "What type of challenge do you seek?(⇵|⇆)\n\n"
Displayable.write_options(game_options)
puts "\n\n"
puts "Aren't you ready for the challenge? Press CTRL+C to exit".rjust(108+WE)
key = `#{KRAGE_DIR}/ext/gen_keyboard`
case key
when ''
GAME_HARD = game_options.any?(/⚔️/)
GAME_WITH_TIMER = game_options[1][5] == '⮕'
HANDICAP = (GAME_HARD ? 0.15 : 0.45) + (GAME_WITH_TIMER ? 0.25 : 0)
break
when "\e[A", "\e[B"
Displayable.change_option(game_options)
when "\e[C", "\e[D"
Displayable.change_option(game_options, true)
end
end
space = ' ' * (71+WE)
print_input_txt = ->(n) { print "\n#{space}\e[4#{n}m#{n}. Player Name:\e[0m " }
Displayable.intro
print "\e[?25hHow many brave souls will participate?(2-4) ".rjust(108+WE)
while num_players ||= `#{KRAGE_DIR}/ext/gen_keyboard`.to_i
if num_players.between?(2, 4)
print `tput cup #{32+NS} #{102+WE}`, num_players.to_s
confirm = `#{KRAGE_DIR}/ext/gen_keyboard`
if confirm == ''
`stty echo`
puts "\n"
NUM_OF_PL = num_players
print_input_txt.call(1)
player1 = Krage.create_new_player
print_input_txt.call(NUM_OF_PL == 2 ? 4 : 2)
player2 = Krage.create_new_player
if num_players > 2
print_input_txt.call(3)
player3 = Krage.create_new_player
if num_players == 4
print_input_txt.call(4)
player4 = Krage.create_new_player
end
end
break
elsif confirm.to_i.between?(2, 4)
num_players = confirm.to_i
elsif confirm == "\u007F"
print "\e[1D \e[1D"
num_players = nil
end
else
num_players = nil
end
end
players = [player1, player2, player3, player4].compact
score = {}
`stty -echo -icanon -icrnl`
print "\n\n" * (4-players.size)
print "\e[?25l\n#{space}Alea iacta est"
5.times { print '!'; sleep 0.2 }
players.cycle do |player|
next unless player
player.generate_coords == 'r' ? player.roll : redo
next score[player] = player.calc_player_territory if player.round == 1
loop do
case button ||= player.generate_coords
when Array
button = player.choose_place
button.to_s.match?(/^[sg]$/) ? redo : break if button
when /^[qweyx]$/
player.jokers(button)
when 's'
break player.spawn("paplay #{KRAGE_DIR}/data/skip.ogg")
when 'g'
player.spawn("paplay #{KRAGE_DIR}/data/giveup.ogg")
score.delete(player)
player.clear_giveuper
players[players.index(player)] = nil
players.compact.last.final_choice if players.count(&:itself) < 2
break
end
end
player.show_current_land = false
if score[player]
if player.eater
player.eater = false
score.each_key do |pl|
score[pl] = pl.calc_player_territory
end
else score[player] = player.calc_player_territory
end
end
Krage.class_variable_set(:@@show_turn, true)
player.countdown = '' if GAME_WITH_TIMER
next unless player.p_num >= Krage.class_variable_get(:@@p_info).keys.max
strongest = score.values.max
second_strongest = score.values.max(2)[1]
empty_spaces = Krage.class_variable_get(:@@map).flatten.count('___')
if second_strongest + empty_spaces / (score.size-HANDICAP) < strongest
score.key(strongest).final_choice
end
end