Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
build: add build scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
marcpinet committed Apr 25, 2024
1 parent 455fe74 commit 92c77a3
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 28 deletions.
18 changes: 12 additions & 6 deletions build-apk.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
42 changes: 42 additions & 0 deletions local_revert.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion mobile/www/js/achievements.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
4 changes: 2 additions & 2 deletions mobile/www/js/ai-game-mobile.js
Original file line number Diff line number Diff line change
@@ -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.");
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions mobile/www/js/ai-game.js
Original file line number Diff line number Diff line change
@@ -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.");
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion mobile/www/js/bot-choice-mobile.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down
2 changes: 1 addition & 1 deletion mobile/www/js/bot-choice.js
Original file line number Diff line number Diff line change
@@ -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");
Expand Down
6 changes: 3 additions & 3 deletions mobile/www/js/chatsocket.js
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down
10 changes: 5 additions & 5 deletions mobile/www/js/friend-list.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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: {
Expand Down Expand Up @@ -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");
Expand All @@ -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}`, {
Expand Down Expand Up @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion mobile/www/js/game-retriever.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`,
{
Expand Down
2 changes: 1 addition & 1 deletion mobile/www/js/leaderboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion mobile/www/js/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
2 changes: 1 addition & 1 deletion mobile/www/js/online-game-mobile.js
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down
2 changes: 1 addition & 1 deletion mobile/www/js/online-game.js
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down
2 changes: 1 addition & 1 deletion mobile/www/js/signup.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const baseUrl = "https://quanridor.ps8.academy";
const baseUrl = window.location.origin;

document.addEventListener("DOMContentLoaded", function () {
const form = document.querySelector("form");
Expand Down
2 changes: 1 addition & 1 deletion mobile/www/js/user-info-retriever.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 92c77a3

Please sign in to comment.