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
|
/*
* Copyright (c) 2021, Jamie Mansfield <jmansfield@cadixdev.org>
* Copyright (c) 2021, Mustafa Quraish <mustafa@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Game.h"
#include <Games/Spider/SpiderGML.h>
#include <LibConfig/Client.h>
#include <LibCore/Timer.h>
#include <LibGUI/Action.h>
#include <LibGUI/ActionGroup.h>
#include <LibGUI/Application.h>
#include <LibGUI/Icon.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Menubar.h>
#include <LibGUI/MessageBox.h>
#include <LibGUI/Statusbar.h>
#include <LibGUI/Window.h>
#include <stdio.h>
#include <unistd.h>
enum class StatisticDisplay : u8 {
HighScore,
BestTime,
__Count
};
static String format_seconds(uint64_t seconds_elapsed)
{
uint64_t hours = seconds_elapsed / 3600;
uint64_t minutes = (seconds_elapsed / 60) % 60;
uint64_t seconds = seconds_elapsed % 60;
return String::formatted("{:02}:{:02}:{:02}", hours, minutes, seconds);
}
int main(int argc, char** argv)
{
if (pledge("stdio recvfd sendfd rpath unix", nullptr) < 0) {
perror("pledge");
return 1;
}
auto app = GUI::Application::construct(argc, argv);
auto app_icon = GUI::Icon::default_icon("app-spider");
Config::pledge_domains("Spider");
if (pledge("stdio recvfd sendfd rpath", nullptr) < 0) {
perror("pledge");
return 1;
}
if (unveil("/res", "r") < 0) {
perror("unveil");
return 1;
}
if (unveil(nullptr, nullptr) < 0) {
perror("unveil");
return 1;
}
auto window = GUI::Window::construct();
window->set_title("Spider");
auto mode = static_cast<Spider::Mode>(Config::read_i32("Spider", "Settings", "Mode", static_cast<int>(Spider::Mode::SingleSuit)));
auto update_mode = [&](Spider::Mode new_mode) {
mode = new_mode;
Config::write_i32("Spider", "Settings", "Mode", static_cast<int>(mode));
};
auto mode_id = [&]() {
switch (mode) {
case Spider::Mode::SingleSuit:
return "SingleSuit";
case Spider::Mode::TwoSuit:
return "TwoSuit";
default:
VERIFY_NOT_REACHED();
}
};
auto statistic_display = static_cast<StatisticDisplay>(Config::read_i32("Spider", "Settings", "StatisticDisplay", static_cast<int>(StatisticDisplay::HighScore)));
auto update_statistic_display = [&](StatisticDisplay new_statistic_display) {
statistic_display = new_statistic_display;
Config::write_i32("Spider", "Settings", "StatisticDisplay", static_cast<int>(statistic_display));
};
auto high_score = [&]() {
return static_cast<u32>(Config::read_i32("Spider", "HighScores", mode_id(), 0));
};
auto update_high_score = [&](u32 new_high_score) {
Config::write_i32("Spider", "HighScores", mode_id(), static_cast<int>(new_high_score));
};
auto best_time = [&]() {
return static_cast<u32>(Config::read_i32("Spider", "BestTimes", mode_id(), 0));
};
auto update_best_time = [&](u32 new_best_time) {
Config::write_i32("Spider", "BestTimes", mode_id(), static_cast<int>(new_best_time));
};
auto total_wins = [&]() {
return static_cast<u32>(Config::read_i32("Spider", "TotalWins", mode_id(), 0));
};
auto increment_total_wins = [&]() {
Config::write_i32("Spider", "TotalWins", mode_id(), static_cast<int>(total_wins() + 1));
};
auto total_losses = [&]() {
return static_cast<u32>(Config::read_i32("Spider", "TotalLosses", mode_id(), 0));
};
auto increment_total_losses = [&]() {
Config::write_i32("Spider", "TotalLosses", mode_id(), static_cast<int>(total_losses() + 1));
};
if (mode >= Spider::Mode::__Count)
update_mode(Spider::Mode::SingleSuit);
if (statistic_display >= StatisticDisplay::__Count)
update_statistic_display(StatisticDisplay::HighScore);
auto& widget = window->set_main_widget<GUI::Widget>();
widget.load_from_gml(spider_gml);
auto& game = *widget.find_descendant_of_type_named<Spider::Game>("game");
game.set_focus(true);
auto& statusbar = *widget.find_descendant_of_type_named<GUI::Statusbar>("statusbar");
auto reset_statistic_status = [&]() {
switch (statistic_display) {
case StatisticDisplay::HighScore:
statusbar.set_text(1, String::formatted("High Score: {}", high_score()));
break;
case StatisticDisplay::BestTime:
statusbar.set_text(1, String::formatted("Best Time: {}", format_seconds(best_time())));
break;
default:
VERIFY_NOT_REACHED();
}
};
statusbar.set_text(0, "Score: 0");
reset_statistic_status();
statusbar.set_text(2, "Time: 00:00:00");
app->on_action_enter = [&](GUI::Action& action) {
auto text = action.status_tip();
if (text.is_empty())
text = Gfx::parse_ampersand_string(action.text());
statusbar.set_override_text(move(text));
};
app->on_action_leave = [&](GUI::Action&) {
statusbar.set_override_text({});
};
game.on_score_update = [&](uint32_t score) {
statusbar.set_text(0, String::formatted("Score: {}", score));
};
uint64_t seconds_elapsed = 0;
auto timer = Core::Timer::create_repeating(1000, [&]() {
++seconds_elapsed;
statusbar.set_text(2, String::formatted("Time: {}", format_seconds(seconds_elapsed)));
});
game.on_game_start = [&]() {
seconds_elapsed = 0;
timer->start();
statusbar.set_text(2, "Time: 00:00:00");
};
game.on_game_end = [&](Spider::GameOverReason reason, uint32_t score) {
auto game_was_in_progress = timer->is_active();
if (game_was_in_progress) {
timer->stop();
if (reason != Spider::GameOverReason::Victory)
increment_total_losses();
}
if (reason == Spider::GameOverReason::Victory) {
increment_total_wins();
if (score > high_score()) {
update_high_score(score);
}
auto current_best_time = best_time();
if (seconds_elapsed < current_best_time || current_best_time == 0) {
update_best_time(seconds_elapsed);
}
reset_statistic_status();
}
statusbar.set_text(2, "Timer starts after your first move");
};
window->on_close_request = [&]() {
auto game_in_progress = timer->is_active();
if (game_in_progress) {
auto result = GUI::MessageBox::show(window,
"A game is still in progress, are you sure you would like to quit? Doing so will count as a loss.",
"Game in progress",
GUI::MessageBox::Type::Warning,
GUI::MessageBox::InputType::YesNo);
if (result == GUI::MessageBox::ExecYes)
return GUI::Window::CloseRequestDecision::Close;
else
return GUI::Window::CloseRequestDecision::StayOpen;
}
return GUI::Window::CloseRequestDecision::Close;
};
window->on_close = [&]() {
game.on_game_end(Spider::GameOverReason::Quit, 0);
};
GUI::ActionGroup suit_actions;
suit_actions.set_exclusive(true);
auto single_suit_action = GUI::Action::create_checkable("&Single Suit", [&](auto&) {
update_mode(Spider::Mode::SingleSuit);
reset_statistic_status();
game.setup(mode);
});
single_suit_action->set_checked(mode == Spider::Mode::SingleSuit);
suit_actions.add_action(single_suit_action);
auto two_suit_action = GUI::Action::create_checkable("&Two Suit", [&](auto&) {
update_mode(Spider::Mode::TwoSuit);
reset_statistic_status();
game.setup(mode);
});
two_suit_action->set_checked(mode == Spider::Mode::TwoSuit);
suit_actions.add_action(two_suit_action);
auto& game_menu = window->add_menu("&Game");
game_menu.add_action(GUI::Action::create("&New Game", { Mod_None, Key_F2 }, [&](auto&) {
game.setup(mode);
}));
game_menu.add_separator();
game_menu.add_action(single_suit_action);
game_menu.add_action(two_suit_action);
game_menu.add_separator();
game_menu.add_action(GUI::CommonActions::make_quit_action([&](auto&) { app->quit(); }));
auto& view_menu = window->add_menu("&View");
GUI::ActionGroup statistic_display_actions;
statistic_display_actions.set_exclusive(true);
auto high_score_action = GUI::Action::create_checkable("&High Score", [&](auto&) {
update_statistic_display(StatisticDisplay::HighScore);
reset_statistic_status();
});
high_score_action->set_checked(statistic_display == StatisticDisplay::HighScore);
statistic_display_actions.add_action(high_score_action);
auto best_time_actions = GUI::Action::create_checkable("&Best Time", [&](auto&) {
update_statistic_display(StatisticDisplay::BestTime);
reset_statistic_status();
});
best_time_actions->set_checked(statistic_display == StatisticDisplay::BestTime);
statistic_display_actions.add_action(best_time_actions);
view_menu.add_action(high_score_action);
view_menu.add_action(best_time_actions);
auto& help_menu = window->add_menu("&Help");
help_menu.add_action(GUI::CommonActions::make_about_action("Spider", app_icon, window));
window->set_resizable(false);
window->resize(Spider::Game::width, Spider::Game::height + statusbar.max_height());
window->set_icon(app_icon.bitmap_for_size(16));
window->show();
game.setup(mode);
return app->exec();
}
|