Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import socket
- import time
- from threading import Thread, Lock
- from copy import deepcopy
- import queue
- from flask import *
- import json
- import random
- from User import User
- from Room import Room
- ip = "127.0.0.1"
- port = 8132
- cnt_listeners = 100
- cards_global = [i for i in range(11, 19)] + [i for i in range(21, 29)] \
- + [i for i in range(31, 39)] + [i for i in range(41, 49)]
- class Gaming(Thread):
- def __init__(self, room):
- Thread.__init__(self)
- self.room = room
- self.position = 1
- self.cards = deepcopy(cards_global)
- self.extra_cards = []
- self.choices = {room.users[0].login: None, room.users[1].login: None, room.users[2].login: None}
- self.seq = deepcopy(self.room.users)
- self.min_available = 1
- self.cur_user = 0
- self.begin_user = 0
- self.step_number = 0
- self.local_step = 0
- self.steps = []
- self.game_type = ""
- self.suit = 0
- self.game_type_for_players = ""
- self.min_takes = 0
- self.trump_suit = -1
- self.whists_pass = {}
- self.is_open = False
- self.player_cards = []
- self.was_in_whist = False
- def fresh(self):
- self.whists_pass = []
- self.game_type_for_players = ""
- self.min_takes = 0
- self.trump_suit = -1
- self.suit = 0
- self.game_type = ""
- self.steps = []
- self.local_step = 0
- self.step_number = 0
- self.begin_user = 0 if self.begin_user == 2 else self.begin_user + 1
- self.cur_user = self.begin_user
- self.min_available = 1
- self.seq = deepcopy(self.room.users)
- self.choices = {self.room.users[0].login: None, self.room.users[1].login: None, self.room.users[2].login: None}
- self.extra_cards = []
- self.cards = deepcopy(cards_global)
- self.position = 1
- data = {
- "type": "fresh"
- }
- for user in self.room.users:
- messages.put((json.dumps(data).encode('utf-8'), login_connections[user.login]))
- def get_cards(self, count):
- cards = []
- for i in range(count):
- print("self.cards", self.cards)
- x = random.randint(0, len(self.cards) - 1)
- print("x", x)
- item = self.cards[x]
- del self.cards[x]
- cards.append(item)
- print("cards", cards)
- cards.sort()
- return cards
- def get_next_user(self):
- if self.cur_user == 2:
- return 0
- else:
- return self.cur_user + 1
- def cnt_pas(self):
- cnt_pas = 0
- for choice in self.choices.keys():
- if self.choices[choice] == 'pas':
- cnt_pas += 1
- return cnt_pas
- def finish_choice(self):
- cnt_pas = self.cnt_pas()
- if cnt_pas >= 2 and not (None in self.choices.values()):
- return True
- else:
- return False
- def need_choose(self, user):
- if self.choices[self.room.users[user].login] == 'pas':
- return False
- if self.choices[self.room.users[user].login] == 'miser':
- return False
- cnt_pas = self.cnt_pas()
- if cnt_pas == 2 and not (None in self.choices.values()):
- return False
- return True
- def has_trump(self):
- print(self.steps, self.trump_suit)
- for i in self.steps:
- if int(i[0]) // 10 == int(self.trump_suit):
- return True
- return False
- def get_max_value_login(self):
- print(self.steps, self.suit)
- self.steps.sort()
- if int(self.steps[2][0]) // 10 == int(self.trump_suit):
- return self.steps[2][1]
- if int(self.steps[1][0]) // 10 == int(self.trump_suit):
- return self.steps[1][1]
- if int(self.steps[0][0]) // 10 == int(self.trump_suit):
- return self.steps[0][1]
- def get_without_trump(self):
- begin = int(self.steps[0][0])
- self.steps.sort()
- if int(self.steps[2][0]) // 10 == begin // 10:
- return self.steps[2][1]
- elif int(self.steps[1][0]) // 10 == begin // 10:
- return self.steps[1][1]
- else:
- return self.steps[0][1]
- def get_login_take(self, pas=False):
- print(pas, self.suit == 5, self.has_trump())
- if pas or self.suit == 5 or not self.has_trump():
- return self.get_without_trump()
- return self.get_max_value_login()
- def player(self):
- for i in self.choices.keys():
- if self.choices[i] != 'pas' and self.choices[i] != 'miser':
- return self.room.get_number_by_login(i)
- def run(self):
- while True:
- if self.position == 1:
- players_cards = [self.get_cards(10) for _ in range(3)]
- self.player_cards = deepcopy(players_cards)
- extra_cards = self.get_cards(2)
- self.extra_cards = extra_cards
- for i in range(3):
- login = self.room.users[i].login
- data = {
- "type": "cards",
- "cards": players_cards[i]
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login]))
- self.position = 2
- elif self.position == 2:
- time.sleep(5)
- data = {
- "type": "choice",
- "min_available": self.min_available
- }
- login_send = self.room.users[self.cur_user].login
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login_send]))
- self.position = -1
- elif self.position == 3:
- data = {
- "type": "extra",
- "number": self.step_number,
- "extra": self.extra_cards[self.step_number]
- }
- for i in self.room.users:
- messages.put((json.dumps(data).encode('utf-8'), login_connections[i.login]))
- login_send = self.room.users[self.cur_user].login
- data = {
- "type": "do_step",
- "is_me": True,
- "who": self.room.get_name_by_login(login_send),
- "suit": 0
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login_send]))
- prev_login = self.room.prev_login(login_send)
- data = {
- "type": "do_step",
- "is_me": False,
- "who": self.room.get_name_by_login(login_send),
- "suit": 0
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[prev_login]))
- next_login = self.room.next_login(login_send)
- data = {
- "type": "do_step",
- "is_me": False,
- "who": self.room.get_name_by_login(login_send),
- "suit": 0
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[next_login]))
- self.position = -1
- elif self.position == 4:
- data = {
- "type": "open_all_extra",
- "extra1": self.extra_cards[0],
- "extra2": self.extra_cards[1]
- }
- for user in self.room.users:
- messages.put((json.dumps(data).encode('utf-8'), login_connections[user.login]))
- self.position = 5
- elif self.position == 5:
- time.sleep(7)
- data = {
- "type": "close_extra",
- }
- for i in self.choices.keys():
- if self.choices[i] == 'pas':
- data["is_me"] = False
- else:
- data["is_me"] = True
- messages.put((json.dumps(data).encode('utf-8'), login_connections[i]))
- self.position = -1
- elif self.position == 6:
- login_send = self.room.users[self.cur_user].login
- if not self.is_open:
- data = {
- "type": "do_step",
- "is_me": True,
- "who": self.room.get_name_by_login(login_send),
- "suit": 0
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login_send]))
- prev_login = self.room.prev_login(login_send)
- data = {
- "type": "do_step",
- "is_me": False,
- "who": self.room.get_name_by_login(login_send),
- "suit": 0
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[prev_login]))
- next_login = self.room.next_login(login_send)
- data = {
- "type": "do_step",
- "is_me": False,
- "who": self.room.get_name_by_login(login_send),
- "suit": 0
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[next_login]))
- else:
- data = {
- "type": "do_step",
- "is_me": True,
- "who": self.room.get_name_by_login(login_send),
- "suit": 0,
- "replace": False
- }
- if login_send in self.whists_pass:
- if self.whists_pass[login_send] == "pas" and self.is_open:
- data["is_me"] = False
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login_send]))
- prev_login = self.room.prev_login(login_send)
- data = {
- "type": "do_step",
- "is_me": False,
- "who": self.room.get_name_by_login(login_send),
- "suit": 0,
- "replace": False
- }
- if prev_login in self.whists_pass:
- if self.whists_pass[prev_login] == "whist" and self.is_open:
- data["replace"] = True
- messages.put((json.dumps(data).encode('utf-8'), login_connections[prev_login]))
- next_login = self.room.next_login(login_send)
- data = {
- "type": "do_step",
- "is_me": False,
- "who": self.room.get_name_by_login(login_send),
- "suit": 0,
- "replace": False
- }
- if next_login in self.whists_pass:
- if self.whists_pass[next_login] == "whist" and self.is_open:
- data["replace"] = True
- messages.put((json.dumps(data).encode('utf-8'), login_connections[next_login]))
- self.position = -1
- elif self.position == 7:
- login = None
- for i in self.choices.keys():
- if self.choices[i] != 'pas':
- login = i
- login = self.room.next_login(login)
- data = {
- "type": "whist_pas"
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login]))
- self.position = -1
- elif self.position == 8:
- login = self.room.users[self.cur_user].login
- if login in self.whists_pass:
- data = {
- "type": "close_open"
- }
- for i in self.whists_pass.keys():
- if self.whists_pass[i] == 'whist':
- login = i
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login]))
- self.position = -1
- else:
- self.position = 6
- class SendingMessage(Thread):
- def __init__(self):
- Thread.__init__(self)
- def run(self):
- while True:
- time.sleep(0.1)
- print("sm")
- print(messages)
- message, address = messages.get()
- print(message, address)
- address.send(message)
- class GettingMessage(Thread):
- def __init__(self, connection):
- Thread.__init__(self)
- self.connection = connection
- self.login = ''
- def run(self):
- while True:
- try:
- data = self.connection.recv(1024)
- print(data)
- data = data.decode('utf-8')
- data = json.loads(data)
- print(data)
- if data['type'] == 'connection':
- login_connections[data['login']] = self.connection
- print(login_connections)
- self.login = data['login']
- except ConnectionResetError:
- connections.remove(self.connection)
- login_connections.pop(self.login)
- break
- print(data)
- if not data:
- print('disconnected')
- break
- class FindingConnection(Thread):
- def __init__(self):
- Thread.__init__(self)
- def run(self):
- while True:
- try:
- print("fd")
- connection, address = sock.accept()
- print(connection, address)
- with lock:
- connections.append(connection)
- gm = GettingMessage(connection)
- getting.append(gm)
- gm.start()
- except socket.error:
- print('something wrong')
- app = Flask(__name__)
- login_connections = dict()
- rooms = []
- users = []
- sock = socket.socket()
- sock.bind((ip, port + 1))
- sock.listen(cnt_listeners)
- messages = queue.Queue()
- connections = []
- lock = Lock()
- fc = FindingConnection()
- fc.start()
- sm = SendingMessage()
- sm.start()
- getting = []
- gaming = []
- @app.route('/registration', methods=["POST"])
- def registration():
- login = request.values['login']
- name = request.values['name']
- password = request.values['password']
- correct_login = True
- for i in users:
- if i.login == login:
- correct_login = False
- if not correct_login:
- return json.dumps({
- "status": False,
- "error": "Такой логин уже используется"
- })
- users.append(User(login, name, password))
- return json.dumps({
- "status": True
- })
- @app.route('/authorization', methods=['POST'])
- def authorization():
- login = request.values['login']
- password = request.values['password']
- is_correct_data = False
- data = ''
- for i in users:
- if i.login == login:
- is_correct_data = True
- if i.password != password:
- is_correct_data = False
- data = 'Некорректный пароль'
- else:
- data = i.name
- if not is_correct_data and len(data) == 0:
- data = 'Такого логина не существует'
- if not is_correct_data:
- return json.dumps({
- "status": False,
- "error": data
- })
- return json.dumps({
- "status": True,
- "name": data
- })
- @app.route('/create_room', methods=["POST"])
- def create_room():
- room_name = request.values['room_name']
- password = request.values['password']
- login = request.values['login']
- user = None
- for i in users:
- if i.login == login:
- user = i
- work = True
- room_id = 0
- while work:
- room_id = random.randint(1, 10000)
- work = True in list(map(lambda x: x == room_id, [i.room_id for i in rooms]))
- rooms.append(Room(user, room_name, password, room_id))
- return json.dumps({
- "status": True,
- "room_id": room_id
- })
- @app.route('/get_rooms_ids', methods=['GET'])
- def get_rooms_ids():
- mas = []
- for i in rooms:
- mas.append(i.room_id)
- return json.dumps({
- "status": True,
- "rooms_ids": mas
- })
- @app.route('/has_password', methods=['GET'])
- def has_password():
- room_id = request.values['room_id']
- answer = False
- for i in rooms:
- print(i.room_id, room_id, i.room_id == room_id, int(i.room_id) == int(room_id))
- if int(i.room_id) == int(room_id):
- if len(i.password) > 0:
- answer = True
- return json.dumps({
- "status": True,
- "has_password": answer
- })
- @app.route('/connect_to_room', methods=['POST'])
- def connect_to_room():
- login = request.values['login']
- password = request.values['password']
- room_id = request.values['room_id']
- user = None
- for i in users:
- if i.login == login:
- user = i
- exist = False
- error = ''
- print(rooms)
- for i in rooms:
- print(i.room_id, room_id, int(i.room_id) == int(room_id))
- print(",,,,,,,", room_id)
- for i in rooms:
- if int(i.room_id) == int(room_id):
- if i.password != password:
- error = 'Неверный пароль'
- elif len(i.users) == 3:
- error = 'Эта комната заполнена'
- elif login in [local_user.login for local_user in i.users]:
- error = 'Вы уже состоите в этой комнате'
- else:
- i.users.append(user)
- exist = True
- if not exist and len(error) == 0:
- error = 'Неверный ID'
- if not exist:
- return json.dumps({
- "status": False,
- "error": error
- })
- return json.dumps({
- "status": True
- })
- @app.route('/get_rooms', methods=["GET"])
- def get_rooms():
- return json.dumps({
- "count": len(rooms),
- "rooms": [i.to_json() for i in rooms]
- })
- @app.route('/ready', methods=["GET"])
- def ready():
- login = request.values['login']
- for room in rooms:
- print("here1")
- if room.exist(login):
- print("here2")
- room.set_ready(login)
- readies = room.get_readies()
- for i in range(3):
- print("here3")
- if room.users[i].login in login_connections:
- print("here4")
- prev_name = room.prev_name(room.users[i].login)
- next_name = room.next_name(room.users[i].login)
- prev_login = room.prev_login(room.users[i].login)
- next_login = room.next_login(room.users[i].login)
- data = {
- "type": "connection_info",
- "next": {
- "name": next_name,
- "is_connect": next_login in readies
- },
- "prev": {
- "name": prev_name,
- "is_connect": prev_login in readies
- }
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[room.users[i].login]))
- if len(readies) == 3:
- game = Gaming(room)
- gaming.append(game)
- game.start()
- return json.dumps({
- "status": True
- })
- @app.route('/room_by_login', methods=['GET'])
- def room_by_login():
- login = request.values['login']
- room_id = -1
- for room in rooms:
- for user in room.users:
- if user.login == login:
- room_id = room.room_id
- return json.dumps({
- "status": True,
- "room_id": room_id
- })
- @app.route("/set_choice", methods=["POST"])
- def set_choice():
- login = request.values["login"]
- choice = request.values["choice"]
- print(gaming)
- for game in gaming:
- print(game)
- if game.room.exist(login):
- if choice == "pas":
- game.choices[login] = "pas"
- elif choice == "miser":
- game.choices[login] = "miser"
- game.min_available = 16
- else:
- game.choices[login] = choice
- game.min_available = int(choice) + 1
- if game.cur_user < 2:
- game.cur_user += 1
- else:
- game.cur_user = 0
- if not game.finish_choice():
- if game.need_choose(game.cur_user):
- pass
- elif game.need_choose(game.get_next_user()):
- game.cur_user = game.get_next_user()
- else:
- game.cur_user = game.get_next_user()
- game.cur_user = game.get_next_user()
- game.position = 2
- login_prev = game.room.prev_login(login)
- login_next = game.room.next_login(login)
- data = {
- "type": "change_choice",
- "next": game.choices[login],
- "prev": None,
- "me": None
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login_prev]))
- data = {
- "type": "change_choice",
- "prev": game.choices[login],
- "next": None,
- "me": None
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login_next]))
- data = {
- "type": "change_choice",
- "prev": None,
- "next": None,
- "me": game.choices[login]
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login]))
- if game.finish_choice() and game.cnt_pas() == 3:
- game.position = 3
- game.cur_user = game.begin_user
- game.type_game = "pas"
- elif game.finish_choice() and game.cnt_pas() == 2:
- game.position = 4
- game.cur_user = game.player()
- game.type_game = "player"
- return json.dumps({
- "status": True
- })
- @app.route('/set_step', methods=['POST'])
- def set_step():
- login = request.values['login']
- cards = request.values['cards']
- is_replace = request.values["is_replace"]
- if is_replace == "true":
- is_replace = True
- else:
- is_replace = False
- for game in gaming:
- if game.room.exist(login):
- game.local_step += 1
- if game.local_step == 1:
- game.suit = int(cards) // 10
- login_send = login
- if is_replace:
- for i in game.whists_pass.keys():
- if game.whists_pass[i] == 'pas':
- login_send = i
- login_prev = game.room.prev_login(login_send)
- login_next = game.room.next_login(login_send)
- game.steps.append((cards, login))
- data = {
- "type": "change_step",
- "next": cards,
- "prev": None,
- "me": None
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login_prev]))
- data = {
- "type": "change_step",
- "prev": cards,
- "next": None,
- "me": None
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login_next]))
- data = {
- "type": "change_step",
- "prev": None,
- "next": None,
- "me": cards
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login_send]))
- if game.local_step == 3 and game.cnt_pas() == 3:
- login_take = game.get_login_take(pas=True)
- login_prev = game.room.prev_login(login_take)
- login_next = game.room.next_login(login_take)
- data = {
- "type": "take",
- "who": "next"
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login_prev]))
- data = {
- "type": "take",
- "who": "prev"
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login_next]))
- data = {
- "type": "take",
- "who": "me"
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login_take]))
- game.steps = []
- game.local_step = 0
- game.step_number += 1
- if game.step_number < 2:
- game.cur_user = game.begin_user
- game.position = 3
- elif game.step_number == 2:
- game.cur_user = game.begin_user
- login_send = game.room.users[game.begin_user].login
- data = {
- "type": "do_step",
- "is_me": True,
- "who": game.room.get_name_by_login(login_send),
- "suit": 0
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login_send]))
- prev_login = game.room.prev_login(login_send)
- data = {
- "type": "do_step",
- "is_me": False,
- "who": game.room.get_name_by_login(login_send),
- "suit": 0
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[prev_login]))
- next_login = game.room.next_login(login_send)
- data = {
- "type": "do_step",
- "is_me": False,
- "who": game.room.get_name_by_login(login_send),
- "suit": 0
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[next_login]))
- elif game.step_number < 10:
- login_send = login_take
- game.cur_user = game.room.get_number_by_login(login_take)
- data = {
- "type": "do_step",
- "is_me": True,
- "who": game.room.get_name_by_login(login_send),
- "suit": 0
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login_send]))
- prev_login = game.room.prev_login(login_send)
- data = {
- "type": "do_step",
- "is_me": False,
- "who": game.room.get_name_by_login(login_send),
- "suit": 0
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[prev_login]))
- next_login = game.room.next_login(login_send)
- data = {
- "type": "do_step",
- "is_me": False,
- "who": game.room.get_name_by_login(login_send),
- "suit": 0
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[next_login]))
- else:
- game.fresh()
- elif game.local_step == 3:
- login_take = game.get_login_take()
- login_prev = game.room.prev_login(login_take)
- login_next = game.room.next_login(login_take)
- data = {
- "type": "take",
- "who": "next"
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login_prev]))
- data = {
- "type": "take",
- "who": "prev"
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login_next]))
- data = {
- "type": "take",
- "who": "me"
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login_take]))
- game.steps = []
- game.local_step = 0
- game.step_number += 1
- login_send = login_take
- game.cur_user = game.room.get_number_by_login(login_take)
- data = {
- "type": "do_step",
- "is_me": True,
- "who": game.room.get_name_by_login(login_send),
- "suit": 0,
- "replace": False
- }
- if login_send in game.whists_pass:
- if game.whists_pass[login_send] == "pas" and game.is_open:
- data["is_me"] = False
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login_send]))
- prev_login = game.room.prev_login(login_send)
- data = {
- "type": "do_step",
- "is_me": False,
- "who": game.room.get_name_by_login(login_send),
- "suit": 0,
- "replace": False
- }
- if prev_login in game.whists_pass:
- if game.whists_pass[prev_login] == "whist" and game.is_open:
- data["replace"] = True
- messages.put((json.dumps(data).encode('utf-8'), login_connections[prev_login]))
- next_login = game.room.next_login(login_send)
- data = {
- "type": "do_step",
- "is_me": False,
- "who": game.room.get_name_by_login(login_send),
- "suit": 0,
- "replace": False
- }
- if next_login in game.whists_pass:
- if game.whists_pass[next_login] == "whist" and game.is_open:
- data["replace"] = True
- messages.put((json.dumps(data).encode('utf-8'), login_connections[next_login]))
- if game.step_number == 10:
- game.fresh()
- else:
- game.cur_user = game.get_next_user()
- is_whist = 'whist' in game.whists_pass.values()
- is_pas = 'pas' in game.whists_pass.values()
- if not game.was_in_whist and is_pas and is_whist:
- game.position = 8
- else:
- login_send = game.room.users[game.cur_user].login
- data = {
- "type": "do_step",
- "is_me": True,
- "who": game.room.get_name_by_login(login_send),
- "suit": 0,
- "replace": False
- }
- if login_send in game.whists_pass:
- if game.whists_pass[login_send] == "pas" and game.is_open:
- data["is_me"] = False
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login_send]))
- prev_login = game.room.prev_login(login_send)
- data = {
- "type": "do_step",
- "is_me": False,
- "who": game.room.get_name_by_login(login_send),
- "suit": 0,
- "replace": False
- }
- if prev_login in game.whists_pass:
- if game.whists_pass[prev_login] == "whist" and game.is_open:
- data["replace"] = True
- messages.put((json.dumps(data).encode('utf-8'), login_connections[prev_login]))
- next_login = game.room.next_login(login_send)
- data = {
- "type": "do_step",
- "is_me": False,
- "who": game.room.get_name_by_login(login_send),
- "suit": 0,
- "replace": False
- }
- if next_login in game.whists_pass:
- if game.whists_pass[next_login] == "whist" and game.is_open:
- data["replace"] = True
- messages.put((json.dumps(data).encode('utf-8'), login_connections[next_login]))
- return json.dumps({
- "status": True
- })
- @app.route('/trump', methods=["POST"])
- def trump():
- login = request.values['login']
- game_type = request.values['game_type']
- count = request.values['count']
- trump_suit = request.values['suit']
- for game in gaming:
- if game.room.exist(login):
- for i in game.choices.keys():
- data = {
- "type": "game_type",
- "game_type": game_type
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[i]))
- game.min_takes = count
- game.trump_suit = trump_suit
- game.cur_user = game.begin_user
- game.position = 7
- return json.dumps({
- "status": True
- })
- @app.route('/set_choice_whist_pas', methods=['POST'])
- def set_choice_whist_pas():
- login = request.values['login']
- choice = request.values['choice']
- for game in gaming:
- if game.room.exist(login):
- game.whists_pass[login] = choice
- login_prev = game.room.prev_login(login)
- login_next = game.room.next_login(login)
- data = {
- "type": "change_choice_whist_pas",
- "who": "next",
- "choice": choice
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login_prev]))
- data = {
- "type": "change_choice_whist_pas",
- "who": "prev",
- "choice": choice
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login_next]))
- data = {
- "type": "change_choice_whist_pas",
- "who": "me",
- "choice": choice
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login]))
- if len(game.whists_pass) == 1:
- data = {
- "type": "whist_pas"
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[game.room.next_login(login)]))
- else:
- game.cur_user = game.begin_user
- game.position = 8
- return json.dumps({
- "status": True
- })
- @app.route('/open_close', methods=['POST'])
- def open_close():
- login = request.values['login']
- is_open = request.values['is_open']
- if is_open == "true":
- is_open = True
- else:
- is_open = False
- for game in gaming:
- if game.room.exist(login):
- if is_open:
- game.is_open = True
- game.was_in_whist = True
- for player in game.whists_pass.keys():
- pos = game.room.get_number_by_login(player)
- cards = game.player_cards[pos]
- login_prev = game.room.prev_login(player)
- login_next = game.room.next_login(player)
- data = {
- "type": "open_cards",
- "who": "next",
- "cards": cards
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login_prev]))
- data = {
- "type": "open_cards",
- "who": "prev",
- "cards": cards
- }
- messages.put((json.dumps(data).encode('utf-8'), login_connections[login_next]))
- else:
- game.was_in_whist = True
- game.position = 6
- return json.dumps({
- "status": True
- })
- @app.route('/close', methods=['GET'])
- def close():
- fc.join()
- sm.join()
- for i in getting:
- i.join()
- for i in gaming:
- i.join()
- for i in connections:
- i.close()
- sock.close()
- return json.dumps({
- "status": True
- })
- if __name__ == '__main__':
- app.run(host=ip, port=port)
Add Comment
Please, Sign In to add comment