Esempio n. 1
0
    $team_data = array();
    foreach ($required_fields as $field => $label) {
        if (isset($post[$field]) && strlen(trim($post[$field]))) {
            $team_data[$field] = trim($post[$field]);
        } else {
            $errors[] = array("field" => $field, "msg" => "{$label} is required");
            $success = false;
        }
    }
    foreach ($other_fields as $field) {
        if (isset($post[$field]) && strlen(trim($post[$field]))) {
            $team_data[$field] = trim($post[$field]);
        }
    }
    if ($success) {
        $teams = $tba->get("event/{$team_data["event_code"]}/teams");
        if (is_array($teams) && count($teams)) {
            foreach ($teams as $team) {
                $existing = $sdb->getItem("team", array("team_number" => $team["team_number"]), array("id"));
                if (!$existing) {
                    $sdb->addTeam(array_merge($default_fields, array("team_number" => $team["team_number"], "team_name" => $team["nickname"])));
                }
            }
        } else {
            $data = array();
            $errors[] = "Invalid event code \"{$team_data["event_code"]}\"";
            $success = false;
        }
    }
}
$output = array("data" => $data, "error" => $errors, "success" => $success);
$file_contents = file_get_contents($file);
$data = array("stats" => array("defenses" => json_decode($file_contents, 1)));
$team_stats = array();
foreach ($data["stats"] as $stat => $teams) {
    if (is_string($teams)) {
        $teams = json_decode($teams, 1);
    }
    foreach ($teams as $team_number => $stats) {
        if (!isset($team_stats[$team_number])) {
            $team_stats[$team_number] = array();
        }
        $team_stats[$team_number][$stat] = $stats;
    }
}
$json_fields = array("questions", "stats");
foreach ($team_stats as $team_number => $stats) {
    $existing = $sdb->getItem("team", array("team_number" => $team_number), array("id", "stats_json"));
    if ($existing["id"] > 0) {
        $sdb->updateTeam($team_number, array_merge($existing, array("stats" => $stats)));
    } else {
        $tba_team = $tba->get("team/frc{$team_number}");
        $data = array_merge($default_fields, $tba_team, array("team_number" => $team_number, "team_name" => $tba_team["nickname"], "stats" => $stats));
        foreach ($json_fields as $field) {
            if (isset($data[$field])) {
                $data["{$field}_json"] = json_encode($data[$field]);
            }
        }
        $sdb->addTeam($data);
    }
}
die(print_r($team_stats));
Esempio n. 3
0
    $team_data = array();
    foreach ($required_fields as $field => $label) {
        if (isset($post[$field]) && strlen(trim($post[$field]))) {
            $team_data[$field] = trim($post[$field]);
        } else {
            $errors[] = array("field" => $field, "msg" => "{$label} is required");
            $success = false;
        }
    }
    foreach ($other_fields as $field) {
        if (isset($post[$field]) && strlen(trim($post[$field]))) {
            $team_data[$field] = trim($post[$field]);
        }
    }
    if ($success) {
        $existing = $sdb->getItem("team", array("team_number" => $team_data["team_number"]));
        if (count($existing)) {
            $success = false;
            $errors[] = array("field" => "team_number", "msg" => "Team #{$team_data["team_number"]} already in use!");
        }
    }
    if ($success) {
        $data = $sdb->addTeam($team_data);
        if (!isset($data["id"]) || !$data["id"]) {
            $success = false;
            // $data = array();
            $errors[] = "Database error";
        }
    }
}
$output = array("data" => $data, "error" => $errors, "success" => $success);