Skip to content

Commit

Permalink
Python client
Browse files Browse the repository at this point in the history
  • Loading branch information
anatoliliotych committed Dec 4, 2014
1 parent 3596126 commit 021e692
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 0 deletions.
12 changes: 12 additions & 0 deletions python/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
How to use client?
1. Install all requirements:
```
pip install -r requirements.txt
```

2. update necessary params in __init__.py

3. for launch:
```
python __init__.py
```
17 changes: 17 additions & 0 deletions python/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from player import Player
from codenjoy_connection import CodenjoyConnection


try:
name = 'anatoliliotych' # name which was used during registration
port = '8080' # game port
host = 'localhost' # game host
game_url = 'codenjoy-contest/ws?' # game url

url = "ws://{0}:{1}/{2}user={3}".format(host, port, game_url, name)
player = Player()
ws = CodenjoyConnection(url, player)
ws.connect()
ws.run_forever()
except KeyboardInterrupt:
ws.close()
12 changes: 12 additions & 0 deletions python/codenjoy_connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from ws4py.client.threadedclient import WebSocketClient

class CodenjoyConnection(WebSocketClient):
def __init__(self, url, player):
super(CodenjoyConnection, self).__init__(url)
self.player = player

def received_message(self, m):
print "Received from server: %s" % (str(m))
self.player.process_data(m)
self.send(self.player.make_step())

8 changes: 8 additions & 0 deletions python/player.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Player:
def process_data(self,data):
# process data here
pass
def make_step(self):
# prepare response here
# pass
return "right=1"
1 change: 1 addition & 0 deletions python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ws4py

0 comments on commit 021e692

Please sign in to comment.