AlexNash91 / Going-Medieval

MIT License
0 stars 0 forks source link

Functionality - Push data from scene to server #52

Closed Hezakai closed 1 year ago

Hezakai commented 1 year ago

Clicking the "CLAIM" button should update the Mapset model to change ownership of the tile with id (activeTile[0]) to the string stored in the playerName variable. The issue is that I am unable to touch the Mapset model. I've tried both "require" and "import" but both throw errors.

I first tried adding

const Mapset = require("../../Models/Mapset");

to the first line of game.js and that returned the error

Uncaught ReferenceError: require is not defined

then I replaced that with

import Mapset from "../../Models/Mapset";

but that threw an error as well

Uncaught SyntaxError: Cannot use import statement outside a module

Phaser CLAIM button code in game.js

    //onclick should update DB via model
    button.on('pointerdown', function () {
        console.log('Button clicked!');
         Mapset.update({
            own: playerName
                }, {
                where: {
                   id: activeTile[0]
                   }
            }).then(function (result) {
              console.log("Data updated successfully!");
            });
    });
Hezakai commented 1 year ago

fixed. updated route

router.patch('/api/map', async (req, res) => { try { const updatedMapset = await Mapset.update( { own: req.body.own }, { where: { id: req.body.id } } ); res.json(updatedMapset); } catch (err) { console.log(err); res.status(500).send({ error: "Error updating mapset in the database." }); } });

updated button

let button = self.add.rectangle(160,200, 120, 50, 0x000000);
button.setStrokeStyle(2, 0xffffff);
let buttonText = self.add.text(160,200,'Claim Tile', { font: '24px Arial', fill: '#ffffff' });
buttonText.setOrigin(0.5);
button.setInteractive();
button.on('pointerdown', function () {
    console.log('Button clicked!');
    fetch('/api/map', {
        method: 'PATCH',
        headers: {
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({ id: activeTile[0], own: player })
      })
        .then(res => res.json())
        .then(data => console.log(data))
        .catch(error => console.error(error));
});