Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Custom event triggered when two network players pick a choice

    I am working on a simple two-party network game so when a match finishes both players are asked if they want to play again. The rules are:

    1) No one action will be taken before both parties choose an answer
    2) A new match will be started if both answer "Y"
    3) The game will be abandoned if at least one answer "N"

    What I need is an event to be triggered after both parties has entered a valid answer.
    It has to be done in the client side.

    I have access to a server property called 'snapshot' from where one player can capture the oponnent answer.

    Normally I would do that by storing both players answers in variables and use an interval to keep continuously checking them until have valid values. Although it works fine I think that this is an ugly solution and would like to see an approach to do it with a true custom event.

    Any idea?

    Thanks!

  • #2
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,020
    Thanks
    56
    Thanked 566 Times in 563 Posts
    I'm guessing it depends on the nature of the "snapshot" object that the other player can capture. But it would seem to me that you can just check the other players answer at the time that the player gives their answer. If the other player hasn't answered yet (I'm assuming that snapshot would be null in that case), then do nothing, or give a message that you are waiting for the other answer. If they have, decide what to do from there.

    Although from the rules you cite, it would seem to make more sense to end the game as soon as someone picks "No", but anyway.

    Some pseudocode...

    Code:
    if (player.answer=="Y"){
    switch(snapshot.answer){
    case "Y":
    alert("let's play again!");
    break;
    case "N":
    alert("opponent resigned");
    break;
    default:
    alert("waiting for opponent's response");
    break; 
    	}
    } else {
    switch(snapshot.answer){
    case "Y":
    alert("you resigned - your opponent wins!");
    break;
    case "N":
    alert("both of you resigned");
    break;
    default:
    alert("waiting for opponent's response");
    break; 
    	}
    }

  • #3
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for replying xelawho, but I was more after how to code the custom event than what to do when the conditions are attended...

  • #4
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    3,020
    Thanks
    56
    Thanked 566 Times in 563 Posts
    what event? when the players make their choice?

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    how are the players sending moves/clicks to eachother now? socket.io? ajax polling? signalR? pubnub?
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #6
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The clicks/moves etc are sent through a server.

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    in my 2player games, i send the clicks to start a new game just like i do a turn, but with one extra flag to indicate vote. that send pushes the choice into an array, choices.push(theVote), and when the array is of length==2, i ask if every value is true, choices.every(Boolean). if all true, send a "startGame" command, else do liitle or nothing... i'm not sure how you're using your server, specifically if you can push to the client or have to poll. if you must poll, then you'll want some sort of limited time period after the game to vote, during which you poll for choices.
    Last edited by rnd me; 05-29-2014 at 10:00 PM.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #8
    New to the CF scene
    Join Date
    Jul 2008
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hey "rnd me", yeah, this was exactly what I did. I just thought that could may have a more elegant way to do this. Anyway, I used a timer to bang the array every one second to check the entries. It kept checking until to have both valid answers and then take the proper action (two 'YES' or at least one 'NO'). The server (Firebase) is to exchange the data between the two oponents.

    Thanks for your time posting here!

  • #9
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    Quote Originally Posted by yankleber View Post
    The server (Firebase) is to exchange the data between the two oponents.
    if you're using firebase, you don't need to poll anything, it should raise events anytime something changes..
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •