-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
153 lines (134 loc) · 4.88 KB
/
Copy pathserver.py
File metadata and controls
153 lines (134 loc) · 4.88 KB
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
#this file is the server that handles the two players. should be run separately. change the server name to host online
import socket
import sys
from _thread import *
from card import Card
from player import Player
from state import GameState
import random
#creating a default socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print('we did it boys')
#default port
port, server = 1234, 'localhost'
serverIP = socket.gethostbyname(server)
try:
s.bind((server, port))
print(f'socket bound to port {port}')
except socket.error as e:
print(str(e))
s.listen(5)
print('we are listening')
currId = '0'
player0 = Player()
player1 = Player()
player0.currentPlayer = False
player1.currentPlayer = False
deck0Str = ''
deck1Str = ''
connectedIDs = []
#init the shared game
def startGame(player0, player1):
print('Starting Game')
coinFlip = random.randint(0,2)
if coinFlip == 1:
player1.currentPlayer = True
player1.firstTurn = False
player0.message = 'game started opponents turn'
player1.message = 'game started your turn'
else:
player0.currentPlayer = True
player0.firstTurn = False
player1.message = 'game started opponents turn'
player0.message = 'game started your turn'
random.shuffle(player0.deck)
random.shuffle(player1.deck)
for i in range(5):
player0.pickupCard()
player1.pickupCard()
player0.gameStarted = True
player1.gameStarted = True
def threadedClient(c):
global currId, player0, player1, connectedIDs
theID = currId
connectedIDs.append(currId)
if currId == '0':
currState = GameState(player0, player1)
c.send(str.encode(currId + '|||' + str(currState)))
currId = '1'
deck0Str = c.recv(4096).decode()
#print('DECK 0 RECIEVED')
#print('THIS IS THE DECK STRING:', deck0Str)
player0.buildDeckFromString(deck0Str)
player0.message = 'CONNECTED waiting for other player'
currState = GameState(player0, player1)
elif currId == '1':
currState = GameState(player1, player0)
c.send(str.encode(currId + '|||' + str(currState)))
deck1Str = c.recv(4096).decode()
#print('THIS IS THE DECK STRING:', deck1Str)
player1.buildDeckFromString(deck1Str)
#print('DECK 1 RECIEVED')
#print(deck1Str)
startGame(player0, player1)
reply = ''
running = True
while running:
try:
data = c.recv(4096)
reply = data.decode('utf-8')
if not data: #nothing was recieved or something broke
c.send(str.encode("Ended"))
running = False
break
else:
print('*************************************************')
print('Recieved ', reply)
print('*************************************************')
if reply[-1] != '*':
contents = reply.split('|||')
playerNum = contents[0] #gets the ID
activeP = contents[1] #gets that clients player 1
passiveP = contents[2] #gets that clients player 2
if playerNum == '0':
player0.buildPlayerFromString(activeP)
player1.buildPlayerFromString(passiveP)
stateToReturn = GameState(player0, player1)
else:
player1.buildPlayerFromString(activeP)
player0.buildPlayerFromString(passiveP)
stateToReturn = GameState(player1, player0)
else:
playerNum = reply[0]
if playerNum == '0':
stateToReturn = GameState(player0, player1)
else:
stateToReturn = GameState(player1, player0)
#build the state from the strings
sendBack = str(stateToReturn)
print('*************************************************')
print(sendBack)
print('*************************************************')
c.sendall(str.encode(sendBack))
print('sent reply')
except socket.error as e:
print(e)
break
print('connection finishied', theID)
#resets server when both parties disconect
connectedIDs.pop()
if len(connectedIDs) == 0:
print('RESET')
currId = '0'
player0 = Player()
player1 = Player()
player0.currentPlayer = False
player1.currentPlayer = False
deck0Str = ''
deck1Str = ''
finishedIDs = []
c.close()
while True:
c, addr = s.accept()
print(f'Got Connection from {addr}')
start_new_thread(threadedClient, (c,))