giants-stupid-server/giants/player.py
2019-02-28 04:01:03 +01:00

50 lines
1.9 KiB
Python

from .entity import Entity
import random
import struct
from . import ChatColor, ChatType
from dpnet.DFrame import DFrame
class PlayerPhases:
NONE = 0
CFRAME_CONNECT = 1
CFRAME_CONNECTED = 2
DN_INTERNAL_MESSAGE_PLAYER_CONNECT_INFO_EX = 3
DN_SEND_CONNECT_INFO = 4
DN_ACK_CONNECT_INFO = 5
INGAME = 6
class Player(Entity):
def __init__(self, name, session):
super().__init__()
self.name = name
self.session = session
self.team = 2
self.score = 0
self.id = random.getrandbits(16)
self.phase = PlayerPhases.NONE
self.ping = 1
async def change_team(self, newteam):
self.team = newteam
await self.session.send_gamedata(b"\x10"+struct.pack("<B", self.team)+struct.pack("<L", self.id)+b"\x01\x00", acknow=False)
await self.session.send_gamedata(b"\x10"+struct.pack("<B", self.team)+struct.pack("<L", self.id)+b"\x01\x00", acknow=False)
async def send_ping(self, ping):
await self.session.send_gamedata(b"\x2f\x02\x00\x00" + struct.pack("<H", ping) + b"\x00", acknow=False)
async def send_message(self, message, type=ChatType.All, playerindex=0, color=ChatColor.Orange):
options = type | color
playeri = 0x80 + playerindex
await self.session.send_gamedata(b"\x35" + struct.pack("<B", playeri)+ struct.pack("<B", options) + message.encode("utf8") + b"\x00\x00", acknow=False)
async def kick(self):
dframe = DFrame()
dframe.Command = DFrame.PACKET_COMMAND_DATA | DFrame.PACKET_COMMAND_RELIABLE | DFrame.PACKET_COMMAND_SEQUENTIAL | DFrame.PACKET_COMMAND_POLL | DFrame.PACKET_COMMAND_NEW_MSG | DFrame.PACKET_COMMAND_END_MSG
dframe.Control = DFrame.PACKET_CONTROL_END_STREAM
dframe.Seq = self.session.next_send
dframe.NRcv = self.session.next_expected_seq
dframe.SessID = self.session.SessID
self.session.send(dframe)