From 0d64899705849993a086ef0910cb9ddb866dbdab Mon Sep 17 00:00:00 2001 From: Mahmood - Zer0xFF <5013823+Zer0xFF@users.noreply.github.com> Date: Sat, 27 Oct 2018 15:23:00 +0100 Subject: [PATCH 1/9] Add Discord Bot --- website/actions/add_game.php | 4 + website/actions/delete_art.php | 2 + website/actions/delete_game.php | 7 +- website/actions/edit_game.php | 7 +- website/actions/uploads.php | 22 ++- website/include/DiscordUtils.class.php | 201 +++++++++++++++++++++++++ 6 files changed, 234 insertions(+), 9 deletions(-) create mode 100644 website/include/DiscordUtils.class.php diff --git a/website/actions/add_game.php b/website/actions/add_game.php index 0188c00..d8e2bd6 100644 --- a/website/actions/add_game.php +++ b/website/actions/add_game.php @@ -47,6 +47,7 @@ function returnJSONAndDie($code, $msg) require_once __DIR__ . "/../../include/TGDB.API.php"; +require_once __DIR__ . "/../include/DiscordUtils.class.php"; try { @@ -57,6 +58,9 @@ function returnJSONAndDie($code, $msg) if($res) { + $filters = ['game_title' => true, 'overview' => true, 'youtube' => true, 'release_date' => true, 'players' => true, 'coop' => true, 'developers' => true, 'publishers' => true, 'genres' => true, 'rating' => true]; + $new_game_data = $API->GetGameByID($res, 0, 1, $filters)[0]; + DiscordUtils::PostGameUpdate($_user, [], $new_game_data, 0); returnJSONAndDie(1, $res); } diff --git a/website/actions/delete_art.php b/website/actions/delete_art.php index bae5674..9c507d3 100644 --- a/website/actions/delete_art.php +++ b/website/actions/delete_art.php @@ -32,6 +32,7 @@ function returnJSONAndDie($code, $msg) require_once __DIR__ . "/../../include/TGDB.API.php"; require_once __DIR__ . "/../include/WebUtils.class.php"; +require_once __DIR__ . "/../include/DiscordUtils.class.php"; try { @@ -58,6 +59,7 @@ function returnJSONAndDie($code, $msg) $res = $API->DeleteGameImages($_user->GetUserID(), $_REQUEST['game_id'], $_REQUEST['image_id'], $cover->type); if($res) { + DiscordUtils::PostImageUpdate($_user, $_REQUEST['game_id'], '', $_REQUEST['type'], $_REQUEST['sub_type'], 2); returnJSONAndDie(1, "success!!"); } } diff --git a/website/actions/delete_game.php b/website/actions/delete_game.php index 833f847..10a5d2c 100644 --- a/website/actions/delete_game.php +++ b/website/actions/delete_game.php @@ -27,12 +27,15 @@ function returnJSONAndDie($code, $msg) } require_once __DIR__ . "/../../include/TGDB.API.php"; +require_once __DIR__ . "/../include/DiscordUtils.class.php"; try { $API = TGDB::getInstance(); - if(empty($API->GetGameByID($_REQUEST['game_id'], 0, 1))) + $filters = ['game_title' => true, 'overview' => true, 'youtube' => true, 'release_date' => true, 'players' => true, 'coop' => true, 'developers' => true, 'publishers' => true, 'genres' => true, 'rating' => true]; + $games = $API->GetGameByID($_REQUEST['game_id'], 0, 1, $filters); + if(empty($games)) { returnJSONAndDie(0, "No game in record to delete."); } @@ -56,7 +59,7 @@ function returnJSONAndDie($code, $msg) } $res = $API->DeleteGame($_user->GetUserID(), $_REQUEST['game_id']); - + DiscordUtils::PostGameUpdate($_user, [], $games[0], 2); returnJSONAndDie(1, "success!!"); diff --git a/website/actions/edit_game.php b/website/actions/edit_game.php index 7c8dfee..ef58d06 100644 --- a/website/actions/edit_game.php +++ b/website/actions/edit_game.php @@ -52,16 +52,21 @@ function returnJSONAndDie($code, $msg) } require_once __DIR__ . "/../../include/TGDB.API.php"; +require_once __DIR__ . "/../include/DiscordUtils.class.php"; try { - + $filters = ['game_title' => true, 'overview' => true, 'youtube' => true, 'release_date' => true, 'players' => true, 'coop' => true, 'developers' => true, 'publishers' => true, 'genres' => true, 'rating' => true]; $API = TGDB::getInstance(); + $old_game_data = $API->GetGameByID($_REQUEST['game_id'], 0, 1, $filters)[0]; + $res = $API->UpdateGame( $_user->GetUserID(), $_REQUEST['game_id'], $_REQUEST['game_title'], $_REQUEST['overview'], $_REQUEST['youtube'], $_REQUEST['release_date'], $_REQUEST['players'], $_REQUEST['coop'], $_REQUEST['developers'], $_REQUEST['publishers'], $_REQUEST['genres'], $_REQUEST['rating']); if($res) { + $new_game_data = $API->GetGameByID($_REQUEST['game_id'], 0, 1, $filters)[0]; + DiscordUtils::PostGameUpdate($_user, $old_game_data, $new_game_data, 1); returnJSONAndDie(1, "success!!"); } diff --git a/website/actions/uploads.php b/website/actions/uploads.php index 2d453e6..13e6995 100644 --- a/website/actions/uploads.php +++ b/website/actions/uploads.php @@ -4,6 +4,7 @@ require_once __DIR__ . "/../include/ErrorPage.class.php"; require_once __DIR__ . "/../include/login.phpbb.class.php"; require_once __DIR__ . "/../include/WebUtils.class.php"; +require_once __DIR__ . "/../include/DiscordUtils.class.php"; function save_image($original_image, $dest_image, $type) { @@ -207,25 +208,34 @@ function get_request_method() } if($_REQUEST['subtype'] == $cover->side) { - $res = $API->DeleteAndInsertGameImages($_user->GetUserID(), $cover->id, $_REQUEST['game_id'], $_REQUEST['type'], - $_REQUEST['type'] . "/" . $_REQUEST['subtype'] . "/" . $image_name, $_REQUEST['subtype']); + $image_path = $_REQUEST['type'] . "/" . $_REQUEST['subtype'] . "/" . $image_name; + $res = $API->DeleteAndInsertGameImages($_user->GetUserID(), $cover->id, $_REQUEST['game_id'], $_REQUEST['type'], $image_path, $_REQUEST['subtype']); + DiscordUtils::PostImageUpdate($_user, $_REQUEST['game_id'], CommonUtils::getImagesBaseURL()['thumb'] . $image_path, $_REQUEST['type'], $_REQUEST['subtype'], 1); echo json_encode($result); return; } } } - $res = $API->InsertGameImages($_user->GetUserID(), $_REQUEST['game_id'], $_REQUEST['type'], - $_REQUEST['type'] . "/" . $_REQUEST['subtype'] . "/" . $image_name, $_REQUEST['subtype']); + $image_path = $_REQUEST['type'] . "/" . $_REQUEST['subtype'] . "/" . $image_name; + $res = $API->InsertGameImages($_user->GetUserID(), $_REQUEST['game_id'], $_REQUEST['type'], $image_path, $_REQUEST['subtype']); } else { - $res = $API->InsertGameImages($_user->GetUserID(), $_REQUEST['game_id'], $_REQUEST['type'], - $_REQUEST['type'] . "/" . $image_name); + $image_path = $_REQUEST['type'] . "/" . $image_name; + $res = $API->InsertGameImages($_user->GetUserID(), $_REQUEST['game_id'], $_REQUEST['type'], $image_path); } if(!isset($res) || !$res) { returnJSONAndDie("Failed to update database."); } + else if(!empty($image_path)) + { + $sub_type = ""; + if(!empty($_REQUEST['subtype'])) + $sub_type = $_REQUEST['subtype']; + DiscordUtils::PostImageUpdate($_user, $_REQUEST['game_id'], CommonUtils::getImagesBaseURL()['thumb'] . $image_path, $_REQUEST['type'], $sub_type, 0); + + } } else { diff --git a/website/include/DiscordUtils.class.php b/website/include/DiscordUtils.class.php new file mode 100644 index 0000000..83fb28d --- /dev/null +++ b/website/include/DiscordUtils.class.php @@ -0,0 +1,201 @@ + $embeds['author']['name'], + 'avatar_url' => $embeds['author']['icon_url'], + ); + + $data['embeds'] = [$embeds]; + $json_string = json_encode($data); + + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL, Config::$DiscordWebhook); + curl_setopt($curl, CURLOPT_POST, 1); + curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/json')); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($curl, CURLOPT_POSTFIELDS, $json_string); + + $output = curl_exec($curl); + $output = json_decode($output, true); + + if (curl_getinfo($curl, CURLINFO_HTTP_CODE) != 204) + { + if(isset($output['message'])) + { + throw new \Exception($output['message']); + } + else if(isset($output)) + { + throw new \Exception(json_encode($output)); + } + } + + curl_close($curl); + return true; + } + + static function PostGameUpdate($_user, $old_game_data, $new_game_data, $type = 0) + { + if(!isset($new_game_data)) + return; + + try + { + //TODO: move htmlspecialchars_decodeArrayRecursive to CommonUtils:: + //htmlspecialchars_decodeArrayRecursive($old_game_data); + //htmlspecialchars_decodeArrayRecursive($new_game_data); + + $embeds = array(); + $embeds["author"] = array( + "name" => $_user->GetUsername(), + "url" => "https://forums.thegamesdb.net/memberlist.php?mode=viewprofile&u=" . $_user->GetUserID(), + "icon_url" => $_user->GetAvatar() + ); + $embeds["title"] = $new_game_data->game_title; + $embeds["url"] = CommonUtils::$WEBSITE_BASE_URL . "game.php?id=$new_game_data->id"; + switch($type) + { + case 0: + $embeds["color"] = 0x00b159; + $embeds["footer"] = ['text' => "Game Added"]; + break; + case 1: + $embeds["color"] = 0xffc425; + $embeds["footer"] = ['text' => "Game Updated"]; + break; + case 2: + $embeds["color"] = 0xd11141; + $embeds["footer"] = ['text' => "Game Removed"]; + break; + } + + $embeds["fields"][] = ["name" => "id", "value" => $new_game_data->id, "inline" => "true"]; + $embeds["fields"][] = ["name" => "platform", "value" => $new_game_data->platform, "inline" => "true"]; + $is_change = false; + foreach($new_game_data as $key => $val) + { + if(($type == 0 || $type == 2) && ($key == "id" || $key == "platform" || $key == "game_title")) + continue; + + if(!empty($val) && (empty($old_game_data) || empty($old_game_data->$key) || $val != $old_game_data->$key)) + { + if(is_array($val)) + $val = implode(",", $val); + $data = ["name" => $key, "value" => $val]; + if($key != "overview") + $data["inline"] = "true"; + + $embeds["fields"][] = $data; + $is_change = true; + } + else if(empty($val) && (!empty($old_game_data) && !empty($old_game_data->$key))) + { + $embeds["fields"][] = ["name" => $key, "value" => "[REMOVED]", "inline" => "true"]; + $is_change = true; + } + } + if(!$is_change) + return; + + if($type == 1) + { + $API = TGDB::getInstance(); + $boxarts = $API->GetGameBoxartByID($new_game_data->id, 0, 9999, 'boxart'); + if(isset($boxarts[$new_game_data->id])) + { + $new_game_data->boxart = $boxarts[$new_game_data->id]; + + $box_cover = new \stdClass(); + $box_cover->front = TGDBUtils::GetAllCovers($new_game_data, 'boxart', 'front'); + + if(!empty($box_cover->front)) + { + $embeds["thumbnail"] = array( + "url" => $box_cover->front[0]->thumbnail + ); + } + } + } + + DiscordUtils::Send($embeds); + } + catch(Exception $e) + { + error_log($e); + } + + } + + static function PostImageUpdate($_user, $game_id, $image_path, $type, $sub_type, $action_type) + { + if(!isset($game_id)) + return; + + try + { + $API = TGDB::getInstance(); + $game = $API->GetGameByID($game_id, 0, 1)[0]; + //htmlspecialchars_decodeArrayRecursive($game->game_title); + + $embeds = array(); + $embeds["author"] = array( + "name" => $_user->GetUsername(), + "url" => "https://forums.thegamesdb.net/memberlist.php?mode=viewprofile&u=" . $_user->GetUserID(), + "icon_url" => $_user->GetAvatar() + ); + $embeds["title"] = $game->game_title; + $embeds["url"] = CommonUtils::$WEBSITE_BASE_URL . "game.php?id=$game->id"; + + switch($action_type) + { + case 0: + $embeds["color"] = 0x00b159; + $embeds["footer"] = ['text' => "Image Added"]; + break; + case 1: + $embeds["color"] = 0xffc425; + $embeds["footer"] = ['text' => "Image Replaced"]; + break; + case 2: + $embeds["color"] = 0xd11141; + $embeds["footer"] = ['text' => "Image Removed"]; + break; + } + $embeds["fields"][] = ["name" => "game_id", "value" => $game->id, "inline" => "true"]; + $embeds["fields"][] = ["name" => "type", "value" => $type, "inline" => "true"]; + if(!empty($sub_type)) + { + $embeds["fields"][] = ["name" => "subtype", "value" => $sub_type, "inline" => "true"]; + } + + if($action_type < 2) + { + $embeds["image"] = array( + "url" => $image_path + ); + } + + DiscordUtils::Send($embeds); + } + catch(Exception $e) + { + error_log($e); + } + + } +} +?> From 1ed7ff749c9193c6823f8e6638dc98377969bcb1 Mon Sep 17 00:00:00 2001 From: Mahmood - Zer0xFF <5013823+Zer0xFF@users.noreply.github.com> Date: Sat, 27 Oct 2018 15:23:21 +0100 Subject: [PATCH 2/9] Move htmlspecialchars_decodeArrayRecursive to CommonUtils.class.php --- API/include/Utils.class.php | 15 --------------- API/include/routes.php | 20 ++++++++++---------- include/CommonUtils.class.php | 15 +++++++++++++++ website/include/DiscordUtils.class.php | 7 +++---- 4 files changed, 28 insertions(+), 29 deletions(-) diff --git a/API/include/Utils.class.php b/API/include/Utils.class.php index e507dab..9b65409 100644 --- a/API/include/Utils.class.php +++ b/API/include/Utils.class.php @@ -100,21 +100,6 @@ static function getValidNumericFromArray(array $args, $index) } return $IDs; } - - static function htmlspecialchars_decodeArrayRecursive(&$array) - { - foreach($array as &$sub_array_item) - { - if(is_array($sub_array_item) || is_object($sub_array_item)) - { - Utils::htmlspecialchars_decodeArrayRecursive($sub_array_item); - } - else if(!is_numeric($sub_array_item) && !empty($sub_array_item)) - { - $sub_array_item = htmlspecialchars_decode($sub_array_item); - } - } - } } ?> diff --git a/API/include/routes.php b/API/include/routes.php index ec86b8f..3dc7280 100644 --- a/API/include/routes.php +++ b/API/include/routes.php @@ -62,7 +62,7 @@ if($has_next_page = count($list) > $limit) unset($list[$limit]); - Utils::htmlspecialchars_decodeArrayRecursive($list); + CommonUtils::htmlspecialchars_decodeArrayRecursive($list); $JSON_Response = Utils::getStatus(200); $JSON_Response['data'] = array("count" => count($list), "games" => $list); @@ -116,7 +116,7 @@ if($has_next_page = count($list) > $limit) unset($list[$limit]); - Utils::htmlspecialchars_decodeArrayRecursive($list); + CommonUtils::htmlspecialchars_decodeArrayRecursive($list); $JSON_Response = Utils::getStatus(200); $JSON_Response['data'] = array("count" => count($list), "games" => $list); @@ -165,7 +165,7 @@ if($has_next_page = count($list) > $limit) unset($list[$limit]); - Utils::htmlspecialchars_decodeArrayRecursive($list); + CommonUtils::htmlspecialchars_decodeArrayRecursive($list); $JSON_Response = Utils::getStatus(200); $JSON_Response['data'] = array("count" => count($list), "games" => $list); if(count($list) > 0) @@ -244,7 +244,7 @@ if($has_next_page = count($list) > $limit) unset($list[$limit]); - Utils::htmlspecialchars_decodeArrayRecursive($list); + CommonUtils::htmlspecialchars_decodeArrayRecursive($list); $JSON_Response = Utils::getStatus(200); $JSON_Response['data'] = array("count" => count($list), "updates" => $list); $JSON_Response['pages'] = Utils::getJsonPageUrl($page, $has_next_page); @@ -265,7 +265,7 @@ $API = TGDB::getInstance(); $list = $API->GetPlatformsList($fields); - Utils::htmlspecialchars_decodeArrayRecursive($list); + CommonUtils::htmlspecialchars_decodeArrayRecursive($list); $JSON_Response = Utils::getStatus(200); $JSON_Response['data'] = array("count" => count($list), "platforms" => $list); if(isset($options['boxart'])) @@ -297,7 +297,7 @@ $API = TGDB::getInstance(); $list = $API->GetPlatforms($IDs, $fields); - Utils::htmlspecialchars_decodeArrayRecursive($list); + CommonUtils::htmlspecialchars_decodeArrayRecursive($list); $JSON_Response = Utils::getStatus(200); $JSON_Response['data'] = array("count" => count($list), "platforms" => $list); if(isset($options['boxart'])) @@ -336,7 +336,7 @@ $API = TGDB::getInstance(); $list = $API->SearchPlatformByName($searchTerm, $fields); - Utils::htmlspecialchars_decodeArrayRecursive($list); + CommonUtils::htmlspecialchars_decodeArrayRecursive($list); $JSON_Response = Utils::getStatus(200); $JSON_Response['data'] = array("count" => count($list), "platforms" => $list); if(isset($options['boxart'])) @@ -392,7 +392,7 @@ $API = TGDB::getInstance(); $list = $API->GetGenres(); - Utils::htmlspecialchars_decodeArrayRecursive($list); + CommonUtils::htmlspecialchars_decodeArrayRecursive($list); $JSON_Response = Utils::getStatus(200); $JSON_Response['data'] = array("count" => count($list), "genres" => $list); return $response->withJson($JSON_Response); @@ -406,7 +406,7 @@ $API = TGDB::getInstance(); $list = $API->GetDevsList(); - Utils::htmlspecialchars_decodeArrayRecursive($list); + CommonUtils::htmlspecialchars_decodeArrayRecursive($list); $JSON_Response = Utils::getStatus(200); $JSON_Response['data'] = array("count" => count($list), "developers" => $list); return $response->withJson($JSON_Response); @@ -421,7 +421,7 @@ $API = TGDB::getInstance(); $list = $API->GetPubsList(); - Utils::htmlspecialchars_decodeArrayRecursive($list); + CommonUtils::htmlspecialchars_decodeArrayRecursive($list); $JSON_Response = Utils::getStatus(200); $JSON_Response['data'] = array("count" => count($list), "publishers" => $list); return $response->withJson($JSON_Response); diff --git a/include/CommonUtils.class.php b/include/CommonUtils.class.php index c95caa6..aa39438 100644 --- a/include/CommonUtils.class.php +++ b/include/CommonUtils.class.php @@ -18,6 +18,21 @@ static function getImagesBaseURL() "large" => CommonUtils::$BOXART_BASE_URL . "large/", ]; } + + static function htmlspecialchars_decodeArrayRecursive(&$array) + { + foreach($array as &$sub_array_item) + { + if(is_array($sub_array_item) || is_object($sub_array_item)) + { + CommonUtils::htmlspecialchars_decodeArrayRecursive($sub_array_item); + } + else if(!is_numeric($sub_array_item) && !empty($sub_array_item)) + { + $sub_array_item = htmlspecialchars_decode($sub_array_item); + } + } + } } ?> diff --git a/website/include/DiscordUtils.class.php b/website/include/DiscordUtils.class.php index 83fb28d..ca80327 100644 --- a/website/include/DiscordUtils.class.php +++ b/website/include/DiscordUtils.class.php @@ -55,9 +55,8 @@ static function PostGameUpdate($_user, $old_game_data, $new_game_data, $type = 0 try { - //TODO: move htmlspecialchars_decodeArrayRecursive to CommonUtils:: - //htmlspecialchars_decodeArrayRecursive($old_game_data); - //htmlspecialchars_decodeArrayRecursive($new_game_data); + CommonUtils::htmlspecialchars_decodeArrayRecursive($old_game_data); + CommonUtils::htmlspecialchars_decodeArrayRecursive($new_game_data); $embeds = array(); $embeds["author"] = array( @@ -149,7 +148,7 @@ static function PostImageUpdate($_user, $game_id, $image_path, $type, $sub_type, { $API = TGDB::getInstance(); $game = $API->GetGameByID($game_id, 0, 1)[0]; - //htmlspecialchars_decodeArrayRecursive($game->game_title); + CommonUtils::htmlspecialchars_decodeArrayRecursive($game); $embeds = array(); $embeds["author"] = array( From 1c82cadd459dd0c60345ee2455ffcc8f4536ac62 Mon Sep 17 00:00:00 2001 From: Mahmood - Zer0xFF <5013823+Zer0xFF@users.noreply.github.com> Date: Mon, 29 Oct 2018 14:46:27 +0000 Subject: [PATCH 3/9] Enable addition/editing of alt names --- include/TGDB.API.php | 95 ++++++++++++++++++++++++++++++++++- website/actions/add_game.php | 4 +- website/actions/edit_game.php | 4 +- website/add_game.php | 50 +++++++++++++++++- website/edit_game.php | 56 +++++++++++++++++++++ 5 files changed, 202 insertions(+), 7 deletions(-) diff --git a/include/TGDB.API.php b/include/TGDB.API.php index c66e53c..4311f1a 100644 --- a/include/TGDB.API.php +++ b/include/TGDB.API.php @@ -1564,6 +1564,90 @@ function InsertUserEdits($user_id, $game_id, $type, $value, $subtype = '') return $sth->execute(); } + function InsertGamesAltName($game_id, $alt_name) + { + $dbh = $this->database->dbh; + $sth = $dbh->prepare("INSERT IGNORE INTO games_alts (games_id, name, SOUNDEX) VALUES (:games_id, :alt_name, soundex(:alt_name2));"); + $sth->bindValue(':games_id', $game_id, PDO::PARAM_INT); + $sth->bindValue(':alt_name', $alt_name, PDO::PARAM_STR); + $sth->bindValue(':alt_name2', $alt_name, PDO::PARAM_STR); + return $sth->execute(); + } + + function DeleteGamesAltName($game_id, $alt_name) + { + $dbh = $this->database->dbh; + $sth = $dbh->prepare("DELETE FROM games_alts WHERE games_id=:games_id AND name=:alt_name"); + $sth->bindValue(':games_id', $game_id, PDO::PARAM_INT); + $sth->bindValue(':alt_name', $alt_name, PDO::PARAM_STR); + return $sth->execute(); + } + + function UpdateGamesAltName($user, $games_id, $new_alt_names) + { + $dbh = $this->database->dbh; + + $is_changed = false; + + $current_alt_names = $this->GetGamesAlts($games_id, false); + if(!empty($current_alt_names[$games_id])) + { + $current_alt_names = $current_alt_names[$games_id]; + } + if(!empty($new_alt_names)) + { + foreach($new_alt_names as $new_alt_name) + { + if(!empty($new_alt_name)) + { + $valid_alt_name[] = $new_alt_name; + + if(!in_array($new_alt_name, $current_alt_names, true)) + { + $res = $this->InsertGamesAltName($games_id, $new_alt_name); + if(!$dbh->inTransaction() && !$res) + { + return false; + } + $is_changed = true; + } + } + } + } + + if(!empty($current_alt_names)) + { + foreach($current_alt_names as $current_alt_name) + { + if(!in_array($current_alt_name, $new_alt_names, true)) + { + $res = $this->DeleteGamesAltName($games_id, $current_alt_name); + if(!$dbh->inTransaction() && !$res) + { + return false; + } + $is_changed = true; + } + } + } + + if($is_changed) + { + $valid_alt_name = array_unique($valid_alt_name); + if(!empty($valid_alt_name)) + { + $qouted_alt_names = array(); + foreach($valid_alt_name as $alt_name) + { + $qouted_alt_names[] = "\"" . $alt_name . "\""; + } + $alt_str = implode(", ", $qouted_alt_names); + $this->InsertUserEdits($user, $games_id, "alternates", "[$alt_str]"); + } + } + return true; + } + function InsertGamesGenre($game_id, $genres_id) { $dbh = $this->database->dbh; @@ -1785,7 +1869,7 @@ function UpdateGamesPub($user_id, $games_id, $new_ids) return true; } - function UpdateGame($user_id, $game_id, $game_title, $overview, $youtube, $release_date, $players, $coop, $new_developers, $new_publishers, $new_genres, $ratings) + function UpdateGame($user_id, $game_id, $game_title, $overview, $youtube, $release_date, $players, $coop, $new_developers, $new_publishers, $new_genres, $ratings, $alternate_names) { $dbh = $this->database->dbh; { @@ -1803,6 +1887,8 @@ function UpdateGame($user_id, $game_id, $game_title, $overview, $youtube, $relea } { + $this->UpdateGamesAltName($user_id, $game_id, $alternate_names); + if(!empty($new_genres)) { $this->UpdateGamesGenre($user_id, $game_id, $new_genres); @@ -1963,7 +2049,7 @@ function DeleteGame($user_id, $games_id) return $dbh->commit(); } - function InsertGame($user_id, $game_title, $overview, $youtube, $release_date, $players, $coop, $new_developers, $new_publishers, $platform, $new_genres, $ratings) + function InsertGame($user_id, $game_title, $overview, $youtube, $release_date, $players, $coop, $new_developers, $new_publishers, $platform, $new_genres, $ratings, $alternate_names) { $game_id = 0; $dbh = $this->database->dbh; @@ -2033,6 +2119,11 @@ function InsertGame($user_id, $game_title, $overview, $youtube, $release_date, $ $this->UpdateGamesPub($user_id, $game_id, $new_publishers); } + if(!empty($alternate_names)) + { + $this->UpdateGamesAltName($user_id, $game_id, $alternate_names); + } + $GameArrayFields = ['game_title', 'overview', 'release_date', 'players', 'coop', 'youtube', 'platform', 'rating']; foreach($GameArrayFields as $key) { diff --git a/website/actions/add_game.php b/website/actions/add_game.php index d8e2bd6..0abeb4a 100644 --- a/website/actions/add_game.php +++ b/website/actions/add_game.php @@ -54,11 +54,11 @@ function returnJSONAndDie($code, $msg) $API = TGDB::getInstance(); $res = $API->InsertGame($_user->GetUserID(), $_REQUEST['game_title'], $_REQUEST['overview'], $_REQUEST['youtube'], $_REQUEST['release_date'], - $_REQUEST['players'], $_REQUEST['coop'], $_REQUEST['developers'], $_REQUEST['publishers'], $_REQUEST['platform'], $_REQUEST['genres'], $_REQUEST['rating']); + $_REQUEST['players'], $_REQUEST['coop'], $_REQUEST['developers'], $_REQUEST['publishers'], $_REQUEST['platform'], $_REQUEST['genres'], $_REQUEST['rating'], $_REQUEST['alternate_names']); if($res) { - $filters = ['game_title' => true, 'overview' => true, 'youtube' => true, 'release_date' => true, 'players' => true, 'coop' => true, 'developers' => true, 'publishers' => true, 'genres' => true, 'rating' => true]; + $filters = ['game_title' => true, 'overview' => true, 'youtube' => true, 'release_date' => true, 'players' => true, 'coop' => true, 'developers' => true, 'publishers' => true, 'genres' => true, 'rating' => true, 'alternates' => true]; $new_game_data = $API->GetGameByID($res, 0, 1, $filters)[0]; DiscordUtils::PostGameUpdate($_user, [], $new_game_data, 0); returnJSONAndDie(1, $res); diff --git a/website/actions/edit_game.php b/website/actions/edit_game.php index ef58d06..9f9955a 100644 --- a/website/actions/edit_game.php +++ b/website/actions/edit_game.php @@ -56,12 +56,12 @@ function returnJSONAndDie($code, $msg) try { - $filters = ['game_title' => true, 'overview' => true, 'youtube' => true, 'release_date' => true, 'players' => true, 'coop' => true, 'developers' => true, 'publishers' => true, 'genres' => true, 'rating' => true]; + $filters = ['game_title' => true, 'overview' => true, 'youtube' => true, 'release_date' => true, 'players' => true, 'coop' => true, 'developers' => true, 'publishers' => true, 'genres' => true, 'rating' => true, 'alternates' => true]; $API = TGDB::getInstance(); $old_game_data = $API->GetGameByID($_REQUEST['game_id'], 0, 1, $filters)[0]; $res = $API->UpdateGame( $_user->GetUserID(), $_REQUEST['game_id'], $_REQUEST['game_title'], $_REQUEST['overview'], $_REQUEST['youtube'], $_REQUEST['release_date'], - $_REQUEST['players'], $_REQUEST['coop'], $_REQUEST['developers'], $_REQUEST['publishers'], $_REQUEST['genres'], $_REQUEST['rating']); + $_REQUEST['players'], $_REQUEST['coop'], $_REQUEST['developers'], $_REQUEST['publishers'], $_REQUEST['genres'], $_REQUEST['rating'], $_REQUEST['alternate_names']); if($res) { diff --git a/website/add_game.php b/website/add_game.php index db71219..41f4fec 100644 --- a/website/add_game.php +++ b/website/add_game.php @@ -218,7 +218,47 @@ function isJSON(json) } - + + print(); ?> @@ -274,6 +314,14 @@ function isJSON(json)

+
+
+ +
+ +
+
+

diff --git a/website/edit_game.php b/website/edit_game.php index f9e7221..7b67536 100644 --- a/website/edit_game.php +++ b/website/edit_game.php @@ -488,6 +488,46 @@ function delete_image(image_id) }); }); + + print(); ?> @@ -558,6 +598,22 @@ function delete_image(image_id)

+
+ alternates) && !empty($alt_name = array_shift($Game->alternates))) : ?> +
+ +
+ +
+
+ +
+ +
+ +
+
+

From 8a46f8c49c19d7a73f82c38b8c0cd123f2365636 Mon Sep 17 00:00:00 2001 From: Mahmood - Zer0xFF <5013823+Zer0xFF@users.noreply.github.com> Date: Mon, 29 Oct 2018 15:19:57 +0000 Subject: [PATCH 4/9] Cleanup --- include/TGDB.API.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/include/TGDB.API.php b/include/TGDB.API.php index 4311f1a..2711039 100644 --- a/include/TGDB.API.php +++ b/include/TGDB.API.php @@ -1275,7 +1275,7 @@ function GetUserEdits($condition = '', $order = '', $offset = 0, $limit = 100) $res = $sth->fetchAll(PDO::FETCH_OBJ); foreach($res as $edit) { - if($edit->type == 'genres' || $edit->type == 'developers' || $edit->type == 'publishers') + if($edit->type == 'genres' || $edit->type == 'developers' || $edit->type == 'publishers' || $edit->type == 'alternates') { $edit->value = json_decode($edit->value); } @@ -1596,6 +1596,11 @@ function UpdateGamesAltName($user, $games_id, $new_alt_names) } if(!empty($new_alt_names)) { + foreach($new_alt_names as &$new_alt_name) + { + $new_alt_name = trim($new_alt_name); + } + unset($new_alt_name); foreach($new_alt_names as $new_alt_name) { if(!empty($new_alt_name)) From c6253c097bdf7e755e00068f81e1c76584a9da7c Mon Sep 17 00:00:00 2001 From: Mahmood - Zer0xFF <5013823+Zer0xFF@users.noreply.github.com> Date: Mon, 29 Oct 2018 15:23:32 +0000 Subject: [PATCH 5/9] Cleanup --- include/TGDB.API.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/include/TGDB.API.php b/include/TGDB.API.php index 2711039..80ba590 100644 --- a/include/TGDB.API.php +++ b/include/TGDB.API.php @@ -1275,9 +1275,14 @@ function GetUserEdits($condition = '', $order = '', $offset = 0, $limit = 100) $res = $sth->fetchAll(PDO::FETCH_OBJ); foreach($res as $edit) { - if($edit->type == 'genres' || $edit->type == 'developers' || $edit->type == 'publishers' || $edit->type == 'alternates') + //Note, these listing return a json array of data, thus must be decoded to any array to be encoded correctly later + switch($edit->type) { - $edit->value = json_decode($edit->value); + case 'genres': + case 'developers': + case 'publishers': + case 'alternates': + $edit->value = json_decode($edit->value); } } return $res; From c0598c4e8be1ea720945c30d21924c8b16854a37 Mon Sep 17 00:00:00 2001 From: Mahmood - Zer0xFF <5013823+Zer0xFF@users.noreply.github.com> Date: Mon, 29 Oct 2018 17:58:09 +0000 Subject: [PATCH 6/9] Cleanup --- include/TGDB.API.php | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/include/TGDB.API.php b/include/TGDB.API.php index 80ba590..138e5cd 100644 --- a/include/TGDB.API.php +++ b/include/TGDB.API.php @@ -1646,13 +1646,7 @@ function UpdateGamesAltName($user, $games_id, $new_alt_names) $valid_alt_name = array_unique($valid_alt_name); if(!empty($valid_alt_name)) { - $qouted_alt_names = array(); - foreach($valid_alt_name as $alt_name) - { - $qouted_alt_names[] = "\"" . $alt_name . "\""; - } - $alt_str = implode(", ", $qouted_alt_names); - $this->InsertUserEdits($user, $games_id, "alternates", "[$alt_str]"); + $this->InsertUserEdits($user_id, $games_id, "alternates", json_encode($valid_alt_name)); } } return true; @@ -1726,8 +1720,7 @@ function UpdateGamesGenre($user_id, $games_id, $new_ids) $valid_ids = array_unique($valid_ids); if(!empty($valid_ids)) { - $genres_str = implode(",", $valid_ids); - $this->InsertUserEdits($user_id, $games_id, "genres", "[$genres_str]"); + $this->InsertUserEdits($user_id, $games_id, "genres", json_encode($valid_ids, JSON_NUMERIC_CHECK)); } } return true; @@ -1799,8 +1792,7 @@ function UpdateGamesDev($user_id, $games_id, $new_ids) $valid_ids = array_unique($valid_ids); if(!empty($valid_ids)) { - $ids_str = implode(",", $valid_ids); - $this->InsertUserEdits($user_id, $games_id, "developers", "[$ids_str]"); + $this->InsertUserEdits($user_id, $games_id, "developers", json_encode($valid_ids, JSON_NUMERIC_CHECK)); } } return true; @@ -1872,8 +1864,7 @@ function UpdateGamesPub($user_id, $games_id, $new_ids) $valid_ids = array_unique($valid_ids); if(!empty($valid_ids)) { - $ids_str = implode(",", $valid_ids); - $this->InsertUserEdits($user_id, $games_id, "publishers", "[$ids_str]"); + $this->InsertUserEdits($user_id, $games_id, "publishers", json_encode($valid_ids, JSON_NUMERIC_CHECK)); } } return true; From 30e7b8c9e843063343d7422d7f7f537ec0cb646f Mon Sep 17 00:00:00 2001 From: Zer0xFF <5013823+Zer0xFF@users.noreply.github.com> Date: Tue, 30 Oct 2018 09:43:05 +0000 Subject: [PATCH 7/9] Cleanup Avoid potential warning, when alt/genre/dev/pub is cleared with no new entry added --- include/TGDB.API.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/include/TGDB.API.php b/include/TGDB.API.php index 138e5cd..cd93236 100644 --- a/include/TGDB.API.php +++ b/include/TGDB.API.php @@ -1593,6 +1593,7 @@ function UpdateGamesAltName($user, $games_id, $new_alt_names) $dbh = $this->database->dbh; $is_changed = false; + $valid_alt_name = array(); $current_alt_names = $this->GetGamesAlts($games_id, false); if(!empty($current_alt_names[$games_id])) @@ -1674,6 +1675,7 @@ function UpdateGamesGenre($user_id, $games_id, $new_ids) { $dbh = $this->database->dbh; $is_changed = false; + $valid_ids = array(); $list = $this->GetGenres(); @@ -1749,6 +1751,8 @@ function UpdateGamesDev($user_id, $games_id, $new_ids) $dbh = $this->database->dbh; $is_changed = false; + $valid_ids = array(); + $list = $this->GetDevsListByIDs($new_ids); $current_ids = $this->GetGamesDevs($games_id); if(!empty($current_ids[$games_id])) @@ -1821,6 +1825,8 @@ function UpdateGamesPub($user_id, $games_id, $new_ids) $dbh = $this->database->dbh; $is_changed = false; + $valid_ids = array(); + $list = $this->GetPubsListByIDs($new_ids); $current_ids = $this->GetGamesPubs($games_id); if(!empty($current_ids[$games_id])) From aef29fc2e0b7aedfeb1c65a8d42717173f1d1081 Mon Sep 17 00:00:00 2001 From: Mahmood - Zer0xFF <5013823+Zer0xFF@users.noreply.github.com> Date: Sun, 4 Nov 2018 12:57:09 +0000 Subject: [PATCH 8/9] during search, take into account alts names --- include/TGDB.API.php | 121 ++++++++++++++++--------------------------- 1 file changed, 44 insertions(+), 77 deletions(-) diff --git a/include/TGDB.API.php b/include/TGDB.API.php index cd93236..18dd9f9 100644 --- a/include/TGDB.API.php +++ b/include/TGDB.API.php @@ -186,55 +186,7 @@ function GetGameByID($IDs, $offset = 0, $limit = 20, $fields = array()) function SearchGamesByName($searchTerm, $offset = 0, $limit = 20, $fields = array()) { - $dbh = $this->database->dbh; - - $qry = "Select id, game_title, release_date, platform "; - - if(!empty($fields)) - { - foreach($fields as $key => $enabled) - { - if($enabled && $this->is_valid_games_col($key)) - { - $qry .= ", $key "; - } - } - } - - $qry .= " FROM games WHERE game_title LIKE :name OR game_title=:name2 OR soundex(game_title) LIKE soundex(:name3) OR soundex(game_title) LIKE soundex(:name4) - GROUP BY id ORDER BY CASE - WHEN game_title like :name5 THEN 3 - WHEN game_title like :name6 THEN 0 - WHEN game_title like :name7 THEN 1 - WHEN game_title like :name8 THEN 2 - ELSE 4 - END, game_title LIMIT :limit OFFSET :offset"; - - $sth = $dbh->prepare($qry); - - $sth->bindValue(':name', "%$searchTerm%"); - $sth->bindValue(':name2', $searchTerm); - $sth->bindValue(':name3', "$searchTerm%"); - $sth->bindValue(':name4', "% %$searchTerm% %"); - - $sth->bindValue(':name5', "%$searchTerm"); - $sth->bindValue(':name6', $searchTerm); - $sth->bindValue(':name7', "$searchTerm%"); - $sth->bindValue(':name8', "% %$searchTerm% %"); - - - $sth->bindValue(':offset', $offset, PDO::PARAM_INT); - $sth->bindValue(':limit', $limit, PDO::PARAM_INT); - - if($sth->execute()) - { - $res = $sth->fetchAll(PDO::FETCH_OBJ); - if(!empty($res)) - { - $this->PopulateOtherData($res, $fields); - } - return $res; - } + return $this->SearchGamesByNameByPlatformID($searchTerm, '', $offset, $limit, $fields); } function SearchGamesByExactName($searchTerm, $offset = 0, $limit = 20, $fields = array()) @@ -278,19 +230,6 @@ function SearchGamesByNameByPlatformID($searchTerm, $IDs, $offset = 0, $limit = { $dbh = $this->database->dbh; - $qry = "Select id, game_title, release_date, platform "; - - if(!empty($fields)) - { - foreach($fields as $key => $enabled) - { - if($enabled && $this->is_valid_games_col($key)) - { - $qry .= ", $key "; - } - } - } - $PlatformIDs = array(); if(is_array($IDs)) { @@ -307,21 +246,45 @@ function SearchGamesByNameByPlatformID($searchTerm, $IDs, $offset = 0, $limit = $PlatformIDs = $IDs; } - $qry .= " FROM games WHERE "; + $qry = "Select games.id, games.game_title, games.release_date, games.platform "; + if(!empty($fields)) + { + foreach($fields as $key => $enabled) + { + if($enabled && $this->is_valid_games_col($key)) + { + $qry .= ", games.$key "; + } + } + } + $qry .= " FROM games, + ( + SELECT games_names_merged.id, @rank := @rank + 1 AS rank FROM + ( + ( + SELECT games_id as id, name as game_title, SOUNDEX from games_alts + WHERE name LIKE :name OR name=:name2 OR SOUNDEX LIKE soundex(:name3) OR SOUNDEX LIKE soundex(:name4) + ) + UNION + ( + SELECT id, game_title, SOUNDEX from games + WHERE game_title LIKE :name_2 OR game_title=:name_2_2 OR SOUNDEX LIKE soundex(:name_2_3) OR SOUNDEX LIKE soundex(:name_2_4) + ) + ) games_names_merged, (SELECT @rank := 0) t1 + ORDER BY CASE + WHEN game_title like :name_3_2 THEN 0 + WHEN game_title like :name_3_3 THEN 1 + WHEN game_title like :name_3_4 THEN 2 + WHEN game_title like :name_3 THEN 3 + ELSE 4 + END, game_title + ) games_ordered where games.id = games_ordered.id "; if(!empty($PlatformIDs)) { - $qry .= " platform IN ($PlatformIDs) AND "; + $qry .= " AND games.platform IN ($PlatformIDs) "; } - - $qry .= " (game_title LIKE :name OR game_title=:name2 OR soundex(game_title) LIKE soundex(:name3) OR soundex(game_title) LIKE soundex(:name4)) - GROUP BY id ORDER BY CASE - WHEN game_title like :name5 THEN 3 - WHEN game_title like :name6 THEN 0 - WHEN game_title like :name7 THEN 1 - WHEN game_title like :name8 THEN 2 - ELSE 4 - END, game_title LIMIT :limit OFFSET :offset"; + $qry .= "GROUP BY games.id ORDER BY MIN(games_ordered.rank) LIMIT :limit OFFSET :offset"; $sth = $dbh->prepare($qry); @@ -330,11 +293,15 @@ function SearchGamesByNameByPlatformID($searchTerm, $IDs, $offset = 0, $limit = $sth->bindValue(':name3', "$searchTerm%"); $sth->bindValue(':name4', "% %$searchTerm% %"); - $sth->bindValue(':name5', "%$searchTerm"); - $sth->bindValue(':name6', $searchTerm); - $sth->bindValue(':name7', "$searchTerm%"); - $sth->bindValue(':name8', "% %$searchTerm% %"); + $sth->bindValue(':name_2', "%$searchTerm%"); + $sth->bindValue(':name_2_2', $searchTerm); + $sth->bindValue(':name_2_3', "$searchTerm%"); + $sth->bindValue(':name_2_4', "% %$searchTerm% %"); + $sth->bindValue(':name_3', "%$searchTerm"); + $sth->bindValue(':name_3_2', $searchTerm); + $sth->bindValue(':name_3_3', "$searchTerm%"); + $sth->bindValue(':name_3_4', "% %$searchTerm% %"); $sth->bindValue(':offset', $offset, PDO::PARAM_INT); $sth->bindValue(':limit', $limit, PDO::PARAM_INT); From f2a544c91b5c2cb6d75675dea0effdaa5d49480b Mon Sep 17 00:00:00 2001 From: Mahmood - Zer0xFF <5013823+Zer0xFF@users.noreply.github.com> Date: Sun, 4 Nov 2018 22:08:01 +0000 Subject: [PATCH 9/9] Fix typo --- include/TGDB.API.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/TGDB.API.php b/include/TGDB.API.php index 18dd9f9..cbc36fa 100644 --- a/include/TGDB.API.php +++ b/include/TGDB.API.php @@ -1555,7 +1555,7 @@ function DeleteGamesAltName($game_id, $alt_name) return $sth->execute(); } - function UpdateGamesAltName($user, $games_id, $new_alt_names) + function UpdateGamesAltName($user_id, $games_id, $new_alt_names) { $dbh = $this->database->dbh;