Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is colyseus suitable for turn base game? #7

Closed
takaaptech opened this issue Feb 23, 2016 · 2 comments
Closed

is colyseus suitable for turn base game? #7

takaaptech opened this issue Feb 23, 2016 · 2 comments

Comments

@takaaptech
Copy link

Hi!
I am develop a turn base game (likes tic tac toe online). In game, users start match and play turn by turn, user move is limited by time (e.x 30 second each move) . And after some conditional, match over and then user start match again . So how can i implement this feature in colyseus server?
Thank so much!

@endel
Copy link
Member

endel commented Feb 24, 2016

Hi @takaaptech,

Yes, it's suitable! I'm starting to write the documentation for the server, you can take a look here: http://gamestd.io/colyseus/docs/room-state.html (feedbacks are welcome!)

Considering you want to make a "tic tac toe" like game, you'd need to allow only 2 clients per room, and the room state might look like this:

var Room = require('colyseus').Room

class TicTacToeRoom extends Room {
  constructor (options) {
    super(options, 1000)

    this.players = {}
    this.setState({
      currentPlayer: null,
      board: [[0,0,0], [0,0,0], [0,0,0]]
    })
  }

  requestJoin() {
    return this.clients.length < 2
  }

  onJoin(client) {
    this.players[client.id] = (this.clients.length == 1) ? "X" : "O"
  }

  onMessage (client, data) {
    // change this.state.board[x][y] to client's attribute (X or O?)
    this.state.board[data.x][data.y] = this.players[client.id]
  }
}

module.exports = TicTacToeRoom

I didn't tested this code, just wrote it quickly here. You'd basically have to deal with the board state in the server, and request commands through the client on the position you want to update. When the state has successfully changed, you can apply the change in the client-side through the patch callback in the client-side.

To deal with timing events, you could use built-in setInterval and setTimeout methods, and switch the state.currentPlayer variable to the other player. It's also a good idea to check for the currentPlayer when accepting movements inside onMessage method.

Hope this helps, let me know if you have any questions.

@endel endel closed this as completed Feb 24, 2016
@takaaptech
Copy link
Author

Thank so much!

@endel endel added the question label Nov 10, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants