"""A second co-op player without a second game, for tests on one PC (docs/coop.md, "Checks"). It speaks the stand-in for Steam (runtime/coop_local.c: datagrams between UDP ports on 127.0.0.1) and the room's messages (runtime/coop.c, runtime/coop_game.c) to a game started with NX2NAVI_COOP_LOCAL= and NX2NAVI_COOP_LOCAL_MAKE=1: python tests/coop_peer.py --host 27601 [--port 27602] [--seconds 120] [--flags HASH[,HASH...]] It joins the game's room, says HELLO, and once the game's Link is in the world stands two metres beside him (so the game shows its name there), says a chat line, asks for the game's shared progress and, with --flags, sends those flags (hex hashes of on/off flags) as set. It prints what the game sends and ends with "peer: ok" when it was let in, saw the game's Link in the world and got its progress; python's exit code says the same. Standard library only. """ import argparse import socket import struct import sys import time L_JOIN, L_MEMBERS, L_DATA, L_LEAVE = 1, 2, 3, 4 K_HELLO, K_WELCOME, K_REFUSED, K_ROSTER, K_POSE, K_CHAT = 1, 2, 3, 4, 5, 6 K_FLAGS, K_ASK = 16, 17 PROTOCOL, GAME_BUILD = 1, 0xE0F3CCFA def pose_bytes(number, pos, scene): s = scene.encode() return struct.pack('= 1.0: sock.sendto(bytes([L_JOIN]), host) last_join = now if in_lobby and not welcomed and now - last_hello >= 1.0: send(K_HELLO, struct.pack('= 0.05: number += 1 if host_pose and host_pose[0]: send(K_POSE, pose_bytes(number, (host_pose[1] + 2.0, host_pose[2], host_pose[3] + 2.0), host_pose[0])) else: send(K_POSE, pose_bytes(number, (0.0, 0.0, 0.0), '')) last_pose = now if welcomed and host_pose and host_pose[0] and not chatted: send(K_CHAT, 'hello from the test peer'.encode()) chatted = True print('peer: sent a chat line') if welcomed and schema is not None and now - last_ask >= 5.0: send(K_ASK, struct.pack('= 2: kind, b = d[1], d[2:] if kind == K_WELCOME: welcomed = True print('peer: the host let it in') elif kind == K_REFUSED: print(f'peer: REFUSED, reason {b[0] if b else "?"}') return 1 elif kind == K_ROSTER and b: if b[0] != roster: roster = b[0] print(f'peer: the host lists {roster} players: {[struct.unpack_from("= 34: n, x, y, z, vx, vy, vz, yaw, fl, sl = struct.unpack_from('= 4: if schema is None: schema = struct.unpack_from(' 0 print('peer: ok' if ok else 'peer: FAILED') return 0 if ok else 1 if __name__ == '__main__': sys.exit(main())