haxball / haxball-issues

115 stars 43 forks source link

room.getPlayer auth / conn undefined #883

Open toderitagcristian opened 5 years ago

toderitagcristian commented 5 years ago

Hi !

Trying to simplify my headless script and avoid looping through localStorage, but ...

I am trying to get the player.auth string and display it on chat. But it throws undefined errors. On chat !test command gives the correct nickname, but the auth and conn are undefined

Why aren't these values fetched ?

P.S : The code tag displays badly the code, so I had to use quote tag.

Results :

BOT: Mr. D is correct nickname.

BOT: undefined is correct auth.

BOT: undefined is correct connection.

Script :

if (["!test"].includes(message[0].toLowerCase())) { if (player.admin) { if (Number.isNaN(Number.parseInt(message[1]))) { if (message[1].length > 1 && message[1][0] == "#") { message[1] = message[1].substring(1, message[1].length); if ( !Number.isNaN(Number.parseInt(message[1])) && room.getPlayer(Number.parseInt(message[1])) != null) { room.sendChat(room.getPlayer(Number.parseInt(message[1])).name + " is correct nickname. "); room.sendChat(room.getPlayer(Number.parseInt(message[1])).auth + " is correct auth. "); room.sendChat(room.getPlayer(Number.parseInt(message[1])).conn + " is correct connection. "); } } } } }

AnddyAnddy commented 5 years ago

Auth and conn are only defined in onPlayerJoin event

toderitagcristian commented 5 years ago

@AnddyAnddy : Thank you ! I saw that on the wiki page, but I thought is persistent through the script. My bad. I managed to get around with localStorage.key and loop through them. Also, I realised i could've avoided localStorage looping and use a bigger Array to store values. :)) Silly me ...

toderitagcristian commented 5 years ago

Hi ! I reopened the issue. My problem is fixed on my part and also I managed to use either localStorage or Object of arrays.

The main problem is that player.auth and player.conn cannot be retrieved outside of room.onPlayerJoin. This leads to cumbersome coding in script and having separate arrays, duplicating them, unnecesary looping over arays etc

Why can't they be made globally available such as player.id, player.name etc ? It will open a wide range of better scripting execution & maybe a semi-login interface that can be build on host part @basro

saviola777 commented 5 years ago

Have you checked out haxroomie? Or, more specifically, the players plugin which, among other things, gives access to the auth and conn property in all handlers and also fixes async problems (e.g., when you move a player and immediately check his team and it's still the old one).

toderitagcristian commented 5 years ago

@saviola777 yes. I actually used it to play around and learn some coding. It was really nice and clean work from you.

But still, why overcomplicate things when, I guess, it all can be set in-game player properties. I would tag this thread as feature or improvement.

saviola777 commented 5 years ago

I agree, but if Basro doesn't (have the time to / want to) implement it and the API is powerful enough for us to do it ourselves, then it's fine like this (especially because it's no overcomplication for the "user", I mean the developer, because the complicated stuff happens in the background).

ghost commented 5 years ago

Because you don't need auth or conn anywhere except onPlayerJoin.

Yeah I think like that. Checking it when player joins thats okay but why more? I mean what are you gonna achieve by writing players auth on chat or calling the player auth when goal scored? This is useless imo.

saviola777 commented 5 years ago

You need the player auth whenever you want to store data that should not be lost once he leaves – which is pretty often.

toderitagcristian commented 5 years ago

@saviola777 yeah ...true

Anyway .... I am using an object to store date with key as player_auth. Does the job, but complicates code all around if I want to update DB. Some getters and setters are used to modify the object based on player.id - player.auth relation which I enforce to update onplayerJoin event. So far it works, but persistent auth could simplify coding a lot

Sielinton commented 4 years ago

@toderitagcristian I have the same intention, can you show how the code was?

toderitagcristian commented 4 years ago

@Sielinton Hey I think I resorted to using sqlite. My complete project includes discord bot connection and management and a react interface. You can have a look on my profile at https://github.com/toderitagcristian/Haxball-Bundle There is also another older approach : https://github.com/toderitagcristian/haxball-framework

Good luck :)