-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
156 lines (135 loc) · 4.79 KB
/
client.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
"""
This file is part of Azisai_Othello_GUI
Copyright (c) 2022 YuaHyodo
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
"""
"""
リポジトリ: https://github.com/YuaHyodo/Azisai_Othello_GUI
"""
from threading import Thread
import socket
import time
import ssl
k = '\n'
class Client:
def __init__(self, host, port=4081):
self.host = host
self.port=port
self.buf_size = 1024
self.keep_connect = False
def connect(self):
self.socket0 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
context = ssl.create_default_context()
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
self.socket = context.wrap_socket(self.socket0, server_hostname=self.host)
self.socket.connect((self.host, self.port))
self.keep_connact = True
keep_connect_thread = Thread(target=self.keep_connect)
keep_connect_thread.start()
return
def keep_connect(self):
while self.keep_connect:
self.send('')
time.sleep(10)
def send(self, message):
if k not in message:
message += k
self.socket.send(message.encode('utf-8'))
return
def recv(self):
message = self.socket.recv(self.buf_size).decode('utf-8')
return str(message)
def login(self, name, password):
self.connect()
self.send('LOGIN ' + name + ' ' + password)
while True:
message = self.recv()
if 'LOGIN' in message:
break
if 'incorrect' in message:
raise ValueError('LOGIN failed')
return
def logout(self):
self.send('LOGOUT')
self.keep_connect = False
self.keep_connect_thread.join()
self.socket.close()
return
def agree(self):
self.send('AGREE')
while True:
m = self.recv()
if 'START' in m:
break
if 'REJECT' in m:
break
return
def resign(self):
self.send('RESIGN')
while True:
m = self.recv()
if '#' in m:
break
return
def wait(self):
while True:
message = self.recv()
if 'Game_Summary' in message:
break
return self.Parse_summary(message)
def Parse_summary(self, summary):
lines = summary.splitlines()
output = {'position': 'startpos moves', 'time': {'total': 0, 'inc': 0}, 'color': 0}
for L in lines:
if 'Your_Turn:' in L:
if '+' in L:
output['color'] = 'black'
else:
output['color'] = 'white'
if 'Total_Time:' in L:
output['time']['total'] = int(L[11:])
if 'Increment:' in L:
output['time']['inc'] = int(L[10:])
return output
def get_move(self):
end = ['#ILLEGAL_MOVE', '#RESIGN', '#TIME_UP', '#DOUBLE_PASS', '#ABNORMAL', '#WIN', '#DRAW', '#LOSE']
t = 0
while True:
m = self.recv()
for e in end:
if e in m:
m = 'end'
break
if m == 'end':
break
if 'PASS' in m:
m = 'pass'
break
if ',T' in m:
t = int(m[5:])
m = m[1:3]
break
return m, t
def send_move(self, usix_move, c):
color = {'black': '+', 'white': '-'}[c]
if usix_move == 'pass':
self.send('PASS')
else:
self.send(color + usix_move)
m, t = self.get_move()
is_end = (m == 'end')
return is_end, t