error->code . ": " . $response->error->error . "\n"; $maintainer[0] = $response->error->code; $maintainer[1] = $response->error->error; } else { $pdo = null; try { $pdo = new PDO("sqlite:" . "${db_path}/slork.sqlite"); } catch (PDOException $e) { // handle the exception here } $query = "INSERT OR REPLACE INTO maintainer (maintainer, api) VALUES (?, ?);"; $stmt = $pdo->prepare( $query ); $stmt->execute( [$response->player_id, $api] ); $query = "INSERT OR REPLACE INTO player (maintainer, player, property, value) VALUES (?, ?, ?, ?);"; $stmt = $pdo->prepare( $query ); $stmt->execute( [$response->player_id, $response->player_id, 'name', $response->name] ); // $pdo->commit(); $maintainer[0] = $response->player_id; $maintainer[1] = $response->name; } $pdo = null; return $maintainer; } function get_maintainer( $id ) { /** * @param string $id Maintainer's XID * @return array XID and name of maintainer */ global $db_path; $pdo = null; try { $pdo = new PDO("sqlite:" . "${db_path}/slork.sqlite"); } catch (PDOException $e) { // handle the exception here } $query = "SELECT value FROM player WHERE maintainer = ? AND player = ? AND property = 'name';"; $stmt = $pdo->prepare( $query ); $stmt->execute( [$id, $id] ); $maintainer = [0, '?']; $row = $stmt->fetch(); $maintainer[0] = $id; $maintainer[1] = $row['value']; $pdo = null; return $maintainer; } function add_player( $maintainer, $player ) { global $db_path; $pdo = null; try { $pdo = new PDO("sqlite:" . "${db_path}/slork.sqlite"); } catch (PDOException $e) { // handle the exception here } $query = <<prepare( $query ); $properties = '["name"]'; $stmt->execute( [$maintainer, $player, $properties] ); // For starters add player's name $query = "SELECT api FROM maintainer WHERE maintainer = ?;"; $stmt = $pdo->prepare( $query ); $stmt->execute( [$maintainer] ); $api = $stmt->fetch()['api']; $url = "https://api.torn.com/user/${player}?key="; $url .= $api; $response = json_decode( file_get_contents( $url ) ); if ( property_exists( $response, "error" ) ) { print "ERROR " . $response->error->code . ": " . $response->error->error . "\n"; $maintainer[0] = $response->error->code; $maintainer[1] = $response->error->error; } else { $name = $response->name; $query = <<prepare($query); $stmt->execute([$maintainer, $player, $name]); $maintainer = get_maintainer( $maintainer ); } return $maintainer; } function delete_player( $player ) { global $db_path; $pdo = null; try { $pdo = new PDO("sqlite:" . "${db_path}/slork.sqlite"); } catch (PDOException $e) { // handle the exception here } $maintainer_id = $_REQUEST['maintainer']; $query = "DELETE FROM player WHERE player = ? AND maintainer = ?;"; $stmt = $pdo->prepare( $query ); $stmt->execute( [$player, $maintainer_id] ); $query = "DELETE FROM player_properties WHERE player = ? AND maintainer = ?;"; $stmt = $pdo->prepare( $query ); $stmt->execute( [$player, $maintainer_id] ); $maintainer = get_maintainer( $maintainer_id ); if ( $player == $maintainer_id ) { // Poof $query = "DELETE FROM maintainer WHERE maintainer = ?"; $stmt = $pdo->prepare( $query ); $stmt->execute( [$maintainer[0]] ); unset( $_REQUEST['action'] ); unset( $_REQUEST['maintainer'] ); unset( $_REQUEST['player'] ); $maintainer = [0, '?']; } $pdo = null; return $maintainer; } function store_player_properties( $player, $maintainer, $properties ) { global $db_path; $pdo = null; try { $pdo = new PDO("sqlite:" . "${db_path}/slork.sqlite"); } catch (PDOException $e) { // handle the exception here } $query = "INSERT OR REPLACE INTO player_properties (maintainer, player, property) VALUES (?, ?, ?);"; $stmt = $pdo->prepare( $query ); if ( ! in_array( 'name', $properties ) ) { $properties[] = 'name'; } $json = json_encode( $properties ); $stmt->execute( [$maintainer[0], $player, $json] ); // Next remove all properties that are in player // but not in the new list $query = "SELECT property FROM player WHERE maintainer = ? AND player = ?"; $stmt = $pdo->prepare( $query ); $stmt->execute( [$maintainer[0], $player] ); $rows = $stmt->fetchAll(); $properties_delete = []; foreach ( $rows as $row ) { if ( ! in_array( $row['property'], $properties ) ) { $properties_delete[] = $row['property']; } } $query = "DELETE FROM player WHERE maintainer = ? AND player = ? AND property = ?;"; $stmt = $pdo->prepare( $query ); foreach ( $properties_delete as $property ) { $stmt->execute( [$maintainer[0], $player, $property] ); } } function get_property( $maintainer, $player, $property ) { global $db_path; $pdo = null; try { $pdo = new PDO("sqlite:" . "${db_path}/slork.sqlite"); } catch (PDOException $e) { // handle the exception here } $query = "SELECT value FROM player WHERE maintainer = ? AND player = ? and property = ?;"; $stmt = $pdo->prepare( $query ); $stmt->execute( [$maintainer, $player, $property] ); return $stmt->fetch()['value']; }