• Help
  • Title screen player.save() does not save data to the MongoDB

Hello,

I found out that player.save() with title-screen module loaded does not save data to the MongoDB

To make a save, run the method player.save(). The method has been overloaded to store the data in MongoDB

For me player.save() returns JSON and I do not see information about user in my MongoDB

    dominx99

    My bad, it is saved properly to the database in data field.

    But I have a problem with login after saving some data.

    I registered and logged in, next, I put this code to the player hook:

        async onJoinMap(player: RpgPlayer, map: RpgMap) {
            player.addItem(Sword);
            player.addItem(Shield);
    
            player.save();
        },

    Next, I rebuild the game and try to log in and I get An error has occurred.

    Login issue

    I think something does not working with loading data back because when I put wrong credentials then I am getting other error
    Also without saving data I can login that many times I want

    Here is JSON from database if needed to reproduce:

    {
      "position": {
        "x": 706.261,
        "y": 378.63,
        "z": 0
      },
      "direction": 3,
      "param": {
        "maxHp": 700,
        "maxSp": 534,
        "str": 67,
        "int": 36,
        "dex": 54,
        "agi": 58
      },
      "hp": 700,
      "sp": 534,
      "gold": 0,
      "level": 1,
      "expForNextlevel": 50,
      "exp": 0,
      "name": "dominik3",
      "items": [
        {
          "nb": 1,
          "item": {
            "name": "Sword",
            "description": "Attack 10",
            "price": 2000,
            "id": "sword"
          }
        },
        {
          "nb": 1,
          "item": {
            "name": "Shield",
            "id": "shield"
          }
        }
      ],
      "_class": {
        "name": "Warrior",
        "description": "A warrior is a person specializing in combat or warfare, especially within the context of a tribal or clan-based warrior culture society that recognizes a separate warrior class or caste.",
        "id": "warriorclass"
      },
      "layout": {
        "top": {
          "lines": [
            {
              "col": [
                {
                  "id": "text",
                  "value": {
                    "text": "{name}",
                    "style": {
                      "fill": "#ffffff",
                      "fontSize": 15
                    }
                  }
                }
              ]
            }
          ]
        },
        "bottom": {},
        "left": {},
        "right": {},
        "center": {
          "lines": [
            {
              "col": [
                {
                  "id": "graphic",
                  "value": "hero"
                }
              ]
            },
            {
              "col": [
                {
                  "id": "graphic",
                  "value": "robe"
                }
              ]
            },
            {
              "col": [
                {
                  "id": "graphic",
                  "value": "fire-bullet"
                }
              ]
            }
          ]
        }
      },
      "map": "map1",
      "speed": 3,
      "frequency": 0,
      "canMove": true,
      "through": false,
      "throughOtherPlayer": true,
      "width": 32,
      "height": 32,
      "wHitbox": 32,
      "hHitbox": 32,
      "tmpPositions": null,
      "initialLevel": 1,
      "finalLevel": 99,
      "mongoId": "64c170c2bd9c5c70c9585cdf",
      "variables": []
    }

    Thank you for the feedback. I will look at the different issues in the next few days

    Thank you for your hard work, anyway I will wait until you fix spritesheets because without that I can't go forward with many features

    10 days later

    It's been corrected on the latest version, check that it's ok 😉

    Have you deployed the newest version? I see only rc.3 version which is the previous one

    Normally 4.0.0-rc.4 is released

    Hmm, I see on github this rc.4 tag, but here: https://www.npmjs.com/search?q=rpgjs it is not available, might be npm issue.
    I can't either install this version because npm could not find it.

    npm update                   13:43:08
    npm ERR! code ETARGET
    npm ERR! notarget No matching version found for @rpgjs/types@^4.0.0-rc.4.
    npm ERR! notarget In most cases you or one of your dependencies are requesting
    npm ERR! notarget a package version that doesn't exist.

      dominx99 strange, because some packages are well up to date. I've republished @rpgjs/types

        Samarium

        I successfully updated dependencies,
        but unfortunately, issue persists.
        I prepared a branch if you want to reproduce: title-screen-integration

        I save data on level-up (to gain level just kill 2 mobs with C button):

            onLevelUp(player: RpgPlayer, leveledUpBy: number) {
                player.showAnimation('level-up', 'default');
        
                player.allRecovery();
        
                player.save();
            },

        Then I refresh the page, login in and I am getting the same error: An error has occurred without any error in console :/


        I have this dynamic database for items with custom properties:

        import { v4 } from 'uuid';
        import Shield from './../../../item/Shield';
        import { RpgPlayer } from '@rpgjs/server';
        
        export default function CircleShield() {
            @Shield({
                id: v4(),
                name: 'Shield',
                graphic: 'shield',
                price: 100,
                pdef: Math.round(Math.random() * 5) + 3,
            })
            class CircleShield {
            }
        
            return CircleShield;
        }

        and also I added custom properties for player:

        declare module '@rpgjs/server' {
            export interface RpgPlayer {
                inventoryGui?: Gui;
                isInventoryOpened: boolean;
        
                profileGui?: Gui;
                profileGuiOpened: boolean;
        
                inventory?: Backpacks,
            }
        }
          17 days later

          dominx99

          I have no problem even testing your game. It saves well.

          What version of mongodb do you have?

            Samarium

            It's docker image of mongo:6-jammy
            it's inside my docker-compose.yml

            But where did you put player.save()?
            like this?

                onLevelUp(player: RpgPlayer, leveledUpBy: number) {
                    player.save();
            
                    player.showAnimation('level-up', 'default');
            
                    player.allRecovery();
                },

            Hmm, I can understand that. Animations don't use synchronization but just make a call to the client (socket.emit). And so the call to the client is faster than synchronization, so the animation takes place before the event appears on the client side (and is therefore not displayed).

            You could say it's a bug, yes.

            But I think it's easily solved:

            // create event 
            // [...]
            server.send() // force synchronization with you can also use player.server to access the server
            // event.showAnimation(...)