-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpychaserclient.py
113 lines (88 loc) · 2.35 KB
/
pychaserclient.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import os
import socket
from logging import getLogger
BUF_SIZE = 4096
LEFT = 0
RIGHT = 1
UP = 2
DOWN = 3
FLAG_CONNECTION_CLOSE = 0
class ConnectionCloseSignal(Exception):
pass
def strarr_to_intarr(val):
"""
文字列をintのリストにする
"""
return [int(s) for s in val]
class PyChaserConnect
def __init__(self, username, host, port):
self._client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self._host = host
self._port = port
self._username = username
self._client.connect((host, port))
def disconnect(self):
self._client.close()
def username(self):
return send(self._username)
def send(self, msg):
return self._client.send("%s\r\n" % msg)
def recv(self):
result = self._client.recv(BUF_SIZE).strip()
return result
def getReady(self):
_ = self.recv()
send("gr")
msg = recv()
def walk(self, direction):
if direction is LEFT:
send("wl")
elif direction is RIGHT:
send("wr")
elif direction is UP:
send("wu")
elif direction is DOWN:
send("wd")
result = recv()
info = strarr_to_intarr(result)
turnEnd()
return info
def search(self, direction):
if direction is LEFT:
send("sl")
elif direction is RIGHT:
send("sr")
elif direction is UP:
send("su")
elif direction is DOWN:
send("sd")
result = recv()
info = strarr_to_intarr(result)
turnEnd()
return info
def look(self, direction):
if direction is LEFT:
send("ll")
elif direction is RIGHT:
send("lr")
elif direction is UP:
send("lu")
elif direction is DOWN:
send("ld")
result = recv()
info = strarr_to_intarr(result)
turnEnd()
def put(self, direction):
if direction is LEFT:
send("pl")
elif direction is RIGHT:
send("pr")
elif direction is UP:
send("pu")
elif direction is DOWN:
send("pd")
result = recv()
info = strarr_to_intarr(result)
turnEnd()
def turnEnd(self):
send(r"#")