Currently, I am working on developing a JavaScript game embedded in a website that utilizes a Flask database.
For some added context, my goal is to allow users to earn virtual currency, known as Lamocoins, by playing the mini-game. However, I seem to be facing challenges in implementing this feature effectively with the current coding approach.
Below are the snippets of code:
minigame.js
function handleGameOver(){
ctx.fillStyle = 'black';
ctx.fillText('GAME OVER', 270, 230);
ctx.fillText('You have earned ' + score*2 + ' Lamocoins!', 70, 300);
gameOver = true;
fetch('/gain_currency', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: {'score': score},
})
.then(response => {
setTimeout(() => {
location.reload();
}, 1000);
})
.catch(error => {
console.error(error);
});
}
Disclaimer: This function is triggered upon collision with an enemy and not connected to any button.
app.py
@app.route('/gain_currency', methods=['POST'])
@login_required
def gain_currency():
data = request.get_json()
score = data['score']
current_user.currency_balance += score
db.session.commit()
Here's a snapshot of the database for reference:
Thank you in advance for addressing my query!