Type hint a little more
This commit is contained in:
parent
468566d831
commit
a7658383ac
|
@ -51,7 +51,7 @@ class CtlConfig(DispConfig):
|
|||
Zoom speed, in scroll steps per zoom range.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
DispConfig.__init__(self)
|
||||
self.options.add_argument(
|
||||
'--mouse-speed', type=float, dest='mouspeed',
|
||||
|
@ -68,10 +68,10 @@ class CtlConfig(DispConfig):
|
|||
return self.__mouspeed / 800
|
||||
|
||||
@mouspeed.setter
|
||||
def mouspeed(self, value):
|
||||
def mouspeed(self, value: float) -> None:
|
||||
self.__mouspeed = value
|
||||
|
||||
def fallback(self):
|
||||
def fallback(self) -> None:
|
||||
"""Parse fallback configurations."""
|
||||
DispConfig.fallback(self)
|
||||
self.mouspeed = self.config.getfloat('Control', 'Mouse speed')
|
||||
|
@ -161,7 +161,7 @@ class Control(Display):
|
|||
"""Return whether given keys are pressed."""
|
||||
return any(glfw.get_key(self.window, k) == glfw.PRESS for k in keys)
|
||||
|
||||
def control(self):
|
||||
def control(self) -> None:
|
||||
"""Handle events controlling the protagonist."""
|
||||
Display.control(self)
|
||||
right, upward, forward = 0, 0, 0
|
||||
|
|
|
@ -71,7 +71,7 @@ class DispConfig(PeerConfig):
|
|||
Zoom level.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
PeerConfig.__init__(self)
|
||||
self.options.add_argument(
|
||||
'--size', type=int, nargs=2, metavar=('X', 'Y'),
|
||||
|
@ -108,7 +108,7 @@ class DispConfig(PeerConfig):
|
|||
return
|
||||
self.zmlvl = log2(rad)
|
||||
|
||||
def fallback(self):
|
||||
def fallback(self) -> None:
|
||||
"""Parse fallback configurations."""
|
||||
PeerConfig.fallback(self)
|
||||
self.size = (self.config.getint('Graphics', 'Screen width'),
|
||||
|
@ -201,7 +201,7 @@ class Display(Peer):
|
|||
|
||||
# Create OpenGL context
|
||||
self.context = context = moderngl.create_context()
|
||||
context.enable_only(moderngl.DEPTH_TEST)
|
||||
context.enable_only(context.DEPTH_TEST)
|
||||
|
||||
# GLSL program and vertex array for map rendering
|
||||
self.maprog = context.program(vertex_shader=MAP_VERTEX,
|
||||
|
@ -356,7 +356,7 @@ class Display(Peer):
|
|||
Peer.add_pico(self, address)
|
||||
self.colors[address] = randint(0, 5)
|
||||
|
||||
def render(self):
|
||||
def render(self) -> None:
|
||||
"""Render the scene before post-processing."""
|
||||
visibility = self.visibility
|
||||
projection = matrix44.create_perspective_projection(
|
||||
|
@ -379,7 +379,7 @@ class Display(Peer):
|
|||
for shard in pico.shards.values(): self.render_shard(shard)
|
||||
if pico is not self.camera: self.render_pico(pico)
|
||||
|
||||
def update(self):
|
||||
def update(self) -> None:
|
||||
"""Update and render the map."""
|
||||
# Update states
|
||||
Peer.update(self)
|
||||
|
@ -420,7 +420,7 @@ class Display(Peer):
|
|||
self.postr, *self.addr, self.fpstr))
|
||||
|
||||
@abstractmethod
|
||||
def control(self):
|
||||
def control(self) -> None:
|
||||
"""Poll resizing and closing events."""
|
||||
glfw.poll_events()
|
||||
|
||||
|
|
28
axuy/peer.py
28
axuy/peer.py
|
@ -56,7 +56,7 @@ class PeerConfig:
|
|||
Address of the peer that created the map.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
def __init__(self) -> None:
|
||||
dirs = AppDirs(appname='axuy', appauthor=False, multipath=True)
|
||||
parents = dirs.site_config_dir.split(pathsep)
|
||||
parents.append(dirs.user_config_dir)
|
||||
|
@ -91,7 +91,7 @@ class PeerConfig:
|
|||
'-s', '--seeder', metavar='ADDRESS',
|
||||
help='address of the peer that created the map')
|
||||
|
||||
def fallback(self):
|
||||
def fallback(self) -> None:
|
||||
"""Parse fallback configurations."""
|
||||
self.host = self.config.get('Peer', 'Host')
|
||||
self.port = self.config.getint('Peer', 'Port')
|
||||
|
@ -105,7 +105,7 @@ class PeerConfig:
|
|||
return self.__seed
|
||||
|
||||
@seeder.setter
|
||||
def seeder(self, value):
|
||||
def seeder(self, value: str) -> None:
|
||||
host, port = value.split(':')
|
||||
self.__seed = host, int(port)
|
||||
|
||||
|
@ -115,7 +115,7 @@ class PeerConfig:
|
|||
value = getattr(arguments, option)
|
||||
if value is not None: setattr(self, option, value)
|
||||
|
||||
def parse(self):
|
||||
def parse(self) -> None:
|
||||
"""Parse all configurations."""
|
||||
args = self.options.parse_args()
|
||||
if args.cfgout is not None:
|
||||
|
@ -145,9 +145,9 @@ class Peer(ABC):
|
|||
q : Queue[Tuple[bytes, Tuple[str, int]]]
|
||||
Queue of (data, addr), where addr is the address of the peer
|
||||
who sent the raw data.
|
||||
peers : List[Tuple[str, int], ...]
|
||||
peers : List[Tuple[str, int]]
|
||||
Addresses of connected peers.
|
||||
mapid : List[int, ...]
|
||||
mapid : List[int]
|
||||
Permutation of map building blocks.
|
||||
space : numpy.ndarray of shape (12, 12, 9) of bools
|
||||
3D array of occupied space.
|
||||
|
@ -204,10 +204,10 @@ class Peer(ABC):
|
|||
return self.pico.fps
|
||||
|
||||
@fps.setter
|
||||
def fps(self, fps):
|
||||
def fps(self, fps: float) -> None:
|
||||
self.pico.fps = fps
|
||||
|
||||
def serve(self):
|
||||
def serve(self) -> None:
|
||||
"""Initiate other peers."""
|
||||
with socket() as server: # TCP server
|
||||
server.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
|
||||
|
@ -220,7 +220,7 @@ class Peer(ABC):
|
|||
conn.close()
|
||||
server.close()
|
||||
|
||||
def pull(self):
|
||||
def pull(self) -> None:
|
||||
"""Receive other peers' states."""
|
||||
while self.is_running: self.q.put(self.sock.recvfrom(1 << 16))
|
||||
while not self.q.empty():
|
||||
|
@ -235,7 +235,7 @@ class Peer(ABC):
|
|||
"""Add pico from given address."""
|
||||
self.picos[address] = Pico(address, self.space)
|
||||
|
||||
def sync(self):
|
||||
def sync(self) -> None:
|
||||
"""Synchronize states received from other peers."""
|
||||
for data, addr in self.ready:
|
||||
if addr not in self.picos:
|
||||
|
@ -243,7 +243,7 @@ class Peer(ABC):
|
|||
self.add_pico(addr)
|
||||
self.picos[addr].sync(*loads(data))
|
||||
|
||||
def push(self):
|
||||
def push(self) -> None:
|
||||
"""Push states to other peers."""
|
||||
shards = {i: (s.pos, s.rot, s.power)
|
||||
for i, s in self.pico.shards.items()}
|
||||
|
@ -251,11 +251,11 @@ class Peer(ABC):
|
|||
for peer in self.peers: self.sock.sendto(data, peer)
|
||||
|
||||
@abstractmethod
|
||||
def control(self):
|
||||
def control(self) -> None:
|
||||
"""Control the protagonist."""
|
||||
self.pico.update() # just a reminder that this needs to be called
|
||||
|
||||
def update(self):
|
||||
def update(self) -> None:
|
||||
"""Update internal states and send them to other peers."""
|
||||
next_time = self.get_time()
|
||||
self.fps = 1 / (next_time-self.last_time)
|
||||
|
@ -272,7 +272,7 @@ class Peer(ABC):
|
|||
pico.shards = shards
|
||||
self.push()
|
||||
|
||||
def run(self):
|
||||
def run(self) -> None:
|
||||
"""Start main loop."""
|
||||
Thread(target=self.serve, daemon=True).start()
|
||||
Thread(target=self.pull, daemon=True).start()
|
||||
|
|
Loading…
Reference in New Issue