Ejemplo n.º 1
0
    def test_valid_command(self):
        '''This test ensures that a valid command is correctly checked'''

        deck = Deck(1)
        player_1 = Player(0)
        player_2 = Player(1)

        players = [player_1]
        layouts = Layouts(1)

        deck.shuffle()
        deck.deal(players)

        seven_suits = [c.suit for c in player_1.hand if c.rank == 7]
        test_suit = seven_suits[0]
        card_cmd_str = "7" + test_suit

        layout_dict = {
            "C": "L0",
            "D": "L1",
            "H": "L2",
            "S": "L3"
        }

        layout_cmd_str = layout_dict[test_suit]
        cmd_str = card_cmd_str + layout_cmd_str

        cmd = Command(cmd_str, layouts)
        self.assertTrue(cmd.is_valid(player_1.hand))
Ejemplo n.º 2
0
 def __init__(self, js=None):
     self.queue=[]
     if js:
         queue=js
         for cmd in queue:
             self.queue.append(Command.from_js(cmd))
     self._lock=Lock()
Ejemplo n.º 3
0
 def from_json(js):
     t = Task(js["clients"], Command.from_js(js["cmd"]))
     t._id = js["id"]
     t._start_time = js["start_time"]
     t._interval = js["interval"]
     t._repeat = js["repeat"]
     t._done = js["done"]
     t._task_type = js["type"]
     t._cmd_ttd = js["cmd_ttd"]
     return t
Ejemplo n.º 4
0
    def request_command(self, curr_layouts):
        '''Prompts user for input when player's turn and validates the input'''

        self.current_command = Command.get_user_command(curr_layouts)

        # Check that command is valid
        if not self.current_command.is_valid(self.hand):
            self.request_command(curr_layouts)

        return self.current_command
Ejemplo n.º 5
0
 def run_command(self, command_name, *args, **kwargs):
     """
     method to trigger command call
     :param command_name:
     :param args:
     :param kwargs:
     :return:
     """
     cmd = Command(command_name, self, *args, **kwargs)
     self.command_q.put(cmd)
Ejemplo n.º 6
0
    def test_round_end(self):
        '''This test check that a winner is found'''

        test_input = {
            'decks': 1,
            'rounds': 2,
            'players': 1,
            'human players': 1,
            'comp_levels': {}
        }

        state = GameState(test_input)
        state.start_new_round()

        player_obj = state.players.get_player_by_id(state.current_player)
        player_obj.hand = Deck(1).cards

        state.print_round_state_to_cli()

        test_cmd_strs_1 = [
            str(i) + s + "L" + str(suit_layout_dict[s])
            for s in suit_layout_dict.keys() for i in range(7, 15)
        ]

        test_cmd_strs_2 = [
            str(i) + s + "L" + str(suit_layout_dict[s])
            for s in suit_layout_dict.keys() for i in range(2, 7)
        ]

        test_cmd_strs_2.reverse()
        test_cmd_strs = test_cmd_strs_1 + test_cmd_strs_2
        test_commands = [Command(c, state.layouts) for c in test_cmd_strs]

        player_obj = state.players.get_player_by_id(state.current_player)

        while not state.check_round_winner():
            for test_command in test_commands:
                state.current_command = test_command
                state.update()
                state.print_round_state_to_cli()

        state.start_new_round()

        self.assertEqual(state.round_number, 2)
        self.assertEqual(state.dealer_id, 0)

        while not state.check_round_winner():
            for test_command in test_commands:
                state.current_command = test_command
                state.update()
                state.print_round_state_to_cli()

        print("Total rounds: ", state.total_rounds)
        print("round_number: ", state.round_number)
        self.assertTrue(state.check_game_end())
Ejemplo n.º 7
0
def cmd_sched(server: AppServer, args):
    app = server._clients
    clients = server._clients._clients
    if args[0].lower() == "add":
        tt = time.time()
        arr, args = parse_args(
            args[1:], {
                "clients": (["-c", "--client"], None),
                "interval": (["-i", "--interval"], -1),
                "start": (["-s", "--start"], time.time()),
                "repeat": (["-r", "--repeat"], -1)
            })
        print("-------->", (time.time() - tt) * 1000)
        c = args["clients"]
        if not c:
            c = []
            for k in clients:
                c.append(k)

        t = Task.interval(c, Command.from_args(arr[0], arr[1:]),
                          float(args["start"]), float(args["interval"]),
                          float(args["repeat"]))
        server._clients.get_scheduler().add_task(t)
    return CommandReturn(0, "")
Ejemplo n.º 8
0
 def _process_packet(self, pkt):
     cmd = self.parser.parse(pkt)
     if cmd != None:
         print("[Sniffer] Received encrypted command: " + cmd)
         self.queue.enqueue(Command(cmd))
Ejemplo n.º 9
0
    def test_card_ten(self):
        '''This test check that a card can be played with a double digit rank'''

        test_input = {
            'decks': 1,
            'rounds': 1,
            'players': 1,
            'human players': 1,
            'comp_levels': {}
        }

        state = GameState(test_input)

        state.start_new_round()

        player_obj = state.players.get_player_by_id(state.current_player)

        player_obj.hand = [c for c in Deck(1)]

        state.print_round_state_to_cli()

        test_commands = []

        test_commands.append(Command("7CL0", state.layouts))
        test_commands.append(Command("8CL0", state.layouts))
        test_commands.append(Command("9CL0", state.layouts))
        test_commands.append(Command("10CL0", state.layouts))
        test_commands.append(Command("JCL0", state.layouts))
        test_commands.append(Command("QCL0", state.layouts))
        test_commands.append(Command("KCL0", state.layouts))
        test_commands.append(Command("ACL0", state.layouts))
        test_commands.append(Command("6CL0", state.layouts))
        test_commands.append(Command("5CL0", state.layouts))
        test_commands.append(Command("4CL0", state.layouts))
        test_commands.append(Command("3CL0", state.layouts))
        test_commands.append(Command("2CL0", state.layouts))

        for test_command in test_commands:
            state.current_command = test_command
            state.update()

        state.print_round_state_to_cli()

        extra_command = Command("7HL2", state.layouts)
        state.current_command = extra_command
        state.update()
        state.print_round_state_to_cli()
Ejemplo n.º 10
0
 def admin_on_command(self, req: HTTPRequest, res: HTTPResponse):
     if not self.is_authorized(req, res): return
     data = req.body_json()
     cmd = Command.from_js(data["cmd"])
     self._on_command(cmd, req, res)
Ejemplo n.º 11
0
 def admin_on_server_command(self, req: HTTPRequest, res: HTTPResponse):
     if not self.is_authorized(req, res): return
     data = req.body_json()
     cmd = Command.from_text(data["cmd"])
     out = cmd.start(self).json()
     res.ok(errors.OK, "OK", out)
Ejemplo n.º 12
0
 def execCommandFromLine(self, line):
     return Command.fromText(line).start(self)
Ejemplo n.º 13
0
 def execCommand(self, cmd):
     return Command(cmd).start(self)
Ejemplo n.º 14
0
 def get_computer_command(self):
     '''Calculates computer command.'''
     self.current_command = Command.get_computer_command(
         self.calculate_move(), self.current_layouts)