gale.net.room_code module

gale.net.room_code: encode a host/port pair as a short, human-typeable code (e.g. “7QK3M-2XHDR” with the default format) instead of making players share a raw IP and port. This is a pure, local, symmetric encoding: there is no matchmaking/relay server involved, and no dependency on Server/Client — a Client still connects directly to the decoded (host, port).

The default format (Crockford base32, grouped 5-5) is a sane default for “read it over voice chat” room codes, but every part of it - alphabet, group size, separator - is configurable per game through RoomCodeFormat, so a game can match whatever house style it wants (e.g. lowercase, 3-3-3 groups, no separator at all).

See docs/examples/net.rst for a walkthrough.

Author: Alejandro Mujica (aledrums@gmail.com)

exception gale.net.room_code.RoomCodeError[source]

Bases: ValueError

Raised when a string isn’t a valid room code for a given format.

class gale.net.room_code.RoomCodeFormat(alphabet='0123456789ABCDEFGHJKMNPQRSTVWXYZ', group_size=5, group_separator='-')[source]

Bases: object

How a room code is rendered: which characters it can use, how many of them are grouped together, and what separates the groups. Two RoomCodeFormat instances with the same alphabet and group_size round-trip each other’s codes regardless of group_separator, since decoding strips every group_separator character before decoding.

Usage example:

# “7qk3-m2x-hdr” style: lowercase, 4-3-3 groups, dash-separated. my_format = RoomCodeFormat(

alphabet=”0123456789abcdefghjkmnpqrstvwxyz”, group_size=4,

)

Parameters:
  • alphabet (str)

  • group_size (int)

  • group_separator (str)

alphabet: str = '0123456789ABCDEFGHJKMNPQRSTVWXYZ'
group_size: int = 5
group_separator: str = '-'
property char_count: int

How many characters, in this format’s alphabet, are needed to represent any (host, port) pair.

Type:

returns

gale.net.room_code.encode(host, port, fmt=RoomCodeFormat(alphabet='0123456789ABCDEFGHJKMNPQRSTVWXYZ', group_size=5, group_separator='-'))[source]
Parameters:
  • host (str) – An IPv4 address (e.g. “203.0.113.7”) or a resolvable hostname.

  • port (int) – A UDP/TCP port, between 0 and 65535.

  • fmt (RoomCodeFormat) – The format to render the code in. The default value is DEFAULT_FORMAT (“XXXXX-XXXXX”, Crockford base32).

Returns:

A short code that decode() can turn back into (host, port).

Return type:

str

gale.net.room_code.decode(code, fmt=RoomCodeFormat(alphabet='0123456789ABCDEFGHJKMNPQRSTVWXYZ', group_size=5, group_separator='-'))[source]
Parameters:
  • code (str) – A code produced by encode() with the same fmt (whitespace, group_separator, and, for a same-case alphabet, letter case are all ignored).

  • fmt (RoomCodeFormat) – The format code was rendered in. The default value is DEFAULT_FORMAT.

Returns:

The (host, port) pair encode() was given.

Raises:

RoomCodeError – If code contains a character outside fmt.alphabet.

Return type:

Tuple[str, int]