From 2598a5014d3f9e8d874d620d7272b9b4da7a8e85 Mon Sep 17 00:00:00 2001 From: Bui Date: Sat, 27 Oct 2018 22:42:28 +0100 Subject: [PATCH] add game config --- .gitignore | 1 + app.py | 16 ++++++++++++++++ config.example.json | 3 +++ public/src/js/assets.js | 2 ++ public/src/js/controller.js | 2 +- public/src/js/loader.js | 4 ++++ public/src/js/loadsong.js | 6 +++--- public/src/js/songselect.js | 6 +++--- public/src/js/view.js | 2 +- 9 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 config.example.json diff --git a/.gitignore b/.gitignore index 120c2ed..0bc88c6 100644 --- a/.gitignore +++ b/.gitignore @@ -46,3 +46,4 @@ public/api taiko.db version.json public/index.html +config.json \ No newline at end of file diff --git a/app.py b/app.py index 1fa7c94..ed02b6f 100644 --- a/app.py +++ b/app.py @@ -163,6 +163,22 @@ def route_api_songs(): return jsonify(songs_out) +@app.route('/api/config') +def route_api_config(): + if os.path.isfile('config.json'): + config = json.load(open('config.json', 'r')) + else: + print 'WARNING: No config.json found, using default values' + config = { + 'songs_baseurl': ''.join([request.host_url, 'songs']) + '/' + } + + if not config.get('songs_baseurl'): + config['songs_baseurl'] = ''.join([request.host_url, 'songs']) + '/' + + return jsonify(config) + + def make_preview(song_id, song_type): song_path = 'public/songs/%s/main.mp3' % song_id prev_path = 'public/songs/%s/preview.mp3' % song_id diff --git a/config.example.json b/config.example.json new file mode 100644 index 0000000..c5291d4 --- /dev/null +++ b/config.example.json @@ -0,0 +1,3 @@ +{ + "songs_baseurl": "" +} diff --git a/public/src/js/assets.js b/public/src/js/assets.js index 92da861..3c903f6 100644 --- a/public/src/js/assets.js +++ b/public/src/js/assets.js @@ -125,3 +125,5 @@ var assets = { "image": {}, "pages": {} } + +var gameConfig = {} diff --git a/public/src/js/controller.js b/public/src/js/controller.js index 08d2bfa..680afdb 100644 --- a/public/src/js/controller.js +++ b/public/src/js/controller.js @@ -7,7 +7,7 @@ class Controller{ this.touchEnabled = touchEnabled this.snd = this.multiplayer ? "_p" + this.multiplayer : "" - var backgroundURL = "/songs/" + this.selectedSong.folder + "/bg.png" + var backgroundURL = gameConfig.songs_baseurl + this.selectedSong.folder + "/bg.png" if(selectedSong.type === "tja"){ this.parsedSongData = new ParseTja(songData, selectedSong.difficulty, selectedSong.offset) diff --git a/public/src/js/loader.js b/public/src/js/loader.js index 59954b6..2ee3251 100644 --- a/public/src/js/loader.js +++ b/public/src/js/loader.js @@ -78,6 +78,10 @@ class Loader{ this.promises.push(this.ajax("/api/songs").then(songs => { assets.songs = JSON.parse(songs) })) + + this.promises.push(this.ajax("/api/config").then(conf => { + gameConfig = JSON.parse(conf) + })) assets.views.forEach(name => { var id = this.getFilename(name) diff --git a/public/src/js/loadsong.js b/public/src/js/loadsong.js index 3af964e..dc6c2d6 100644 --- a/public/src/js/loadsong.js +++ b/public/src/js/loadsong.js @@ -19,7 +19,7 @@ class loadSong{ resolve() }) img.id = "music-bg" - img.src = "/songs/" + id + "/bg.png" + img.src = gameConfig.songs_baseurl + id + "/bg.png" document.getElementById("assets").appendChild(img) })) @@ -34,7 +34,7 @@ class loadSong{ songObj.sound.gain = snd.musicGain resolve() }else{ - snd.musicGain.load("/songs/" + id + "/main.mp3").then(sound => { + snd.musicGain.load(gameConfig.songs_baseurl + id + "/main.mp3").then(sound => { songObj.sound = sound resolve() }, reject) @@ -51,7 +51,7 @@ class loadSong{ }) } getSongPath(selectedSong){ - var directory = "/songs/" + selectedSong.folder + "/" + var directory = gameConfig.songs_baseurl + selectedSong.folder + "/" if(selectedSong.type === "tja"){ return directory + "main.tja" }else{ diff --git a/public/src/js/songselect.js b/public/src/js/songselect.js index e5da1a1..1d428ac 100644 --- a/public/src/js/songselect.js +++ b/public/src/js/songselect.js @@ -1,11 +1,11 @@ class SongSelect{ constructor(fromTutorial, fadeIn, touchEnabled){ this.touchEnabled = touchEnabled - + loader.changePage("songselect") this.canvas = document.getElementById("song-sel-canvas") this.ctx = this.canvas.getContext("2d") - + this.songSkin = { "selected": { background: "#ffdb2c", @@ -1363,7 +1363,7 @@ class SongSelect{ var previewFilename = prvTime > 0.1 ? "/preview.mp3" : "/main.mp3" var loadPreview = previewFilename => { - return snd.previewGain.load("/songs/" + id + previewFilename) + return snd.previewGain.load(gameConfig.songs_baseurl + id + previewFilename) } songObj.preview_time = 0 diff --git a/public/src/js/view.js b/public/src/js/view.js index b34d7bc..dbc1c27 100644 --- a/public/src/js/view.js +++ b/public/src/js/view.js @@ -751,7 +751,7 @@ setBackground(){ var gameDiv = document.getElementById("game") var selectedSong = this.controller.selectedSong - var bg = "/songs/" + selectedSong.folder + "/bg.png" + var bg = gameConfig.songs_baseurl + selectedSong.folder + "/bg.png" if(selectedSong.defaultBg){ var categories = { "J-POP": 0,