mirror of
https://github.com/MaSzyna-EU07/maszyna.git
synced 2026-10-18 12:39:18 +02:00
multiplayer: names instead of peer numbers, a chat, and the exe as a dedicated server
Three things that turn out to be one thing: a session with people in it needs
to be able to say who they are, and a machine that only referees one should not
have to open a window to do it.
Names. A peer id is what the protocol addresses; "player 4" is no use to anybody
reading a lobby. CLIENT_HELLO carries a requested nickname now, and the server
decides what it actually is: trimmed of control characters and runs of spaces,
capped at 24, and made unique - two people called Marcin are two people and the
lobby has to say which is which. The result goes out as PEER_ROSTER whenever
somebody joins or leaves, and to a joining peer just before its snapshot, so the
crew section resolves to names rather than numbers. The name comes from
multiplayer.nickname in the ini or --nick on the command line, falling back to
the account name and, failing that, to the old numbering. The host is a peer
like any other here too, and registers its own name at startup.
Chat, on the key left of 1. Going up a line carries only its text: who said it
is decided by the connection it arrived on, never by the field in the packet,
which is what stops anybody putting words in anybody's mouth. The server tidies
and caps it, says it, and passes it on - a client waits to hear itself back, so
that everybody sees the conversation in the same order. While the box has the
keyboard imgui swallows the input before the driver mode sees it, so typing "d"
types "d" instead of opening the doors of whatever you are driving; that also
means the key that opened the panel cannot close it, hence escape.
And --nogui. No glfw, no window, no imgui, the null renderer, sound off, log to
the console it was started from, and nobody in a cab - the scenario is left to
its own drivers and this process is a referee rather than a participant. Every
window-dependent path is guarded on an empty window list rather than on a flag
scattered through the code, and the loop is paced at fifty steps a second
because nothing is waiting on a swapchain to slow it down. Chat lines go to the
log, which is what a machine with no window has instead of a chat panel.
eu07.exe -s scenario.scn --host 0.0.0.0:7420 --nogui --nick Dispatcher
This commit is contained in:
@@ -22,6 +22,146 @@ namespace network
|
||||
{
|
||||
|
||||
entity_registry Entities;
|
||||
peer_registry Peers;
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
// long enough for a name worth having, short enough not to wreck a lobby column
|
||||
constexpr size_t NICKNAME_LIMIT = 24;
|
||||
|
||||
std::string tidy_name(std::string const &Raw)
|
||||
{
|
||||
std::string name;
|
||||
name.reserve(Raw.size());
|
||||
|
||||
bool space{false};
|
||||
for (char const character : Raw)
|
||||
{
|
||||
// control characters and tabs have no business in a name that goes on somebody
|
||||
// else's screen, and a run of spaces is one space
|
||||
unsigned char const value = (unsigned char)character;
|
||||
if (value < 0x20 || value == 0x7f)
|
||||
continue;
|
||||
|
||||
if (character == ' ')
|
||||
{
|
||||
space = !name.empty();
|
||||
continue;
|
||||
}
|
||||
|
||||
if (space)
|
||||
{
|
||||
name += ' ';
|
||||
space = false;
|
||||
}
|
||||
|
||||
name += character;
|
||||
if (name.size() >= NICKNAME_LIMIT)
|
||||
break;
|
||||
}
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::string local_nickname()
|
||||
{
|
||||
auto name = tidy_name(Global.multiplayer_nickname);
|
||||
if (!name.empty())
|
||||
return name;
|
||||
|
||||
// nothing configured: whoever is logged in will do, and failing that nothing at all -
|
||||
// the registry has a stand-in for that case
|
||||
for (char const *variable : {"EU07_NICKNAME", "USERNAME", "USER", "LOGNAME"})
|
||||
{
|
||||
char const *value = std::getenv(variable);
|
||||
if (value == nullptr)
|
||||
continue;
|
||||
name = tidy_name(value);
|
||||
if (!name.empty())
|
||||
return name;
|
||||
}
|
||||
|
||||
return std::string();
|
||||
}
|
||||
|
||||
bool peer_registry::taken(std::string const &Name, PeerId const Except) const
|
||||
{
|
||||
for (auto const &entry : m_names)
|
||||
{
|
||||
if (entry.first == Except)
|
||||
continue;
|
||||
if (entry.second == Name)
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string peer_registry::assign(PeerId const Peer, std::string const &Requested)
|
||||
{
|
||||
std::string name = tidy_name(Requested);
|
||||
|
||||
if (name.empty())
|
||||
name = (Peer == PEER_HOST ? std::string("host") : "player " + std::to_string(Peer));
|
||||
|
||||
// two people called Marcin are two people, and the lobby has to say which is which
|
||||
if (taken(name, Peer))
|
||||
{
|
||||
std::string const base = name;
|
||||
for (int suffix = 2; suffix < 100; ++suffix)
|
||||
{
|
||||
name = base + " (" + std::to_string(suffix) + ")";
|
||||
if (!taken(name, Peer))
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
m_names[Peer] = name;
|
||||
|
||||
return name;
|
||||
}
|
||||
|
||||
void peer_registry::forget(PeerId const Peer)
|
||||
{
|
||||
m_names.erase(Peer);
|
||||
}
|
||||
|
||||
void peer_registry::adopt(std::vector<peer_entry> const &Roster)
|
||||
{
|
||||
m_names.clear();
|
||||
for (auto const &entry : Roster)
|
||||
m_names[entry.id] = entry.name;
|
||||
}
|
||||
|
||||
std::string peer_registry::name_of(PeerId const Peer) const
|
||||
{
|
||||
auto const lookup = m_names.find(Peer);
|
||||
if (lookup != m_names.end())
|
||||
return lookup->second;
|
||||
|
||||
if (Peer == PEER_HOST)
|
||||
return "host";
|
||||
if (Peer == PEER_NONE)
|
||||
return "nobody";
|
||||
|
||||
return "player " + std::to_string(Peer);
|
||||
}
|
||||
|
||||
std::vector<peer_entry> peer_registry::roster() const
|
||||
{
|
||||
std::vector<peer_entry> entries;
|
||||
entries.reserve(m_names.size());
|
||||
for (auto const &entry : m_names)
|
||||
entries.emplace_back(peer_entry{entry.first, entry.second});
|
||||
return entries;
|
||||
}
|
||||
|
||||
void peer_registry::clear()
|
||||
{
|
||||
m_names.clear();
|
||||
}
|
||||
|
||||
PeerId allocate_peer_id()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user