Coder Social home page Coder Social logo

Comments (2)

endel avatar endel commented on May 18, 2024

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.

from colyseus.

takaaptech avatar takaaptech commented on May 18, 2024

Thank so much!

from colyseus.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.