Created
May 18, 2020 18:30
-
-
Save asmodat/fa797b99a643806d941096d1c19d2ea9 to your computer and use it in GitHub Desktop.
Leaderboard - Game of Zone Phase 1b
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
# Dependencies: | |
# python3 | |
# pip3 install --upgrade requests | |
# pip3 install 'python-dateutil>=2.7.0,<2.9.9' --force-reinstall | |
import json | |
import requests | |
import time | |
import socket | |
import time | |
import datetime | |
import dateutil | |
import dateutil.parser | |
from datetime import datetime, timezone | |
print(f"GoZ Phase 1b scoreboard") | |
f = requests.get("https://goz.bitsong.network/api/clients?_limit=2000") | |
time_now = datetime.utcnow() | |
stats = json.loads(f.text) | |
game_start = dateutil.parser.isoparse("2020-05-18T07:00:00.000Z").replace(tzinfo=None) | |
eligiblePlayers=[] | |
for stat in stats: | |
created_at=dateutil.parser.isoparse(stat["createdAt"]).replace(tzinfo=None) | |
# player is out of the game if disconnected even once | |
# (because there already exist palyers with trust above 71h that are eligable to be scored) | |
if created_at > game_start: | |
continue | |
# assume that client connected == client updated at least at the time when connection was initiated | |
updated_at=dateutil.parser.isoparse(stat.get("updatedAt", stat["createdAt"])).replace(tzinfo=None) | |
session_duration=(time_now - updated_at).total_seconds() # how much time passed since last update | |
trusting = int(stat["trusting_period"][:-9]) # how ofthen the update must be made (trim zeros from nanoseconds) | |
if trusting < session_duration: # player is out of the game if more time passed then trusting period | |
continue | |
eligiblePlayers.append(stat) # collect all eligible players | |
# sort players by trusting period (trim nanoseconds in case they do not fit in the integer value range) | |
eligiblePlayers = sorted(eligiblePlayers, key=lambda k: int(k['trusting_period'][:-9])) | |
# leaderboard should contain only unique competitors | |
position=0 | |
distinctPlayers=[] | |
print(f"#TOP\t| CHAIN ID\t| TRUST PERIOD") # print header | |
for player in eligiblePlayers: | |
chain_id = player["chain_id"] | |
if any(p["chain_id"] == chain_id for p in distinctPlayers): # remove already added players | |
continue | |
distinctPlayers.append(player) | |
position = position + 1 | |
trusting = int(player["trusting_period"][:-9]) # how ofthen the uc must be made | |
print(f"#{position}\t| {chain_id}\t| {trusting}s") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment