Skip to content

Commit

Permalink
add Added pretty print to matches and method to find revertible matches
Browse files Browse the repository at this point in the history
  • Loading branch information
souzatharsis committed Jul 11, 2021
1 parent f7720c2 commit b24ed74
Show file tree
Hide file tree
Showing 18 changed files with 136 additions and 27 deletions.
Binary file modified docs/build/doctrees/environment.pickle
Binary file not shown.
Binary file modified docs/build/doctrees/pyETT.doctree
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/build/html/.buildinfo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Sphinx build info version 1
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done.
config: aeed516466d05f64899f9231d051f83c
config: 132f96269b84295c115c2f79b9c8abea
tags: 645f666f9bcd5a90fca523b33c5a78b7
2 changes: 1 addition & 1 deletion docs/build/html/_modules/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<title>Overview: module code &mdash; pyETT 0.2.3 documentation</title>
<title>Overview: module code &mdash; pyETT 0.2.4 documentation</title>



Expand Down
2 changes: 1 addition & 1 deletion docs/build/html/_static/documentation_options.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var DOCUMENTATION_OPTIONS = {
URL_ROOT: document.getElementById("documentation_options").getAttribute('data-url_root'),
VERSION: '0.2.3',
VERSION: '0.2.4',
LANGUAGE: 'None',
COLLAPSE_INDEX: false,
BUILDER: 'html',
Expand Down
18 changes: 13 additions & 5 deletions docs/build/html/genindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<title>Index &mdash; pyETT 0.2.3 documentation</title>
<title>Index &mdash; pyETT 0.2.4 documentation</title>



Expand Down Expand Up @@ -237,10 +237,10 @@ <h2 id="G">G</h2>
</ul></li>
<li><a href="pyETT.html#pyETT.ett.ETT.get_leaderboard_dataframe">get_leaderboard_dataframe() (pyETT.ett.ETT method)</a>
</li>
</ul></td>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="pyETT.html#pyETT.ett_parser.get_leaderboard_official_tournament">get_leaderboard_official_tournament() (in module pyETT.ett_parser)</a>
</li>
</ul></td>
<td style="width: 33%; vertical-align: top;"><ul>
<li><a href="pyETT.html#pyETT.ett_parser.get_matches">get_matches() (in module pyETT.ett_parser)</a>

<ul>
Expand All @@ -255,6 +255,8 @@ <h2 id="G">G</h2>
<li><a href="pyETT.html#pyETT.ett.Player.get_matches_dataframe">(pyETT.ett.Player method)</a>
</li>
</ul></li>
<li><a href="pyETT.html#pyETT.ett.Player.get_matches_revertible">get_matches_revertible() (pyETT.ett.Player method)</a>
</li>
<li><a href="pyETT.html#pyETT.ett_parser.get_matchup">get_matchup() (in module pyETT.ett_parser)</a>
</li>
<li><a href="pyETT.html#pyETT.ett_parser.get_or_create_eventloop">get_or_create_eventloop() (in module pyETT.ett_parser)</a>
Expand Down Expand Up @@ -310,15 +312,21 @@ <h2 id="P">P</h2>
</li>
<li><a href="pyETT.html#pyETT.ett.Cohort.players_dataframe">players_dataframe() (pyETT.ett.Cohort method)</a>
</li>
<li><a href="pyETT.html#pyETT.ett.Match.print">print() (pyETT.ett.Match method)</a>
</li>
<li><a href="pyETT.html#pyETT.ett.print_match">print_match() (in module pyETT.ett)</a>
</li>
<li><a href="pyETT.html#pyETT.ett.Player.print_matches_revertible">print_matches_revertible() (pyETT.ett.Player method)</a>
</li>
</ul></td>
<td style="width: 33%; vertical-align: top;"><ul>
<li>
pyETT

<ul>
<li><a href="pyETT.html#module-pyETT">module</a>
</li>
</ul></li>
</ul></td>
<td style="width: 33%; vertical-align: top;"><ul>
<li>
pyETT.ett

Expand Down
2 changes: 1 addition & 1 deletion docs/build/html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<title>Welcome to pyETT’s documentation! &mdash; pyETT 0.2.3 documentation</title>
<title>Welcome to pyETT’s documentation! &mdash; pyETT 0.2.4 documentation</title>



Expand Down
2 changes: 1 addition & 1 deletion docs/build/html/modules.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<title>pyETT &mdash; pyETT 0.2.3 documentation</title>
<title>pyETT &mdash; pyETT 0.2.4 documentation</title>



Expand Down
Binary file modified docs/build/html/objects.inv
Binary file not shown.
2 changes: 1 addition & 1 deletion docs/build/html/py-modindex.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<title>Python Module Index &mdash; pyETT 0.2.3 documentation</title>
<title>Python Module Index &mdash; pyETT 0.2.4 documentation</title>



Expand Down
44 changes: 42 additions & 2 deletions docs/build/html/pyETT.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<title>pyETT package &mdash; pyETT 0.2.3 documentation</title>
<title>pyETT package &mdash; pyETT 0.2.4 documentation</title>



Expand Down Expand Up @@ -312,6 +312,11 @@ <h2>Submodules<a class="headerlink" href="#submodules" title="Permalink to this
</dl>
</dd></dl>

<dl class="py method">
<dt id="pyETT.ett.Match.print">
<code class="sig-name descname">print</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/pyETT/ett.html#Match.print"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pyETT.ett.Match.print" title="Permalink to this definition"></a></dt>
<dd></dd></dl>

</dd></dl>

<dl class="py class">
Expand Down Expand Up @@ -355,10 +360,34 @@ <h2>Submodules<a class="headerlink" href="#submodules" title="Permalink to this

<dl class="py method">
<dt id="pyETT.ett.Player.get_matches_dataframe">
<code class="sig-name descname">get_matches_dataframe</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">unranked</span><span class="o">=</span><span class="default_value">False</span></em><span class="sig-paren">)</span> &#x2192; pandas.core.frame.DataFrame<a class="reference internal" href="_modules/pyETT/ett.html#Player.get_matches_dataframe"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pyETT.ett.Player.get_matches_dataframe" title="Permalink to this definition"></a></dt>
<code class="sig-name descname">get_matches_dataframe</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">unranked</span><span class="p">:</span> <span class="n">bool</span> <span class="o">=</span> <span class="default_value">False</span></em><span class="sig-paren">)</span> &#x2192; pandas.core.frame.DataFrame<a class="reference internal" href="_modules/pyETT/ett.html#Player.get_matches_dataframe"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pyETT.ett.Player.get_matches_dataframe" title="Permalink to this definition"></a></dt>
<dd><p>Return player’s matches in a pandas dataframe.</p>
</dd></dl>

<dl class="py method">
<dt id="pyETT.ett.Player.get_matches_revertible">
<code class="sig-name descname">get_matches_revertible</code><span class="sig-paren">(</span><span class="sig-paren">)</span> &#x2192; List<span class="p">[</span>numpy.int64<span class="p">]</span><a class="reference internal" href="_modules/pyETT/ett.html#Player.get_matches_revertible"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pyETT.ett.Player.get_matches_revertible" title="Permalink to this definition"></a></dt>
<dd><p>Returns player’s lost matches that are eligible to be reverted/cancelled.
If you have an incomplete match because of connection issues or because your opponent left in the middle of the play,
you can request an Eleven Moderator (Discord) to revert/cancel the match and hence update your Elo rating.</p>
<dl class="field-list simple">
<dt class="field-odd">Returns</dt>
<dd class="field-odd"><p>List of matches ids that are eligible to be reverted/cancelled.</p>
</dd>
<dt class="field-even">Return type</dt>
<dd class="field-even"><p>List[np.int64]</p>
</dd>
</dl>
</dd></dl>

<dl class="py method">
<dt id="pyETT.ett.Player.print_matches_revertible">
<code class="sig-name descname">print_matches_revertible</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference internal" href="_modules/pyETT/ett.html#Player.print_matches_revertible"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pyETT.ett.Player.print_matches_revertible" title="Permalink to this definition"></a></dt>
<dd><p>Pretty print player’s lost matches that are eligible to be reverted/cancelled.
If you have an incomplete match because of connection issues or because your opponent left in the middle of the play,
you can request an Eleven Moderator (Discord) to revert/cancel the match and hence update your Elo rating</p>
</dd></dl>

</dd></dl>

<dl class="py class">
Expand Down Expand Up @@ -413,6 +442,17 @@ <h2>Submodules<a class="headerlink" href="#submodules" title="Permalink to this
available at <a class="reference external" href="http://lavadesignstudio.co.uk/eleven-rankings/">http://lavadesignstudio.co.uk/eleven-rankings/</a>.</p>
</dd></dl>

<dl class="py function">
<dt id="pyETT.ett.print_match">
<code class="sig-prename descclassname">pyETT.ett.</code><code class="sig-name descname">print_match</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">match</span><span class="p">:</span> <span class="n"><a class="reference internal" href="#pyETT.ett.Match" title="pyETT.ett.Match">pyETT.ett.Match</a></span></em><span class="sig-paren">)</span><a class="reference internal" href="_modules/pyETT/ett.html#print_match"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#pyETT.ett.print_match" title="Permalink to this definition"></a></dt>
<dd><p>Pretty print a match with rounds.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters</dt>
<dd class="field-odd"><p><strong>match</strong> (<a class="reference internal" href="#pyETT.ett.Match" title="pyETT.ett.Match"><em>Match</em></a>) – A match.</p>
</dd>
</dl>
</dd></dl>

</div>
<div class="section" id="module-pyETT.ett_parser">
<span id="pyett-ett-parser-module"></span><h2>pyETT.ett_parser module<a class="headerlink" href="#module-pyETT.ett_parser" title="Permalink to this headline"></a></h2>
Expand Down
2 changes: 1 addition & 1 deletion docs/build/html/pyETT_demo.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<title>Getting Started &mdash; pyETT 0.2.3 documentation</title>
<title>Getting Started &mdash; pyETT 0.2.4 documentation</title>



Expand Down
2 changes: 1 addition & 1 deletion docs/build/html/search.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

<meta name="viewport" content="width=device-width, initial-scale=1.0" />

<title>Search &mdash; pyETT 0.2.3 documentation</title>
<title>Search &mdash; pyETT 0.2.4 documentation</title>



Expand Down
2 changes: 1 addition & 1 deletion docs/build/html/searchindex.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/source/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
author = "Tharsis T. P. Souza"

# The full version, including alpha/beta/rc tags
release = "0.2.3"
release = "0.2.4"


# -- General configuration ---------------------------------------------------
Expand Down
77 changes: 69 additions & 8 deletions pyETT/ett.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __new__(cls, user_id, player=None, legacy_api=False):
return instance

def __init__(self, user_id, player=None, legacy_api=False):
self.id = user_id
self.id = np.int64(user_id)
if player is not None:
self.name = player["user-name"] if legacy_api else player["username"]
self.elo = player["elo"]
Expand All @@ -46,13 +46,13 @@ def __str__(self):
return self.name

def __hash__(self):
return self.id
return int(self.id)

def __lt__(self, other):
return self.elo < other.elo

def __eq__(self, other):
return self.id == other.id
return np.int64(self.id) == np.int64(other.id)

def get_friends(self) -> List["Player"]:
"""
Expand Down Expand Up @@ -91,14 +91,44 @@ def get_matches(self, unranked: bool = False) -> List["Match"]:
matches = self.matches
return matches

def get_matches_dataframe(self, unranked=False) -> pd.DataFrame:
def get_matches_dataframe(self, unranked: bool = False) -> pd.DataFrame:
"""
Return player’s matches in a pandas dataframe.
"""
return pd.DataFrame(
[vars(m) for m in self.get_matches(unranked) if m is not None]
)

def get_matches_revertible(self) -> List[np.int64]:
"""Returns player's lost matches that are eligible to be reverted/cancelled.
If you have an incomplete match because of connection issues or because your opponent left in the middle of the play,
you can request an Eleven Moderator (Discord) to revert/cancel the match and hence update your Elo rating.
Returns:
List[np.int64]: List of matches ids that are eligible to be reverted/cancelled.
"""
m = self.get_matches_dataframe()
m["number_of_played_rounds"] = m["home_score"] + m["away_score"]
m["winner"] = m["home_player"]
m.loc[m["winning_team"] == 1, "winner"] = m.loc[
m["winning_team"] == 1, "away_player"
]
m_incomplete = m.loc[
((m["ranked"]) & (m["winner"] != self))
& ((m["state"] != 1) | (m["number_of_played_rounds"] < 2)),
]

return list(m_incomplete.id.values)

def print_matches_revertible(self):
"""Pretty print player's lost matches that are eligible to be reverted/cancelled.
If you have an incomplete match because of connection issues or because your opponent left in the middle of the play,
you can request an Eleven Moderator (Discord) to revert/cancel the match and hence update your Elo rating
"""
m_incomplete_id_values = self.get_matches_revertible()

[print_match(m) for m in self.get_matches() if m.id in m_incomplete_id_values]

def get_elo_history(self) -> pd.DataFrame:
"""
Returns player’s elo score history.
Expand Down Expand Up @@ -137,7 +167,7 @@ class Round:
"""

def __init__(self, round_attributes):
self.id = round_attributes["id"]
self.id = np.int64(round_attributes["id"])
self.round_number = round_attributes["round-number"]
self.state = round_attributes["state"]
self.away_score = round_attributes["away-score"]
Expand All @@ -147,7 +177,7 @@ def __init__(self, round_attributes):

def __init__(self, match_id, match):
self.created_at = match["created-at"]
self.id = match_id
self.id = np.int64(match_id)

self.ranked = match["ranked"]
self.number_of_rounds = match["number-of-rounds"]
Expand All @@ -156,6 +186,7 @@ def __init__(self, match_id, match):
self.losing_team = match["losing-team"]
self.home_score = match["home-score"]
self.away_score = match["away-score"]
self.elo_change = match["elo-change"]

home_player_index = 0 if match["players"][0]["team"] == Player.HOME else 1
self.home_player = Player(
Expand All @@ -167,7 +198,10 @@ def __init__(self, match_id, match):
match["players"][1 - home_player_index],
)

self.rounds = [self.Round(r) for r in match["rounds"]]
self.rounds = [self.Round(r) for r in match["rounds"]][::-1]

def print(self):
print_match(self)

# Keeping inside Match for organizational / readability purposes even though it's a static method.
def get_rounds_dataframe(rounds: List["Round"]) -> pd.DataFrame: # type: ignore
Expand All @@ -178,6 +212,7 @@ def get_rounds_dataframe(rounds: List["Round"]) -> pd.DataFrame: # type: ignore
Returns:
pd.DataFrame: Dataframe with rounds, one per row.
"""
# I reverse the list because rounds come in reverse order from the web api for some reason
return pd.DataFrame([vars(r) for r in rounds if r is not None])


Expand Down Expand Up @@ -370,7 +405,7 @@ def matches_stats(matches, ranked):
)
cohort_stats["id"] = [p.id for p in cohort_stats["index"]]
cohort_players = self.players_dataframe()[["id", "elo", "rank"]]
cohort_players["id"] = cohort_players["id"].astype(np.int64)
# cohort_players["id"] = cohort_players["id"].astype(np.int64)

return cohort_stats.merge(
cohort_players,
Expand Down Expand Up @@ -464,3 +499,29 @@ def official_tournament_leaderboard_dataframe() -> pd.DataFrame:
available at http://lavadesignstudio.co.uk/eleven-rankings/.
"""
return ett_parser.get_leaderboard_official_tournament()[0]


def print_match(match: Match):
"""Pretty print a match with rounds.
Args:
match (Match): A match.
"""
print(f"Match #{match.id} : {match.created_at}\n")
home_elo_sign = (-1) ** match.winning_team
df = pd.DataFrame(
{
"USERNAME": [match.home_player.name, match.away_player.name],
"ELO +-": [
home_elo_sign * match.elo_change,
-home_elo_sign * match.elo_change,
],
"MATCH SCORE": [match.home_score, match.away_score],
}
)
df_rounds = Match.get_rounds_dataframe(match.rounds)
for index2, row2 in df_rounds.iterrows():
round_index = int(index2) + 1
df[f"ROUND {round_index}"] = [row2["home_score"], row2["away_score"]]
print(df)
print("\n")
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

setup(
name="pyETT",
version="0.2.3",
version="0.2.4",
description="Python library for Eleven VR Table Tennis data",
long_description=readme,
long_description_content_type="text/markdown",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_ett.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_match_rounds(self):
matches = player.get_matches_dataframe()
rounds = ett.Match.get_rounds_dataframe(
rounds=matches.loc[
matches["id"] == "9866478",
matches["id"] == 9866478,
].rounds.values[0]
)

Expand Down

0 comments on commit b24ed74

Please sign in to comment.