diff --git a/build-apk.py b/build-apk.py index 45aafee..6916616 100644 --- a/build-apk.py +++ b/build-apk.py @@ -5,18 +5,22 @@ import shutil def replace_in_file(file_path, search_replace_pairs): - with open(file_path, 'r') as file: + with open(file_path, 'r', encoding='utf-8') as file: content = file.read() for search, replace in search_replace_pairs: content = re.sub(search, replace, content) - with open(file_path, 'w') as file: + with open(file_path, 'w', encoding='utf-8') as file: file.write(content) def process_files(root_dir, website_url): search_replace_pairs = [ (r'const baseUrl = window\.location\.origin;', f'const baseUrl = "{website_url}";'), (r'const socket = io\("/api/game"\);', f'const socket = io("{website_url}/api/game");'), - (r'const socket = io\("/api/social"\);', f'const socket = io("{website_url}/api/social");') + (r'const socket = io\("/api/social"\);', f'const socket = io("{website_url}/api/social");'), + (r'const socket2 = io\("/api/social"\);', f'const socket2 = io("{website_url}/api/social");'), + (r'const socket2 = io\("/api/game"\);', f'const socket2 = io("{website_url}/api/game");'), + (r'const permanent_socket2 = io\("/api/game"\);', f'const permanent_socket2 = io("{website_url}/api/game");'), + (r'const permanent_socket = io\("/api/social"\);', f'const permanent_socket = io("{website_url}/api/social");'), ] for root, dirs, files in os.walk(root_dir): @@ -26,9 +30,11 @@ def process_files(root_dir, website_url): replace_in_file(file_path, search_replace_pairs) def build_android(): - subprocess.run(['cordova', 'platform', 'rm', 'android']) - subprocess.run(['cordova', 'platform', 'add', 'android']) - subprocess.run(['cordova', 'build', 'android']) + os.chdir('mobile') + subprocess.run(['cordova', 'platform', 'rm', 'android'], shell=True) + subprocess.run(['cordova', 'platform', 'add', 'android'], shell=True) + subprocess.run(['cordova', 'build', 'android'], shell=True) + os.chdir('..') def restore_files(backup_dir, root_dir): for root, dirs, files in os.walk(backup_dir): diff --git a/local_revert.py b/local_revert.py new file mode 100644 index 0000000..eec6e90 --- /dev/null +++ b/local_revert.py @@ -0,0 +1,42 @@ +import os +import sys +import re +import subprocess +import shutil + +def replace_in_file(file_path, search_replace_pairs): + with open(file_path, 'r', encoding='utf-8') as file: + content = file.read() + for search, replace in search_replace_pairs: + content = re.sub(search, replace, content) + with open(file_path, 'w', encoding='utf-8') as file: + file.write(content) + +def process_files(root_dir, website_url): + search_replace_pairs = [ + (re.compile(fr'const permanent_socket = io\("({re.escape(website_url)}/api/social)"\);'), r'const permanent_socket = io("/api/social");'), + (re.compile(fr'const permanent_socket2 = io\("({re.escape(website_url)}/api/game)"\);'), r'const permanent_socket2 = io("/api/game");'), + (re.compile(fr'const socket2 = io\("({re.escape(website_url)}/api/game)"\);'), r'const socket2 = io("/api/game");'), + (re.compile(fr'const socket2 = io\("({re.escape(website_url)}/api/social)"\);'), r'const socket2 = io("/api/social");'), + (re.compile(fr'const socket = io\("({re.escape(website_url)}/api/social)"\);'), r'const socket = io("/api/social");'), + (re.compile(fr'const socket = io\("({re.escape(website_url)}/api/game)"\);'), r'const socket = io("/api/game");'), + (re.compile(fr'const baseUrl = "{re.escape(website_url)}";'), r'const baseUrl = window.location.origin;'), + ] + + for root, dirs, files in os.walk(root_dir): + for file in files: + if file.endswith('.html') or file.endswith('.js'): + file_path = os.path.join(root, file) + replace_in_file(file_path, search_replace_pairs) + +def main(): + if len(sys.argv) > 1: + website_url = sys.argv[1].rstrip('/') + else: + website_url = "https://quanridor.ps8.academy" + + root_dir = 'mobile/www' + process_files(root_dir, website_url) + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/mobile/www/js/achievements.js b/mobile/www/js/achievements.js index 58a4eb6..ac75d17 100644 --- a/mobile/www/js/achievements.js +++ b/mobile/www/js/achievements.js @@ -6,7 +6,7 @@ document.addEventListener("DOMContentLoaded", function () { return; } - const baseUrl = "https://quanridor.ps8.academy"; + const baseUrl = window.location.origin; fetch(`${baseUrl}/api/achievements`, { method: "GET", headers: { diff --git a/mobile/www/js/ai-game-mobile.js b/mobile/www/js/ai-game-mobile.js index eb85b99..d7d9d2a 100644 --- a/mobile/www/js/ai-game-mobile.js +++ b/mobile/www/js/ai-game-mobile.js @@ -1,4 +1,4 @@ -const socket = io("https://quanridor.ps8.academy/api/game"); +const socket = io("/api/game"); socket.on("connect", () => { console.log("Connected to server."); @@ -120,7 +120,7 @@ document.addEventListener("DOMContentLoaded", async () => { socket.on("gameCreated", (game) => { gameId = game._id; initializeGame(game); - //const baseUrl = "https://quanridor.ps8.academy"; + //const baseUrl = window.location.origin; //window.location.href = `${baseUrl}/ai-game.html?id=` + gameId; // Setting the game info display diff --git a/mobile/www/js/ai-game.js b/mobile/www/js/ai-game.js index eb85b99..d7d9d2a 100644 --- a/mobile/www/js/ai-game.js +++ b/mobile/www/js/ai-game.js @@ -1,4 +1,4 @@ -const socket = io("https://quanridor.ps8.academy/api/game"); +const socket = io("/api/game"); socket.on("connect", () => { console.log("Connected to server."); @@ -120,7 +120,7 @@ document.addEventListener("DOMContentLoaded", async () => { socket.on("gameCreated", (game) => { gameId = game._id; initializeGame(game); - //const baseUrl = "https://quanridor.ps8.academy"; + //const baseUrl = window.location.origin; //window.location.href = `${baseUrl}/ai-game.html?id=` + gameId; // Setting the game info display diff --git a/mobile/www/js/bot-choice-mobile.js b/mobile/www/js/bot-choice-mobile.js index 8ffe889..0f07149 100644 --- a/mobile/www/js/bot-choice-mobile.js +++ b/mobile/www/js/bot-choice-mobile.js @@ -1,4 +1,4 @@ -const socket = io("https://quanridor.ps8.academy/api/game"); +const socket = io("/api/game"); const AI0 = document.getElementById("AI0"); const AI1 = document.getElementById("AI1"); diff --git a/mobile/www/js/bot-choice.js b/mobile/www/js/bot-choice.js index 934ccce..e71ea35 100644 --- a/mobile/www/js/bot-choice.js +++ b/mobile/www/js/bot-choice.js @@ -1,4 +1,4 @@ -const socket = io("https://quanridor.ps8.academy/api/game"); +const socket = io("/api/game"); const AI0 = document.getElementById("AI0"); const AI1 = document.getElementById("AI1"); diff --git a/mobile/www/js/chatsocket.js b/mobile/www/js/chatsocket.js index 4416841..c308ae6 100644 --- a/mobile/www/js/chatsocket.js +++ b/mobile/www/js/chatsocket.js @@ -1,10 +1,10 @@ if (/Mobi|Android/i.test(navigator.userAgent)) { document.addEventListener("DOMContentLoaded", function () { - const permanent_socket = io("https://quanridor.ps8.academy/api/social"); - const permanent_socket2 = io("https://quanridor.ps8.academy/api/game"); + const permanent_socket = io("/api/social"); + const permanent_socket2 = io("/api/game"); const token = localStorage.getItem("token"); - const baseUrl = "https://quanridor.ps8.academy"; + const baseUrl = window.location.origin; fetch(`${baseUrl}/api/users`, { method: "GET", headers: { diff --git a/mobile/www/js/friend-list.js b/mobile/www/js/friend-list.js index b924e9d..f4d79e0 100644 --- a/mobile/www/js/friend-list.js +++ b/mobile/www/js/friend-list.js @@ -1,6 +1,6 @@ // -------------------------- FRIENDS -------------------------- -const socket2 = io("https://quanridor.ps8.academy/api/game"); +const socket2 = io("/api/game"); const friendIdToUsername = {}; let currentSelectedFriendId = null; let isChatVisible = false; @@ -12,7 +12,7 @@ document.addEventListener("DOMContentLoaded", function () { return; } - const baseUrl = "https://quanridor.ps8.academy"; + const baseUrl = window.location.origin; fetch(`${baseUrl}/api/users`, { method: "GET", headers: { @@ -45,7 +45,7 @@ document.addEventListener("DOMContentLoaded", function () { let currentUserId; document.addEventListener("DOMContentLoaded", function () { - const socket = io("https://quanridor.ps8.academy/api/social"); + const socket = io("/api/social"); const sendMessageButton = document.getElementById("send"); const messageInput = document.getElementById("message-input"); @@ -57,7 +57,7 @@ document.addEventListener("DOMContentLoaded", function () { return; } - const baseUrl = "https://quanridor.ps8.academy"; + const baseUrl = window.location.origin; const fetchFriendDetails = (friendId) => { return fetch(`${baseUrl}/api/users/${friendId}`, { @@ -180,7 +180,7 @@ document.addEventListener("DOMContentLoaded", function () { return; } - const baseUrl = "https://quanridor.ps8.academy"; + const baseUrl = window.location.origin; fetch(`${baseUrl}/api/users`, { method: "GET", headers: { diff --git a/mobile/www/js/game-retriever.js b/mobile/www/js/game-retriever.js index ce020b9..3e51c6d 100644 --- a/mobile/www/js/game-retriever.js +++ b/mobile/www/js/game-retriever.js @@ -11,7 +11,7 @@ document.addEventListener("DOMContentLoaded", function () { // Function to retrieve games and update the UI async function retrieveGamesInProgress() { try { - const baseUrl = "https://quanridor.ps8.academy"; + const baseUrl = window.location.origin; const response = await fetch( `${baseUrl}/api/game?multiple=true&withStatus=1`, { diff --git a/mobile/www/js/leaderboard.js b/mobile/www/js/leaderboard.js index 42c53c7..a133d15 100644 --- a/mobile/www/js/leaderboard.js +++ b/mobile/www/js/leaderboard.js @@ -9,7 +9,7 @@ document.addEventListener("DOMContentLoaded", function () { async function retrieveLeaderboard() { try { - const baseUrl = "https://quanridor.ps8.academy"; + const baseUrl = window.location.origin; const token = localStorage.getItem("token"); fetch(`${baseUrl}/api/users`, { method: "GET", diff --git a/mobile/www/js/login.js b/mobile/www/js/login.js index 457eebe..524e08d 100644 --- a/mobile/www/js/login.js +++ b/mobile/www/js/login.js @@ -6,7 +6,7 @@ document.addEventListener("DOMContentLoaded", function () { // Get the values from the form const username = form.querySelector('input[name="username"]').value; const password = form.querySelector('input[name="password"]').value; - const baseUrl = "https://quanridor.ps8.academy"; + const baseUrl = window.location.origin; // Construct the JSON object to send const data = { username, password }; diff --git a/mobile/www/js/online-game-mobile.js b/mobile/www/js/online-game-mobile.js index 1645408..1729da8 100644 --- a/mobile/www/js/online-game-mobile.js +++ b/mobile/www/js/online-game-mobile.js @@ -1,4 +1,4 @@ -const socket = io("https://quanridor.ps8.academy/api/game"); +const socket = io("/api/game"); const urlParams = new URLSearchParams(window.location.search); let roomId = urlParams.get("roomId"); socket.on("connect", () => { diff --git a/mobile/www/js/online-game.js b/mobile/www/js/online-game.js index 1b64ef2..aff0f30 100644 --- a/mobile/www/js/online-game.js +++ b/mobile/www/js/online-game.js @@ -1,4 +1,4 @@ -const socket = io("https://quanridor.ps8.academy/api/game"); +const socket = io("/api/game"); const urlParams = new URLSearchParams(window.location.search); let roomId = urlParams.get("roomId"); socket.on("connect", () => { diff --git a/mobile/www/js/signup.js b/mobile/www/js/signup.js index 2752680..f1ec2d8 100644 --- a/mobile/www/js/signup.js +++ b/mobile/www/js/signup.js @@ -1,4 +1,4 @@ -const baseUrl = "https://quanridor.ps8.academy"; +const baseUrl = window.location.origin; document.addEventListener("DOMContentLoaded", function () { const form = document.querySelector("form"); diff --git a/mobile/www/js/user-info-retriever.js b/mobile/www/js/user-info-retriever.js index 18aefb4..4617aa7 100644 --- a/mobile/www/js/user-info-retriever.js +++ b/mobile/www/js/user-info-retriever.js @@ -5,7 +5,7 @@ document.addEventListener("DOMContentLoaded", function () { console.log("No token found in local storage."); return; } - const baseUrl = "https://quanridor.ps8.academy"; + const baseUrl = window.location.origin; fetch(`${baseUrl}/api/users`, { method: "GET", headers: {