-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add logic to handle EstablishedPeer messages
- Loading branch information
Showing
6 changed files
with
390 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from collections import defaultdict | ||
|
||
|
||
class ConnectionMatrix: | ||
def __init__(self, established_peers: dict[int, set[int]]): | ||
self.established_peers = established_peers | ||
|
||
def get_unconnected_peer_ids(self) -> set[int]: | ||
unconnected_peer_ids: set[int] = set() | ||
|
||
# Group players by number of connected peers | ||
players_by_num_peers = defaultdict(list) | ||
for player_id, peer_ids in self.established_peers.items(): | ||
players_by_num_peers[len(peer_ids)].append((player_id, peer_ids)) | ||
|
||
# Mark players with least number of connections as unconnected if they | ||
# don't meet the connection threshold. Each time a player is marked as | ||
# 'unconnected', remaining players need 1 less connection to be | ||
# considered connected. | ||
connected_peers = dict(self.established_peers) | ||
for num_connected, peers in sorted(players_by_num_peers.items()): | ||
if num_connected < len(connected_peers) - 1: | ||
for player_id, peer_ids in peers: | ||
unconnected_peer_ids.add(player_id) | ||
del connected_peers[player_id] | ||
|
||
return unconnected_peer_ids |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.