-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_v1.py
178 lines (164 loc) · 6.56 KB
/
main_v1.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""
This file is part of Ari-Othello-Server
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.
"""
import game_HTML_v2 as game_HTML
from output_v1 import HTML_update
from play_game_v1 import game
from security_v1 import Security
from datetime import datetime
from Player_class import Player
from threading import Thread
from logger_v1 import logger
from setting import*
import random
import socket
import time
import ssl
class Server_v1:
def __init__(self):
#初期化
self.log = logger()
self.output = HTML_update()
self.security = Security()
self.log.clear_log()
self.waiting_players = []
self.games = []
self.log.write('init_server')
self.lock_waiting_players = False
def connect_and_login_client(self):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
if no_ssl:
s2 = s
else:
context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
context.load_cert_chain(certfile=cerfile, keyfile=keyfile)
s2 = context.wrap_socket(s, server_side=True)
while True:
try:
client = s2.accept()
break
except:
pass
login_thread = Thread(target=self.login_client, args=(client,))
login_thread.start()
return client
def login_client(self, client):
#ログイン待機
client = client[0]
self.log.write('login waiting')
while True:
m = client.recv(buf_size).decode('utf-8')
if 'LOGIN' in m:
break
#usernameを抽出
message = ''.join(m.splitlines()).split()
username = message[1]
password = message[2]
if self.security.login_check(username, password):
self.log.write('user ' + username + ' login')
client.send(str('LOGIN:' + username + ' OK' + k).encode('utf-8'))
#待機プレーヤのlistに追加
while True:
if not self.lock_waiting_players:
player = Player(client, username, password)
self.waiting_players.append(player)
break
else:
self.log.write('login failed username: ' + username)
client.send(('LOGIN:incorrect' + k).encode('utf-8'))
return
def match_make(self):
try:
self.log.write('start match make')
self.log.write('waiting clients:' + str(len(self.waiting_players)))
names = [player.name for player in self.waiting_players]
self.log.write('waiting players list: ' + str(names))
if len(self.waiting_players) < 2:
#プレーヤー数が不十分
self.log.write('cancel match make')
return
self.lock_waiting_players = True
#ランダムにマッチング
random.shuffle(self.waiting_players)
a = list(range(0, len(self.waiting_players), 2))
if len(self.waiting_players) % 2 == 1:
a.pop(-1)
for i in a:
player1 = self.waiting_players[i]
player2 = self.waiting_players[i + 1]
self.games.append(game(player1, player2))
#待機listから外す
del self.waiting_players[0:a[-1] + 2]
self.lock_waiting_players = False
self.log.write('make ' + str(len(self.games)) + ' matchs')
#対局を開始する
threads_list = []
for g in self.games:
thread = Thread(target=g.start)
threads_list.append(thread)
game_HTML.update_recent_games_list(self.games)
for t in range(len(threads_list)):
threads_list[t].start()
self.log.write('start game ' + self.games[t].ID)
#すべての対局が終わるまで待機
for t in range(len(threads_list)):
threads_list[t].join()
self.log.write('finish game ' + self.games[t].ID)
self.games.clear()
self.output.update()
except:
self.log.write('match make error')
self.waiting_players.clear()
return
def login_client_loop(self):
while True:
self.connect_and_login_client()
self.log.write('waiting clients:' + str(len(self.waiting_players)))
return
def html_update_loop(self):
while True:
game_HTML.main()
time.sleep(10)
return
def main(self):
login_client_thread = Thread(target=self.login_client_loop)
login_client_thread.start()
html_thread = Thread(target=self.html_update_loop)
html_thread.start()
while True:#無限に稼働
if datetime.now().minute % 5 == 0:
match_thread = Thread(target=self.match_make)
match_thread.start()
time.sleep(60)
return
def test1(self):
#テスト
self.log.write('start test1')
while len(self.waiting_players) < 2:
self.connect_and_login_client()
self.log.write('waiting clients:' + str(len(self.waiting_players)))
self.match_make()
self.log.write('finish test1')
return
if __name__ == '__main__':
server = Server_v1()
#server.test1()
server.main()