Skip to content

Commit 48a1605

Browse files
committed
Added some game state function
1 parent 2c88f4c commit 48a1605

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

backend/GameState.py

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@ def __init__(self, room_id, session):
2323
self._president = ""
2424
self._chancellor = ""
2525
self._votes = dict()
26+
self._policy_cards = []
2627
self._game_in_progress = False
2728
self._pres_cycle = None
29+
self._temp_chancellor = ""
2830

2931
def join(self,user_id, username):
3032
if(user_id in self._users or len(self._users)>=6 or self._game_in_progress):
@@ -53,7 +55,7 @@ def emit_roles():
5355
geese_json = json.dumps({"geese" : geese_names, "mrgoose" : mrgoose_name})
5456
self._session.emit_to_users(self._geese, GEESE_ROLE_INFORMATION, geese_json)
5557

56-
for i in [uid for uid in users is uid not in self._geese]:
58+
for i in [uid for uid in users if uid not in self._geese]:
5759
self._session.emit_to_user(uid, USER_ROLE_INFORMATION, json.dumps({"role" : "student"}))
5860

5961
def start_game():
@@ -70,14 +72,24 @@ def start_game():
7072
next_pres()
7173

7274
def draw(n=3):
73-
cards = []
75+
self._policy_cards = []
7476
for i in range(n):
7577
if(len(self._deck)==0):
7678
self._deck = self._discard_pile
79+
self._discard_pile = []
7780
random.shuffle(self._deck)
78-
cards.append(self._deck.pop())
79-
return cards
81+
self._policy_cards.append(self._deck.pop())
82+
self._session.emit_to_user(self._president, PRESIDENT_POLICIES_OPTIONS, json.dumps({"cards" : self._policy_cards}))
8083

84+
def president_discard(idx):
85+
discard(self._policy_cards[idx])
86+
del self._policy_cards[idx]
87+
88+
def chancellor_play(idx):
89+
discard(self._policy_cards[1 - idx])
90+
del self._policy_cards[idx]
91+
pass_policy(self._policy_cards[0])
92+
8193
def discard(card):
8294
self._discard_pile.append(card)
8395

@@ -86,13 +98,50 @@ def pass_policy(policy):
8698
self._num_liberal_passed += 1
8799
else:
88100
self._num_fascist_passed += 1
101+
self._session.emit_to_users(self._users, CHANCELLOR_POLICY, json.dumps({"status" : policy}))
89102

90103
def vote(username, vote):
91104
self._votes[username] = vote
92105
if(len(self._votes) == len(self._users)):
93106
tally = sum([1 for v in self._votes if self._votes[v]=="yes"])
107+
result = "fail"
94108
if(tally > len(users)/2):
95-
return "pass", self._votes
109+
self._election_tracker = 0
110+
elected_chancellor(self._temp_chancellor)
96111
else:
97-
return "fail", self._votes
98-
return "continue",dict()
112+
self._election_tracker += 1
113+
if self._election_tracker == 4:
114+
pass_policy(self._deck.pop())
115+
self._election_tracker = 0
116+
next_pres()
117+
118+
119+
def kill(username):
120+
if username == self._mrgoose:
121+
self._session.emit_to_users(self._users, ANNOUNCE_WINNER, json.dumps({"winner" : "students"}))
122+
return
123+
124+
for i in range(len(self._users)):
125+
if (self._users[i] == username):
126+
del self._users[i]
127+
break
128+
129+
self._session.emit_to_users(self._users, BROADCAST_KILL, json.dumps({"user_id" : username}))
130+
131+
def nomination_list():
132+
nominate = [uid for uid in users if uid not in [self._president, self._chancellor]]
133+
self._session.emit_to_user(self._president, CANDIDATE_CHANCELLOR, json.dumps({"users" : nominate}))
134+
135+
def nominate(username):
136+
self._temp_chancellor = username
137+
self._session.emit_to_user(self._president, VOTE_CHANCELLOR, json.dumps({"user_id" : username}))
138+
139+
140+
def elected_chancellor(username):
141+
if self._num_fascist_passed >= 3 and username == self._mrgoose:
142+
self._session.emit_to_users(self._users, ANNOUNCE_WINNER, json.dumps({"winner" : "geese"}))
143+
return
144+
self._chancellor = username
145+
self._session.emit_to_users(self.users, ELECTED_CHANCELLOR, json.dumps({"user_id" : username}))
146+
147+

backend/game_events.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
START_GAME_RESPONSE = "StartGameInfo" # Backend -> Frontend; username->userid mapping
55

66
NEW_PRESIDENT = "President" # Backend -> Frontend; userid of the president, govtFailCount
7+
CANDIDATES_CHANCELLOR = "CandidateChancellor" # Frontend -> Backend; userids of candidates for the role of chancellor
78
NOMINATED_CHANCELLOR = "NominatedChancellor" # Frontend -> Backend; userid of the chancellor
89
VOTE_CHANCELLOR = "VoteChancellor" # Backend -> Frontend; userid of the chancellor in case voting is required.
910
ELECTED_CHANCELLOR = "ElectedChancellor" # Backend -> Frontend; userid of the chancellor

0 commit comments

Comments
 (0)