How to detect "has current player won" using JavaScript only.

Game development with Board Game Arena Studio
User avatar
JonChambers
Posts: 97
Joined: 03 July 2021, 11:40

How to detect "has current player won" using JavaScript only.

Post by JonChambers »

Surely I must be missing something. I've been going through all the docs, all the game data, all of everything, and cannot figure it out...

In javascript, is there a quick and simple way of writing:

Code: Select all

if(i_have_won_the_game){
    //do something
}
Of course it's easy if I get PHP involved, but can working out whether or not the current player has won be solved with pure javascript?
Last edited by JonChambers on 17 July 2021, 05:58, edited 2 times in total.
vincentt
Posts: 254
Joined: 01 September 2017, 17:25

Re: Probably the dumbest question I'll ever ask here...

Post by vincentt »

Hi
No, you should use a specific state for this that will trigger the JS code :)

Cheers
User avatar
JonChambers
Posts: 97
Joined: 03 July 2021, 11:40

Re: Probably the dumbest question I'll ever ask here...

Post by JonChambers »

vincentt wrote: 13 July 2021, 11:03 Hi
No, you should use a specific state for this that will trigger the JS code :)

Cheers
Just in case I didn't make it clear, the game state is "gameEnd". Looking for something that differentiates a game I just won from a game I just lost. It's definitely clear that it's a game I just finished.
User avatar
Tisaac
Posts: 2736
Joined: 26 August 2014, 21:28

Re: Probably the dumbest question I'll ever ask here...

Post by Tisaac »

JonChambers wrote: 13 July 2021, 11:51
vincentt wrote: 13 July 2021, 11:03 Hi
No, you should use a specific state for this that will trigger the JS code :)

Cheers
Just in case I didn't make it clear, the game state is "gameEnd". Looking for something that differentiates a game I just won from a game I just lost. It's definitely clear that it's a game I just finished.
Just outputing state args in the onEnteringState function. I think you should figured it out from here.

Code: Select all

Entering state: gameEnd 
Object { id: 99, active_player: 2322020, args: {…}, type: "manager", reflexion: {…}, name: "gameEnd", description: "End of game", action: "stGameEnd" }
	action: "stGameEnd"
	active_player: 2322020
	args: Object { table: {…}, result: (2) […] }
		result: Array [ {…}, {…} ]
			0: Object { name: "Tisaac0", color: "7b7b7b", score: "-14", … }
			1: Object { name: "Tisaac1", color: "ff0000", score: "-20", … }
​​​		table: Object { concede: false, disagreement: false, neutralized: "0", … }
​​​		concede: false
		disagreement: false
		neutralized: "0"
​​​		stats: Object { 1: 2, 2: -17, 3: "-14" }
​​​	description: "End of game"
​	id: 99
​	name: "gameEnd"
	type: "manager"
​
User avatar
JonChambers
Posts: 97
Joined: 03 July 2021, 11:40

Re: Probably the dumbest question I'll ever ask here...

Post by JonChambers »

Tisaac wrote: 13 July 2021, 12:03
JonChambers wrote: 13 July 2021, 11:51
vincentt wrote: 13 July 2021, 11:03 Hi
No, you should use a specific state for this that will trigger the JS code :)

Cheers
Just in case I didn't make it clear, the game state is "gameEnd". Looking for something that differentiates a game I just won from a game I just lost. It's definitely clear that it's a game I just finished.
Just outputing state args in the onEnteringState function. I think you should figured it out from here.

Code: Select all

Entering state: gameEnd 
Object { id: 99, active_player: 2322020, args: {…}, type: "manager", reflexion: {…}, name: "gameEnd", description: "End of game", action: "stGameEnd" }
	action: "stGameEnd"
	active_player: 2322020
	args: Object { table: {…}, result: (2) […] }
		result: Array [ {…}, {…} ]
			0: Object { name: "Tisaac0", color: "7b7b7b", score: "-14", … }
			1: Object { name: "Tisaac1", color: "ff0000", score: "-20", … }
​​​		table: Object { concede: false, disagreement: false, neutralized: "0", … }
​​​		concede: false
		disagreement: false
		neutralized: "0"
​​​		stats: Object { 1: 2, 2: -17, 3: "-14" }
​​​	description: "End of game"
​	id: 99
​	name: "gameEnd"
	type: "manager"
​
Okay, so it wasn’t a stupid question.

My solution is simpler than that, and it still works even if the page is reloaded. Thanks anyway.
User avatar
Tisaac
Posts: 2736
Joined: 26 August 2014, 21:28

Re: Probably the dumbest question I'll ever ask here...

Post by Tisaac »

Simpler than getting the highest score of the two players in the js ??
These are also sent in getAllDatas by framework, so you really don't have to do anything more than that.
User avatar
JonChambers
Posts: 97
Joined: 03 July 2021, 11:40

Re: Probably the dumbest question I'll ever ask here...

Post by JonChambers »

Tisaac wrote: 13 July 2021, 12:27 Simpler than getting the highest score of the two players in the js ??
These are also sent in getAllDatas by framework, so you really don't have to do anything more than that.
The highest score of the two players is not in JavaScript. The need for the client to know the score never came up. And I’m not going to create a client side scoring system just for that one issue.
User avatar
Tisaac
Posts: 2736
Joined: 26 August 2014, 21:28

Re: Probably the dumbest question I'll ever ask here...

Post by Tisaac »

JonChambers wrote: 13 July 2021, 12:47
Tisaac wrote: 13 July 2021, 12:27 Simpler than getting the highest score of the two players in the js ??
These are also sent in getAllDatas by framework, so you really don't have to do anything more than that.
And I’m not going to create a client side scoring system just for that one issue.
What are you talking about ?? The BGA framework already do that for you, that's the little stars right next the player names. They get automatically initialized with the value from the DB that is send in getAllDatas without you having to do anyting.
You can access basic player infos by using this.gamedatas.players
User avatar
thoun
Posts: 1618
Joined: 10 December 2020, 22:25

Re: Probably the dumbest question I'll ever ask here...

Post by thoun »

You need more than player's score, in case of a tie
User avatar
JonChambers
Posts: 97
Joined: 03 July 2021, 11:40

Re: Probably the dumbest question I'll ever ask here...

Post by JonChambers »

Tisaac wrote: 13 July 2021, 14:30
JonChambers wrote: 13 July 2021, 12:47
Tisaac wrote: 13 July 2021, 12:27 Simpler than getting the highest score of the two players in the js ??
These are also sent in getAllDatas by framework, so you really don't have to do anything more than that.
And I’m not going to create a client side scoring system just for that one issue.
What are you talking about ?? The BGA framework already do that for you, that's the little stars right next the player names. They get automatically initialized with the value from the DB that is send in getAllDatas without you having to do anyting.
You can access basic player infos by using this.gamedatas.players
I'm talking about exactly what you think. I've read the documentation too. I know exactly how to do everything you suggested. But I won't.

Partly because it's a couple of lines longer, but more because the score doesn't get involved until the last second (1 for winner, 0 for loser) and I want to avoid displaying said score to the player as much as possible. I know veteran players know what 0 stars mean, but for me it's just clutter. Dash-star isn't much better, but it's a mild improvement.

Also, my solution is slightly shorter.
Post Reply

Return to “Developers”