giantsd/giants/player.py

59 lines
2.1 KiB
Python
Raw Normal View History

2019-01-22 01:30:42 +01:00
from .entity import Entity
2019-02-05 01:35:22 +01:00
import random
2019-02-19 01:01:31 +01:00
import struct
2019-03-13 01:37:49 +01:00
from . import ChatColor, ChatType, Models
2019-02-28 04:01:03 +01:00
from dpnet.DFrame import DFrame
2019-03-13 01:37:49 +01:00
from dpnet.gamepackets import MSG_CHANGE_TEAM, MSG_PLAYER_PING
2019-02-05 01:35:22 +01:00
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
2019-02-28 04:01:03 +01:00
INGAME = 6
2019-01-22 01:30:42 +01:00
class Player(Entity):
def __init__(self, name, session):
super().__init__()
self.name = name
self.session = session
2019-02-28 04:01:03 +01:00
self.team = 2
2019-01-22 01:30:42 +01:00
self.score = 0
2019-02-28 04:01:03 +01:00
self.id = random.getrandbits(16)
2019-02-05 01:35:22 +01:00
self.phase = PlayerPhases.NONE
2019-02-19 01:01:31 +01:00
self.ping = 1
2019-03-13 01:37:49 +01:00
self.opts = {"debug": True}
2019-02-19 01:01:31 +01:00
2019-03-13 01:37:49 +01:00
def get_index(self):
return self.session.server.get_player_index(self)
async def change_team(self, newteam, respawn=True):
2019-02-19 01:01:31 +01:00
self.team = newteam
await self.session.server.broadcast_event("on_player_change_team", self, self.team)
2019-03-13 01:37:49 +01:00
await self.session.server.broadcast_gamedata(MSG_CHANGE_TEAM(self, newteam, respawn).to_packet())
2019-02-19 01:01:31 +01:00
async def send_ping(self, ping):
2019-03-13 01:37:49 +01:00
await self.session.server.broadcast_gamedata(MSG_PLAYER_PING(self, ping).to_packet())
2019-02-19 01:01:31 +01:00
async def send_message(self, message, type=ChatType.All, playerindex=0, color=ChatColor.Orange):
options = type | color
playeri = 0x80 + playerindex
2019-02-28 04:01:03 +01:00
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)
2019-03-13 01:37:49 +01:00
def model(self):
# TODO: Fix
return Models.Characters_Mecc