-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestBananaServer.py
121 lines (108 loc) · 4.18 KB
/
TestBananaServer.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
import socket
import threading
import random
from queue import Queue
HOST = "128.237.209.154" #current IP in GHC6115
PORT = 50003
BACKLOG = 4
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST,PORT))
server.listen(BACKLOG)
print("looking for connection")
class Pile(object):
def __init__(self):
pass
d = ({'A':13, 'B':3, 'C':3, 'D':6, 'E':18, 'F':3, 'G':4,'H':3, 'I':12,
'J':2, 'K':2, 'L':5, 'M':3, 'N':8, 'O':11, 'P':3, 'Q':2, 'R':9,
'S':6, 'T':9, 'U':6, 'V':3, 'W':3, 'X':2, 'Y':3, 'Z':2})
total = 140
@staticmethod
def peel():
while True:
n = random.randint(65,90)
letter = chr(n)
if Pile.d[letter] > 0:
Pile.d[letter] -= 1
break
print(letter +" was peeled!")
return letter
@staticmethod
def start():
letterList = []
for i in range(7):
letterList.append(Pile.peel())
letterList=",".join(letterList)
return letterList
@staticmethod
def exchange(letter):
letterList = []
for i in range(3):
letterList.append(Pile.peel())
d[letter]+=1
letterList=",".join(letterList)
return letterList
def handleClient(client, serverChannel, cID, clientele): #this adds the client ID and msg to Q
client.setblocking(1)
msg = ""
while True:
try:
msg += client.recv(10).decode("UTF-8") #creates message based on input
command = msg.split("\n") #splits message based on \n
while (len(command) > 1): #if we have a complete command
readyMsg = command[0] #the first part is rdy message to be run
msg = "\n".join(command[1:]) #the rest is shit that begins the next msg
serverChannel.put(str(cID) + "_" + readyMsg) #add client ID and rdy msg to Q
command = msg.split("\n") #go to next command (if there is one)
except: #if gets bad shit, remove client
clientele.pop(cID)
return
def serverThread(clientele, serverChannel): #processes shit on the Q
while True:
msg = serverChannel.get(True, None) #get first item on Q
print("msg recv: ", msg) #print the message rcvd!
senderID, msg = int(msg.split("_")[0]), "_".join(msg.split("_")[1:]) #separates id from msg
if (msg):
ind = (msg.split(":")[0])
if ind=="Peel":
print ("I should be peeling!")
ind+=":"
for cID in clientele: #for each client
if cID != senderID: #if client not the sender
txt = str(senderID) + " Peeled!" +":"
info = Pile.peel()
sendMsg = ind+txt+info+"\n" #create message to all other players!
if cID == senderID: #if client is sender
txt = "You Peeled!" +":"
info = Pile.peel()
sendMsg = ind+txt+info+"\n" #create message to player
clientele[cID].send(sendMsg.encode()) #encode and add it to dict
if ind == "Exchange":
print("Exchange a letter with three")
if Pile.total < 3:
print("Not enough letters to exchange")
clientele[senderID] = "Exchange falied!:Not enough letters to exchange:"
else:
repLetter = msg.split(":")[0]
ind += ":"
txt = "You exchanged " + letter + ":"
info = Pile.exchange(letter)
clientele[senderID] = ind + txt + info
serverChannel.task_done() #remove item from Q
clientele = {}
currID = 0
serverChannel = Queue(100) #initialize Q
threading.Thread(target = serverThread, args = (clientele, serverChannel)).start()
#start_new_thread(serverThread, (clientele, serverChannel))
while True: #loop for adding clients
client, address = server.accept()
print(currID) #curr client ID
for cID in clientele: #tell all other peoples that there is a new player!
print (repr(cID), repr(currID))
clientele[cID].send(("newPlayer %d \n" % currID).encode()) #send new player info!
client.send(("newPlayer %d \n" % cID).encode()) #tell the new player about all the old players
clientele[currID] = client #dont really understand this line
print("connection received")
threading.Thread(target = handleClient, args = #create a new thread for this new client
(client ,serverChannel, currID, clientele)).start()
#start_new_thread(handleClient, (client,serverChannel, currID))
currID += 1