Skip to content

Commit

Permalink
Version 3 Update 🚩
Browse files Browse the repository at this point in the history
  • Loading branch information
olijeffers0n committed Sep 23, 2021
1 parent 4d95d25 commit 5255926
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 168 deletions.
2 changes: 1 addition & 1 deletion rustplus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@

__name__ = "rustplus"
__author__ = "olijefferson"
__version__ = "2.3.1"
__version__ = "3.0"
__support__ = "Discord: https://discord.gg/nQqJe8qvP8"
243 changes: 86 additions & 157 deletions rustplus/api/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class RustSocket:
def __init__(self, ip : str, port : str, steamid : int, playertoken : int) -> None:

self.seq = 1
self.error_checker = ErrorChecker()
self.ip = ip
self.port = port
self.steamid = steamid
self.playertoken = playertoken
self.error_checker = ErrorChecker()

def __repr__(self) -> str:
return "RustSocket: ip = {} | port = {} | steamid = {} | playertoken = {}".format(self.ip, self.port, self.steamid, self.playertoken)
Expand All @@ -31,11 +31,9 @@ def __initProto(self) -> AppRequest:
request.playerToken = self.playertoken
return request

def __getTime(self) -> dict:
request = self.__initProto()
request.getTime.CopyFrom(AppEmpty())
data = request.SerializeToString()
def __sendAndRecieve(self, request) -> AppMessage:

data = request.SerializeToString()
self.ws.send_binary(data)

return_data = self.ws.recv()
Expand All @@ -45,6 +43,15 @@ def __getTime(self) -> dict:

self.error_checker.check(app_message)

return app_message

def __getTime(self) -> dict:

request = self.__initProto()
request.getTime.CopyFrom(AppEmpty())

app_message = self.__sendAndRecieve(request)

time_parser = TimeParser()

return {
Expand All @@ -58,33 +65,17 @@ def __getInfo(self):

request = self.__initProto()
request.getInfo.CopyFrom(AppEmpty())
data = request.SerializeToString()

self.ws.send_binary(data)

returndata = self.ws.recv()

appMessage = AppMessage()
appMessage.ParseFromString(returndata)

self.error_checker.check(appMessage)

app_message = self.__sendAndRecieve(request)

return appMessage
return app_message

def __getMap(self, MAPSIZE):

request = self.__initProto()
request.getMap.CopyFrom(AppEmpty())
data = request.SerializeToString()

self.ws.send_binary(data)

return_data = self.ws.recv()

app_message = AppMessage()
app_message.ParseFromString(return_data)

self.error_checker.check(app_message)

app_message = self.__sendAndRecieve(request)

map = app_message.response.map
monuments = list(map.monuments)
Expand All @@ -100,56 +91,80 @@ def __getMap(self, MAPSIZE):

return (im, monuments)

def __getRawMapData(self):
def __getAndFormatMap(self, addIcons : bool, addEvents : bool, addVendingMachines : bool, overrideImages : dict = {}):

request = self.__initProto()
request.getMap.CopyFrom(AppEmpty())
data = request.SerializeToString()
MAPSIZE = int(self.__getInfo().response.info.mapSize)

self.ws.send_binary(data)
map, monuments = self.__getMap(MAPSIZE)

return_data = self.ws.recv()
if addIcons or addEvents or addVendingMachines:
cood_formatter = CoordUtil()

app_message = AppMessage()
app_message.ParseFromString(return_data)
if addIcons:
monument_name_converter = MonumentNameToImage(overrideImages)
for monument in monuments:
if str(monument.token) == "DungeonBase":
continue
icon = monument_name_converter.convert(monument.token)
icon = icon.resize((150, 150))
if str(monument.token) == "train_tunnel_display_name":
icon = icon.resize((100, 125))
map.paste(icon, (cood_formatter.format(int(monument.x), int(monument.y), MAPSIZE)), icon)

self.error_checker.check(app_message)
mapMarkers = list(self.__getMarkers().response.mapMarkers.markers)

return app_message.response.map
if addVendingMachines:
with resources.path("rustplus.api.icons", "vending_machine.png") as path:
vendingMachine = Image.open(path).convert("RGBA")
vendingMachine = vendingMachine.resize((100, 100))

def __getMarkers(self):
for marker in mapMarkers:
if addEvents:
markerConverter = MapMarkerConverter()
if marker.type == 2 or marker.type == 4 or marker.type == 5 or marker.type == 6:
icon = markerConverter.convert(str(marker.type), marker.rotation)
if marker.type == 6:
x = marker.x
y = marker.y
if y > MAPSIZE: y = MAPSIZE
if y < 0: y = 100
if x > MAPSIZE: x = MAPSIZE - 75
if x < 0: x = 50
map.paste(icon, (int(x), MAPSIZE - int(y)), icon)
else:
map.paste(icon, (cood_formatter.format(int(marker.x), int(marker.y), MAPSIZE)), icon)
if addVendingMachines:
if marker.type == 3:
map.paste(vendingMachine, (int(marker.x) - 50, MAPSIZE - int(marker.y) - 50), vendingMachine)

return map.resize((2000, 2000), Image.ANTIALIAS)

def __getRawMapData(self):

request = self.__initProto()
request.getMapMarkers.CopyFrom(AppEmpty())
data = request.SerializeToString()
request.getMap.CopyFrom(AppEmpty())

app_message = self.__sendAndRecieve(request)

self.ws.send_binary(data)
return app_message.response.map

returndata = self.ws.recv()
def __getMarkers(self):

appMessage = AppMessage()
appMessage.ParseFromString(returndata)
request = self.__initProto()
request.getMapMarkers.CopyFrom(AppEmpty())

self.error_checker.check(appMessage)
app_message = self.__sendAndRecieve(request)

return appMessage
return app_message

def __getTeamChat(self):

request = self.__initProto()
request.getTeamChat.CopyFrom(AppEmpty())
data = request.SerializeToString()

self.ws.send_binary(data)

returndata = self.ws.recv()

appMessage = AppMessage()
appMessage.ParseFromString(returndata)

self.error_checker.check(appMessage)

app_message = self.__sendAndRecieve(request)

return appMessage
return app_message

def __sendTeamChatMessage(self, message):

Expand Down Expand Up @@ -185,54 +200,30 @@ def __getCameraFrame(self, id, frame):

request = self.__initProto()
request.getCameraFrame.CopyFrom(cameraPacket)
data = request.SerializeToString()

self.ws.send_binary(data)

returndata = self.ws.recv()
app_message = self.__sendAndRecieve(request)

appMessage = AppMessage()
appMessage.ParseFromString(returndata)

self.error_checker.check(appMessage)

return appMessage
return app_message

def __getTeamInfo(self):

request = self.__initProto()
request.getTeamInfo.CopyFrom(AppEmpty())
data = request.SerializeToString()

self.ws.send_binary(data)

returndata = self.ws.recv()

appMessage = AppMessage()
appMessage.ParseFromString(returndata)

self.error_checker.check(appMessage)
app_message = self.__sendAndRecieve(request)

return appMessage
return app_message

def __getEntityInfo(self, eid : int):

request = self.__initProto()

request.entityId = eid
request.getEntityInfo.CopyFrom(AppEmpty())
data = request.SerializeToString()

self.ws.send_binary(data)

returndata = self.ws.recv()

appMessage = AppMessage()
appMessage.ParseFromString(returndata)

self.error_checker.check(appMessage)

app_message = self.__sendAndRecieve(request)

return appMessage
return app_message

def __updateSmartDevice(self, eid : int, value : bool) -> AppMessage:

Expand All @@ -244,18 +235,9 @@ def __updateSmartDevice(self, eid : int, value : bool) -> AppMessage:
request.entityId = eid
request.setEntityValue.CopyFrom(entityValue)

data = request.SerializeToString()

self.ws.send_binary(data)

returndata = self.ws.recv()

appMessage = AppMessage()
appMessage.ParseFromString(returndata)
app_message = self.__sendAndRecieve(request)

self.error_checker.check(appMessage)

return appMessage
return app_message

def __promoteToTeamLeader(self, SteamID : int):

Expand All @@ -264,20 +246,13 @@ def __promoteToTeamLeader(self, SteamID : int):

request = self.__initProto()
request.promoteToLeader.CopyFrom(leaderPacket)
data = request.SerializeToString()

self.ws.send_binary(data)

returndata = self.ws.recv()

appMessage = AppMessage()
appMessage.ParseFromString(returndata)

self.error_checker.check(appMessage)

app_message = self.__sendAndRecieve(request)

return appMessage
return app_message

def __getTCStorage(self, EID, combineStacks):

returnedData = self.__getEntityInfo(EID)

returnDict = {}
Expand Down Expand Up @@ -381,57 +356,11 @@ def getRawMapData(self) -> AppMap:
"""
return self.__getRawMapData()

def getMap(self, addIcons : bool = False, addEvents : bool = False, addVendingMachines : bool = False) -> Image:
def getMap(self, addIcons : bool = False, addEvents : bool = False, addVendingMachines : bool = False, overrideImages : dict = {}) -> Image:
"""
Returns the Map of the server with the option to add icons.
"""
MAPSIZE = int(self.__getInfo().response.info.mapSize)

map, monuments = self.__getMap(MAPSIZE)

if addIcons or addEvents or addVendingMachines:
cood_formatter = CoordUtil()

if addIcons:
monument_name_converter = MonumentNameToImage()
for monument in monuments:
if str(monument.token) == "DungeonBase":
continue
icon = monument_name_converter.convert(monument.token)
icon = icon.resize((150, 150))
if str(monument.token) == "train_tunnel_display_name":
icon = icon.resize((100, 125))
map.paste(icon, (cood_formatter.format(int(monument.x), int(monument.y), MAPSIZE)), icon)

mapMarkers = list(self.__getMarkers().response.mapMarkers.markers)

if addVendingMachines:
with resources.path("rustplus.api.icons", "vending_machine.png") as path:
vendingMachine = Image.open(path).convert("RGBA")
vendingMachine = vendingMachine.resize((100, 100))

for marker in mapMarkers:
if addEvents:
markerConverter = MapMarkerConverter()
if marker.type == 2 or marker.type == 4 or marker.type == 5 or marker.type == 6:
icon = markerConverter.convert(str(marker.type), marker.rotation)
if marker.type == 6:
x = marker.x
y = marker.y
if y > MAPSIZE: y = MAPSIZE
if y < 0: y = 100
if x > MAPSIZE: x = MAPSIZE - 75
if x < 0: x = 50
map.paste(icon, (int(x), MAPSIZE - int(y)), icon)
else:
map.paste(icon, (cood_formatter.format(int(marker.x), int(marker.y), MAPSIZE)), icon)
if addVendingMachines:
if marker.type == 3:
map.paste(vendingMachine, (int(marker.x) - 50, MAPSIZE - int(marker.y) - 50), vendingMachine)

map = map.resize((2000, 2000), Image.ANTIALIAS)

return map
return self.__getAndFormatMap(addIcons, addEvents, addVendingMachines, overrideImages)

def getMarkers(self) -> AppMapMarkers:
"""
Expand Down
2 changes: 1 addition & 1 deletion rustplus/exceptions/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class ClientError(Error):
pass

class ImageError(Error):
"""Raised when the Image Returned is not valid"""
"""Raised when the Returned Image Bytes are not valid"""
pass

class ServerNotResponsiveError(Error):
Expand Down
1 change: 0 additions & 1 deletion rustplus/objects/chat_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ def __init__(self, steamID, senderName, message, colour):
self.colour = colour

def __repr__(self):

return "Chat Message (steamID = {} | senderName = {} | message = {} | colour = {})".format(self.steamID, self.senderName, self.message, self.colour)
Loading

0 comments on commit 5255926

Please sign in to comment.