Exemple #1
0
    def Divine(self, seer, player):
        if not player:
            LogUtility.Information(f"Seer '{seer.Name}' waits.", self)
            return VoteResultTypeEnum.WaitAction

        LogUtility.Information(f"Seer '{seer.Name}' divines '{player.Name}'.",
                               self)
        return VoteResultTypeEnum.SuccessfulAction
Exemple #2
0
    def Guard(self, guard, player):
        if not player:
            LogUtility.Information(f"Guard '{guard.Name}' waits.", self)
            return VoteResultTypeEnum.WaitAction

        guard.Role._Guard__canGuardTimes -= 1
        LogUtility.Information(f"Guard '{guard.Name}' guards '{player.Name}'.",
                               self)
        return VoteResultTypeEnum.SuccessfulAction
Exemple #3
0
    def Attack(self, werewolf, player):
        if not player:
            # Should werewolves be able to wait? Is this even an actual use case?
            LogUtility.Information(f"Werewolf '{werewolf.Name}' waits.", self)
            return VoteResultTypeEnum.WaitAction

        LogUtility.Information(
            f"Werewolf '{werewolf.Name}' attacks '{player.Name}'.", self)

        if player.Role.Type == PlayerTypeEnum.Werewolf:
            return VoteResultTypeEnum.WerewolfCannibalism

        return VoteResultTypeEnum.SuccessfulAction
Exemple #4
0
def DistributeRolesBaseGame(game):
    players = game.Players;
    playerCount = len(players);

    numberOfWerewolves = GetPlayerCountForRole(playerCount, GameConstants.PLAYERS_PER_WEREWOLF);
    numberOfSeers = GetPlayerCountForRole(playerCount, GameConstants.PLAYERS_PER_SEER);
    numberOfGuards = GetPlayerCountForRole(playerCount, GameConstants.PLAYERS_PER_GUARD);

    numberOfVillagers = playerCount\
        - numberOfWerewolves\
        - numberOfSeers\
        - numberOfGuards;

    if (numberOfVillagers < 0):
        LogUtility.Error("Cannot have a game with less than 0 villagers.", game);

    if (numberOfVillagers == 0):
        LogUtility.Warning("Game has 0 villagers.", game);

    rolesBag = sum([\
        numberOfWerewolves * [Werewolf()],\
        numberOfSeers * [Seer()],\
        numberOfGuards * [Guard()],\
        numberOfVillagers * [Villager()]], []);

    for player in players:
        roleType = random.choice(rolesBag);
        rolesBag.remove(roleType);

        player._Player__role = roleType;

        LogUtility.Information(f"'{player.Name}' is a {player.Role.Type}.", game);

    return;
Exemple #5
0
    def Connect(self, connection, packet):
        dto = packet.Data;

        player = Player(dto.ClientName, dto.ClientIdentifier);

        connectionKey = connection.getpeername();
        self.Connections[connectionKey] = player;

        LogUtility.Information(f"Connected to server (player {player}) - {connectionKey}");
        self.ShowActiveConnections();

        connection.sendall(pickle.dumps(player));

        return;
Exemple #6
0
    def __init__(self):
        self.__connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
        self.__handlerContext = HandlerContext(self);
        self.__validPacketTypes = PacketTypeEnum.Values();
        self.__connections = dict();
        self.__games = dict();

        self.__handlerContext.CreateGame("Game Alpha");
        self.__handlerContext.CreateGame("Game Beta");
        self.__handlerContext.CreateGame("Game Gamma");
        self.__handlerContext.CreateGame("Game Delta");

        LogUtility.Information("Server start up");

        return;
Exemple #7
0
    def Run(self):
        try:
            self.__connection.bind(NetConstants.ADDRESS);
            self.__connection.listen();

            LogUtility.Information(f"Server successfully started at {NetConstants.IP}:{NetConstants.PORT}");

            self.ShowActiveConnections();
        except socket.error as error:
            trace = traceback.format_exc();
            LogUtility.Error(str(error) + "\n\n" + trace);

        while True:
            connection, address = self.__connection.accept();
            threading.Thread(target = self.ClientHandle, args = (connection, address)).start();
        return;
Exemple #8
0
    def Disconnect(self, connection):
        connectionKey = connection.getpeername();
        player = None;

        if connectionKey in self.Connections.keys():
            player = self.Connections[connectionKey];
            gameIdentifier = self.HandlerContext.IsPlayerAlreadyInAGame(player);

            if gameIdentifier:
                game = self.HandlerContext.GetGameWithIdentifier(gameIdentifier);
                game.Leave(player);

            self.Connections.pop(connectionKey);

        LogUtility.Information(f"Lost connection (player {player}) to server - {connectionKey}");
        connection.shutdown(socket.SHUT_RDWR);
        connection.close();

        self.ShowActiveConnections();

        return;
Exemple #9
0
 def ShowActiveConnections(self):
     LogUtility.Information(f"Active connections - {len(self.__connections)}");