Skip to content

Commit

Permalink
Factorio: Colored ingame text relay for AP texts
Browse files Browse the repository at this point in the history
  • Loading branch information
Berserker66 committed Jun 6, 2021
1 parent 09b6698 commit dd3c612
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions FactorioClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

import Utils
import random
from NetUtils import RawJSONtoTextParser, NetworkItem, ClientStatus
from NetUtils import RawJSONtoTextParser, NetworkItem, ClientStatus, JSONtoTextParser, JSONMessagePart

from worlds.factorio.Technologies import lookup_id_to_name

Expand Down Expand Up @@ -67,6 +67,7 @@ def __init__(self, *args, **kwargs):
self.rcon_client = None
self.awaiting_bridge = False
self.raw_json_text_parser = RawJSONtoTextParser(self)
self.factorio_json_text_parser = FactorioJSONtoTextParser(self)

async def server_auth(self, password_requested):
if password_requested and not self.password:
Expand All @@ -82,16 +83,18 @@ def on_print(self, args: dict):
logger.info(args["text"])
if self.rcon_client:
cleaned_text = args['text'].replace('"', '')
self.rcon_client.send_command(f"/sc game.print(\"Archipelago: {cleaned_text}\")")
self.rcon_client.send_command(f"/sc game.print(\"[font=default-large-bold]Archipelago:[/font] {cleaned_text}\")")

def on_print_json(self, args: dict):
if not self.found_items and args.get("type", None) == "ItemSend" and args["receiving"] == args["sending"]:
pass # don't want info on other player's local pickups.
text = self.raw_json_text_parser(args["data"])
text = self.raw_json_text_parser(copy.deepcopy(args["data"]))
logger.info(text)
if self.rcon_client:
text = self.factorio_json_text_parser(args["data"])
logger.info(text)
cleaned_text = text.replace('"', '')
self.rcon_client.send_command(f"/sc game.print(\"Archipelago: {cleaned_text}\")")
self.rcon_client.send_command(f"/sc game.print(\"[font=default-large-bold]Archipelago:[/font] {cleaned_text}\")")

async def game_watcher(ctx: FactorioContext, bridge_file: str):
bridge_logger = logging.getLogger("FactorioWatcher")
Expand Down Expand Up @@ -245,3 +248,17 @@ async def main():
loop.run_until_complete(main())
loop.close()
colorama.deinit()


class FactorioJSONtoTextParser(JSONtoTextParser):
def _handle_color(self, node: JSONMessagePart):
colors = node["color"].split(";")
for color in colors:
if color in {"red", "green", "blue", "orange", "yellow", "pink", "purple", "white", "black", "gray",
"brown", "cyan", "acid"}:
node["text"] = f"[color={node['color']}]{node['text']}[/color]"
return self._handle_text(node)
elif color == "magenta":
node["text"] = f"[color=pink]{node['text']}[/color]"
return self._handle_text(node)
return self._handle_text(node)

0 comments on commit dd3c612

Please sign in to comment.