def __init__(self, control_port, user_port, private=False): self.__board = [] self.__players = [] self.__controllock = threading.Lock() self.__private = private self.__control = ControlSocket(control_port) self.__users = GameSocket(user_port) self.__users.setGame(self) self.__halted = False self.__timer = None self.__currentTurn = None
from GameSocket import * import random, os, time, sys PORT = 5564 addr = input("Please enter the server address: ") s = GameSocket() connected = False cnt = 0 while not connected: if cnt == 5: print("Something is wrong with this connection. Please try a different address.") addr = input("Address: ") cnt = 0 try: s.connect(addr.split(":")[0], port=int(addr.split(":")[1])) print("Waiting to be assigned a number") num = int(s.receive()) print("Received number!") if not num: print("There was an error with the connection.") print("Reconnecting...") continue elif num == 1: print("Getting direct connection") peer = GameSocket(is_server=True, port=PORT) data = peer.getName()[0] + ":" + str(peer.getName()[1]) s.send(data)
from GameSocket import * s=GameSocket(is_server=True) print("Address: "+s.getName()[0]+":"+str(s.getName()[1])) while True: try: s.acceptConnections(2) print("Making match: "+str(s.peers[0][1])+" "+str(s.peers[1][1])) #Assign player numbers s.send(1,peer=s.peers[0]) s.send(2,peer=s.peers[1]) #Create a direct connection data=s.receive(peer=s.peers[0]) s.send(data,peer=s.peers[1]) result=int(s.receive(peer=s.peers[1])) if result==1: print("Match made.") else: print("Clients failed to connect.") print("Terminating connections.") s.disconnect(peer=s.peers[0]) s.disconnect(peer=s.peers[1]) s.removePeers() except Exception as e: print("\n"+str(e)+"\n") print("Notifying active peers of the error.") for peer in s.peers: