Skip to content

Commit

Permalink
store PB on server and display it in My Ratings
Browse files Browse the repository at this point in the history
  • Loading branch information
sylae committed Oct 12, 2022
1 parent 9228158 commit 44f58cb
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/Handler/VoteHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function respond(array $vars)
$map->fetchTMXid();

// try to vote
$map->addVote($user, $body->vote);
$map->addVote($user, $body->vote, $body->pb ?? null);

// return vote totals
echo $map->getMapSummaryJSON($user);
Expand Down
40 changes: 32 additions & 8 deletions src/Map.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public static function createNewMap(
return self::createFromID($trs, $id);
}

public function addVote(User $from, string $type)
public function addVote(User $from, string $type, ?int $PB = null)
{
$vT = match ($type) {
"++" => self::VOTE_UP,
Expand Down Expand Up @@ -137,15 +137,17 @@ public function addVote(User $from, string $type)
);
} else {
$this->trs->db->executeStatement(
"replace into votes (idMap, idUser, vote) values (?, ?, ?)",
"replace into votes (idMap, idUser, vote, PB) values (?, ?, ?, ?)",
[
$this->id,
$from->id,
$vT
$vT,
$PB
],
[
'text',
'text',
'integer',
'integer'
]
);
Expand Down Expand Up @@ -176,7 +178,9 @@ public function getMapSummaryJSON(?User $from): string
$x->vDown = $total['--'];

if (!is_null($from)) {
$x->myvote = $this->getUserVote($from);
$mv = $this->getUserVote($from);
$x->myvote = $mv[0];
$x->pb = $mv[1];
}

return json_encode($x, JSON_PRETTY_PRINT);
Expand Down Expand Up @@ -265,20 +269,23 @@ public function getVoteTotals(bool $breakCache = false): array
return $x;
}

public function getUserVote(User $from): string
/**
* @return array [string (vote), int (PB)]
*/
public function getUserVote(User $from): array
{
$sql = "select * from votes where idMap = ? and idUser = ? group by vote ";
$res = $this->trs->db->executeQuery($sql, [$this->id, $from->id], ['string', 'string']);

if ($row = $res->fetchAssociative()) {
switch ($row['vote']) {
case self::VOTE_UP:
return "++";
return ["++", $row["PB"]];
case self::VOTE_DOWN:
return "--";
return ["--", $row["PB"]];
}
}
return "O";
return ["O", null];
}

public function getTMXScreenshot(): ?string
Expand Down Expand Up @@ -306,4 +313,21 @@ public function getTMXLink(): string
}
}

public function formatPB(?int $PB): string
{
if (is_null($PB)) {
return "n/a";
}

$ci = \Carbon\CarbonInterval::milliseconds($PB)->cascade();
$str = "";
if ($ci->hours > 0) {
return substr($ci->format("%H:%I:%S.%F"), 0, -3);
} elseif ($ci->minutes > 0) {
return substr($ci->format("%I:%S.%F"), 0, -3);
} else {
return substr($ci->format("%S.%F"), 0, -3);
}
}

}
3 changes: 2 additions & 1 deletion src/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ public function getUserRatings(): Collection
$v = [];
$v['map'] = Map::createFromDBRow($this->trs, $row);
$v['vote'] = $row['vote'];
$v['PB'] = $row['PB'];
$v['voteTime'] = new Carbon($row['voteTime']);
$x->set($row['idMap'], $v);
}
Expand All @@ -245,7 +246,7 @@ public function getSessions(): Collection

public function jsonSerialize(): object
{
return (object) [
return (object)[
'uid' => $this->id,
'login' => $this->login,
'displayName' => $this->displayName,
Expand Down
24 changes: 14 additions & 10 deletions tpl/myratings.twig
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<tr>
<th class="nowrap"><!-- Thumbnail --></th>
<th class="nowrap">Map</th>
<th class="nowrap">Author</th>
<th class="nowrap">Overall Rating</th>
<th class="nowrap">Your Rating</th>
<th class="nowrap">Voted on</th>
Expand All @@ -18,7 +17,7 @@
<tbody>
{% if ratings.count() == 0 %}
<tr>
<td colspan="7">You have no ratings! <a href="https://openplanet.dev/plugin/trackratings"
<td colspan="6">You have no ratings! <a href="https://openplanet.dev/plugin/trackratings"
target="_blank">Grab the plugin</a> and start voting!
</td>
</tr>
Expand All @@ -27,24 +26,29 @@
<tr>
<td class="mapScreenshot"
{% if item.map.getTMXScreenshot() %}style="background-image:url('{{ item.map.getTMXScreenshot() }}');"{% endif %} ></td>
<td>{{ item.map.name }}</td>
<td>{{ item.map.authorName }}</td>
<td>{{ item.map.name }}<br/><small><em>by <a
href="https://trackmania.io/#/player/{{ item.map.authorLogin }}" target="_blank"
class="tmioLink">{{ item.map.authorName }}</a></em></small>
</td>
<td class="nowrap">
{{ (item.map.getVoteTotals()["++"] / (item.map.getVoteTotals()["--"]+item.map.getVoteTotals()["++"]))|format_percent_number(locale=trs.user.locale) }}
({{ (item.map.getVoteTotals()["--"]+item.map.getVoteTotals()["++"])|format_number(locale=trs.user.locale) }}
votes)
<br/>
<small><em>({{ (item.map.getVoteTotals()["--"]+item.map.getVoteTotals()["++"])|format_number(locale=trs.user.locale) }}
votes)</em></small>
</td>
<td class="nowrap"
data-sort="{{ item.vote }}">{% if item.vote == constant('\Map::VOTE_UP') %}++{% elseif item.vote == constant('\Map::VOTE_DOWN') %}--{% else %}??{% endif %}</td>
data-sort="{{ item.vote }}">{% if item.vote == constant('\Map::VOTE_UP') %}++{% elseif item.vote == constant('\Map::VOTE_DOWN') %}--{% else %}??{% endif %}
<br/><small><em>{% if item.PB > 0 %}{{ item.map.formatPB(item.PB) }}{% endif %}</em></small>
</td>
<td class="nowrap"
data-sort="{{ item.voteTime.timestamp }}">{{ item.voteTime.locale(trs.user.locale).isoFormat("lll") }}
<br /><small><em>{{ item.voteTime.locale(trs.user.locale).longRelativeDiffForHumans(2) }}</em></small>
<br/><small><em>{{ item.voteTime.locale(trs.user.locale).longRelativeDiffForHumans(2) }}</em></small>
</td>
<td class="mapLinkBar">
<a href="https://trackmania.io/#/leaderboard/{{ item.map.id }}"><img src="/img/iconTIO.png"
alt="Trackmania.io" /></a>
alt="Trackmania.io"/></a>
<a href="{{ item.map.getTMXLink() }}"><img src="/img/iconTMX.png"
alt="TrackmaniaExchange" /></a>
alt="TrackmaniaExchange"/></a>
</td>
</tr>
{% endfor %}
Expand Down

0 comments on commit 44f58cb

Please sign in to comment.