Skip to content

Commit

Permalink
Use the API provided websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
tedivm committed Jun 6, 2016
1 parent b130d6a commit 5ca6990
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 50 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ nose==1.3.7
PyYAML==3.11
requests==2.10.0
Requires==0.0.3
-e git+https://github.com/tedivm/python-screeps.git@c300073133dcd08d2d5f100a1a006bda60d9ce05#egg=screeps
-e git+https://github.com/tedivm/python-screeps.git@952a5af1d6e665eb36f32700bdfdda5fe19befbd#egg=screepsapi
six==1.10.0
urwid==1.3.1
websocket-client==0.37.0
4 changes: 2 additions & 2 deletions screeps_console/command.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

from autocomplete import Autocomplete
import calendar
from screeps import ScreepsConnection
import screepsapi
from settings import getSettings
import re
from themes import themes
Expand Down Expand Up @@ -37,7 +37,7 @@ def setDisplayWidgets(self, loop, frame, listbox, listwalker, edit, consolemonit
def getApiClient(self):
if not self.apiclient:
settings = getSettings()
self.apiclient = ScreepsConnection(
self.apiclient = screepsapi.API(
u=settings['screeps_username'],
p=settings['screeps_password'],
ptr=settings['screeps_ptr'])
Expand Down
58 changes: 11 additions & 47 deletions screeps_console/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,28 @@
import logging
from outputparser import parseLine
from outputparser import tagLine
from screeps import ScreepsConnection
import screepsapi
from settings import getSettings
from time import sleep
import websocket
from StringIO import StringIO
import sys


class ScreepsConsole(object):
class ScreepsConsole(screepsapi.Socket):
format = 'color'

def __init__(self, user, password, ptr=False, logging=False):
self.format = 'color'
self.user = user
self.password = password
self.ptr = ptr
self.logging = False
def set_subscriptions(self):
self.subscribe_user('console')
pass

def on_message(self, ws, message):
print message

def on_error(self, ws, error):
print error

def on_close(self, ws):
print "### closed ###"

def on_open(self, ws):
screepsConnection = ScreepsConnection(u=self.user,p=self.password,ptr=self.ptr)
me = screepsConnection.me()
self.user_id = me['_id']
ws.send('auth ' + screepsConnection.token)
self.disconnect()


def on_message(self, ws, message):

if (message.startswith('auth ok')):
ws.send('subscribe user:' + self.user_id + '/console')
return
Expand All @@ -49,7 +37,9 @@ def on_message(self, ws, message):
return

if (message.startswith('gz')):
message = GzipFile(fileobj=StringIO(b64decode(message[3:])))
gzipFile = GzipFile(fileobj=StringIO(b64decode(message[3:])))
message = gzipFile.read()


data = json.loads(message)

Expand Down Expand Up @@ -107,32 +97,6 @@ def on_message(self, ws, message):

print('on_message', message)


def connect(self):

if self.logging:
logging.getLogger('websocket').addHandler(logging.StreamHandler())
websocket.enableTrace(True)
else:
logging.getLogger('websocket').addHandler(logging.NullHandler())
websocket.enableTrace(False)

if not self.ptr:
url = 'wss://screeps.com/socket/websocket'
else:
url = 'wss://screeps.com/ptr/socket/websocket'

ws = websocket.WebSocketApp(url=url,
on_message=self.on_message,
on_error=self.on_error,
on_close=self.on_close,
on_open=self.on_open)
if 'http_proxy' in settings and settings['http_proxy'] is not None:
http_proxy_port = settings['http_proxy_port'] if 'http_proxy_port' in settings else 8080
ws.run_forever(http_proxy_host=settings['http_proxy'], http_proxy_port=http_proxy_port, ping_interval=1)
else:
ws.run_forever(ping_interval=1)

def start(self):
self.connect()

Expand Down

0 comments on commit 5ca6990

Please sign in to comment.