Skip to content

Commit

Permalink
implement the protector role
Browse files Browse the repository at this point in the history
  • Loading branch information
lukakralik committed Jun 26, 2023
1 parent 86b8a4e commit 285e397
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/freefang/__pycache__
/freefang/__pycache__
/freefang/tests/gametest2.sh
12 changes: 4 additions & 8 deletions freefang/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def __init__(self):
self.voted_by = 0 # Number of people who voted for this player
self.voted = False
self.time = 0 # 0 = Night, 1 = Day
self.protected = None # name of the protected player
def iswerewolf(self):
return issubclass(self.role, Werewolf) and self.alive

Expand Down Expand Up @@ -117,24 +118,19 @@ def kill_player(self, player, reason=None):
# Add player to list of dead players
self.dead.append(player)









def handle_disconnections(self):
disconnected_players = [] # Track multiple disconnections at a time
for player in self.players:
try:
player.connection.send(b"") # Sending empty message to check connection status

except:
disconnected_players.append(player)

for player in disconnected_players:
if player.protected:
player.protected = None

self.remove_player(player)

#self.update_player_count()
Expand Down
5 changes: 3 additions & 2 deletions freefang/packets.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ def __init__(self, username):
}

class Game_created:
def __init__(self, gameid):
def __init__(self, gameid, roles):
self.action = "game_created"
self.headers = {
"id": gameid
"id": gameid,
"roles" : roles # supplied list of playing roles
}


Expand Down
18 changes: 17 additions & 1 deletion freefang/roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ def vote(headers, game, connection):
target = game.getplayerbyname(headers.target)

if game.up == Werewolf and headers.sender.iswerewolf() and headers.sender.voted == False and target.alive:

if target == headers.sender.protected:
return 3 # protected player case (3)
vt = WerewolfVote(headers.target, headers.sender)
game.votes.append(vt)
headers.sender.voted = True
Expand Down Expand Up @@ -139,6 +140,21 @@ def kill(headers, game, connection):
return 2
return 1

# can prevent one person of his choosing from dying at each night
class Protector(Role):
def __init__(self):
super(Protector, self).__init__()

@staticmethod
def protect(headers, game, connection):
target = game.getplayerbyname(headers.target)

# if both protector and target are alive, modify the headers
if headers.sender.alive and target.alive:
headers.sender.protected = target
return 2
return 1

class Vote:
def __init__(self, target, sender):
self.sender = sender
Expand Down

0 comments on commit 285e397

Please sign in to comment.