Advertisement
Guest User

Untitled

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