-
Notifications
You must be signed in to change notification settings - Fork 0
/
serverMonop.py
154 lines (134 loc) · 4.91 KB
/
serverMonop.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import os
import sys
from tkinter import *
import socket
import select
import threading
import queue
import datetime
import random
import time
class Serveur:
def __init__(self, q):
self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
self._sock.bind((socket.gethostbyname('127.0.0.1'), int(1233)))
self.nb_con = 2
self._sock.listen(self.nb_con)
self.pseudo = ""
self.logged = []
self.run = True
try:
while len(self.logged) <= self.nb_con:
client, adresse = self._sock.accept()
print(adresse, " connected\n")
self._t = threading.Thread(target=self.recv, args=(client,)).start()
self.logged.append(client)
client.send(('PSD ' + self.pseudo + '\n').encode())
except KeyboardInterrupt:
self._sock.close()
def recv(self, client):
print("threads actifs == ", threading.active_count())
pseud = ''
try:
while self.run:
msg = client.recv(1024).decode().strip()
if msg.startswith('PSD'):
pseud = msg.split(' ', 1)[1]
if not pseud in self.pseudo:
self.pseudo+=msg.split(' ', 1)[1]
print(self.pseudo)
self.pseudo+='/'
if len(self.logged) == self.nb_con:
self.inf(self.logged, (str(self.nb_con) + '/' + self.pseudo))
time.sleep(5)
de1 = random.randint(1,6)
de2 = random.randint(1,6)
self.plt(self.logged, str(de1+de2)+'/0/0/0/0/0/1500/1500/1500/1500/1500/1500/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/0/'+ str(de1) + '/' + str(de2) +'/0/0')
elif msg.startswith('PLT'):
list = []
datas = msg.split(' ',1)[1]
self.getplt(datas, list)
list[68] = random.randint(1,6)
list[69] = random.randint(1,6)
list[list[71]] += list[68]+list[69]
if list[list[71]] > 39:
list[list[71]] -= 40
print("check ", list[71])
self.sendplt(list)
elif msg.startswith('FIN'):
pseudo = msg.split(' ',1)[1]
self.fin(self.logged, pseudo)
else:
client.send('ERR\n'.encode())
except BrokenPipeError:
self.pseudo = ''
self.logged.remove(client)
client.close()
def getplt(self, datas, list):
data = datas.split('/')
for i in range(0, 72):
if i < 6:
list.append(int(data[i]))
elif 5 < i < 12:
list.append(int(data[i]))
elif 11 < i < 68:
list.append(int(data[i]))
elif i == 68:
list.append(int(data[i]))
elif i == 69:
list.append(int(data[i]))
elif i == 70:
list.append(int(data[i]))
elif i == 71:
list.append(int(data[i]))
def sendplt(self, list):
data = ""
for i in range(0, 72):
if i < 6:
data += str(list[i])
data += ('/')
elif 5 < i < 12:
data += str(list[i])
data += ('/')
elif 11 < i < 68:
data += str(list[i])
data += ('/')
elif i == 68:
data += str(list[i])
data += ('/')
elif i == 69:
data += str(list[i])
data += ('/')
elif i == 70:
data += str(list[i])
data += ('/')
elif i == 71:
data += str(list[i])
self.plt(self.logged, data)
def fin(self, clients, msg):
if clients:
for clien in clients:
clien.send(('FIN ' + msg + '\n').encode())
#clien.close()
def plt(self, clients, msg):
if clients:
for clien in clients:
clien.send(('PLT ' + msg + '\n').encode())
def inf(self, clients, msg):
if clients:
for clien in clients:
clien.send(('INF ' + msg + '\n').encode())
def msg(self, clients, msg, pseud):
for clien in clients:
clien.send(('MSG ' + pseud + ' ' + msg + '\n').encode())
def send(self, client):
while self.run:
print("Ecrivez un message")
msg = input()
client.send(msg.encode())
class App:
def __init__(self):
self._q = queue.Queue()
self._serveur = Serveur(self._q)
if __name__ == '__main__':
App()