Comments (2)
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.
Thank so much!
from colyseus.
Related Issues (20)
- [Bug]: Broken/invalid Schema examples HOT 1
- [Bug]: cannot validate Room Options/Availability HOT 4
- [Bug]: about server matchMaker.createRoom() HOT 2
- [Bug]: static onAuth not fired when using joinById HOT 9
- [Bug]: maxClients not (fully) respected HOT 4
- [Bug]: Token is undefined in static async onAuth in Defold client HOT 8
- [Bug]: OnChange doesn't trigger on the client in the Unity sample HOT 1
- [Bug]: disconnect() during onCreate() HOT 9
- [Bug]: Colyseus throws an exception when a client tries to reconnect after leaving HOT 3
- [Bug]: Stats reports negative player count HOT 2
- TypeError: roomHistory.clients is not iterable HOT 7
- [Bug]: Uncaught Error: ChangeTree: missing index for field "undefined"
- [Bug]: Map Schema Removing value creates issue HOT 2
- [Bug]: tsx must be loaded with --import instead of --loader HOT 3
- [Feature]: need Request in static onAuth HOT 6
- [Bug]: Cannot use Matchmaker w/o Server HOT 1
- [Bug]: Error code lost when throwing ServerError in onCreate HOT 2
- [Bug]: Cant join rooms from react typescript Application HOT 2
- [Feature]: UDP HOT 3
- [Bug]: States are not triggered in the expected order HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from colyseus.