Exemplo n.º 1
0
 def generateEvent(message):
     messageParts = message.split(":", 1)
     event = EventFactory.createEvent(messageParts[0])
     if event != None:
         event.fillArguments(messageParts[1])
         queueController = Singleton(QueueController)
         queueController.addIngoingEvent(event)
Exemplo n.º 2
0
    def _joinGame(self, yourId):
        self._yourId = yourId
        print("JoinGameEvent: {}".format(self._yourId))

        event = AddPlayerEvent(self._teamName, self._characterNames)
        queueController = Singleton(QueueController)
        queueController.outEvents.put(event)
Exemplo n.º 3
0
 def execute(self):
     world = Singleton(World)
     world._endGame()
     
Exemplo n.º 4
0
class MyAI(AIDefault):
    world = Singleton(World)
    otherTeam = None
    oponent1 = None
    oponent2 = None
    tank1 = None
    tank2 = None
    oponentLastPosition1 = None
    oponentLastPosition2 = None
    firstTick = True

    def setNames(self):
        teamName = "Felix team"
        characterNames = ["Batman", "Robin"]
        self.world._setNames(teamName, characterNames)

    def initWorld(self):
        self.tank1 = self.world.getMyTeam().getFirstCharacter()
        self.tank2 = self.world.getMyTeam().getSecondCharacter()
        ':type tank1: Character'
        self.otherTeam = self.world.getOpponentTeam()
        ':type otherTeam: Team'
        self.oponent1 = self.otherTeam.getFirstCharacter()
        self.oponent2 = self.otherTeam.getSecondCharacter()
        ':type oponent: Character'
        self.firstTick = False

    def tick(self):
        if self.firstTick:
            self.initWorld()

        if self.isAttackable(
                self.tank1.getPosition(), self.oponent1.getPosition(
                )) and self.oponent1.isAlive() and self.tank1.isAlive():
            targetDirection = MathUtils.getDirectionFromPositions(
                self.tank1.getPosition(), self.oponent1.getPosition())
            self.tank1.shootMissile(targetDirection)
        elif self.oponent1.getPosition() != self.oponentLastPosition1:
            self.tank1.goTo(self.oponent1.getPosition())
            self.oponentLastPosition1 = copy.deepcopy(
                self.oponent1.getPosition())

        if self.isAttackable(
                self.tank2.getPosition(), self.oponent2.getPosition(
                )) and self.oponent2.isAlive() and self.tank2.isAlive():
            targetDirection = MathUtils.getDirectionFromPositions(
                self.tank2.getPosition(), self.oponent2.getPosition())
            self.tank2.shootMissile(targetDirection)
        elif self.oponent2.getPosition() != self.oponentLastPosition2:
            self.tank2.goTo(self.oponent2.getPosition())
            self.oponentLastPosition2 = copy.deepcopy(
                self.oponent2.getPosition())

    def isAttackable(self, fromposition: Vector2, toposition: Vector2):
        ret = fromposition.x == toposition.x or fromposition.y == toposition.y
        if ret is True:
            objects = self.world.whatIsInTheWay(
                fromposition,
                MathUtils.getDirectionVector(fromposition, toposition))
            ret = len(objects) == 0
        return ret
Exemplo n.º 5
0
class AIDefault(object):
    '''
    Represents the AI that need to be implemented
    '''

    world = Singleton(World)
    '''
    The world singleton needed to get the info on the current state of the world
       (see :class:`.World`)
    '''

    aiStatus = AIStatus.INIT
    '''
    The current status of the AI example
    (Can be deleted)
    '''

    position1 = Vector2(0, 0)
    position2 = Vector2(7, 0)
    position3 = Vector2(0, 7)
    position4 = Vector2(7, 7)

    def setNames(self):
        '''
        Function call to set the name of the team and characters
        (Cannot be deleted)
        '''
        teamName = "Team python"
        characterNames = ["Python character1", "Python character2"]
        self.world._setNames(teamName, characterNames)

    def tick(self):
        '''
        Function call every 30 ms, this is the starting point for the AI
        (Cannot be deleted)
        '''
        if self.aiStatus == AIStatus.INIT:
            self.initState()
        elif self.aiStatus == AIStatus.LOWER_RIGHT:
            self.lowerRight()
        elif self.aiStatus == AIStatus.LOWER_LEFT:
            self.lowerLeft()
        elif self.aiStatus == AIStatus.UPPER_RIGHT:
            self.upperRight()
        elif self.aiStatus == AIStatus.UPPER_LEFT:
            self.upperLeft()

    def initState(self):
        '''
        Function call for the init state of the AI example
        (Can be deleted)
        '''
        character1 = self.world.getMyTeam().getFirstCharacter()
        character2 = self.world.getMyTeam().getSecondCharacter()

        character1.goTo(self.position2)
        character2.goTo(self.position3)

        character1.shootMissile(Direction.RIGHT)
        character2.shootMissile(Direction.LEFT)

        self.aiStatus = AIStatus.LOWER_RIGHT

    def lowerRight(self):
        '''
        Function call for the lowerRight state of the AI example
        (Can be deleted)
        '''
        character1 = self.world.getMyTeam().getFirstCharacter()
        character2 = self.world.getMyTeam().getSecondCharacter()

        if character1.position == self.position2:
            character1.goTo(self.position1)
            character2.goTo(self.position4)

            character1.dropMine()
            character2.dropMine()

            character1.shootMissile(Direction.LEFT)
            character2.shootMissile(Direction.RIGHT)

            self.aiStatus = AIStatus.LOWER_LEFT

    def lowerLeft(self):
        '''
        Function call for the lowerLeft state of the AI example
        (Can be deleted)
        '''
        character1 = self.world.getMyTeam().getFirstCharacter()
        character2 = self.world.getMyTeam().getSecondCharacter()

        if character1.position == self.position1:
            character1.goTo(self.position4)
            character2.goTo(self.position1)

            character1.shootMissile(Direction.UP)
            character2.shootMissile(Direction.DOWN)

            character1.dropMine()
            character2.dropMine()

            self.aiStatus = AIStatus.UPPER_RIGHT

    def upperRight(self):
        '''
        Function call for the upperRight state of the AI example
        (Can be deleted)
        '''
        character1 = self.world.getMyTeam().getFirstCharacter()
        character2 = self.world.getMyTeam().getSecondCharacter()

        if character1.position == self.position4:
            character1.goTo(self.position3)
            character2.goTo(self.position2)

            character1.shootMissile(Direction.DOWN)
            character2.shootMissile(Direction.UP)

            character1.dropMine()
            character2.dropMine()

            self.aiStatus = AIStatus.UPPER_LEFT

    def upperLeft(self):
        '''
        Function call for the upperLeft state of the AI example
        (Can be deleted)
        '''
        character1 = self.world.getMyTeam().getFirstCharacter()
        character2 = self.world.getMyTeam().getSecondCharacter()

        if character1.position == self.position3:
            character1.goTo(self.position2)
            character2.goTo(self.position3)

            character1.shootMissile(Direction.RIGHT)
            character2.shootMissile(Direction.LEFT)

            character1.dropMine()
            character2.dropMine()

            self.aiStatus = AIStatus.LOWER_RIGHT
Exemplo n.º 6
0
 def executeIngoingEvents(self):
     queueController = Singleton(QueueController)
     while not queueController.inEvents.empty():
         event = queueController.inEvents.get()
         event.execute()
Exemplo n.º 7
0
 def execute(self):
     world = Singleton(World)
     world._joinGame(self.yourId)
Exemplo n.º 8
0
 def execute(self):
     world = Singleton(World)
     world.getTeam(
         self.hitTeamId).characters[self.hitCharacterId]._hitByMine()
     world.getTeam(
         self.originTeamId).characters[self.originCharacterId]._mineHit()
Exemplo n.º 9
0
 def execute(self):
     world = Singleton(World)
     world.getTeam(self.originTeamId).characters[self.originCharacterId]._missileHit()
     if(self.hitEntity == 1):
         world.getTeam(self.hitTeamId).characters[self.hitCharacterId]._hitByMissile()
     elif(self.hitEntity == 2):
         world.getTeam(self.hitTeamId).characters[self.hitCharacterId]._mineHit()
     elif(self.hitEntity == 3):
         world.getTeam(self.hitTeamId).characters[self.hitCharacterId]._missileHit()
     
     if(self.backfire == 1):
         world.getTeam(self.originTeamId).characters[self.originCharacterId]._hitByMissile()
Exemplo n.º 10
0
'''
Created on Dec 17, 2014

@author: scarriere
'''

import time
from event.EventController import EventController
from network.NetworkController import NetworkController
from aiclient.Singleton import Singleton
from world.World import World
from aiclient.AI_Felix import MyAI

eventController = Singleton(EventController)
world = Singleton(World)
netController = Singleton(NetworkController)

ai = MyAI()
'''
If your create a new class for the ai, replace it here
'''


class Client(object):
    '''
    Main class (Any changes made to this class won't be taken)
    '''
    @staticmethod
    def main():
        '''
        Main loop
Exemplo n.º 11
0
 def execute(self):
     world = Singleton(World)
     world._updateBox(self.x, self.y)
Exemplo n.º 12
0
 def execute(self):
     world = Singleton(World)
     world._startGame()
Exemplo n.º 13
0
 def execute(self):
     world = Singleton(World)
     world.getTeam(
         self.teamId).characters[self.characterId]._updatePosition(
             self.position)
Exemplo n.º 14
0
class NetworkController(object):
    _instance = None
    HOST, PORT = "localhost", 1337
    webSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    readerThread = None
    connected = False
    queueController = Singleton(QueueController)

    def __init__(self):
        self.readerThread = threading.Thread(target=self.readFunctionThread)
        try:
            self.webSocket.connect((self.HOST, self.PORT))
            self.connected = True
        except:
            print("Error the GameClient is not started")

    def sendMessage(self, message):
        try:
            self.webSocket.sendall(message.encode())
        except:
            print("Error while sending to the socket")

    def readMessage(self):
        try:
            message = self.webSocket.recv(1024)
            return message
        except:
            print("Error while reading from the socket")
            return "".encode()

    def init(self):
        self.sendMessage("AIClientReady\n")
        self.readerThread.start()

    def executeOutgoingEvents(self):
        outQueue = self.queueController.getOutQueue()
        while not outQueue.empty():
            event = outQueue.get()
            self.sendMessage(event.toString() + "\n")

    def readFunctionThread(self):
        mustExit = False
        while not mustExit:
            message = self.readMessage().decode()

            if message == "Net:OkForExit\n" or message == "":
                mustExit = True
                self.connected = False
                break

            self.dispatchMessage(message)

    def dispatchMessage(self, message):
        messageParts = message.split(":", 1)
        if messageParts[0] == "Net":
            self.parseNetMessage(messageParts[1])
        elif messageParts[0] == "Game":
            EventFactory.generateEvent(messageParts[1])

    def parseNetMessage(self, message):
        if message == "JoinGameFailed":
            self.connectionRetry()
        elif message == "ErrorGameClientDisconnect":
            print("Error : the game client was disconnected")
        else:
            print("unknow net message:" + message)

    def closeConnection(self):
        self.sendMessage("Exit")
        self.readerThread.join()
        self.webSocket.close()
        self.connected = False

    def connectionRetry(self):
        print("Error : the game client was not connected")
        print("Info : connection retry in few seconds...")
        time.sleep(2)
        self.sendMessage("AIClientReady\n")
Exemplo n.º 15
0
 def execute(self):
     world = Singleton(World)
     world._sendGameInfos(self.mapWidth, self.mapHeight, self.numberOfTeam,
                          self.numberOfCharacter, self.teamsIds)
Exemplo n.º 16
0
class Character(object):
    '''
    Class that contain all the informations about the Character
        (You can't use any of the functions or variables that start with an "_")
    '''
    _queueController = Singleton(QueueController)

    def __init__(self, characterId, position):
        self._characterId = characterId
        self.position = Vector2(position.x, position.y)
        '''
        The current position of the character
            (see :class:`.Vector2`)
        '''
        self.mineReady = True
        '''
        Is the mine ready to be use
            (see :class:`.bool`)
        '''
        self.missile = Missile(Vector2(0, 0))
        '''
        The missile of the character
            (see :class:`.Missile`)
        '''
        self.life = 3
        '''
        The life total of the character
            (see :class:`.int`)
        '''

    def _updatePosition(self, position):
        self.position.x = position.x
        self.position.y = position.y

    def _hitByMine(self):
        if self.isAlive():
            self.life -= 1

    def _mineHit(self):
        self.mineReady = True

    def _hitByMissile(self):
        if self.isAlive():
            self.life -= 1

    def _missileHit(self):
        self.missile.isReady = True

    def goTo(self, position):
        '''
        Send the order to move the character
        to a certain position(:class:`.Vector2`)
            
        Exemple::
        
            aCharacter.goTo(Vector2(6,7))
        '''
        event = MoveCharacterEvent(self._characterId, position.x, position.y)
        self._queueController.outEvents.put(event)

    def dropMine(self):
        '''
        Send the order to drop a mine on the current position(:class:`.Vector2`)
            if the mine is ready
            
        Exemple::
        
            aCharacter.dropMine()
        '''
        if (self.mineReady):
            event = DropMineEvent(self._characterId)
            self._queueController.addOutgoingEvent(event)
            self.mineReady = False

    def shootMissile(self, direction):
        '''
        Send the order to shoot a missile on a certain direction(:class:`.Direction`)
            if the missile is ready
            
        Exemple::
        
            aCharacter.shootMissile(Direction.UP)
        '''
        if (self.missile.isReady):
            event = ThrowMissileEvent(self._characterId, direction)
            self._queueController.addOutgoingEvent(event)
            self.missile.isReady = False

    def isAlive(self):
        '''
        Check if the character still have some life point
            
        Exemple::
        
            checkIfAlive = aCharacter.isAlive()
        '''
        return self.life > 0

    def getPosition(self):
        '''
        Return the position of the character
            
        Exemple::

            position = aCharacter.getPosition()
        '''
        return self.position