From 641d9c233439f778cf3df870fc3db75c1c3e7717 Mon Sep 17 00:00:00 2001 From: 11-04-a-12 Date: Tue, 3 Oct 2017 15:55:26 +0300 Subject: [PATCH 01/10] some changes --- diler.py | 3 +++ server.py | 1 + 2 files changed, 4 insertions(+) diff --git a/diler.py b/diler.py index 58cee0e..41ae9f5 100644 --- a/diler.py +++ b/diler.py @@ -129,3 +129,6 @@ def opening(self): else: res = 'победители: ' + ', '.join(map(str, win)) return res + + def next_turn(self): + self.clients.append(self.clients.pop(0)) \ No newline at end of file diff --git a/server.py b/server.py index 119b35f..93c1e48 100644 --- a/server.py +++ b/server.py @@ -44,6 +44,7 @@ def start(self): self.broadcast(self.d.turn()) self.broadcast(self.d.river()) self.broadcast(self.d.opening()) + self.d.next_turn(self) self.broadcast("Спасибо за игру!") def broadcast(self, msg): From 4668a2cbaf006f43252a63f322b953cea559ae85 Mon Sep 17 00:00:00 2001 From: 11-04-a-12 Date: Tue, 3 Oct 2017 16:00:38 +0300 Subject: [PATCH 02/10] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80?= =?UTF-8?q?=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server.py | 1 + 1 file changed, 1 insertion(+) diff --git a/server.py b/server.py index 93c1e48..f080a1c 100644 --- a/server.py +++ b/server.py @@ -54,5 +54,6 @@ def broadcast(self, msg): def dispense(self): for client in self.clients: self.send(client[0], str(self.d.request(client))) + №ылдводлывао game = Game() From 587e70e65ba423f21167ceee1d69353bb0e3081c Mon Sep 17 00:00:00 2001 From: Daler9999 <32322981+Daler9999@users.noreply.github.com> Date: Wed, 4 Oct 2017 09:56:54 +0300 Subject: [PATCH 03/10] Update server.py --- server.py | 1 - 1 file changed, 1 deletion(-) diff --git a/server.py b/server.py index f080a1c..93c1e48 100644 --- a/server.py +++ b/server.py @@ -54,6 +54,5 @@ def broadcast(self, msg): def dispense(self): for client in self.clients: self.send(client[0], str(self.d.request(client))) - №ылдводлывао game = Game() From 4fa4cc163a23f46bee779cd468fea62d4a166bf6 Mon Sep 17 00:00:00 2001 From: Daler9999 <32322981+Daler9999@users.noreply.github.com> Date: Mon, 16 Oct 2017 11:45:30 +0300 Subject: [PATCH 04/10] =?UTF-8?q?=D0=A3=D0=B1=D0=B8=D1=80=D0=B0=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- comparator.py | 92 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/comparator.py b/comparator.py index 51826c9..bdd9668 100644 --- a/comparator.py +++ b/comparator.py @@ -1,5 +1,97 @@ from random import choice +from cards import * class Comparator: def compare(self, clients, table): return [choice(list(clients.keys()))] + +class Flush: + def __str__(self): + return "Flush" + + def check(cards): + suits = {c.suit for c in cards} + return len(suits) == 1 + +class Pair: + def __str__(self): + return "Pair" + + def check(cards): + nom = {} + for v in map(lambda x: x.value.value, cards): + nom[v] = nom.get(v, 0) + 1 + return len(nom) == 4 + +class TwoPairs: + def __str__(self): + return "TwoPairs" + + def check(cards): + nom = {} + for v in map(lambda x: x.value.value, cards): + nom[v] = nom.get(v, 0) + 1 + return len(nom) == 3 and max(nom.values()) == 2 and min(nom.values()) == 1 + +class Set: + def __str__(self): + return "Set" + + def check(cards): + nom = {} + for v in map(lambda x: x.value.value, cards): + nom[v] = nom.get(v, 0) + 1 + return len(nom) == 3 and max(nom.values()) == 3 and min(nom.values()) == 1 + +class FullHouse: + def __str__(self): + return "FullHouse" + + def check(cards): + nom = {} + for v in map(lambda x: x.value.value, cards): + nom[v] = nom.get(v, 0) + 1 + return len(nom) == 2 and max(nom.values()) == 3 + +class Quads: + def __str__(self): + return "Quads" + + def check(cards): + nom = {} + for v in map(lambda x: x.value.value, cards): + nom[v] = nom.get(v, 0) + 1 + return len(nom) == 2 and max(nom.values()) == 4 + +class Straight: + def __str__(self): + return "Straight" + + def check(cards): + s = list(map(lambda x: x.value.value, cards)) + return not Pair.check(cards) and max(s) - min(s) == 4 + +class High: + def check(cards): + return True + +class StraightFlush(): + def __str__(self): + return "StraightFlush" + + def check(cards): + return Straight.check(cards) and Flush.check(cards) + +def compare(cards): + for c in combinations: + if c.check(cards): + return c() + +combinations = [StraightFlush, Quads, FullHouse, Flush, Straight, Set, TwoPairs, Pair, High] + +Suits = [Diamonds, Clubs, Hearts, Spades] +cards = [Card(2, Diamonds), Card(5, Diamonds), Card(5, Diamonds), Card(5, Diamonds), Card(2, Diamonds)] +#cards = [Card(i, Diamonds) for i in range(2, 7)] + +print(compare(cards)) +print(' '.join(map(str, cards))) From a4ed4e2f65587c7503ea23140f9c09e438d6032c Mon Sep 17 00:00:00 2001 From: Daler9999 <32322981+Daler9999@users.noreply.github.com> Date: Mon, 16 Oct 2017 11:46:07 +0300 Subject: [PATCH 05/10] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE?= =?UTF-8?q?=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- diler.py | 68 +++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 15 deletions(-) diff --git a/diler.py b/diler.py index 41ae9f5..84154e8 100644 --- a/diler.py +++ b/diler.py @@ -1,6 +1,7 @@ from cards import createCards from random import randint from comparator import Comparator +from time import sleep class Diler: class Client: @@ -18,13 +19,16 @@ def ready(self): class Check(Ready): pass - class AllIn(Ready): + class Bet(Ready): pass - class Called(Ready): + class AllIn(Bet): pass - class Rised(Ready): + class Called(Bet): + pass + + class Rised(Bet): pass class Pass(Ready): @@ -36,6 +40,9 @@ def __init__(self, id, conn): self.id = id self.cards = [] self.status = Diler.Client.NotReady() + self.bet = 0 + self.money = 1000 + self.pas = False def __eq__(self, other): return self.id == other.id @@ -43,8 +50,8 @@ def __eq__(self, other): def addCard(self, card): self.cards.append(card) - def ready(self): - return self.status.ready() + def ready(self, max_bet): + return self.money == 0 or self.bet >= max_bet or self.pas def __init__(self, server): self.server = server @@ -52,6 +59,10 @@ def __init__(self, server): self.table = [] self.clients = [] self.comparator = Comparator() + self.rise_client = None + self.bet = 0 + self.bank = 0 + self.big_blind = 100 def getClient(self, client): missing = Diler.Client(client[1][1], client[0]) @@ -72,33 +83,47 @@ def getState(self): return self.clients def roundRun(self): - if len(list(filter(lambda x: type(x.status) != Diler.Client.Pass, self.clients))) < 2: - return - for client in filter(lambda x: type(x.status) != Diler.Client.Pass, self.clients): client.status = Diler.Client.NotReady() - k = 0 if rise_client is None else rise_client + k = 0 if self.rise_client is None else self.rise_client - while not all([client.ready() for client in self.clients]): + while not all([client.ready() for client in self.clients]) or len(list(filter(lambda x: type(x.status) != Diler.Client.Pass, self.clients))) < 2: if not self.clients[k].ready(): - self.server.send(self.clients[k].conn, 'ask') + sleep(0.01) + if self.bet > 0: + self.server.send(self.clients[k].conn, 'ask1 ' + str(bet - self.clients[k].bet)) + else: + self.server.send(self.clients[k].conn, 'ask0') ans = self.server.recv(self.clients[k].conn) - self.server.broadcast('info: игрок ' + str(self.clients[k].id) + ' ответил ' + ans) - if ans == 'pass': + self.broadcast('info: игрок ' + str(self.clients[k].id) + ' ответил ' + ans) + if ans == 'check': + self.clients[k].status = Diler.Client.Check() + elif ans == 'pass': self.clients[k].status = Diler.Client.Pass() elif ans == 'call': self.clients[k].status = Diler.Client.Called() - elif ans == 'rise': + elif ans[:4] == 'rise' or ans[:3] == 'bet': for c in filter(lambda x: type(x.status) != Diler.Client.Pass, self.clients): c.status = Diler.Client.NotReady() self.clients[k].status = Diler.Client.Rised() self.rise_client = k + bet = int(ans[4:]) + self.bank += bet + self.clients[k].bet = bet else: self.clients[k].status = Diler.Client.Pass() print('error: непонятный ответ от клиента') k = (k + 1) % len(self.clients) + def blind(self): + self.bank += self.big_blind // 2 + self.bank += self.big_blind + self.clients[0].bed(self.big_blind // 2) + self.clients[1].bed(self.big_blind) + self.bet = True + return "Игрок " + str(self.clients[0].id) + " поставил " + str(self.big_blind // 2) + ", игрок " + str(self.clients[1].id) + " поставил " + str(self.big_blind) + def flop(self): self.roundRun() for i in range(3): @@ -130,5 +155,18 @@ def opening(self): res = 'победители: ' + ', '.join(map(str, win)) return res + def game(self): + self.broadcast(self.blind()) + self.broadcast(self.flop()) + self.broadcast(self.turn()) + self.broadcast(self.river()) + self.broadcast(self.opening()) + self.next_turn() + self.broadcast("Спасибо за игру!") + + def broadcast(self, msg): + for client in self.clients: + self.server.send(client.conn, msg) + def next_turn(self): - self.clients.append(self.clients.pop(0)) \ No newline at end of file + self.clients.append(self.clients.pop(0)) From 18e3bbda9faa3c3edf4bc828ad50888e5a18b348 Mon Sep 17 00:00:00 2001 From: Daler9999 <32322981+Daler9999@users.noreply.github.com> Date: Mon, 16 Oct 2017 11:46:53 +0300 Subject: [PATCH 06/10] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE?= =?UTF-8?q?=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server.py | 58 ++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/server.py b/server.py index 93c1e48..1e550da 100644 --- a/server.py +++ b/server.py @@ -1,12 +1,23 @@ import diler import cards import network +import socket +import os +import time class Game: def __init__(self): self.d = diler.Diler(self) + self.logins = {} + self.money = 10000 self.clients = [] #[(conn, addr)] self.server = network.Server() + try: + self.f = open('file.txt', 'a') + except IOError as e: + print('Файла с логинами не существует!') + print('Создаю новый файл') + self.f = open('file.txt', 'w+') self.main() def main(self): @@ -17,18 +28,52 @@ def main(self): print('q - quit') ans = input('Введите команду(a/w/s/q): ') if ans == 'a': - self.accept() + try: + self.accept() + except socket.timeout: + print('Клиент не отвечает') + continue elif ans == 's': self.start() elif ans == 'q': print('By!') + self.f = open('file.txt', 'w') + for log, mon in self.logins.items(): + self.f.write(log + '.' + str(mon) + '\n') + self.f.close() exit(0) else: print('Команда не распознана') + def fromFiletoDict(self, File): + for line in open(File.name): + if os.stat("file").st_size == 0: + break + self.logins[line[:line.index('.')]] = line[line.index('.')+1:] + def accept(self): + self.money = 10000 self.clients.append(self.server.accept()) print('Установлена связь с клиентом', self.clients[-1][1][1]) + self.send((self.clients[-1][0]), 'Введите ваш логин:') + s = self.recv(self.clients[-1][0]) + print('Clients Login = ' + s) + try: #проверяем сущесвтование файла + self.f = open('file.txt') + self.fromFiletoDict(self.f) + if not(s in self.logins.keys()): + self.send(self.clients[-1][0], 'Добро пожаловать, новый игрок') + self.logins[s] = self.money #10000 - изначальное кол-во денег + else: + self.money = int(self.logins[s]) + self.send(self.clients[-1][0], 'Добро пожаловать ' + s) + except FileNotFoundError as e: + if not(s in self.logins): + self.send(self.clients[-1][0], 'Добро пожаловать, новый игрок') + self.logins[s] = self.money #10000 - изначальное кол-во денег + time.sleep(0.01) + self.send(self.clients[-1][0], str(self.money)) + self.recv(self.clients[-1][0]) self.send((self.clients[-1][0]), str(self.clients[-1][1][1])) def send(self, conn, msg): @@ -40,16 +85,7 @@ def recv(self, conn): def start(self): for i in range(2): self.dispense() - self.broadcast(self.d.flop()) - self.broadcast(self.d.turn()) - self.broadcast(self.d.river()) - self.broadcast(self.d.opening()) - self.d.next_turn(self) - self.broadcast("Спасибо за игру!") - - def broadcast(self, msg): - for client in self.clients: - self.send(client[0], msg) + self.d.game() def dispense(self): for client in self.clients: From 54e251435f792994a20ca272002a34ad6a53d6e1 Mon Sep 17 00:00:00 2001 From: Daler9999 <32322981+Daler9999@users.noreply.github.com> Date: Mon, 16 Oct 2017 11:48:09 +0300 Subject: [PATCH 07/10] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE?= =?UTF-8?q?=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- network.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/network.py b/network.py index 3e5376c..50afebb 100644 --- a/network.py +++ b/network.py @@ -1,7 +1,7 @@ import socket class Connection: - "Класс для констант подключения" + timeout = 5 adress = '127.0.0.1' port = 1234 players = 2 @@ -27,6 +27,7 @@ class Server: Занимается созданием сокета, установлением связи с клиентами, а также и получением и отправкой им сообщений.""" def __init__(self): self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + self.socket.settimeout(Connection.timeout) self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) self.socket.bind((Connection.adress, Connection.port)) self.socket.listen(Connection.players) From b6da2a6ca7198ce3afd15b4fad5d97cac7aa9731 Mon Sep 17 00:00:00 2001 From: Daler9999 <32322981+Daler9999@users.noreply.github.com> Date: Mon, 16 Oct 2017 11:48:47 +0300 Subject: [PATCH 08/10] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE?= =?UTF-8?q?=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/client.py b/client.py index 27d64ad..d2fe746 100644 --- a/client.py +++ b/client.py @@ -54,8 +54,14 @@ def game(self): def main(self): if input('Подключиться к серверу (yes(y)/no(n))?: ').lower() == 'y': - self.id = self.recv() + print(self.recv())#Логиин + self.send(input()) + print(self.recv())#Добро пожаловать + money = self.recv() + self.send("All is norm") + print('Ваши деньги:' + money + "\n") print("Регистрация на сервере успешно выполнена.") + self.id = self.recv() self.wait() self.game() else: From 8bca1a8fcd3177aa6d21a0b71735cdb540e839cd Mon Sep 17 00:00:00 2001 From: Daler Hamzaev Date: Wed, 18 Oct 2017 14:39:35 +0300 Subject: [PATCH 09/10] Changes --- .idea/vcs.xml | 6 ++++++ README.md | 4 ++++ comparator.py | 49 +++++++++++++++++++++++++++++++++++++------------ diler.py | 4 ++-- file.txt | 0 5 files changed, 49 insertions(+), 14 deletions(-) create mode 100644 .idea/vcs.xml create mode 100644 README.md create mode 100644 file.txt diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..48b3d47 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# DZcards +Были добавлены логины +Изначальное кол-во денег игрока = 1000 +В конце игры логин игрока и его деньги записываются в файл(file.txt) diff --git a/comparator.py b/comparator.py index bdd9668..7c1c8e1 100644 --- a/comparator.py +++ b/comparator.py @@ -1,10 +1,6 @@ from random import choice from cards import * -class Comparator: - def compare(self, clients, table): - return [choice(list(clients.keys()))] - class Flush: def __str__(self): return "Flush" @@ -82,16 +78,45 @@ def __str__(self): def check(cards): return Straight.check(cards) and Flush.check(cards) -def compare(cards): - for c in combinations: - if c.check(cards): - return c() - combinations = [StraightFlush, Quads, FullHouse, Flush, Straight, Set, TwoPairs, Pair, High] +class Comparator: + def combine(self, cards): + r = [] + for i in range(6): + for j in range(i + 1, 7): + r.append(cards[:i] + cards[i + 1: j] + cards[j + 1:]) + return r + + def compare(self, clients, table): + res = {} + for client_id, cards in clients: + res[max(map(max_combine, combine(cards + table)))].append(client_id) + ans = [] + for key in sorted(res): + ans.append(res[key]) + return ans + + def max_combine(cards): + for c in combinations: + if c.check(cards): + return c() + Suits = [Diamonds, Clubs, Hearts, Spades] -cards = [Card(2, Diamonds), Card(5, Diamonds), Card(5, Diamonds), Card(5, Diamonds), Card(2, Diamonds)] + +cards = [ + Card(2, Diamonds), + Card(5, Diamonds), + Card(9, Diamonds), + Card(11, Diamonds), + Card(0, Diamonds), + Card(2, Hearts), + Card(5, Hearts), + Card(5, Clubs), + Card(5, Clubs), + Card(2, Clubs) +] + #cards = [Card(i, Diamonds) for i in range(2, 7)] -print(compare(cards)) -print(' '.join(map(str, cards))) +print(*cards) diff --git a/diler.py b/diler.py index 84154e8..6a73f06 100644 --- a/diler.py +++ b/diler.py @@ -42,7 +42,7 @@ def __init__(self, id, conn): self.status = Diler.Client.NotReady() self.bet = 0 self.money = 1000 - self.pas = False + self.pass = False def __eq__(self, other): return self.id == other.id @@ -51,7 +51,7 @@ def addCard(self, card): self.cards.append(card) def ready(self, max_bet): - return self.money == 0 or self.bet >= max_bet or self.pas + return self.money == 0 or self.bet >= max_bet or self.pass def __init__(self, server): self.server = server diff --git a/file.txt b/file.txt new file mode 100644 index 0000000..e69de29 From c2a9d280ab728f7f9455bbcfbb17b1aec2dc3bc0 Mon Sep 17 00:00:00 2001 From: Daler Hamzaev Date: Wed, 18 Oct 2017 14:46:00 +0300 Subject: [PATCH 10/10] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE?= =?UTF-8?q?=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- diler.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/diler.py b/diler.py index 6a73f06..84154e8 100644 --- a/diler.py +++ b/diler.py @@ -42,7 +42,7 @@ def __init__(self, id, conn): self.status = Diler.Client.NotReady() self.bet = 0 self.money = 1000 - self.pass = False + self.pas = False def __eq__(self, other): return self.id == other.id @@ -51,7 +51,7 @@ def addCard(self, card): self.cards.append(card) def ready(self, max_bet): - return self.money == 0 or self.bet >= max_bet or self.pass + return self.money == 0 or self.bet >= max_bet or self.pas def __init__(self, server): self.server = server