Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added some game state function #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 56 additions & 7 deletions backend/GameState.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ def __init__(self, room_id, session):
self._president = ""
self._chancellor = ""
self._votes = dict()
self._policy_cards = []
self._game_in_progress = False
self._pres_cycle = None
self._temp_chancellor = ""

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

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

def start_game():
Expand All @@ -70,14 +72,24 @@ def start_game():
next_pres()

def draw(n=3):
cards = []
self._policy_cards = []
for i in range(n):
if(len(self._deck)==0):
self._deck = self._discard_pile
self._discard_pile = []
random.shuffle(self._deck)
cards.append(self._deck.pop())
return cards
self._policy_cards.append(self._deck.pop())
self._session.emit_to_user(self._president, PRESIDENT_POLICIES_OPTIONS, json.dumps({"cards" : self._policy_cards}))

def president_discard(idx):
discard(self._policy_cards[idx])
del self._policy_cards[idx]

def chancellor_play(idx):
discard(self._policy_cards[1 - idx])
del self._policy_cards[idx]
pass_policy(self._policy_cards[0])

def discard(card):
self._discard_pile.append(card)

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

def vote(username, vote):
self._votes[username] = vote
if(len(self._votes) == len(self._users)):
tally = sum([1 for v in self._votes if self._votes[v]=="yes"])
result = "fail"
if(tally > len(users)/2):
return "pass", self._votes
self._election_tracker = 0
elected_chancellor(self._temp_chancellor)
else:
return "fail", self._votes
return "continue",dict()
self._election_tracker += 1
if self._election_tracker == 4:
pass_policy(self._deck.pop())
self._election_tracker = 0
next_pres()


def kill(username):
if username == self._mrgoose:
self._session.emit_to_users(self._users, ANNOUNCE_WINNER, json.dumps({"winner" : "students"}))
return

for i in range(len(self._users)):
if (self._users[i] == username):
del self._users[i]
break

self._session.emit_to_users(self._users, BROADCAST_KILL, json.dumps({"user_id" : username}))

def nomination_list():
nominate = [uid for uid in users if uid not in [self._president, self._chancellor]]
self._session.emit_to_user(self._president, CANDIDATE_CHANCELLOR, json.dumps({"users" : nominate}))

def nominate(username):
self._temp_chancellor = username
self._session.emit_to_user(self._president, VOTE_CHANCELLOR, json.dumps({"user_id" : username}))


def elected_chancellor(username):
if self._num_fascist_passed >= 3 and username == self._mrgoose:
self._session.emit_to_users(self._users, ANNOUNCE_WINNER, json.dumps({"winner" : "geese"}))
return
self._chancellor = username
self._session.emit_to_users(self.users, ELECTED_CHANCELLOR, json.dumps({"user_id" : username}))


1 change: 1 addition & 0 deletions backend/game_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
START_GAME_RESPONSE = "StartGameInfo" # Backend -> Frontend; username->userid mapping

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