File size: 1,888 Bytes
2dbfb72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from flows.base_flows import Flow
from typing import Dict

from .RockPaperScissorsPlayer import RockPaperScissorsPlayer


class RockPaperScissorsJudge(Flow):

    def __init__(self, **kwargs):
        super(RockPaperScissorsJudge, self).__init__(**kwargs)

        self.flow_state["A"] = RockPaperScissorsPlayer(name="Player A", description="RockPaperScissorsPlayer")
        self.flow_state["B"] = RockPaperScissorsPlayer(name="Player B", description="RockPaperScissorsPlayer")
        self.flow_state["A_score"] = 0
        self.flow_state["B_score"] = 0
        self.flow_state["n_party_played"] = 0

    def run(self, input_data, expected_outputs) -> Dict:
        flow_a = self.flow_state["A"]
        flow_b = self.flow_state["B"]

        for _ in range(3):
            A_task = self.package_task_message(flow_a, "run", {}, expected_outputs=["choice"])
            B_task = self.package_task_message(flow_b, "run", {}, expected_outputs=["choice"])
            # play another round
            A_output = flow_a(A_task)
            self._log_message(A_output)
            B_output = flow_b(B_task)
            self._log_message(B_output)

            A_choice = A_output.data["choice"]
            B_choice = B_output.data["choice"]

            self._update_state({"n_party_played": self.flow_state["n_party_played"] + 1})

            if A_choice == B_choice:
                # neither has won
                pass
            elif (A_choice == "rock" and B_choice == "scissors"
                  or A_choice == "paper" and B_choice == "rock"
                  or A_choice == "scissors" and B_choice == "paper"):
                self._update_state({"A_score": self.flow_state["A_score"] + 1})
            else:
                self._update_state({"B_score": self.flow_state["B_score"] + 1})

        return self._get_keys_from_state(expected_outputs, allow_class_namespace=False)