Skip to content

Commit

Permalink
new build
Browse files Browse the repository at this point in the history
  • Loading branch information
ilanaliouchouche committed May 24, 2024
1 parent 72241eb commit 0417d66
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 62 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
FROM nginx:alpine

COPY app/ /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf
#COPY nginx.conf /etc/nginx/nginx.conf

EXPOSE 80

Expand Down
61 changes: 38 additions & 23 deletions app/js/end_game.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
document.addEventListener('DOMContentLoaded', () => {

// Get game infos
const params = new URLSearchParams(window.location.search);
const score = params.get('score');
const meanTime = params.get('mean_time');
Expand All @@ -8,14 +9,17 @@ document.addEventListener('DOMContentLoaded', () => {
const difficulty = params.get('difficulty').toLowerCase();
const mode = params.get('mode').toLowerCase();

// Ensure the time is displayed in a user-friendly format (seconds with two decimal places)
const formattedTime = parseFloat(meanTime).toFixed(2);

// Display the score and mean time
document.getElementById('scoreValue').textContent = `${score}`;
document.getElementById('timeValue').textContent = `${formattedTime} seconds`;
document.getElementById('playerNameValue').textContent = `${player_name}`;
document.getElementById('difficultyValue').textContent = `${difficulty}`;
document.getElementById('totalRoundsValue').textContent = `${totalRounds}`;

// Send the score to the server
const postData = {
user: player_name,
score: parseInt(score),
Expand All @@ -24,36 +28,47 @@ document.addEventListener('DOMContentLoaded', () => {
difficulty: difficulty
};

console.log('data to be sent:');
console.log(JSON.stringify(postData));

fetch('http://localhost:8000/add_score', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(postData)
})
.then(response => response.json())
.then(data => {
console.log('Success:', data);
})
.catch((error) => {
console.error('Error:', error);
});

fetch(`http://localhost:8000/scores?mode=${mode}&difficulty=${difficulty}`)
// console.log('data to be sent:');
// console.log(JSON.stringify(postData));

// fetch('http://127.0.0.1:8000/add_score', {
// method: 'POST',
// headers: {
// 'Content-Type': 'application/json'
// },
// body: JSON.stringify(postData)
// })
// .then(response => response.json())
// .then(data => {
// console.log('Success:', data);
// })
// .catch((error) => {
// console.error('Error:', error);
// });

// Get the podium from the server
fetch('http://localhost:8000/scores')
.then(response => response.json())
.then(data => {
console.log('Podium data:', data);
displayPodium(data);
console.log('All scores data:', data);
// Sort the data by score (descending) and mean_time (ascending)
const sortedData = data.sort((a, b) => {
if (b.score === a.score) {
return a.mean_time - b.mean_time;
}
return b.score - a.score;
});
// Take the top 3 scores
const top3 = sortedData.slice(0, 3);
displayPodium(top3);
})
.catch((error) => {
console.error('Error fetching podium data:', error);
console.error('Error fetching scores data:', error);
});

function displayPodium(scores) {
const podiumContainer = document.getElementById('podium-container');
podiumContainer.innerHTML = ''; // Clear previous podium data

scores.forEach((score, index) => {
const podiumBlock = document.createElement('div');
Expand Down Expand Up @@ -87,4 +102,4 @@ document.addEventListener('DOMContentLoaded', () => {
podiumContainer.appendChild(podiumBlock);
});
}
});
});
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
- "8000:8000"
networks:
- quickdraw-network
command: ["/app/endpoints/entrypoint.sh", "--device", "cpu"]
command: ["/app/endpoints/entrypoint.sh", "--device", "mps"]

quickdraw:
image: ghcr.io/mlengineershub/quickdraw:latest
Expand Down
14 changes: 4 additions & 10 deletions endpoints/predict_cpu.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,9 @@ async def get_labels():


@app.get("/scores")
async def get_scores(mode: str, difficulty: str):
async def get_scores():
"""
Function to return the top 3 scores from the database
filtered by the given mode and difficulty.
Function to return all scores from the database without any filters.
It returns a dictionary with the scores.
score1: { user: user1,
Expand Down Expand Up @@ -205,14 +204,9 @@ async def get_scores(mode: str, difficulty: str):

data = _data[1]
df = pd.DataFrame(data)
df_filtered = df[(df['mode'] == mode) & (df['difficulty'] == difficulty)]

df_filtered = df_filtered.sort_values(by=['score', 'mean_time'],
ascending=[False, True])

df_top3 = df_filtered.head(3)

scores = df_top3.to_dict(orient='records')
# Return the DataFrame as a dictionary without any filtering
scores = df.to_dict(orient='records')

return scores

Expand Down
27 changes: 0 additions & 27 deletions nginx.conf
Original file line number Diff line number Diff line change
@@ -1,27 +0,0 @@
upstream api {
server localhost:8000;
}

server {
listen 80;
server_name localhost;

location / {

if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';
add_header 'Content-Type' 'application/json';
add_header 'Content-Length' 0;
return 204;
}

add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';

proxy_pass http://api/;
}
}

0 comments on commit 0417d66

Please sign in to comment.