Skip to content

Commit

Permalink
fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
thehowl committed Aug 1, 2017
1 parent 15cf8c0 commit 143cab9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 3 deletions.
3 changes: 3 additions & 0 deletions common_functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ function build_where($parts)

function to_3339($date)
{
if ($date === null) {
return "";
}
return \DateTime::createFromFormat("Y-m-d H:i:s", $date)->format(\DateTime::RFC3339);
}

Expand Down
72 changes: 72 additions & 0 deletions methods/teams/team.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

function run_method($state) {
$uid = $state->getSelfId();
if (!$uid) {
error_message("Missing or invalid session token.", 401);
return;
}

$teamID = (int) @$_GET["id"];
if (!$teamID) {
error_message("Missing team ID", 422);
return;
}

$members = $state->db->fetchAll("SELECT attributes, user FROM team_users WHERE team = ?", $teamID);

$found = false;
foreach ($members as $member) {
if ($member["user"] == $uid) {
$found = true;
break;
}
}
if (!$found) {
error_message("Team does not exist or you are not a member of it", 403);
return;
}

array_walk($members, "member_walker");

$team = $state->db->fetch(
"SELECT
teams.id, teams.name, teams.captain, teams.created_at,
t.id as tourn_id, t.name as tourn_name, t.description,
t.mode, t.status, t.team_size, t.exclusivity_starts,
t.exclusivity_ends, t.min_team_size
FROM teams
INNER JOIN tournaments t ON t.id = teams.tournament
WHERE teams.id = ?", [$teamID]);

echo json_encode([
"ok" => true,
"team" => team_obj_creator($team, $members),
], JSON_HEX_TAG);
}

function member_walker(&$member) {
$member["attributes"] = (int) $member["attributes"];
$member["user"] = (int) $member["user"];
}

function team_obj_creator($team, $members) {
return [
"id" => (int) $team["id"],
"name" => $team["name"],
"captain" => (int) $team["captain"],
"created_at" => to_3339($team["created_at"]),
"members" => $members,
"tournament" => [
"id" => (int) $team["tourn_id"],
"name" => $team["tourn_name"],
"description" => $team["description"],
"mode" => (int) $team["mode"],
"status" => (int) $team["status"],
"team_size" => (int) $team["team_size"],
"exclusivity_starts" => $team["exclusivity_starts"],
"exclusivity_ends" => $team["exclusivity_ends"],
"min_team_size" => (int) $team["min_team_size"],
],
];
}
6 changes: 3 additions & 3 deletions methods/tournaments.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ function run_method($state)
t.id, t.name, t.description, t.mode, t.status,
t.status_data, t.created_at, t.updated_at,
t.team_size, t.min_team_size, t.exclusivity_starts,
t.exclusivity_ends, tu.team AS my_team, teams.name as my_team_name,
t.exclusivity_ends, MAX(tu.team) AS my_team, teams.name as my_team_name,
t.max_beatmap_requests
FROM tournaments t
LEFT JOIN teams ON teams.tournament = t.id
LEFT JOIN team_users tu ON tu.team = teams.id AND tu.user = ? AND tu.attributes != 0
";
";
$query .= build_where($parts);
$query .= " ORDER BY t.updated_at DESC LIMIT $offset, 50";
$query .= " GROUP BY t.id ORDER BY t.updated_at DESC LIMIT $offset, 50";

$results = $state->db->fetchAll($query, $params);
array_walk($results, 'walker');
Expand Down

0 comments on commit 143cab9

Please sign in to comment.