haxball / haxball-issues

115 stars 43 forks source link

Update "scored" boolean if the goal was scored? #740

Open lashagosh opened 5 years ago

lashagosh commented 5 years ago

Hi, I'm trying to implement an automated penalty mode for my RS script, but I'm having several troubles. If anyone answers to any of them I'll be very thankful.

My goal is to declare a winner team if any of teams score 4 goals (because there are 4 players in each team). Blues and Reds will consecutively take a shot from the red team. *If one team reaches 4 goals, and it is the 4th shot of the second team and they also make it, the rotation should start from beginning.

1) What is the best way to implement such thing? Just a theory would be enough. If you show me the code part I can die of happiness. Coz I think I'm doing it the hardest way.

2) What I got so far is a function penalties() that is called after a draw. Red and blue team player objects are saved in separate arrays. And GKs in separate arrays. First player from red team is placed in red team, and blue GK is placed in blue. This is where I am stuck. I don't know the next step. I tried to update a global scored boolean to true from onTeamGoal and act accordingly in the penalties() function if the scored is true, but seems like the code in the penalties() function that checks for if (scored == true) gets executed once, when the players are set to teams.. before the goal is even scored.. and can't make it wait for the boolean to be updated.. (I know this is very noobish, and I know why is this happening, but I couldn't find a better way to explain it)

3) (Considering the 2nd issue was non-existent) - Which practice is better, to have a while (redScoresCount != 4) as a check or to have an if (redScoresCount == 4) on every other shot, after 4 shots? I hope I'm being clear enough.

4) (Considering the 2nd and 3rd issues were non-existent) - Which practice is better, to have shots nested in each other, like this: if (scored == true) { redScoresCount++; // updateTeams - with the correct functions and everything. room.startGame(); // and nest other shots here with if-else blocks? i know this is very wrong :( } else { // updateTeams - with the correct functions rooom.startGame(); } Or, what would be the most efficient way to do so?

5) But please be aware that I know everything what I just wrote might be completely wrong. If you also think so, please suggest another way for me to start doing from scratch.

Thanks.

saviola777 commented 5 years ago

For the "waiting" part (issue 2) you could make your functions async and then await promises. For 3) I think it makes sense to check scores after every shot.

lashagosh commented 5 years ago

@saviola777 I'm sorry I didn't quite understand. Should I make my penalties() function async? Or the entire onTeamGoal one? Either of the cases, do you think this is the most correct approach?

olehmisar commented 5 years ago

Haxball API is low-level. You have to manage the state of the game and then make decisions based on that state. For example, the basic state will look like this:

const state = { isInPenaltyMode: true }

And then you make decisions based on that state:

if (state.isInPenaltyMode) {
  // Check if any team had already won.
  // Else put next pair (GK and player) on the field.
}

But using raw haxball API you will end up with spaghetti code. That's why people make frameworks. That's why I created haxilium: https://github.com/olegmisar/haxilium

lashagosh commented 5 years ago

@olegmisar Thanks. But, I forgot to mention that I had that "state" thing figured out. I'm stuck on the "check winner" and "put next pair" part. Also, I'll try to get familiar with haxilium.

Wazarr94 commented 5 years ago

hi, i'm the coder of the HBF room, which possess a complete penalty system, if u need help contact me on Discord : Gouiri#9550