source: geniuswebcore/geniusweb/protocol/session/mopac/phase/DefaultPhase.py@ 100

Last change on this file since 100 was 100, checked in by ruud, 14 months ago

python installs also wheel to avoid error messages

File size: 3.2 KB
Line 
1from abc import abstractmethod
2
3from geniusweb.actions.Action import Action
4from geniusweb.actions.PartyId import PartyId
5from geniusweb.protocol.ProtocolException import ProtocolException
6from geniusweb.protocol.session.mopac.PartyStates import PartyStates
7from geniusweb.protocol.session.mopac.phase.Phase import Phase, PHASE_MINTIME, \
8 PHASE_MAXTIME
9from geniusweb.voting.VotingEvaluator import VotingEvaluator
10
11
12class DefaultPhase(Phase):
13 # deadline for this phase, ms since 1970
14
15 def __init__(self, partyStates:PartyStates, deadline:int,
16 evaluator:VotingEvaluator ):
17 '''
18 @param partyStates the {@link PartyStates}
19 @param deadline deadline for this phase, ms since 1970
20 @param evaluator the {@link VotingEvaluator} to be used
21 '''
22 self._partyStates = partyStates
23 self._deadline = deadline
24 self._evaluator = evaluator
25
26 def isFinal(self, now:int)->bool:
27 return now >= self._deadline or len(self._partyStates.getNotYetActed())==0
28
29 def getDeadline(self)->int:
30 return self._deadline
31
32 def _checkAction(self, actor:PartyId ,action: Action , now:int):
33 '''
34 Check if actor can do given action. Basic checks:
35 <ul>
36 <li>real actor does not match action's actor
37 <li>deadline for this phase has passed
38 <li>action is not allowed in this phase
39 <li>actor already acted in this phase
40 </ul>
41 @param actor the actor that really acted
42 @param action the action that was done
43 @param now current time
44 @throws ProtocolException if the action violates the protocol
45 '''
46 if action == None:
47 raise ProtocolException("Action=null", actor)
48 if actor != action.getActor():
49 raise ProtocolException(
50 "Incorrect actor info in action:" + str(action.getActor()),
51 actor)
52 if self.isFinal(now):
53 raise ProtocolException("passed deadline", actor)
54 if not type(action) in self.getAllowedActions():
55 raise ProtocolException("Action not allowed in "
56 + type(self).__name__ + ": " + str(action), actor)
57 if not actor in self._partyStates.getNotYetActed():
58 raise ProtocolException("Actor can not act anymore", actor)
59 return
60
61 def getPartyStates(self)->PartyStates :
62 '''
63 @return current PartyStates
64 '''
65 return self._partyStates
66
67 def getEvaluator(self)->VotingEvaluator :
68 return self._evaluator
69
70 def next(self, now:int, duration:int)->Phase :
71 if duration < PHASE_MINTIME or duration > PHASE_MAXTIME:
72 raise ValueError("Bug, illegal duration")
73 if not self.isFinal(now):
74 raise ValueError("phase is not final")
75 return self._checkedNext(now + duration)
76
77 @abstractmethod
78 def _checkedNext(self, dl:int)-> Phase:
79 '''
80 As {@link #next(long, long)} but DefaultPhase already checked that 1.
81 there is enough time for a next phase 2. current state is final 3.
82
83 @param dl the deadline for the next phase (ms since 1970).
84 @return the next phase
85 '''
86
87 def __repr__(self):
88 return type(self).__name__ + "[" + str(self._partyStates) + "," + \
89 str(self._deadline) + "," + str(self._evaluator) + "]"
90
91 def __hash__(self):
92 return hash((self._deadline, self._evaluator, self._partyStates))
93
94
95 def __eq__(self, other):
96 return isinstance(other, self.__class__) \
97 and self._partyStates==other._partyStates \
98 and self._deadline==other._deadline\
99 and self._evaluator==other._evaluator
100
Note: See TracBrowser for help on using the repository browser.