haxball / haxball-issues

115 stars 43 forks source link

connect mongodb #1798

Closed peppewarrior1 closed 1 year ago

peppewarrior1 commented 1 year ago

Hi everyone. I'm trying to connect mongodb to my haxball room script but I'm having enough problems. To try out the various features that mongodb can offer I put it up with some test code:

import {MongoClient} from "mongodb";
const dbURI = 'connect';

const mongoClient = new MongoClient(dbURI, {useUnifiedTopology: true});

async function run(){
    await mongoClient.connect();
    console.log('Connessione riuscita');
}
run().catch(err => console.log('Errore: ' + err));

var room = HBInit({
    roomName: "My room",
    maxPlayers: 16,
    noPlayer: true // Remove host player (recommended!)
});
room.setDefaultStadium("Big");
room.setScoreLimit(5);
room.setTimeLimit(0);

// If there are no admins left in the room give admin to one of the remaining players.
function updateAdmins() { 
  // Get all players
  var players = room.getPlayerList();
  if ( players.length == 0 ) return; // No players left, do nothing.
  if ( players.find((player) => player.admin) != null ) return; // There's an admin left so do nothing.
  room.setPlayerAdmin(players[0].id, true); // Give admin to the first non admin player in the list
}

room.onPlayerJoin = function(player) {
  updateAdmins();
}

room.onPlayerLeave = function(player) {
  updateAdmins();
}

I get this error when I try to host locally:

Uncaught SyntaxError: Cannot use import statement outside a module

How do I solve? I would need a solution for both localhost and vps (using haxroomie).

wxyz-abcd commented 1 year ago

After you install mongodb via npm install mongodb, remove the first line and add this instead, then try again:

let mongo = require('mongodb');
let MongoClient = mongo.MongoClient;
peppewarrior1 commented 1 year ago

After you install mongodb via npm install mongodb, remove the first line and add this instead, then try again:

let mongo = require('mongodb');
let MongoClient = mongo.MongoClient;

Locally I can't install mongodb. (By local I mean this: https://www.haxball.com/headless) image

hisham-tareq commented 1 year ago

You cannot use MongoDB directly in the browser. You need to use it in Node.js

Another solutions:

peppewarrior1 commented 1 year ago

You cannot use MongoDB directly in the browser. You need to use it in Node.js

Another solutions:

  • Use Browserify to run NPM packages in the browser
  • You can use firebase instead of MongoDB, it works in the browser and Node.js.

Is there a solution to use it in a vps with haxroomie?

peppewarrior1 commented 1 year ago

I decided that I will use browserify but not finding anything specifically for haxball I would like to know where to find tutorials that help you step by step whether you want to host locally or with VPS via haxroomie. If someone wants to help me explaining step by step I'd be really happy <3

peppewarrior1 commented 1 year ago

@furkanardagul @thenorthstar

thenorthstar commented 1 year ago

@furkanardagul @thenorthstar

@peppewarrior1 Could you stop pinging me in all the issues which you contributed in please? Others are already trying to help you.

furkanardagul commented 1 year ago

For Turkish:

Eğer iletişime geçersen ücretli destek verebilirim. Furkans#9603

For English:

If you contact me I can give passable support. Furkans#9603

peppewarrior1 commented 1 year ago

Per il turco:

Eğer iletişime geçersen ücretli destek verebilirim. Furkan#9603

Per l'inglese:

Se mi contatti posso dare un discreto supporto. Furkan#9603

I can't contact you if you don't accept my friend request.... I'm LyuSkiy#3882