Overview
The Player class represents a connected client in Sakuya AC. It holds information about the client including their username, aircraft, IP address, and connection state.
Constructor
Player(server_messages, client_messages, streamWriterObject)
Creates a new Player instance.
Message queue for server-bound messages
Message queue for client-bound messages
asyncio StreamWriter object for sending data to the client
Properties
The player’s username. Initially empty until login.
The player’s display alias/nickname. Initially empty until login.
The Aircraft instance representing the player’s current aircraft.
The client version number. Initially 0 until login.
The player’s IP address. Initially empty until set.
The asyncio StreamWriter object for network communication.
Flag indicating if the connection is a bot. Set to False after successful LOGIN packet.
Identification Friend or Foe value (team identifier).
Flag indicating if the connection has been closed.
Methods
set_aircraft
set_aircraft(aircraft: Aircraft) -> None
Sets the player’s aircraft.
The Aircraft instance to assign to this player
Example:
from lib.Aircraft import Aircraft
aircraft = Aircraft()
aircraft.name = "F-15C"
player.set_aircraft(aircraft)
login
login(packet: FSNETCMD_LOGON) -> None
Processes a login packet and updates player information.
The login packet containing username, alias, and version information
Updates:
- Sets
username from packet
- Sets
alias from packet
- Sets
version from packet
Example:
logon_packet = FSNETCMD_LOGON()
logon_packet.username = "pilot123"
logon_packet.alias = "TopGun"
logon_packet.version = 20190117
player.login(logon_packet)
print(player.username) # "pilot123"
print(player.alias) # "TopGun"
set_ip
Sets the player’s IP address.
The IP address string to assign to this player
Example:
player.set_ip("192.168.1.100")
print(player.ip) # "192.168.1.100"
check_add_object
check_add_object(packet: FSNETCMD_ADDOBJECT) -> bool
Checks if an ADDOBJECT packet belongs to this player and updates aircraft information if it matches.
packet
FSNETCMD_ADDOBJECT
required
The add object packet containing aircraft spawn information
True if the packet pilot matches this player’s alias and aircraft was updated, False otherwise
Behavior:
- Compares
packet.pilot with player’s alias
- If matched, creates new Aircraft instance and sets:
- Aircraft name from
packet.identifier
- Aircraft ID from
packet.object_id
- Aircraft position from
packet.pos
- IFF configuration from
packet.iff
Example:
add_object_packet = FSNETCMD_ADDOBJECT()
add_object_packet.pilot = "TopGun"
add_object_packet.identifier = "F-15C"
add_object_packet.object_id = 12345
add_object_packet.pos = [100.0, 200.0, 5000.0]
add_object_packet.iff = 1
if player.check_add_object(add_object_packet):
print(f"Aircraft spawned: {player.aircraft.name}")
print(f"Position: {player.aircraft.position}")
else:
print("Packet doesn't belong to this player")
String Representation
The Player class implements __str__() for easy debugging:
print(player)
# Output: "Player pilot123 flying F-15C at [100.0, 200.0, 5000.0]"
Usage Example
import asyncio
from lib.Player import Player
from lib.Aircraft import Aircraft
from lib.PacketManager.packets import FSNETCMD_LOGON, FSNETCMD_ADDOBJECT
# Create player instance
reader, writer = await asyncio.open_connection('127.0.0.1', 7915)
player = Player(server_messages, client_messages, writer)
# Set IP address
player.set_ip(writer.get_extra_info('peername')[0])
# Process login
logon = FSNETCMD_LOGON()
logon.username = "ace_pilot"
logon.alias = "Maverick"
logon.version = 20190117
player.login(logon)
# Check if player is still a bot
if not player.is_a_bot:
print(f"{player.username} successfully logged in")
# Handle aircraft spawn
add_obj = FSNETCMD_ADDOBJECT()
add_obj.pilot = "Maverick"
add_obj.identifier = "F-14A"
add_obj.object_id = 1001
add_obj.pos = [0, 0, 1000]
add_obj.iff = 1
if player.check_add_object(add_obj):
print(f"Player aircraft: {player.aircraft.name}")
print(f"Aircraft ID: {player.aircraft.id}")