From a324f341e4ecfa3b04a428347950537af41304a2 Mon Sep 17 00:00:00 2001 From: Dorian Stoll Date: Tue, 17 Oct 2017 15:29:06 +0200 Subject: [PATCH 1/5] Start to work on multigame support --- index.php | 2 +- scripts/browse-list.js | 14 ++++----- scripts/create.js | 8 ++--- scripts/{index.js => game.js} | 16 +++++----- scripts/mod.js | 46 +++++++++++++++-------------- scripts/sdb.js | 6 ++-- scripts/update.js | 38 +++++++++++++----------- scripts/view-profile.js | 20 ++++++------- src/browse.php | 12 +++++--- src/mods.php | 12 +++++--- templates/browse-list.html | 12 ++++++-- templates/create.html | 1 + templates/{index.html => game.html} | 3 +- templates/layout.html | 21 ++++++++----- templates/mod.html | 4 +-- 15 files changed, 120 insertions(+), 95 deletions(-) rename scripts/{index.js => game.js} (76%) rename templates/{index.html => game.html} (98%) diff --git a/index.php b/index.php index 4ab93be..395d9b4 100644 --- a/index.php +++ b/index.php @@ -34,7 +34,7 @@ $view->getEnvironment()->addGlobal('irc_channel', $container->get('settings')['irc-channel']); $view->getEnvironment()->addGlobal('donation_link', $container->get('settings')['donation-link']); $view->getEnvironment()->addGlobal('backend_url', $container->get('settings')['backend-url']); - $view->getEnvironment()->addGlobal('gameshort', $container->get('settings')['gameshort']); + // $view->getEnvironment()->addGlobal('gameshort', $container->get('settings')['gameshort']); $view->getEnvironment()->addGlobal('debug', $container->get('settings')['debug']); $view->getEnvironment()->addGlobal('update_interval', $container->get('settings')['update-interval']); $view->getEnvironment()->addGlobal('registration', $container->get('settings')['registration']); diff --git a/scripts/browse-list.js b/scripts/browse-list.js index 2f3cc50..dd9f9e5 100644 --- a/scripts/browse-list.js +++ b/scripts/browse-list.js @@ -13,9 +13,9 @@ function fillBrowseList() { browseURL = "/api/browse/" + gameshort + "/featured?site=" + page + "&count=30"; } when(getJSON(backend + '/api/users/current'), - getJSON(backend + '/api/mods/' + gameshort), - getJSON(backend + browseURL)). - done(function(currentUser, mods, browse) { + getJSON(backend + '/api/mods/' + gameshort), + getJSON(backend + browseURL)). + done(function(currentUser, mods, browse) { app = new Vue({ el: '#site', data: { @@ -52,10 +52,10 @@ function updateBrowseList() { if (url == "/browse/featured") { browseURL = "/api/browse/" + gameshort + "/featured?site=" + page + "&count=30"; } - swhen(getJSON(backend + '/api/users/current'), - getJSON(backend + '/api/mods/' + gameshort), - getJSON(backend + browseURL)). - done(function(currentUser, mods, browse) { + when(getJSON(backend + '/api/users/current'), + getJSON(backend + '/api/mods/' + gameshort), + getJSON(backend + browseURL)). + done(function(currentUser, mods, browse) { app.$data.currentUser = currentUser.error ? null : currentUser.data; app.$data.mods = mods; app.$data.browse = browse; diff --git a/scripts/create.js b/scripts/create.js index e30ec50..3b65b14 100644 --- a/scripts/create.js +++ b/scripts/create.js @@ -1,7 +1,7 @@ function fillCreate() { when(getJSON(backend + '/api/users/current'), - getJSON(backend + '/api/games/' + gameshort + '/versions')). - done(function(currentUser, gameversions) { + getJSON(backend + '/api/games/' + gameshort + '/versions')). + done(function(currentUser, gameversions) { if (currentUser.error) { window.location.href = "{{ path_for('accounts.login') }}"; return; @@ -39,8 +39,8 @@ function fillCreate() { function updateCreate() { when(getJSON(backend + '/api/users/current'), - getJSON(backend + '/api/games/' + gameshort + '/versions')). - done(function(currentUser, gameversions) { + getJSON(backend + '/api/games/' + gameshort + '/versions')). + done(function(currentUser, gameversions) { if (currentUser.error) { window.location.href = "{{ path_for('accounts.login') }}"; return; diff --git a/scripts/index.js b/scripts/game.js similarity index 76% rename from scripts/index.js rename to scripts/game.js index e942409..f3b7486 100644 --- a/scripts/index.js +++ b/scripts/game.js @@ -1,9 +1,9 @@ function fillIndex() { when(getJSON(backend + '/api/users/current'), - getJSON(backend + '/api/mods/' + gameshort), - getJSON(backend + '/api/users'), - getJSON(backend + '/api/browse/' + gameshort)). - done(function(currentUser, mods, users, browse) { + getJSON(backend + '/api/mods/' + gameshort), + getJSON(backend + '/api/users'), + getJSON(backend + '/api/browse/' + gameshort)). + done(function(currentUser, mods, users, browse) { app = new Vue({ el: '#site', data: { @@ -32,10 +32,10 @@ function fillIndex() { function updateIndex() { when(getJSON(backend + '/api/users/current'), - getJSON(backend + '/api/mods/' + gameshort), - getJSON(backend + '/api/users'), - getJSON(backend + '/api/browse/' + gameshort)) - done(function(currentUser, mods, users, browse) { + getJSON(backend + '/api/mods/' + gameshort), + getJSON(backend + '/api/users'), + getJSON(backend + '/api/browse/' + gameshort)) + done(function(currentUser, mods, users, browse) { app.$data.currentUser = currentUser.error ? null : currentUser.data; app.$data.mods = mods; app.$data.users = users; diff --git a/scripts/mod.js b/scripts/mod.js index 429baa9..529a3c9 100644 --- a/scripts/mod.js +++ b/scripts/mod.js @@ -1,19 +1,20 @@ function fillMod() { - getJSON(backend + '/api/mods/' + gameshort + '/' + mod_id, function(mod) { + getJSON(backend + '/api/adapter/mods/' + mod_id, function(mod) { if (mod.error) { window.location.href = "{{ path_for('not-found') }}"; } + var gameshort = mod.data.game_short; when(getJSON(backend + '/api/users/current'), - getJSON(backend + '/api/games/' + gameshort), - getJSON(backend + '/api/games/' + gameshort + '/versions'), - getJSON(backend + '/api/users/' + mod.data.user), - getJSON(backend + '/api/mods/' + gameshort + '/' + mod_id + '/stats/downloads'), - getJSON(backend + '/api/mods/' + gameshort + '/' + mod_id + '/stats/follower'), - getJSON(backend + '/api/featured/' + gameshort), - hasPermission('mods-edit', true, {'gameshort': gameshort, 'modid': mod_id}), - hasPermission('mods-feature', true, {'gameshort': gameshort}), - hasPermission('mods-remove', true, {'gameshort': gameshort, 'modid': mod_id})). - done(function(currentUser, game, gameversions, modUser, download_stats, follower_stats, featured, editable, deletable, featureable) { + getJSON(backend + '/api/games/' + gameshort), + getJSON(backend + '/api/games/' + gameshort + '/versions'), + getJSON(backend + '/api/users/' + mod.data.user), + getJSON(backend + '/api/mods/' + gameshort + '/' + mod_id + '/stats/downloads'), + getJSON(backend + '/api/mods/' + gameshort + '/' + mod_id + '/stats/follower'), + getJSON(backend + '/api/featured/' + gameshort), + hasPermission('mods-edit', true, {'gameshort': gameshort, 'modid': mod_id}), + hasPermission('mods-feature', true, {'gameshort': gameshort}), + hasPermission('mods-remove', true, {'gameshort': gameshort, 'modid': mod_id})). + done(function(currentUser, game, gameversions, modUser, download_stats, follower_stats, featured, editable, deletable, featureable) { var mod_users = {} mod_users[mod.data.user] = modUser.data; mod.data.shared_authors.forEach(function(entry) { @@ -77,21 +78,22 @@ function fillMod() { } function updateMod() { - getJSON(backend + '/api/mods/' + gameshort + '/' + mod_id, function(mod) { + getJSON(backend + '/api/adapter/mods/' + mod_id, function(mod) { if (mod.error) { window.location.href = "{{ path_for('not-found') }}"; } + var gameshort = mod.data.game_short; when(getJSON(backend + '/api/users/current'), - getJSON(backend + '/api/games/' + gameshort), - getJSON(backend + '/api/games/' + gameshort + '/versions'), - getJSON(backend + '/api/users/' + mod.data.user), - getJSON(backend + '/api/mods/' + gameshort + '/' + mod_id + '/stats/downloads'), - getJSON(backend + '/api/mods/' + gameshort + '/' + mod_id + '/stats/follower'), - getJSON(backend + '/api/featured/' + gameshort), - hasPermission('mods-edit', true, {'gameshort': gameshort, 'modid': mod_id}), - hasPermission('mods-feature', true, {'gameshort': gameshort}), - hasPermission('mods-remove', true, {'gameshort': gameshort, 'modid': mod_id})). - done(function(currentUser, game, gameversions, modUser, download_stats, follower_stats, featured, editable, deletable, featureable) { + getJSON(backend + '/api/games/' + gameshort), + getJSON(backend + '/api/games/' + gameshort + '/versions'), + getJSON(backend + '/api/users/' + mod.data.user), + getJSON(backend + '/api/mods/' + gameshort + '/' + mod_id + '/stats/downloads'), + getJSON(backend + '/api/mods/' + gameshort + '/' + mod_id + '/stats/follower'), + getJSON(backend + '/api/featured/' + gameshort), + hasPermission('mods-edit', true, {'gameshort': gameshort, 'modid': mod_id}), + hasPermission('mods-feature', true, {'gameshort': gameshort}), + hasPermission('mods-remove', true, {'gameshort': gameshort, 'modid': mod_id})). + done(function(currentUser, game, gameversions, modUser, download_stats, follower_stats, featured, editable, deletable, featureable) { var mod_users = {} mod_users[mod.data.user] = modUser.data; mod.data.shared_authors.forEach(function(entry) { diff --git a/scripts/sdb.js b/scripts/sdb.js index cadaf28..afc84ce 100644 --- a/scripts/sdb.js +++ b/scripts/sdb.js @@ -148,11 +148,11 @@ function followMod(user, mod, callback) { window.location.href = "path_for('register')"; return; } - getJSON(backend + '/api/mods/' + gameshort + '/' + mod.id + '/follow', callback) + getJSON(backend + '/api/mods/' + mod.game_short + '/' + mod.id + '/follow', callback) } function unfollowMod(mod, callback) { - getJSON(backend + '/api/mods/' + gameshort + '/' + mod.id + '/unfollow', callback); + getJSON(backend + '/api/mods/' + mod.game_short + '/' + mod.id + '/unfollow', callback); } function hasPermission(permission, pub, params, callback) { @@ -267,7 +267,7 @@ function createMod(name, gameshort, shortDescription, license, version, gameVers } function updateMod(mod, version, gameVersion, notifyFollowers, isBeta, changelog, _zipFile, callback) { - postJSON(backend + '/api/mods/' + gameshort + '/' + mod.id + '/versions', { + postJSON(backend + '/api/mods/' + mod.game_short + '/' + mod.id + '/versions', { 'version': version, 'game-version': gameVersion, 'notify-followers': notifyFollowers, diff --git a/scripts/update.js b/scripts/update.js index 9589379..f403f0d 100644 --- a/scripts/update.js +++ b/scripts/update.js @@ -1,17 +1,17 @@ function fillUpdate() { - hasPermission('mods-edit', true, {'gameshort': gameshort, 'modid': mod_id}, function(canUpdate) { + getJSON(backend + '/api/adapter/mods/' + mod_id, function (mod) { + if (mod.error) { + window.location.href = "{{ path_for('not-found') }}"; + return; + } + hasPermission('mods-edit', true, {'gameshort': mod.data.game_short, 'modid': mod_id}, function(canUpdate) { if (!canUpdate) { window.location.href = "{{ path_for('not-found') }}"; return; } when(getJSON(backend + '/api/users/current'), - getJSON(backend + '/api/games/' + gameshort + '/versions'), - getJSON(backend + '/api/mods/' + gameshort + '/' + mod_id)). - done(function(currentUser, gameversions, mod) { - if (mod.error) { - window.location.href = "{{ path_for('not-found') }}"; - return; - } + getJSON(backend + '/api/games/' + mod.data.game_short + '/versions')). + done(function(currentUser, gameversions) { app = new Vue({ el: '#site', data: { @@ -39,27 +39,29 @@ function fillUpdate() { }, false); $('#submit').removeAttr('disabled'); $.loadingBlockHide(); - })}); + })})}); } + + function updateUpdate() { -hasPermission('mods-edit', true, {'gameshort': gameshort, 'modid': mod_id}, function(canUpdate) { + getJSON(backend + '/api/adapter/mods/' + mod_id, function (mod) { + if (mod.error) { + window.location.href = "{{ path_for('not-found') }}"; + return; + } + hasPermission('mods-edit', true, {'gameshort': mod.data.game_short, 'modid': mod_id}, function(canUpdate) { if (!canUpdate) { window.location.href = "{{ path_for('not-found') }}"; return; } when(getJSON(backend + '/api/users/current'), - getJSON(backend + '/api/games/' + gameshort + '/versions'), - getJSON(backend + '/api/mods/' + gameshort + '/' + mod_id)). - done(function(currentUser, gameversions, mod) { - if (mod.error) { - window.location.href = "{{ path_for('not-found') }}"; - return; - } + getJSON(backend + '/api/games/' + mod.data.game_short + '/versions')). + done(function(currentUser, gameversions) { app.$data.currentUser = currentUser.error ? null : currentUser.data; app.$data.game_versions = gameversions.data; app.$data.window = window; - })}); + })})}); } function onUploadClick() { diff --git a/scripts/view-profile.js b/scripts/view-profile.js index 9f68568..35650de 100644 --- a/scripts/view-profile.js +++ b/scripts/view-profile.js @@ -4,11 +4,11 @@ function fillViewProfile() { window.location.href = "{{ path_for('not-found') }}"; } when(getJSON(backend + '/api/users/current'), - hasPermission('user-edit', false, {'userid': user_id}), - hasPermission('admin-impersonate', true, {'userid': user_id}), - hasPermission('admin-confirm', true, {}), - hasPermission('view-users-full', false, {})). - done(function (currentUser, editable, canImpersonate, canConfirm, viewFull) { + hasPermission('user-edit', false, {'userid': user_id}), + hasPermission('admin-impersonate', true, {'userid': user_id}), + hasPermission('admin-confirm', true, {}), + hasPermission('view-users-full', false, {})). + done(function (currentUser, editable, canImpersonate, canConfirm, viewFull) { app = new Vue({ el: '#site', data: { @@ -42,11 +42,11 @@ function updateViewProfile() { window.location.href = "{{ path_for('not-found') }}"; } when(getJSON(backend + '/api/users/current'), - hasPermission('user-edit', false, {'userid': user_id}), - hasPermission('admin-impersonate', true, {'userid': user_id}), - hasPermission('admin-confirm', true, {}), - hasPermission('view-users-full', false, {})). - done(function (currentUser, editable, canImpersonate, canConfirm, viewFull) { + hasPermission('user-edit', false, {'userid': user_id}), + hasPermission('admin-impersonate', true, {'userid': user_id}), + hasPermission('admin-confirm', true, {}), + hasPermission('view-users-full', false, {})). + done(function (currentUser, editable, canImpersonate, canConfirm, viewFull) { app.$data.currentUser = currentUser.error ? null : currentUser.data; app.$data.user = user.data; app.$data.editable = editable; diff --git a/src/browse.php b/src/browse.php index 3962406..39745c2 100644 --- a/src/browse.php +++ b/src/browse.php @@ -1,6 +1,6 @@ get('/browse/new[/{page}]', function($request, $response, $args) { +$app->get('/{gameshort}/browse/new[/{page}]', function($request, $response, $args) { $page = ""; if (isset($args['page'])) { $page = $args['page']; @@ -8,6 +8,7 @@ $page = '1'; } return $this->view->render($response, 'templates/browse-list.html', [ + 'gameshort' => $args['gameshort'], 'page' => $page, 'url' => '/browse/new', 'name' => 'Newest Mods', @@ -16,7 +17,7 @@ ]); })->setName('browse.new'); -$app->get('/browse/updated[/{page}]', function($request, $response, $args) { +$app->get('/{gameshort}/browse/updated[/{page}]', function($request, $response, $args) { $page = ""; if (isset($args['page'])) { $page = $args['page']; @@ -24,6 +25,7 @@ $page = '1'; } return $this->view->render($response, 'templates/browse-list.html', [ + 'gameshort' => $args['gameshort'], 'page' => $page, 'url' => '/browse/updated', 'name' => 'Recently Updated Mods', @@ -32,7 +34,7 @@ ]); })->setName('browse.updated'); -$app->get('/browse/top[/{page}]', function($request, $response, $args) { +$app->get('/{gameshort}/browse/top[/{page}]', function($request, $response, $args) { $page = ""; if (isset($args['page'])) { $page = $args['page']; @@ -40,6 +42,7 @@ $page = '1'; } return $this->view->render($response, 'templates/browse-list.html', [ + 'gameshort' => $args['gameshort'], 'page' => $page, 'url' => '/browse/top', 'name' => 'Popular Mods', @@ -48,7 +51,7 @@ ]); })->setName('browse.top'); -$app->get('/browse/featured[/{page}]', function($request, $response, $args) { +$app->get('/{gameshort}/browse/featured[/{page}]', function($request, $response, $args) { $page = ""; if (isset($args['page'])) { $page = $args['page']; @@ -56,6 +59,7 @@ $page = '1'; } return $this->view->render($response, 'templates/browse-list.html', [ + 'gameshort' => $args['gameshort'], 'page' => $page, 'url' => '/browse/featured', 'name' => 'Featured Mods', diff --git a/src/mods.php b/src/mods.php index 934744d..4c51363 100644 --- a/src/mods.php +++ b/src/mods.php @@ -6,17 +6,21 @@ ]); })->setName('mod.view'); -$app->get('/random', function($request, $response, $args) { +$app->get('{gameshort}/random', function($request, $response, $args) { $backend_url = $this->get('settings')['backend-url']; - $gameshort = $this->get('settings')['gameshort']; + // $gameshort = $this->get('settings')['gameshort']; + // TODO(TMSP): Add an general API for this (without gameshort as an URL param) + $gameshort = $args['gameshort']; $json = file_get_contents("http://{$backend_url}/api/mods/{$gameshort}/random"); $random_mod = json_decode($json, true); return $response->withRedirect("mod/{$random_mod['data']['id']}/{$random_mod['data']['name']}", 302); })->setName('mod.random'); -$app->get('/create/mod', function($request, $response, $args) { - return $this->view->render($response, 'templates/create.html'); +$app->get('{gameshort}/create/mod', function($request, $response, $args) { + return $this->view->render($response, 'templates/create.html', [ + 'gameshort' => $args['gameshort'] + ]); })->setName('mod.create'); $app->get('/update/mod/{id}[/{name}]', function($request, $response, $args) { diff --git a/templates/browse-list.html b/templates/browse-list.html index 58a39e9..0bfb7d8 100644 --- a/templates/browse-list.html +++ b/templates/browse-list.html @@ -1,4 +1,5 @@ {% extends "templates/layout.html" %} + {% block styles %} @@ -6,17 +7,20 @@ {% endblock %} + {% block scripts %} {% endblock %} + {% block title %} {% if search == "true" %} Search {{ site_name }} @@ -24,6 +28,7 @@ {{ name }} on {{ site_name }} {% endif %} {% endblock %} + {% block search %} {% endblock %} + {% block body %}
diff --git a/templates/create.html b/templates/create.html index ee34f6c..8f523e1 100644 --- a/templates/create.html +++ b/templates/create.html @@ -8,6 +8,7 @@ {% block scripts %} {% endblock %} diff --git a/templates/index.html b/templates/game.html similarity index 98% rename from templates/index.html rename to templates/game.html index 52d0765..bdc10e8 100644 --- a/templates/index.html +++ b/templates/game.html @@ -7,8 +7,9 @@ {% endblock %} {% block scripts %} - + {% endblock %} diff --git a/templates/layout.html b/templates/layout.html index d7d4335..65ad890 100644 --- a/templates/layout.html +++ b/templates/layout.html @@ -37,14 +37,19 @@ - - - -
+ + + + +
+
+ +
+
+
{% block nav %}
@@ -289,7 +289,7 @@

Version ${v.friendly_version} for Kerbal Space Program ${v.gameversio

No changelog provided

- + Download