-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
119 lines (91 loc) · 3.67 KB
/
server.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
import socket
import sys
import threading
import player
import pickle
class Server:
def __init__(self):
self.threads = []
self.ip = socket.gethostbyname(socket.gethostname())
self.port = 12345
self.sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
self.sock.bind((self.ip, self.port))
self.addr = []
self.connected = False
self.serverDone = False
self.clientDone = False
print("Seu IP: " + self.ip + " e porta: " + str(self.port))
def sendMessage(self, m):
self.sock.sendto(pickle.dumps(m), (self.addr[0], self.addr[1]))
def waitForClient(self):
data = ''
while len(self.addr) == 0 or 'done' not in data:
data, self.addr = self.sock.recvfrom(4096)
data = pickle.loads(data)
if data != '':
dataList = data
data = dataList[0]
self.enemyField = dataList[1]
self.enemyName = dataList[2]
self.clientDone = True
def connect(self):
while len(self.addr) == 0 or data != 'connect':
data, self.addr = self.sock.recvfrom(1024)
data = pickle.loads(data)
print(str(self.addr[0]) + ' conectado')
self.connected = True
self.sendMessage('conectado')
thread = threading.Thread(target=self.waitForClient)
thread.start()
self.threads.append(thread)
import game
g = game.Game()
g.p1 = g.newPlayer(1, g.ships[:], g.p1Field, g.p1BombField)
self.done = True
g.clear()
thread.join()
if self.clientDone == False:
print('Esperando...')
self.waitForClient()
print ('Cliente')
g.p2Field.field = self.enemyField
g.p2 = player.Player(self.enemyName, g.ships[:], g.p2Field, g.p2BombField)
while g.anythingLeft(g.p1.field.field) and g.anythingLeft(g.p2.field.field):
g.clear()
print('Seu campo:\n')
print(g.printfield(g.p1.field.field))
print ('\nCampo do inimigo:\n')
print(g.printfield(g.p1.bombfield.field))
cell = g.selectCell(g.p1)
g.bomb(g.p1, g.p2, cell[0], cell[1])
g.clear()
if g.result == 'X':
print ('Acertou, mizeravi!')
elif g.result == 'O':
print ('Erroooooou!')
else:
print(g.result)
self.sendMessage(['resultado', g.result])
sys.exit()
print('Seu campo:\n')
print(g.printfield(g.p1.field.field))
print('\nCampo do inimigo :\n')
print(g.printfield(g.p1.bombfield.field))
if g.anythingLeft(g.p1.field.field) and g.anythingLeft(g.p2.field.field):
self.sendMessage(['selectCell', g.p2.field.field])
data = ''
print('Esperando...')
while len(self.addr) == 0 or 'cell' not in data:
data, self.addr = self.sock.recvfrom(2048)
data=pickle.loads(data)
if data != '':
dataList = data
data = dataList[0]
cell = dataList[1]
g.bomb(g.p2, g.p1, cell[0], cell[1])
if g.result == 'X' or g.result == 'O':
self.sendMessage(['resultado', g.result])
else:
print (g.result)
self.sendMessage(['resultado', g.result])
sys.exit()