Advertisement
Guest User

Untitled

a guest
May 9th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 44.86 KB | None | 0 0
  1. import socket
  2. import time
  3. from threading import Thread, Lock
  4. from copy import deepcopy
  5. import queue
  6. from flask import *
  7. import json
  8. import random
  9. from User import User
  10. from Room import Room
  11.  
  12. ip = "127.0.0.1"
  13. port = 8132
  14. cnt_listeners = 100
  15. cards_global = [i for i in range(11, 19)] + [i for i in range(21, 29)] \
  16. + [i for i in range(31, 39)] + [i for i in range(41, 49)]
  17.  
  18.  
  19. class Gaming(Thread):
  20. def __init__(self, room):
  21. Thread.__init__(self)
  22. self.room = room
  23. self.position = 1
  24. self.cards = deepcopy(cards_global)
  25. self.extra_cards = []
  26. self.choices = {}
  27. self.seq = deepcopy(self.room.users)
  28. self.min_available = 1
  29. self.cur_user = 0
  30. self.begin_user = 0
  31. self.step_number = 0
  32. self.local_step = 0
  33. self.steps = []
  34. self.game_type = ""
  35. self.suit = 0
  36. self.game_type_for_players = ""
  37. self.min_takes = 0
  38. self.trump_suit = -1
  39. self.whists_pass = {}
  40. self.is_open = False
  41. self.player_cards = []
  42. self.was_in_whist = False
  43.  
  44. def fresh(self):
  45. self.was_in_whist = False
  46. self.player_cards = []
  47. self.is_open = False
  48. self.whists_pass = {}
  49. self.game_type_for_players = ""
  50. self.min_takes = 0
  51. self.trump_suit = -1
  52. self.suit = 0
  53. self.game_type = ""
  54. self.steps = []
  55. self.local_step = 0
  56. self.step_number = 0
  57. self.begin_user = 0 if self.begin_user == 2 else self.begin_user + 1
  58. self.cur_user = self.begin_user
  59. self.min_available = 1
  60. self.seq = deepcopy(self.room.users)
  61. self.choices = {}
  62. self.extra_cards = []
  63. self.cards = deepcopy(cards_global)
  64. if len(self.room.readies) == 3:
  65. self.position = 1
  66. else:
  67. self.position = -1
  68. data = {
  69. "type": "fresh"
  70. }
  71. for user in self.room.users:
  72. messages.put((json.dumps(data).encode('utf-8'), login_connections[user.login]))
  73.  
  74. def get_cards(self, count):
  75. cards = []
  76. for i in range(count):
  77. print("self.cards", self.cards)
  78. x = random.randint(0, len(self.cards) - 1)
  79. print("x", x)
  80. item = self.cards[x]
  81. del self.cards[x]
  82. cards.append(item)
  83. print("cards", cards)
  84. cards.sort()
  85. return cards
  86.  
  87. def get_next_user(self):
  88. if self.cur_user == 2:
  89. return 0
  90. else:
  91. return self.cur_user + 1
  92.  
  93. def cnt_pas(self):
  94. cnt_pas = 0
  95. for choice in self.choices.keys():
  96. if self.choices[choice] == 'pas':
  97. cnt_pas += 1
  98. return cnt_pas
  99.  
  100. def finish_choice(self):
  101. cnt_pas = self.cnt_pas()
  102. if cnt_pas >= 2 and not (None in self.choices.values()):
  103. return True
  104. else:
  105. return False
  106.  
  107. def need_choose(self, user):
  108. if self.choices[self.room.users[user].login] == 'pas':
  109. return False
  110. if self.choices[self.room.users[user].login] == 'miser':
  111. return False
  112. cnt_pas = self.cnt_pas()
  113. if cnt_pas == 2 and not (None in self.choices.values()):
  114. return False
  115. return True
  116.  
  117. def has_trump(self):
  118. print(self.steps, self.trump_suit)
  119. for i in self.steps:
  120. if int(i[0]) // 10 == int(self.trump_suit):
  121. return True
  122. return False
  123.  
  124. def get_max_value_login(self):
  125. print(self.steps, self.suit)
  126. self.steps.sort()
  127. if int(self.steps[2][0]) // 10 == int(self.trump_suit):
  128. return self.steps[2][1]
  129. if int(self.steps[1][0]) // 10 == int(self.trump_suit):
  130. return self.steps[1][1]
  131. if int(self.steps[0][0]) // 10 == int(self.trump_suit):
  132. return self.steps[0][1]
  133.  
  134. def get_without_trump(self):
  135. begin = int(self.steps[0][0])
  136. self.steps.sort()
  137. if int(self.steps[2][0]) // 10 == begin // 10:
  138. return self.steps[2][1]
  139. elif int(self.steps[1][0]) // 10 == begin // 10:
  140. return self.steps[1][1]
  141. else:
  142. return self.steps[0][1]
  143.  
  144. def get_login_take(self, pas=False):
  145. print(pas, self.suit == 5, self.has_trump())
  146. if pas or self.suit == 5 or not self.has_trump():
  147. return self.get_without_trump()
  148. return self.get_max_value_login()
  149.  
  150. def player(self):
  151. for i in self.choices.keys():
  152. if self.choices[i] != 'pas' and self.choices[i] != 'miser':
  153. return self.room.get_number_by_login(i)
  154.  
  155. def run(self):
  156. while True:
  157. if self.position == 1:
  158. players_cards = [self.get_cards(10) for _ in range(3)]
  159. self.player_cards = deepcopy(players_cards)
  160. extra_cards = self.get_cards(2)
  161. self.extra_cards = extra_cards
  162. for i in range(3):
  163. login = self.room.users[i].login
  164. data = {
  165. "type": "cards",
  166. "cards": players_cards[i]
  167. }
  168. messages.put((json.dumps(data).encode('utf-8'), login_connections[login]))
  169. self.position = 2
  170. elif self.position == 2:
  171. time.sleep(5)
  172. data = {
  173. "type": "choice",
  174. "min_available": self.min_available
  175. }
  176. login_send = self.room.users[self.cur_user].login
  177. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_send]))
  178. self.position = -1
  179. elif self.position == 3:
  180. data = {
  181. "type": "extra",
  182. "number": self.step_number,
  183. "extra": self.extra_cards[self.step_number]
  184. }
  185. for i in self.room.users:
  186. messages.put((json.dumps(data).encode('utf-8'), login_connections[i.login]))
  187. login_send = self.room.users[self.cur_user].login
  188. data = {
  189. "type": "do_step",
  190. "is_me": True,
  191. "who": self.room.get_name_by_login(login_send),
  192. "suit": 0
  193. }
  194. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_send]))
  195. prev_login = self.room.prev_login(login_send)
  196. data = {
  197. "type": "do_step",
  198. "is_me": False,
  199. "who": self.room.get_name_by_login(login_send),
  200. "suit": 0
  201. }
  202. messages.put((json.dumps(data).encode('utf-8'), login_connections[prev_login]))
  203. next_login = self.room.next_login(login_send)
  204. data = {
  205. "type": "do_step",
  206. "is_me": False,
  207. "who": self.room.get_name_by_login(login_send),
  208. "suit": 0
  209. }
  210. messages.put((json.dumps(data).encode('utf-8'), login_connections[next_login]))
  211. self.position = -1
  212. elif self.position == 4:
  213. data = {
  214. "type": "open_all_extra",
  215. "extra1": self.extra_cards[0],
  216. "extra2": self.extra_cards[1]
  217. }
  218. for user in self.room.users:
  219. messages.put((json.dumps(data).encode('utf-8'), login_connections[user.login]))
  220. self.position = 5
  221. elif self.position == 5:
  222. time.sleep(7)
  223. data = {
  224. "type": "close_extra",
  225. 'game_type': self.game_type
  226. }
  227. for i in self.choices.keys():
  228. if self.choices[i] == 'pas':
  229. data["is_me"] = False
  230. else:
  231. data["is_me"] = True
  232. messages.put((json.dumps(data).encode('utf-8'), login_connections[i]))
  233. self.position = -1
  234. if self.game_type == 'miser':
  235. self.position = 9
  236. elif self.position == 6:
  237. login_send = self.room.users[self.cur_user].login
  238. if not self.is_open:
  239. data = {
  240. "type": "do_step",
  241. "is_me": True,
  242. "who": self.room.get_name_by_login(login_send),
  243. "suit": 0
  244. }
  245. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_send]))
  246. prev_login = self.room.prev_login(login_send)
  247. data = {
  248. "type": "do_step",
  249. "is_me": False,
  250. "who": self.room.get_name_by_login(login_send),
  251. "suit": 0
  252. }
  253. messages.put((json.dumps(data).encode('utf-8'), login_connections[prev_login]))
  254. next_login = self.room.next_login(login_send)
  255. data = {
  256. "type": "do_step",
  257. "is_me": False,
  258. "who": self.room.get_name_by_login(login_send),
  259. "suit": 0
  260. }
  261. messages.put((json.dumps(data).encode('utf-8'), login_connections[next_login]))
  262. else:
  263. data = {
  264. "type": "do_step",
  265. "is_me": True,
  266. "who": self.room.get_name_by_login(login_send),
  267. "suit": 0,
  268. "replace": False
  269. }
  270. if login_send in self.whists_pass:
  271. if self.whists_pass[login_send] == "pas" and self.is_open:
  272. data["is_me"] = False
  273. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_send]))
  274. prev_login = self.room.prev_login(login_send)
  275. data = {
  276. "type": "do_step",
  277. "is_me": False,
  278. "who": self.room.get_name_by_login(login_send),
  279. "suit": 0,
  280. "replace": False
  281. }
  282. if prev_login in self.whists_pass:
  283. if self.whists_pass[prev_login] == "whist" and self.is_open:
  284. data["replace"] = True
  285. messages.put((json.dumps(data).encode('utf-8'), login_connections[prev_login]))
  286. next_login = self.room.next_login(login_send)
  287. data = {
  288. "type": "do_step",
  289. "is_me": False,
  290. "who": self.room.get_name_by_login(login_send),
  291. "suit": 0,
  292. "replace": False
  293. }
  294. if next_login in self.whists_pass:
  295. if self.whists_pass[next_login] == "whist" and self.is_open:
  296. data["replace"] = True
  297. messages.put((json.dumps(data).encode('utf-8'), login_connections[next_login]))
  298. self.position = -1
  299. elif self.position == 7:
  300. login = None
  301. for i in self.choices.keys():
  302. if self.choices[i] != 'pas':
  303. login = i
  304. login = self.room.next_login(login)
  305. data = {
  306. "type": "whist_pas"
  307. }
  308. messages.put((json.dumps(data).encode('utf-8'), login_connections[login]))
  309. self.position = -1
  310. elif self.position == 8:
  311. login = self.room.users[self.cur_user].login
  312. if login in self.whists_pass:
  313. data = {
  314. "type": "close_open"
  315. }
  316. for i in self.whists_pass.keys():
  317. if self.whists_pass[i] == 'whist':
  318. login = i
  319. messages.put((json.dumps(data).encode('utf-8'), login_connections[login]))
  320. self.position = -1
  321. else:
  322. self.position = 6
  323. elif self.position == 9:
  324. login = None
  325. for i in self.choices.keys():
  326. if self.choices[i] == 'miser':
  327. login = i
  328. miser_cards = self.player_cards[self.room.get_number_by_login(login)] + self.extra_cards
  329. miser_cards.sort()
  330. data = {
  331. "type": "miser_cards",
  332. "cards": miser_cards,
  333. "who": "next"
  334. }
  335. prev_login = self.room.prev_login(login)
  336. messages.put((json.dumps(data).encode('utf-8'), login_connections[prev_login]))
  337. data["who"] = "prev"
  338. next_login = self.room.next_login(login)
  339. messages.put((json.dumps(data).encode('utf-8'), login_connections[next_login]))
  340. self.position = -1
  341. elif self.position == 10:
  342. for player in self.room.users:
  343. if self.choices[player.login] == 'miser':
  344. continue
  345. pos = self.room.get_number_by_login(player.login)
  346. cards = self.player_cards[pos]
  347. login_prev = self.room.prev_login(player.login)
  348. login_next = self.room.next_login(player.login)
  349. data = {
  350. "type": "open_cards",
  351. "who": "next",
  352. "cards": cards
  353. }
  354. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_prev]))
  355. data = {
  356. "type": "open_cards",
  357. "who": "prev",
  358. "cards": cards
  359. }
  360. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_next]))
  361. self.position = 6
  362.  
  363.  
  364. class SendingMessage(Thread):
  365. def __init__(self):
  366. Thread.__init__(self)
  367.  
  368. def run(self):
  369. while True:
  370. time.sleep(0.1)
  371. print("sm")
  372. print(messages)
  373. message, address = messages.get()
  374. print(message, address)
  375. try:
  376. address.send(message)
  377. except socket.error:
  378. break
  379.  
  380.  
  381. class GettingMessage(Thread):
  382. def __init__(self, connection):
  383. Thread.__init__(self)
  384. self.connection = connection
  385. self.login = ''
  386.  
  387. def run(self):
  388. while True:
  389. try:
  390. data = self.connection.recv(1024)
  391. print(data)
  392. data = data.decode('utf-8')
  393. data = json.loads(data)
  394. print(data)
  395. if data['type'] == 'connection':
  396. login_connections[data['login']] = self.connection
  397. print(login_connections)
  398. self.login = data['login']
  399. except ConnectionResetError:
  400. connections.remove(self.connection)
  401. login_connections.pop(self.login)
  402. break
  403. except json.decoder.JSONDecodeError:
  404. connections.remove(self.connection)
  405. login_connections.pop(self.login)
  406. break
  407. print(data)
  408. if not data:
  409. print('disconnected')
  410. break
  411.  
  412.  
  413. class FindingConnection(Thread):
  414. def __init__(self):
  415. Thread.__init__(self)
  416.  
  417. def run(self):
  418. while True:
  419. try:
  420. print("fd")
  421. connection, address = sock.accept()
  422. print(connection, address)
  423. with lock:
  424. connections.append(connection)
  425. gm = GettingMessage(connection)
  426. getting.append(gm)
  427. gm.start()
  428. except socket.error:
  429. print('something wrong')
  430.  
  431.  
  432. class SendingMessageAudio(Thread):
  433. def __init__(self):
  434. Thread.__init__(self)
  435.  
  436. def run(self):
  437. while True:
  438. message, address = messages_audio.get()
  439. try:
  440. address.send(message)
  441. except socket.error:
  442. break
  443.  
  444.  
  445. class GettingMessageAudio(Thread):
  446. def __init__(self, connection):
  447. Thread.__init__(self)
  448. self.connection = connection
  449. self.login = ''
  450.  
  451. def run(self):
  452. while True:
  453. try:
  454. data = self.connection.recv(65536)
  455. try:
  456. data = data.decode('utf-8')
  457. data = json.loads(data)
  458. login_connections_audio[data['login']] = self.connection
  459. self.login = data['login']
  460. except UnicodeDecodeError:
  461. for game in gaming:
  462. if game.room.exist(self.login):
  463. for user in game.room.users:
  464. if user.login != self.login:
  465. if user.login in login_connections_audio:
  466. messages_audio.put((data, login_connections_audio[user.login]))
  467. except ConnectionResetError:
  468. print('here')
  469. connections_audio.remove(self.connection)
  470. login_connections_audio.pop(self.login)
  471. break
  472. except json.decoder.JSONDecodeError:
  473. print('here')
  474. connections_audio.remove(self.connection)
  475. login_connections_audio.pop(self.login)
  476. break
  477. if not data:
  478. print('disconnected')
  479. break
  480.  
  481.  
  482. class FindingConnectionAudio(Thread):
  483. def __init__(self):
  484. Thread.__init__(self)
  485.  
  486. def run(self):
  487. while True:
  488. try:
  489. connection, address = sock_audio.accept()
  490. print("conn_audio", connection)
  491. with lock_audio:
  492. connections_audio.append(connection)
  493. gm = GettingMessageAudio(connection)
  494. # getting.append(gm)
  495. gm.start()
  496. except socket.error:
  497. print('something wrong')
  498.  
  499.  
  500. app = Flask(__name__)
  501. login_connections = dict()
  502. rooms = []
  503. users = []
  504. sock = socket.socket()
  505. sock.bind((ip, port + 1))
  506. sock.listen(cnt_listeners)
  507. messages = queue.Queue()
  508. connections = []
  509. lock = Lock()
  510.  
  511. login_connections_audio = dict()
  512. sock_audio = socket.socket()
  513. sock_audio.bind((ip, port + 2))
  514. sock_audio.listen(cnt_listeners)
  515. messages_audio = queue.Queue()
  516. connections_audio = []
  517. lock_audio = Lock()
  518.  
  519. fc = FindingConnection()
  520. fc.start()
  521. sm = SendingMessage()
  522. sm.start()
  523.  
  524. fc_audio = FindingConnectionAudio()
  525. fc_audio.start()
  526. sm_audio = SendingMessageAudio()
  527. sm_audio.start()
  528.  
  529. getting = []
  530. gaming = []
  531.  
  532.  
  533. @app.route('/registration', methods=["POST"])
  534. def registration():
  535. login = request.values['login']
  536. name = request.values['name']
  537. password = request.values['password']
  538. correct_login = True
  539. for i in users:
  540. if i.login == login:
  541. correct_login = False
  542. if not correct_login:
  543. return json.dumps({
  544. "status": False,
  545. "error": "Такой логин уже используется"
  546. })
  547. users.append(User(login, name, password))
  548. return json.dumps({
  549. "status": True
  550. })
  551.  
  552.  
  553. @app.route('/authorization', methods=['POST'])
  554. def authorization():
  555. login = request.values['login']
  556. password = request.values['password']
  557. is_correct_data = False
  558. data = ''
  559. for i in users:
  560. if i.login == login:
  561. is_correct_data = True
  562. if i.password != password:
  563. is_correct_data = False
  564. data = 'Некорректный пароль'
  565. else:
  566. data = i.name
  567.  
  568. if not is_correct_data and len(data) == 0:
  569. data = 'Такого логина не существует'
  570. if not is_correct_data:
  571. return json.dumps({
  572. "status": False,
  573. "error": data
  574. })
  575. return json.dumps({
  576. "status": True,
  577. "name": data
  578. })
  579.  
  580.  
  581. @app.route('/create_room', methods=["POST"])
  582. def create_room():
  583. room_name = request.values['room_name']
  584. password = request.values['password']
  585. login = request.values['login']
  586. user = None
  587. for i in users:
  588. if i.login == login:
  589. user = i
  590. work = True
  591. room_id = 0
  592. while work:
  593. room_id = random.randint(1, 10000)
  594. work = True in list(map(lambda x: x == room_id, [i.room_id for i in rooms]))
  595. rooms.append(Room(user, room_name, password, room_id))
  596. return json.dumps({
  597. "status": True,
  598. "room_id": room_id
  599. })
  600.  
  601.  
  602. @app.route('/get_rooms_ids', methods=['GET'])
  603. def get_rooms_ids():
  604. mas = []
  605. for i in rooms:
  606. mas.append(i.room_id)
  607. return json.dumps({
  608. "status": True,
  609. "rooms_ids": mas
  610. })
  611.  
  612.  
  613. @app.route('/has_password', methods=['GET'])
  614. def has_password():
  615. room_id = request.values['room_id']
  616. answer = False
  617. for i in rooms:
  618. print(i.room_id, room_id, i.room_id == room_id, int(i.room_id) == int(room_id))
  619. if int(i.room_id) == int(room_id):
  620. if len(i.password) > 0:
  621. answer = True
  622. return json.dumps({
  623. "status": True,
  624. "has_password": answer
  625. })
  626.  
  627.  
  628. @app.route('/connect_to_room', methods=['POST'])
  629. def connect_to_room():
  630. login = request.values['login']
  631. password = request.values['password']
  632. room_id = request.values['room_id']
  633. user = None
  634. for i in users:
  635. if i.login == login:
  636. user = i
  637. exist = False
  638. error = ''
  639. print(rooms)
  640. for i in rooms:
  641. print(i.room_id, room_id, int(i.room_id) == int(room_id))
  642. print(",,,,,,,", room_id)
  643. for i in rooms:
  644. if int(i.room_id) == int(room_id):
  645. if i.password != password:
  646. error = 'Неверный пароль'
  647. elif len(i.users) == 3:
  648. error = 'Эта комната заполнена'
  649. elif login in [local_user.login for local_user in i.users]:
  650. error = 'Вы уже состоите в этой комнате'
  651. else:
  652. i.users.append(user)
  653. exist = True
  654.  
  655. if not exist and len(error) == 0:
  656. error = 'Неверный ID'
  657. if not exist:
  658. return json.dumps({
  659. "status": False,
  660. "error": error
  661. })
  662. return json.dumps({
  663. "status": True
  664. })
  665.  
  666.  
  667. @app.route('/get_rooms', methods=["GET"])
  668. def get_rooms():
  669. return json.dumps({
  670. "count": len(rooms),
  671. "rooms": [i.to_json() for i in rooms]
  672. })
  673.  
  674.  
  675. @app.route('/can_connect', methods=['GET'])
  676. def can_connect():
  677. login = request.values['login']
  678. for room in rooms:
  679. if room.exist(login):
  680. if login in room.readies:
  681. return json.dumps({
  682. "status": False
  683. })
  684. return json.dumps({
  685. "status": True
  686. })
  687.  
  688.  
  689. @app.route('/ready', methods=["GET"])
  690. def ready():
  691. login = request.values['login']
  692. for room in rooms:
  693. print("here1")
  694. if room.exist(login):
  695. print("here2")
  696. if login in room.readies:
  697. return json.dumps({
  698. "status": False
  699. })
  700. room.set_ready(login)
  701. readies = room.get_readies()
  702. for i in range(3):
  703. print("here3")
  704. if room.users[i].login in login_connections:
  705. print("here4")
  706. prev_name = room.prev_name(room.users[i].login)
  707. next_name = room.next_name(room.users[i].login)
  708. prev_login = room.prev_login(room.users[i].login)
  709. next_login = room.next_login(room.users[i].login)
  710. data = {
  711. "type": "connection_info",
  712. "next": {
  713. "name": next_name,
  714. "is_connect": next_login in readies
  715. },
  716. "prev": {
  717. "name": prev_name,
  718. "is_connect": prev_login in readies
  719. }
  720. }
  721. messages.put((json.dumps(data).encode('utf-8'), login_connections[room.users[i].login]))
  722. if len(readies) == 3:
  723. game = Gaming(room)
  724. game.choices = {room.users[0].login: None, room.users[1].login: None, room.users[2].login: None}
  725. gaming.append(game)
  726. game.start()
  727. return json.dumps({
  728. "status": True
  729. })
  730.  
  731.  
  732. @app.route('/room_by_login', methods=['GET'])
  733. def room_by_login():
  734. login = request.values['login']
  735. room_id = -1
  736. for room in rooms:
  737. for user in room.users:
  738. if user.login == login:
  739. room_id = room.room_id
  740. return json.dumps({
  741. "status": True,
  742. "room_id": room_id
  743. })
  744.  
  745.  
  746. @app.route("/set_choice", methods=["POST"])
  747. def set_choice():
  748. login = request.values["login"]
  749. choice = request.values["choice"]
  750. print(gaming)
  751. for game in gaming:
  752. print(game)
  753. if game.room.exist(login):
  754. if choice == "pas":
  755. game.choices[login] = "pas"
  756. elif choice == "miser":
  757. game.choices[login] = "miser"
  758. game.min_available = 16
  759. else:
  760. game.choices[login] = choice
  761. game.min_available = int(choice) + 1
  762. if game.cur_user < 2:
  763. game.cur_user += 1
  764. else:
  765. game.cur_user = 0
  766. if not game.finish_choice():
  767. if game.need_choose(game.cur_user):
  768. pass
  769. elif game.need_choose(game.get_next_user()):
  770. game.cur_user = game.get_next_user()
  771. else:
  772. game.cur_user = game.get_next_user()
  773. game.cur_user = game.get_next_user()
  774. game.position = 2
  775. login_prev = game.room.prev_login(login)
  776. login_next = game.room.next_login(login)
  777. data = {
  778. "type": "change_choice",
  779. "next": game.choices[login],
  780. "prev": None,
  781. "me": None
  782. }
  783. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_prev]))
  784. data = {
  785. "type": "change_choice",
  786. "prev": game.choices[login],
  787. "next": None,
  788. "me": None
  789. }
  790. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_next]))
  791. data = {
  792. "type": "change_choice",
  793. "prev": None,
  794. "next": None,
  795. "me": game.choices[login]
  796. }
  797. messages.put((json.dumps(data).encode('utf-8'), login_connections[login]))
  798. if game.finish_choice() and game.cnt_pas() == 3:
  799. game.position = 3
  800. game.cur_user = game.begin_user
  801. game.game_type = "pas"
  802. elif game.finish_choice() and game.cnt_pas() == 2 and 'miser' not in game.choices.values():
  803. game.position = 4
  804. game.cur_user = game.player()
  805. game.game_type = "player"
  806. elif game.finish_choice() and game.cnt_pas() == 2 and 'miser' in game.choices.values():
  807. game.game_type = 'miser'
  808. game.position = 4
  809.  
  810. return json.dumps({
  811. "status": True
  812. })
  813.  
  814.  
  815. @app.route('/set_step', methods=['POST'])
  816. def set_step():
  817. login = request.values['login']
  818. cards = request.values['cards']
  819. is_replace = request.values["is_replace"]
  820. if is_replace == "true":
  821. is_replace = True
  822. else:
  823. is_replace = False
  824. for game in gaming:
  825. if game.room.exist(login):
  826. if game.choices[login] == 'miser':
  827. login_prev = game.room.prev_login(login)
  828. data = {
  829. "type": "cross",
  830. "card": cards,
  831. "who": "next"
  832. }
  833. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_prev]))
  834. login_next = game.room.next_login(login)
  835. data = {
  836. "type": "cross",
  837. "card": cards,
  838. "who": "prev"
  839. }
  840. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_next]))
  841. game.local_step += 1
  842. if game.local_step == 1:
  843. game.suit = int(cards) // 10
  844. login_send = login
  845. if is_replace:
  846. for i in game.whists_pass.keys():
  847. if game.whists_pass[i] == 'pas':
  848. login_send = i
  849. login_prev = game.room.prev_login(login_send)
  850. login_next = game.room.next_login(login_send)
  851. game.steps.append((cards, login))
  852. data = {
  853. "type": "change_step",
  854. "next": cards,
  855. "prev": None,
  856. "me": None
  857. }
  858. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_prev]))
  859. data = {
  860. "type": "change_step",
  861. "prev": cards,
  862. "next": None,
  863. "me": None
  864. }
  865. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_next]))
  866. data = {
  867. "type": "change_step",
  868. "prev": None,
  869. "next": None,
  870. "me": cards
  871. }
  872. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_send]))
  873. if game.local_step == 3 and game.cnt_pas() == 3:
  874. login_take = game.get_login_take(pas=True)
  875. login_prev = game.room.prev_login(login_take)
  876. login_next = game.room.next_login(login_take)
  877. data = {
  878. "type": "take",
  879. "who": "next"
  880. }
  881. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_prev]))
  882. data = {
  883. "type": "take",
  884. "who": "prev"
  885. }
  886. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_next]))
  887. data = {
  888. "type": "take",
  889. "who": "me"
  890. }
  891. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_take]))
  892. game.steps = []
  893. game.local_step = 0
  894. game.step_number += 1
  895. if game.step_number < 2:
  896. game.cur_user = game.begin_user
  897. game.position = 3
  898. elif game.step_number == 2:
  899. game.cur_user = game.begin_user
  900. login_send = game.room.users[game.begin_user].login
  901. data = {
  902. "type": "do_step",
  903. "is_me": True,
  904. "who": game.room.get_name_by_login(login_send),
  905. "suit": 0
  906. }
  907. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_send]))
  908. prev_login = game.room.prev_login(login_send)
  909. data = {
  910. "type": "do_step",
  911. "is_me": False,
  912. "who": game.room.get_name_by_login(login_send),
  913. "suit": 0
  914. }
  915. messages.put((json.dumps(data).encode('utf-8'), login_connections[prev_login]))
  916. next_login = game.room.next_login(login_send)
  917. data = {
  918. "type": "do_step",
  919. "is_me": False,
  920. "who": game.room.get_name_by_login(login_send),
  921. "suit": 0
  922. }
  923. messages.put((json.dumps(data).encode('utf-8'), login_connections[next_login]))
  924. elif game.step_number < 10:
  925. login_send = login_take
  926. game.cur_user = game.room.get_number_by_login(login_take)
  927. data = {
  928. "type": "do_step",
  929. "is_me": True,
  930. "who": game.room.get_name_by_login(login_send),
  931. "suit": 0
  932. }
  933. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_send]))
  934. prev_login = game.room.prev_login(login_send)
  935. data = {
  936. "type": "do_step",
  937. "is_me": False,
  938. "who": game.room.get_name_by_login(login_send),
  939. "suit": 0
  940. }
  941. messages.put((json.dumps(data).encode('utf-8'), login_connections[prev_login]))
  942. next_login = game.room.next_login(login_send)
  943. data = {
  944. "type": "do_step",
  945. "is_me": False,
  946. "who": game.room.get_name_by_login(login_send),
  947. "suit": 0
  948. }
  949. messages.put((json.dumps(data).encode('utf-8'), login_connections[next_login]))
  950. else:
  951. game.fresh()
  952.  
  953. elif game.local_step == 3:
  954. login_take = game.get_login_take()
  955. login_prev = game.room.prev_login(login_take)
  956. login_next = game.room.next_login(login_take)
  957. data = {
  958. "type": "take",
  959. "who": "next"
  960. }
  961. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_prev]))
  962. data = {
  963. "type": "take",
  964. "who": "prev"
  965. }
  966. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_next]))
  967. data = {
  968. "type": "take",
  969. "who": "me"
  970. }
  971. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_take]))
  972. game.steps = []
  973. game.local_step = 0
  974. game.step_number += 1
  975.  
  976. login_send = login_take
  977. game.cur_user = game.room.get_number_by_login(login_take)
  978. data = {
  979. "type": "do_step",
  980. "is_me": True,
  981. "who": game.room.get_name_by_login(login_send),
  982. "suit": 0,
  983. "replace": False
  984. }
  985. if login_send in game.whists_pass:
  986. if game.whists_pass[login_send] == "pas" and game.is_open:
  987. data["is_me"] = False
  988. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_send]))
  989. prev_login = game.room.prev_login(login_send)
  990. data = {
  991. "type": "do_step",
  992. "is_me": False,
  993. "who": game.room.get_name_by_login(login_send),
  994. "suit": 0,
  995. "replace": False
  996. }
  997. if prev_login in game.whists_pass:
  998. if game.whists_pass[prev_login] == "whist" and game.is_open:
  999. data["replace"] = True
  1000. messages.put((json.dumps(data).encode('utf-8'), login_connections[prev_login]))
  1001. next_login = game.room.next_login(login_send)
  1002. data = {
  1003. "type": "do_step",
  1004. "is_me": False,
  1005. "who": game.room.get_name_by_login(login_send),
  1006. "suit": 0,
  1007. "replace": False
  1008. }
  1009. if next_login in game.whists_pass:
  1010. if game.whists_pass[next_login] == "whist" and game.is_open:
  1011. data["replace"] = True
  1012. messages.put((json.dumps(data).encode('utf-8'), login_connections[next_login]))
  1013. if game.step_number == 10:
  1014. game.fresh()
  1015. else:
  1016. game.cur_user = game.get_next_user()
  1017. is_whist = 'whist' in game.whists_pass.values()
  1018. is_pas = 'pas' in game.whists_pass.values()
  1019. if not game.was_in_whist and is_pas and is_whist:
  1020. game.position = 8
  1021. else:
  1022. login_send = game.room.users[game.cur_user].login
  1023. data = {
  1024. "type": "do_step",
  1025. "is_me": True,
  1026. "who": game.room.get_name_by_login(login_send),
  1027. "suit": 0,
  1028. "replace": False
  1029. }
  1030. if login_send in game.whists_pass:
  1031. if game.whists_pass[login_send] == "pas" and game.is_open:
  1032. data["is_me"] = False
  1033. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_send]))
  1034. prev_login = game.room.prev_login(login_send)
  1035. data = {
  1036. "type": "do_step",
  1037. "is_me": False,
  1038. "who": game.room.get_name_by_login(login_send),
  1039. "suit": 0,
  1040. "replace": False
  1041. }
  1042. if prev_login in game.whists_pass:
  1043. if game.whists_pass[prev_login] == "whist" and game.is_open:
  1044. data["replace"] = True
  1045. messages.put((json.dumps(data).encode('utf-8'), login_connections[prev_login]))
  1046. next_login = game.room.next_login(login_send)
  1047. data = {
  1048. "type": "do_step",
  1049. "is_me": False,
  1050. "who": game.room.get_name_by_login(login_send),
  1051. "suit": 0,
  1052. "replace": False
  1053. }
  1054. if next_login in game.whists_pass:
  1055. if game.whists_pass[next_login] == "whist" and game.is_open:
  1056. data["replace"] = True
  1057. messages.put((json.dumps(data).encode('utf-8'), login_connections[next_login]))
  1058. return json.dumps({
  1059. "status": True
  1060. })
  1061.  
  1062.  
  1063. @app.route('/trump', methods=["POST"])
  1064. def trump():
  1065. login = request.values['login']
  1066. game_type = request.values['game_type']
  1067. count = request.values['count']
  1068. trump_suit = request.values['suit']
  1069. for game in gaming:
  1070. if game.room.exist(login):
  1071. for i in game.choices.keys():
  1072. data = {
  1073. "type": "game_type",
  1074. "game_type": game_type
  1075. }
  1076. messages.put((json.dumps(data).encode('utf-8'), login_connections[i]))
  1077. game.min_takes = count
  1078. game.trump_suit = trump_suit
  1079. game.cur_user = game.begin_user
  1080. game.position = 7
  1081. return json.dumps({
  1082. "status": True
  1083. })
  1084.  
  1085.  
  1086. @app.route('/set_choice_whist_pas', methods=['POST'])
  1087. def set_choice_whist_pas():
  1088. login = request.values['login']
  1089. choice = request.values['choice']
  1090. for game in gaming:
  1091. if game.room.exist(login):
  1092. game.whists_pass[login] = choice
  1093. login_prev = game.room.prev_login(login)
  1094. login_next = game.room.next_login(login)
  1095. data = {
  1096. "type": "change_choice_whist_pas",
  1097. "who": "next",
  1098. "choice": choice
  1099. }
  1100. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_prev]))
  1101. data = {
  1102. "type": "change_choice_whist_pas",
  1103. "who": "prev",
  1104. "choice": choice
  1105. }
  1106. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_next]))
  1107. data = {
  1108. "type": "change_choice_whist_pas",
  1109. "who": "me",
  1110. "choice": choice
  1111. }
  1112. messages.put((json.dumps(data).encode('utf-8'), login_connections[login]))
  1113. if len(game.whists_pass) == 1:
  1114. data = {
  1115. "type": "whist_pas"
  1116. }
  1117. messages.put((json.dumps(data).encode('utf-8'), login_connections[game.room.next_login(login)]))
  1118. else:
  1119. game.cur_user = game.begin_user
  1120. game.position = 8
  1121. return json.dumps({
  1122. "status": True
  1123. })
  1124.  
  1125.  
  1126. @app.route('/open_close', methods=['POST'])
  1127. def open_close():
  1128. login = request.values['login']
  1129. is_open = request.values['is_open']
  1130. if is_open == "true":
  1131. is_open = True
  1132. else:
  1133. is_open = False
  1134. for game in gaming:
  1135. if game.room.exist(login):
  1136. if is_open:
  1137. game.is_open = True
  1138. game.was_in_whist = True
  1139. for player in game.whists_pass.keys():
  1140. pos = game.room.get_number_by_login(player)
  1141. cards = game.player_cards[pos]
  1142. login_prev = game.room.prev_login(player)
  1143. login_next = game.room.next_login(player)
  1144. data = {
  1145. "type": "open_cards",
  1146. "who": "next",
  1147. "cards": cards
  1148. }
  1149. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_prev]))
  1150. data = {
  1151. "type": "open_cards",
  1152. "who": "prev",
  1153. "cards": cards
  1154. }
  1155. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_next]))
  1156. else:
  1157. game.was_in_whist = True
  1158. game.position = 6
  1159. return json.dumps({
  1160. "status": True
  1161. })
  1162.  
  1163.  
  1164. @app.route('/set_miser', methods=['POST'])
  1165. def set_miser():
  1166. login = request.values['login']
  1167. for game in gaming:
  1168. if game.room.exist(login):
  1169. game.position = 10
  1170. return json.dumps({
  1171. "status": True
  1172. })
  1173.  
  1174.  
  1175. @app.route('/exit_room', methods=['POST'])
  1176. def exit_room():
  1177. login = request.values['login']
  1178. for game in gaming:
  1179. if game.room.exist(login):
  1180. i = 0
  1181. try:
  1182. login_prev = game.room.prev_login(login)
  1183. data = {
  1184. "type": "player_exit",
  1185. "who": "next"
  1186. }
  1187. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_prev]))
  1188. except IndexError:
  1189. pass
  1190. try:
  1191. login_next = game.room.prev_login(login)
  1192. data = {
  1193. "type": "player_exit",
  1194. "who": "prev"
  1195. }
  1196. messages.put((json.dumps(data).encode('utf-8'), login_connections[login_next]))
  1197. except IndexError:
  1198. pass
  1199. for user in game.room.users:
  1200. if user.login == login:
  1201. game.room.del_ready(login)
  1202. del game.room.users[i]
  1203. i += 1
  1204. game.fresh()
  1205.  
  1206. return json.dumps({
  1207. "status": True
  1208. })
  1209.  
  1210.  
  1211. @app.route('/close', methods=['GET'])
  1212. def close():
  1213. fc.join()
  1214. sm.join()
  1215. for i in getting:
  1216. i.join()
  1217. for i in gaming:
  1218. i.join()
  1219. for i in connections:
  1220. i.close()
  1221. sock.close()
  1222. return json.dumps({
  1223. "status": True
  1224. })
  1225.  
  1226.  
  1227. if __name__ == '__main__':
  1228. app.run(host=ip, port=port)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement