Skip to content

Commit

Permalink
Fix bug where a role without alive players could still wake up and fr…
Browse files Browse the repository at this point in the history
…eeze the game
  • Loading branch information
Solirs committed Jul 4, 2023
1 parent ca54cc7 commit 5a416e2
Showing 1 changed file with 18 additions and 13 deletions.
31 changes: 18 additions & 13 deletions freefang/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import select, random, json, traceback, sys, datetime, time
import select, random, json, traceback, sys, datetime, time, copy
try:
from freefang.roles import *
import freefang.net as fn
Expand Down Expand Up @@ -70,13 +70,14 @@ def send_packet(self, packet, con):
def distribute_roles(self):
# Get all the players and keep track of those with no roles
print("Distributing roles to players")
roles = copy.deepcopy(self.roles)

# Randomly give each role to the number of players its supposed to be on,
for spl in self.players:
playerrole, _ = random.choice(list(self.roles.items()))
self.roles[playerrole] -= 1
if self.roles[playerrole] == 0:
del self.roles[playerrole]
playerrole, _ = random.choice(list(roles.items()))
roles[playerrole] -= 1
if roles[playerrole] == 0:
del roles[playerrole]

spl.role = playerrole
fn.send_packet(utils.obj_to_json(packets.Role_attributed(role=playerrole.__name__)), spl.connection)
Expand Down Expand Up @@ -163,6 +164,8 @@ def kill_player(self, player, reason=None):

# Add player to list of dead players
self.dead.append(player)

self.roles[player.role] -= 1


def handle_disconnections(self):
Expand Down Expand Up @@ -286,15 +289,17 @@ def gameloop(self):
while self.game_continues():
# Game should go on as long as there are villagers and werewolves, keeping the day night cycle
self.sendall(utils.obj_to_json(packets.Time_change(time="night"))) # Notify everyone night has fallen

for i in self.nightroles:
self.queueall(utils.obj_to_json(packets.Role_wakeup(role=i.__name__))) # Notify everyone role has woken up
self.up = i
# Run the role's wake up event function
try:
i.onwakeup(self)
except:
pass
self.eventloop()
if self.roles[i] > 0:
self.queueall(utils.obj_to_json(packets.Role_wakeup(role=i.__name__))) # Notify everyone role has woken up
self.up = i
# Run the role's wake up event function
try:
i.onwakeup(self)
except:
pass
self.eventloop()

# Remove all protections
for player in self.players:
Expand Down

0 comments on commit 5a416e2

Please sign in to comment.