nanoMMO 0.97.8
Building an MMO: The Engineering Series
Loading...
Searching...
No Matches
duels.cc
Go to the documentation of this file.
1#include "server/duels.h"
2
3#include <chrono>
4#include <cstdint>
5#include <set>
6#include <utility>
7#include <vector>
8
9namespace nanommo {
10
11void Duels::Ask(uint32_t id, uint32_t other) {
12 duels_[id] = {.challenger = id, .challenged = other};
13 challengers_[id] = id;
14 challengers_[other] = id;
15 news_.insert({id, other});
16}
17
18uint32_t Duels::Accept(uint32_t id, Place flag) {
19 const auto in = challengers_.find(id);
20 if (in == challengers_.end() || in->second == id) {
21 return 0;
22 }
23 Duel& duel = duels_.at(in->second);
24 if (duel.state != State::kAsked) {
25 return 0;
26 }
28 duel.left = kCountdown;
29 duel.flag = flag;
30 news_.insert({id, duel.challenger});
31 return duel.challenger;
32}
33
34const Duels::Duel* Duels::Of(uint32_t id) const {
35 const auto in = challengers_.find(id);
36 return in != challengers_.end() ? &duels_.at(in->second) : nullptr;
37}
38
39uint32_t Duels::Opponent(uint32_t id) const {
40 const Duel* duel = Of(id);
41 return duel != nullptr && duel->state == State::kFighting ? duel->Against(id)
42 : 0;
43}
44
45uint32_t Duels::End(uint32_t id) {
46 const auto in = challengers_.find(id);
47 if (in == challengers_.end()) {
48 return 0;
49 }
50 const auto duel = duels_.find(in->second);
51 const uint32_t other = duel->second.Against(id);
52 duels_.erase(duel);
53 challengers_.erase(id);
54 challengers_.erase(other);
55 news_.insert({id, other});
56 return other;
57}
58
59std::vector<Duels::Duel> Duels::Step(std::chrono::nanoseconds time) {
60 std::vector<Duel> ran_out;
61 for (auto& [challenger, duel] : duels_) {
62 if (duel.state == State::kFighting) {
63 continue;
64 }
65 const std::chrono::nanoseconds before = duel.left;
66 duel.left -= time;
67 if (duel.state == State::kAsked) {
68 if (duel.left <= std::chrono::nanoseconds::zero()) {
69 ran_out.push_back(duel);
70 }
71 continue;
72 }
73 // Both sides hear as each second of the countdown goes, so that each
74 // shows the seconds the server has left, and as it ends.
75 if (duel.left <= std::chrono::nanoseconds::zero()) {
76 duel.state = State::kFighting;
77 duel.left = std::chrono::nanoseconds::zero();
78 news_.insert({duel.challenger, duel.challenged});
79 } else if (std::chrono::ceil<std::chrono::seconds>(duel.left) <
80 std::chrono::ceil<std::chrono::seconds>(before)) {
81 news_.insert({duel.challenger, duel.challenged});
82 }
83 }
84 for (const Duel& duel : ran_out) {
85 End(duel.challenger);
86 }
87 return ran_out;
88}
89
90std::set<uint32_t> Duels::TakeNews() { return std::exchange(news_, {}); }
91
92} // namespace nanommo
uint32_t Accept(uint32_t id, Place flag)
Definition duels.cc:18
const Duel * Of(uint32_t id) const
The player's duel, from the ask to the end, or nullptr.
Definition duels.cc:34
uint32_t End(uint32_t id)
Definition duels.cc:45
void Ask(uint32_t id, uint32_t other)
The player asks the other to duel. Neither may be in a duel already.
Definition duels.cc:11
uint32_t Opponent(uint32_t id) const
Who the player's duelling, by their id, once the countdown's over, or 0.
Definition duels.cc:39
std::vector< Duel > Step(std::chrono::nanoseconds time)
Definition duels.cc:59
std::set< uint32_t > TakeNews()
Definition duels.cc:90
static constexpr std::chrono::seconds kCountdown
From the yes to the duel, so that both sides are ready for it.
Definition duels.h:28
Definition chat.cc:9
std::chrono::nanoseconds left
How long the ask has left, and then the countdown.
Definition duels.h:42
uint32_t challenger
Who asked, and who they asked, by their ids.
Definition duels.h:38
uint32_t Against(uint32_t id) const
The other side from the player's, by their id.
Definition duels.h:46
In metres from the middle of a land.
Definition lands.h:13