nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
guild_window.cc
Go to the documentation of this file.
2
3#include <imgui.h>
4
5#include <algorithm>
6#include <cstdint>
7#include <string>
8#include <utility>
9
10#include "absl/strings/str_cat.h"
11#include "absl/strings/substitute.h"
12#include "client/theme.h"
13#include "client/translations.h"
14
15namespace nanommo {
16
17namespace {
18
19// As long as the server keeps an invitation.
20constexpr double kInvitationLife = 60.0;
21
22// As the enemies' names over their heads are.
23constexpr ImVec4 kEnemy{1.0f, 0.31f, 0.27f, 1.0f};
24
25} // namespace
26
27const char* RankName(protocol::Rank rank) {
28 switch (rank) {
29 case protocol::RANK_LEADER:
30 return Tr("Leader");
31 case protocol::RANK_OFFICER:
32 return Tr("Officer");
33 default:
34 return Tr("Member");
35 }
36}
37
38std::string TimeLeft(uint32_t seconds) {
39 const uint32_t past = seconds % 60;
40 return absl::StrCat(seconds / 60, past < 10 ? ":0" : ":", past);
41}
42
43void GuildWindow::Invited(protocol::GuildInvite invite) {
44 invite_ = std::move(invite);
45 invited_at_ = ImGui::GetTime();
46}
47
48std::optional<protocol::GuildAction> GuildWindow::Build(
49 const protocol::Guild& yours, uint32_t you) {
50 std::optional<protocol::GuildAction> action = BuildQuestion();
51 // The news comes as the war starts, or its score moves, so its time left
52 // counts down here in between.
53 if (yours.war().left() != war_left_) {
54 war_left_ = yours.war().left();
55 war_news_at_ = ImGui::GetTime();
56 }
57 if (!visible_) {
58 return action;
59 }
60 const ImVec2 display = ImGui::GetIO().DisplaySize;
61 ImGui::SetNextWindowPos({display.x * 0.5f, display.y * 0.4f},
62 ImGuiCond_FirstUseEver, {0.5f, 0.5f});
63 if (BeginPanel("Guild", &visible_,
64 ImGuiWindowFlags_AlwaysAutoResize |
65 ImGuiWindowFlags_NoSavedSettings)) {
66 const float width = Ui(280.0f);
67 const bool named = name_[0] != '\0';
68 if (yours.name().empty()) {
69 ImGui::TextUnformatted(Tr("You're in no guild. Found one:"));
70 ImGui::TextDisabled("%s",
71 Tr("3 to 24 letters, one space between words."));
72 ImGui::SetNextItemWidth(width);
73 const bool entered = ImGui::InputTextWithHint(
74 "##name", Tr("Its name"), name_.data(), name_.size(),
75 ImGuiInputTextFlags_EnterReturnsTrue);
76 if ((GameButton("Found the guild", {width, 0.0f}, true, named) ||
77 entered) &&
78 named) {
79 action.emplace().set_create(name_.data());
80 }
81 } else {
82 ImGui::TextUnformatted(yours.name().c_str());
83 ImGui::TextDisabled("%s", RankName(yours.rank()));
84 // At war: with whom, the score from our side, and how long it has left.
85 if (yours.has_war()) {
86 const protocol::GuildWar& war = yours.war();
87 const auto since =
88 static_cast<uint32_t>(ImGui::GetTime() - war_news_at_);
89 ImGui::Spacing();
90 ImGui::TextColored(
91 kEnemy, "%s",
92 absl::Substitute(Tr("At war with $0"), war.enemy()).c_str());
93 ImGui::TextUnformatted(absl::Substitute(Tr("Kills: $0 to $1"),
94 war.kills(), war.enemy_kills())
95 .c_str());
96 // Translators: $0 is the time the war has left, as 29:59.
97 ImGui::TextDisabled(
98 "%s", absl::Substitute(
99 Tr("$0 left"),
100 TimeLeft(war.left() > since ? war.left() - since : 0))
101 .c_str());
102 ImGui::Spacing();
103 }
104 // The roster, the highest rank first, and those playing in white.
105 if (ImGui::BeginTable(
106 "Roster", 3,
107 ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingStretchProp,
108 {width, 0.0f})) {
109 ImGui::TableSetupColumn(TrLabel("Name"));
110 ImGui::TableSetupColumn(TrLabel("Rank"));
111 ImGui::TableSetupColumn(TrLabel("Level"));
112 ImGui::TableHeadersRow();
113 const bool leader = yours.rank() == protocol::RANK_LEADER;
114 for (const protocol::GuildMember& member : yours.members()) {
115 ImGui::TableNextRow();
116 ImGui::PushID(static_cast<int>(member.id()));
117 // Grey for those who aren't playing, but not their menu.
118 const ImVec4 color = ImGui::GetStyleColorVec4(
119 member.online() ? ImGuiCol_Text : ImGuiCol_TextDisabled);
120 ImGui::PushStyleColor(ImGuiCol_Text, color);
121 ImGui::TableNextColumn();
122 ImGui::Selectable(member.name().c_str(), false,
123 ImGuiSelectableFlags_SpanAllColumns);
124 ImGui::PopStyleColor();
125 if (member.id() != you && yours.rank() > member.rank() &&
126 ImGui::BeginPopupContextItem("Member")) {
127 if (leader && member.rank() == protocol::RANK_MEMBER &&
128 ImGui::MenuItem(TrLabel("Make an officer"))) {
129 action.emplace().set_promote(member.id());
130 }
131 if (leader && member.rank() == protocol::RANK_OFFICER &&
132 ImGui::MenuItem(TrLabel("Make a member"))) {
133 action.emplace().set_demote(member.id());
134 }
135 if (ImGui::MenuItem(TrLabel("Remove from the guild"))) {
136 action.emplace().set_kick(member.id());
137 }
138 ImGui::EndPopup();
139 }
140 ImGui::PushStyleColor(ImGuiCol_Text, color);
141 ImGui::TableNextColumn();
142 ImGui::TextUnformatted(RankName(member.rank()));
143 ImGui::TableNextColumn();
144 ImGui::Text("%u", member.level());
145 ImGui::PopStyleColor();
146 ImGui::PopID();
147 }
148 ImGui::EndTable();
149 }
150 const auto online = std::ranges::count_if(
151 yours.members(),
152 [](const protocol::GuildMember& member) { return member.online(); });
153 ImGui::TextDisabled("%s", absl::Substitute(Tr("$0 of $1 playing now."),
154 online, yours.members_size())
155 .c_str());
156 // Officers and the leader ask players in, by name.
157 if (yours.rank() != protocol::RANK_MEMBER) {
158 ImGui::Spacing();
159 ImGui::SetNextItemWidth(width);
160 const bool entered = ImGui::InputTextWithHint(
161 "##player", Tr("Someone playing here"), player_.data(),
162 player_.size(), ImGuiInputTextFlags_EnterReturnsTrue);
163 const bool someone = player_[0] != '\0';
164 if ((GameButton("Ask them in", {width, 0.0f}, false, someone) ||
165 entered) &&
166 someone) {
167 action.emplace().set_invite(player_.data());
168 player_.fill('\0');
169 }
170 }
171 ImGui::Spacing();
172 if (GameButton("Leave the guild", {width, 0.0f})) {
173 action.emplace().set_leave(true);
174 }
175 if (yours.rank() == protocol::RANK_LEADER &&
176 GameButton("Disband the guild", {width, 0.0f})) {
177 disbanding_ = true;
178 }
179 }
180 }
181 EndPanel();
182 if (disbanding_ && !yours.name().empty()) {
183 if (std::optional<protocol::GuildAction> disband =
184 BuildDisband(yours.name())) {
185 action = std::move(disband);
186 }
187 }
188 return action;
189}
190
191std::optional<protocol::GuildAction> GuildWindow::BuildDisband(
192 const std::string& guild) {
193 const ImVec2 display = ImGui::GetIO().DisplaySize;
194 ImGui::SetNextWindowPos({display.x / 2.0f, display.y * 0.42f},
195 ImGuiCond_Always, {0.5f, 0.5f});
196 bool open = true;
197 std::optional<protocol::GuildAction> action;
198 BeginPanel("Disband?", &open,
199 ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove |
200 ImGuiWindowFlags_NoSavedSettings);
201 ImGui::TextUnformatted(
202 absl::Substitute(Tr("Disband $0, for everyone in it, for good?"), guild)
203 .c_str());
204 ImGui::Spacing();
205 if (GameButton("Disband", {Ui(150.0f), 0.0f}, true)) {
206 action.emplace().set_disband(true);
207 open = false;
208 }
209 ImGui::SameLine();
210 if (GameButton("Keep it", {Ui(150.0f), 0.0f}) ||
211 ImGui::IsKeyPressed(ImGuiKey_Escape)) {
212 open = false;
213 }
214 EndPanel();
215 disbanding_ = open;
216 return action;
217}
218
219std::optional<protocol::GuildAction> GuildWindow::BuildQuestion() {
220 if (!invite_ || ImGui::GetTime() - invited_at_ > kInvitationLife) {
221 invite_.reset();
222 return std::nullopt;
223 }
224 const ImVec2 display = ImGui::GetIO().DisplaySize;
225 ImGui::SetNextWindowPos({display.x / 2.0f, display.y * 0.3f},
226 ImGuiCond_Always, {0.5f, 0.5f});
227 bool open = true;
228 std::optional<protocol::GuildAction> action;
229 BeginPanel("Join a guild?", &open,
230 ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove |
231 ImGuiWindowFlags_NoSavedSettings);
232 ImGui::TextUnformatted(absl::Substitute(Tr("$0 asks you to join $1."),
233 invite_->from(), invite_->guild())
234 .c_str());
235 ImGui::Spacing();
236 if (GameButton("Join", {Ui(150.0f), 0.0f}, true)) {
237 action.emplace().set_accept(true);
238 open = false;
239 }
240 ImGui::SameLine();
241 // Saying no tells the server, so that another guild can ask.
242 if (GameButton("Not now", {Ui(150.0f), 0.0f})) {
243 action.emplace().set_decline(true);
244 open = false;
245 }
246 EndPanel();
247 if (!open) {
248 invite_.reset();
249 }
250 return action;
251}
252
253} // namespace nanommo
void Invited(protocol::GuildInvite invite)
std::optional< protocol::GuildAction > Build(const protocol::Guild &yours, uint32_t you)
Definition chat.cc:9
bool BeginPanel(const char *name, bool *open, ImGuiWindowFlags flags, float alpha)
Definition theme.cc:320
float Ui(float points)
Definition theme.h:67
void EndPanel()
Definition theme.cc:379
const char * Tr(const char *text)
const char * RankName(protocol::Rank rank)
What a rank is called: Leader, Officer or Member.
const char * TrLabel(const char *label)
bool GameButton(const char *label, ImVec2 size, bool primary, bool enabled)
Definition theme.cc:381
std::string TimeLeft(uint32_t seconds)
How long a war has left, in minutes and seconds, such as 29:05.