def hostServer(name): sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind((netHelper.getIP(), 4230)) global last_message while True: data, addr = sock.recvfrom(1024) last_message = str(data) + "from" + str(addr) print("received message: %s" % data)
def hostServer(name): srvr = socketserver.UDPServer((netHelper.getIP(), 4230), UDPHandler) srvr.serve_forever()
messages = [] def hostServer(name): print("Listener started.") global messages while (True): data, addr = sock.recvfrom(1024) if (str(data) == 'ping'): print("ping received") else: print(str(data)) if (len(messages) != 0): sock.sendto(messages[0].encode(), addr) messages.pop() def sendMessage(msg): messages.append(msg) sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) sock.bind((netHelper.getIP(), 4230)) print("Chat Client Starting") RCV_IP = input("Enter IP to connect to: ") listener = threading.Thread(target=hostServer, args=(1, ), daemon=True) listener.start() while True: MSG = input("MESSAGE: ") sendMessage(MSG)
def host(): print("ServerMode:") print("Hosting on " + netHelper.getIP()) listener = threading.Thread(target=hostServer, args=(1,), daemon=True) listener.start()
def rungame(stdscr): myPlayer = Player(1, 1, random.randint(0, 10000), curses.COLOR_RED) # own Player always FIRST Player, was btw ne blöde Idee ist GAME = Game([myPlayer]) curses.cbreak() stdscr.keypad(True) stdscr.nodelay(True) mainMenu = Menu.menu(["Singleplayer", "Multiplayer"], stdscr) if (mainMenu == Menu.Singleplayer): exit() elif (mainMenu == Menu.Multiplayer): mpMode = Menu.menu(["Host", "Join"], stdscr) if (mpMode == Menu.Host): Netzwerker.getHookedorListen(GAME, isClient=False, IP=netHelper.getIP(), PORT=30814) elif (mpMode == Menu.Join): myPlayer.x += 1 HOST_IP = TextInput.TextInput(stdscr, "Host IP", 15) Netzwerker.getHookedorListen(GAME, isClient=True, IP=HOST_IP, PORT=30814) levelIndex = Menu.menu(printLevels(), stdscr) room = readLevel(printLevels()[levelIndex][:-4]) stdscr.clear() curses.curs_set(0) stdscr.addstr(0, 0, "\n".join(room)) for player in GAME.Players: stdscr.addstr(player.x, player.y, player.rep) stdscr.refresh() stdscr.nodelay(True) while True: kp = stdscr.getch() # DEBUGgging stdscr.addstr(13, 1, str(time.time())) stdscr.addstr(0, 0, "\n".join(room)) for player in range(len(GAME.Players)): # yes this is ugly stdscr.addstr(GAME.Players[player].x, GAME.Players[player].y, "-") if (kp == 113 or kp == 3): exit() elif (kp == ord("d")): myPlayer.move(0, 1, room) myPlayer.rep = ">" elif (kp == ord("a")): myPlayer.move(0, -1, room) myPlayer.rep = "<" elif (kp == ord("w")): myPlayer.move(-1, 0, room) myPlayer.rep = "ʌ" elif (kp == ord("s")): myPlayer.move(1, 0, room) myPlayer.rep = "v" for player in range(len(GAME.Players)): # yes this is ugly stdscr.addstr( 10 + player, 1, str(GAME.Players[player].x) + "," + str(GAME.Players[player].y) + " " + GAME.Players[player].rep) stdscr.addstr(GAME.Players[player].x, GAME.Players[player].y, GAME.Players[player].rep) stdscr.refresh()