Exemplo n.º 1
0
    def recv(self):
        res = {}
        # default values
        res["noise"] = 0
        res["beep"] = 0
        res["trapped"] = -1
        res["dead"] = False
        res["lost"] = False
        res["cap"] = -1
        res["ennemy"] = (-42, -42)

        data = np.recv_end(self.__s).strip()
        if data == "":
            return res
        lines = [_.split(" ") for _ in data.split("\n")]

        res["ennemy"] = (int(lines[0][0]), int(lines[0][1]))
        l = len(lines)
        i = 1

        while l > i:
            if lines[i][0] == np.NOISE_TXT:
                res["noise"] = int(lines[i][1])
            elif lines[i][0] == np.BEEP_TXT:
                res["beep"] = int(lines[i][1])
            elif lines[i][0] == np.TRAPPED_TXT:
                res["trapped"] = int(lines[i][1])
            elif lines[i][0] == np.DEAD_TXT:
                res["dead"] = True
            elif lines[i][0] == np.DEAD_TXT:
                res["lost"] = True
            elif lines[i][0] == np.CAPTURE_TXT:
                res["cap"] = int(lines[i][1])
            i += 1

        return res
Exemplo n.º 2
0
    def recv(self):
        res = {}
        # default values
        res["noise"] = 0
        res["beep"] = 0
        res["trapped"] = -1
        res["dead"] = False
        res["lost"] = False
        res["cap"] = -1
        res["ennemy"] = (-42, -42)

        data = np.recv_end(self.__s).strip()
        if data == "":
            return res
        lines = [_.split(" ") for _ in data.split("\n")]

        res["ennemy"] = (int(lines[0][0]), int(lines[0][1]))
        l = len(lines)
        i = 1

        while l > i:
            if lines[i][0] == np.NOISE_TXT:
                res["noise"] = int(lines[i][1])
            elif lines[i][0] == np.BEEP_TXT:
                res["beep"] = int(lines[i][1])
            elif lines[i][0] == np.TRAPPED_TXT:
                res["trapped"] = int(lines[i][1])
            elif lines[i][0] == np.DEAD_TXT:
                res["dead"] = True
            elif lines[i][0] == np.DEAD_TXT:
                res["lost"] = True
            elif lines[i][0] == np.CAPTURE_TXT:
                res["cap"] = int(lines[i][1])
            i += 1

        return res
Exemplo n.º 3
0
    def run(self, HOST, PORT):
        # Socket init:
        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.s.bind((HOST, PORT))
        self.s.listen(2)  # at max 2 connections
        # Waiting for 2 connections to us:
        self.s1, addr1 = self.s.accept()
        self.s2, addr2 = self.s.accept()

        # Init request:
        self.request = self.s1
        while True:
            # sleep(1) #@TODO remove that
            rep = ""
            try:
                self.data = np.recv_end(self.request)
                self.logger.info(self.data)
            except socket.error, e:
                self.logger.debug(str(e))
                break

            lines = [
                _.strip().split(" ") for _ in self.data.strip().split("\n")
            ]

            self.logger.info("lines:" + str(lines))

            if lines[0][0] == np.SPY_TXT:
                player = self.gs.spy
                ennemy = self.gs.merc
            else:
                player = self.gs.merc
                ennemy = self.gs.spy

            player.pos = (int(lines[1][0]), int(lines[1][1]))
            player.mousePos = (int(lines[2][0]), int(lines[2][1]))

            TRAPPED = ""
            DEAD = False
            if player == self.gs.spy:
                trapped = self.gs.trapped(player)
                if trapped != self.gs.TRAP_FREE:
                    TRAPPED = "\n" + np.TRAPPED_TXT + " " + str(trapped)
                    if trapped == self.gs.TRAP_MINED:
                        DEAD = True

            l = len(lines)
            i = 3
            while l > i:
                self.logger.info(lines[i][0])
                self.logger.info(np.ACTIVATE_TXT)
                self.logger.info(lines[i][0] == np.ACTIVATE_TXT)

                if lines[i][0] == np.SHOOT_TXT:
                    self.gs.shoot(player)
                elif lines[i][0] == np.OBJECT_TXT:
                    self.gs.drop(player, int(lines[i][1]))
                elif lines[i][0] == np.ACTIVATE_TXT:
                    self.gs.activate(player)
                elif lines[i][0] == np.RUN_TXT:
                    self.gs.run(player)
                else:
                    player.capping = False  # the player is not doing any of the previous ones, so it is obviously not capping (even in the following lines). THIS CONDITION HAS TO BE KEPT INE THE "ELSE" /!\
                i += 1

            try:
                if ennemy.pos is not None:  # if we've already instantiated the ennemy position
                    rep += str(ennemy.pos[0]) + " " + str(ennemy.pos[1])
                    rep += "\n" + np.BEEP_TXT + " " + str(
                        self.gs.beep_level(player))
                    rep += "\n" + np.NOISE_TXT + " " + str(
                        self.gs.noise_level(player, ennemy))
                else:
                    rep += "-42 -42"
                rep += TRAPPED
                if DEAD:
                    rep += "\n" + np.DEAD_TXT
                if player.capping:
                    rep += "\n" + np.CAPTURE_TXT + " " + int(
                        player.cap * 100 /
                        (self.gs.TIME_FREQ * self.gs.CAP_TIME))

                self.request.sendall(rep + np.MSG_END)
                self.logger.info("Data sent: " + str(rep))
            except Exception as e:
                self.logger.info("Socket error 3")
                self.logger.info(str(e))
                break

            if self.request == self.s1:
                self.request = self.s2
            else:
                self.request = self.s1
Exemplo n.º 4
0
    def run(self, HOST, PORT):
        # Socket init:
        self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self.s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
        self.s.bind((HOST, PORT))
        self.s.listen(2) # at max 2 connections
        # Waiting for 2 connections to us:
        self.s1, addr1 = self.s.accept()
        self.s2, addr2 = self.s.accept()
        
        # Init request:
        self.request = self.s1
        while True:
            # sleep(1) #@TODO remove that
            rep = ""
            try:
                self.data = np.recv_end(self.request)
                self.logger.info( self.data)
            except socket.error, e:
                self.logger.debug(str(e))
                break

            lines = [_.strip().split(" ") for _ in self.data.strip().split("\n")]

            self.logger.info( "lines:" + str(lines))

            if lines[0][0] == np.SPY_TXT:
                player = self.gs.spy
                ennemy = self.gs.merc
            else:
                player = self.gs.merc
                ennemy = self.gs.spy

            player.pos = (int(lines[1][0]), int(lines[1][1]))
            player.mousePos = (int(lines[2][0]), int(lines[2][1]))

            TRAPPED = ""
            DEAD = False
            if player == self.gs.spy:
                trapped = self.gs.trapped(player)
                if trapped != self.gs.TRAP_FREE:
                    TRAPPED = "\n" + np.TRAPPED_TXT + " " + str(trapped)
                    if trapped == self.gs.TRAP_MINED:
                        DEAD = True

            l = len(lines)
            i = 3
            while l > i:
                self.logger.info(lines[i][0])
                self.logger.info(np.ACTIVATE_TXT)
                self.logger.info(lines[i][0] == np.ACTIVATE_TXT)

                if lines[i][0] == np.SHOOT_TXT:
                    self.gs.shoot(player)
                elif lines[i][0] == np.OBJECT_TXT:
                    self.gs.drop(player, int(lines[i][1]))
                elif lines[i][0] == np.ACTIVATE_TXT:
                    self.gs.activate(player)
                elif lines[i][0] == np.RUN_TXT:
                    self.gs.run(player)
                else:
                    player.capping = False # the player is not doing any of the previous ones, so it is obviously not capping (even in the following lines). THIS CONDITION HAS TO BE KEPT INE THE "ELSE" /!\
                i += 1

            try:
                if ennemy.pos is not None: # if we've already instantiated the ennemy position
                    rep += str(ennemy.pos[0]) + " " + str(ennemy.pos[1])
                    rep += "\n" + np.BEEP_TXT + " " + str(self.gs.beep_level(player))
                    rep += "\n" + np.NOISE_TXT + " " + str(self.gs.noise_level(player, ennemy))
                else:
                    rep += "-42 -42"
                rep += TRAPPED
                if DEAD:
                    rep += "\n" + np.DEAD_TXT
                if player.capping:
                    rep += "\n" + np.CAPTURE_TXT + " " + int(player.cap * 100 / (self.gs.TIME_FREQ * self.gs.CAP_TIME))

                self.request.sendall(rep + np.MSG_END)
                self.logger.info("Data sent: " + str(rep))
            except Exception as e:
                self.logger.info("Socket error 3")
                self.logger.info(str(e))
                break

            if self.request == self.s1:
                self.request = self.s2
            else:
                self.request = self.s1