Guest User

Untitled

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