haxball / haxball-issues

114 stars 42 forks source link

Headless Host Programming/Questions [RP RD] #968

Open secrui opened 4 years ago

secrui commented 4 years ago

Hello,

I have asked this on reddit too but I think I get more help here. I have seen a few videos and websites and start to understand headless host.I like the idea of creating own games.I have a few questions regarding headless host.

  1. Is it always the case that I have to call the html5.haxball.... URL to start a room and if yes, how can I call it on a server that is hosted for example at a shared hoster?
  2. I have seen servers that show and load stats and also show them on the website, how is that possible? Do I have to connect through SQL etc. to my website or do those servers create AJAX calls between the website (for example when "logging in" they send an ajax to domain.com?checkUser=username&pass=password.
  3. If 2 is correct, how can those hosters make sure that noone updates their stats manually and are those ajax links visible at any point (for example traffic catchers/viewers)? For example let's say if a goal is made, they call domain.com/api/updateUser?user=username&scores=29
  4. If 2 is not correct, how to establish a connection to a database?
  5. Let's say I want to create a game where users touch a wall and get a point. How could I do that? My idea would be to check every tick if a user is close to that wall. But how can I define/find the wall and write it into the code?
  6. Is it possible to "draw" stats on the map or the users display? For example in the bottom right of the map/the users view it shows his stats like "Your goals: 100"
  7. Is it possible to draw dynamic objects, like lets say, a user writes "/shoot" and it shows bullets floating away from the user?
  8. If 7. is possible, can I also stick objects to the user for example that it looks like he carries a bag?
  9. If I understood correctly, "every tick" works only while a game is running. Is there an option to let it run always?
  10. I want to prevent shooting goals, but instead, when a goal is happening, players do not respawn but only the ball. How can I do it?

I'm quiet impressed by headless host and appreciate every of your answers 🥰

dixtel commented 4 years ago
  1. Yes, always if you want to create a room you must visit html5.haxball.com/headless and inject javascript. You can use headless browser like chrome headless on VPS and from there create room (see #340). Optionally you can write own script in python on another language which support selenium and make code which is responsible for creating headless browser and injecting your haxball bot code
  2. You can create server on localhost and make conversation between haxball bot <-> server via simple requests. On server you can take request and do cool stuff like saving stats, sending response these stats and more.
  3. As you say. If you have code of your map you have all walls, discs, points. With these information you can calc distance between nearest wall and player and check if there is collision or not.
  4. This is not possible. 7 This is not possible.
  5. I don't understand this question, To read more about game ticks see https://gamedev.stackexchange.com/questions/81608/what-is-a-tick-in-the-context-of-game-development

I dont know if this is clear, i tried to be As you probably see my english is not perfect ;)

secrui commented 4 years ago

Thanks @dixtel !

  1. Regarding 7., is it also not possible to create a ball in front of a player when he enters specific words into the chat?
  2. Is there a possibility to check if the player pressed space(or X) or can I only check any activity (mouse click, movement etc.) with the onPlayerActivity?
  3. Regarding every tick, there's the way to simply create a loop with javascript itself (setInterval).
  4. Do you have an example on how I could check if a player is within a specific spot (for example rectangle or circle)? I have no idea how that could look like and have heard already about minX, minY etc. but don't know how to do it 😄
  5. I think you mentioned the wrong numbers 😃
  6. What exactly is meant with "disc"? I have readed it in the headless host wiki but am not sure what is meant with it.

Thank you :)

dixtel commented 4 years ago

@unknownsteve

  1. I forgot that there is function called setDiscProperties. With this function you can manipulate ball and others discs on map. So line room.setDiscProperties(0, {x: 0, y: 0}); will change ball position to (0, 0). This way you can control all discs on the map in game. So yes, "dynamic objects" can exists :)
  2. Now there is not option to check which key is current pressed.
  3. Yes, always you can create loop as setInterval but why?
  4. To check if player is in specific rectangle, Example:
    
    // example, to get current player position use room.getPlayer(player_id: int).position
    const player_pos = {x: 0, y: 0};
    const rectangle = {x: 0, y: 0, width: 100, height: 100};

if (player_pos.x > rectangle.x && player_pos.x < (rectangle.x + rectangle.width) && player_pos.y > rectangle.y && player_pos.y < (rectangle.y + rectangle.height) ) { // player in spot, do important things... } else { // player outside or on edge of that spot }

for circle:

// example, to get current player position use room.getPlayer(player_id: int).position const player_pos = {x: 0, y: 0}; const player_radius = 15; const circle_center = {x: 50, y: 50}; const circle_radius = 15; const deltax = player_pos.x - circle_center.x; const deltay = player_pos.y - circle_center.y;

// use pythagorean theorem to calc distance between two circles if (deltaxdeltax + deltaydeltay < (player_radius + circle_radius)*(player_radius + circle_radius)) { // player in spot, do important things... } else { // player outside that spot }

secrui commented 4 years ago

Thank you very much @dixtel :)

Wazarr94 commented 4 years ago
  1. Is it possible to "draw" stats on the map or the users display? For example in the bottom right of the map/the users view it shows his stats like "Your goals: 100"

You can, someone made this to display the 3 best players from the room, but you need to do every character as a "map object", which will take you a lot of time probably. And then, considering the map is just a JS Object, you can add it to your map. Oh, and since the map is the same for every player, you cannot show personalized messages like that.

  1. Is it possible to draw dynamic objects, like lets say, a user writes "/shoot" and it shows bullets floating away from the user?

You can not add discs in the game, but you can load a great amount of them outside the map so you can use them for this command. For this you use setDiscProperties where you take one of your hidden discs and put them as a bullet.

  1. If 7. is possible, can I also stick objects to the user for example that it looks like he carries a bag?

You'd need to call setDiscProperties every tick, but it's not supported very well. For now, I would say it's not possible.

  1. If I understood correctly, "every tick" works only while a game is running. Is there an option to let it run always?

setInterval with timeout of 1000/60 ~ 17

  1. I want to prevent shooting goals, but instead, when a goal is happening, players do not respawn but only the ball. How can I do it?

Look at kickoffReset in the map wiki, I don't have experience with it, but it might help. Another solution is to use an alternative score, where you disable goals and you count it yourself. When a goal should be counted, you update your alternative score, and you put the ball back in the center. This solution would work flawlessly if a method setScores was implemented, which is not too hard to implement, except basro doesn't update the game very much.

Finally got the time to answer, so here you go.