Ejemplo n.º 1
0
class RcTogether:
    def __init__(self, callbacks=[]):
        self.connection = Connection(
            origin=f"https://{RC_APP_ENDPOINT}",
            url=f"wss://{RC_APP_ENDPOINT}/cable?app_id={RC_APP_ID}&app_secret={RC_APP_SECRET}",
        )
        self.connection.connect()

        while not self.connection.connected:
            # TODO - use callbacks to detect connection!
            print("Waiting for connection.")
            time.sleep(0.1)
        print("Connected.")


        self.subscription = Subscription(self.connection, identifier={"channel": "ApiChannel"})
        # Websocket library captures and hides tracebacks, so make sruer
        self.subscription.on_receive(with_tracebacks(self.handle_message))
        self.subscription.create()

        self.callbacks = callbacks
        self.bots = {}

    def block_until_done(self):
        self.connection.ws_thread.join()

    def create_bot(self, name, emoji, x, y, handle_update, can_be_mentioned=False):
        bot = Bot.create(name, emoji, x, y, handle_update, can_be_mentioned)
        self.bots[bot.id] = bot
        return bot

    def handle_message(self, message):
        if message["type"] == "world":
            for entity in message["payload"]["entities"]:
                self.handle_entity(entity)
        else:
            self.handle_entity(message["payload"])

    def handle_entity(self, entity):
        for callback in self.callbacks:
            callback(entity)

        if entity['id'] in self.bots:
            self.bots[entity['id']].handle_entity(entity)

    def add_callback(self, callback):
        self.callbacks.append(callback)
Ejemplo n.º 2
0
    def __init__(self, callbacks=[]):
        self.connection = Connection(
            origin=f"https://{RC_APP_ENDPOINT}",
            url=f"wss://{RC_APP_ENDPOINT}/cable?app_id={RC_APP_ID}&app_secret={RC_APP_SECRET}",
        )
        self.connection.connect()

        while not self.connection.connected:
            # TODO - use callbacks to detect connection!
            print("Waiting for connection.")
            time.sleep(0.1)
        print("Connected.")


        self.subscription = Subscription(self.connection, identifier={"channel": "ApiChannel"})
        # Websocket library captures and hides tracebacks, so make sruer
        self.subscription.on_receive(with_tracebacks(self.handle_message))
        self.subscription.create()

        self.callbacks = callbacks
        self.bots = {}
Ejemplo n.º 3
0
            dif = (now - stamp).total_seconds()
            # print(dif)
            if dif < 2:
                lodge.ask_lodge(mess['text'])


if __name__ == "__main__":
    import os
    ID = os.getenv('ID')
    SEC = os.getenv('SEC')
    # import keyring
    # ID = keyring.get_password('summon', 'VIRTUAL_APP_ID')
    # SEC = keyring.get_password('summon', 'VIRTUAL_APP_SEC')
    lodge = SkiLodge(app_id=ID, app_sec=SEC, name="RC Lodge")
    con = Connection(
        origin='https://recurse.rctogether.com',
        url=f'wss://recurse.rctogether.com/cable?app_id={ID}&app_secret={SEC}')
    con.connect()
    while not con.connected:
        time.sleep(0.5)
    sub = Subscription(con, identifier={"channel": "ApiChannel"})
    sub.on_receive(callback=sub_on_receive)
    sub.create()

    time.sleep(5)
    while con.connected:
        if lodge.status == 'closed':
            con.disconnect()
        else:
            time.sleep(5)
Ejemplo n.º 4
0
import json

config = None
with open("config.json", "r") as config_file:
    config = json.loads(config_file.read())

cookie=config["cookie"]
csrf=config["csrf"]

connection = Connection(
    url="wss://recurse.rctogether.com/cable",
    origin="https://recurse.rctogether.com",
    cookie=cookie)
connection.connect()

gwc_subscription = Subscription(connection, identifier={"channel": "GridWorldChannel"})

all_people = {}

world = None
players = []
turn = 0

def gwc_on_receive(message):
    global world
    global players
    global all_people
    if message["type"] == "world":
        world = message["payload"]
    elif message["type"] == "entity" and message["payload"]["type"] == "Avatar":
        avatar_id = message["payload"]["id"]
Ejemplo n.º 5
0
from actioncable.connection import Connection
from actioncable.subscription import Subscription
from actioncable.message import Message
from serial.tools import list_ports
import serial
import time
import json
import time.sleep

connection = Connection(url='ws://localhost:28080')
connection.connect()

subscription = Subscription(connection,
                            identifier={'channel': 'DeviceChannel'})
subscription.create()


def on_receive(message):
    print('New message arrived!')
    print('Action: {} | Data: {}'.format(message.action, message.data))


subscription.on_receive(callback=on_receive)

subscription.connection.send({
    'command':
    'message',
    'identifier':
    json.dumps({'channel': 'DeviceChannel'}),
    'data':
    json.dumps({