A
A
andrei selderei2016-02-13 15:38:39
Canvas
andrei selderei, 2016-02-13 15:38:39

How to build an online card game in PHP and canvas?

I decided to write a simple card (Fool) online game. For the back-end I took Laravel and the front on canvas + Fabric.js . And I faced such a problem: how best to organize the logic of the game, in particular - multiplayer. Below I will try to briefly describe how I see all this and ask you to share your opinion on this issue, experience, advice.
So.
There are 3 tables in the database: users (this is understandable), games (all information about the games: player 1, player 2, whose turn, who has what cards (json), cards in the deck (json), etc.), queue (players get here who do not yet have an opponent).
1. A person logs in on the site, selects a bet and presses the "play" button.
2. If there is a suitable opponent in the queue, we make a new entry in the games table with these players and redirect both to the page with the game. And if there is no opponent, we make an entry in the queue with this player and every couple of seconds, through ajax, we check whether there is an opponent for him and when there is, we redirect him to the game.
3. Each change on the game field is saved in the games table through the same ajax.
4. We update the ajax game field with requests to the games table that go continuously, one after the other: a new request is launched immediately or a second after the completion of the previous one.
That, in principle, is all I could think of at the moment and I'm not sure that this is the best solution. I would like to know your opinion.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuri, 2016-02-13
@acelash

I am also making a card game online. Clone of ancient astral tournament - example https://youtu.be/qZbVjDvKSx4?t=50s
Mainly for training. I do it on nodejs (although I usually work on php / symfony) the node for such things is better adapted, it works more efficiently with sockets, and you have to pervert on PHP (either as you described Ajax with constant requests to hammer the server, or try to screw web sockets too).
Actually on the node, everything is simple. after creating a websocket, you can communicate two-way, on the client and server you can send events and subscribe to them. All logic on the server - the client is stupid only sends events and renders the sent events.
when a player enters - he gets on the list of games, can join any not started or can create his own. in the future I will add another observer mode - to watch any active game.
example: a player presses the button create a game - everything that is done on the client - an event is sent to the server, the server does not even respond, it just sends an event to all players - the game and its parameters have been created. the game will immediately be rendered on all clients (including the one who created it).
if someone selects this game in the list - again only a signal is sent - the player has joined the game. then the server creates a room for them and both of them will receive events related to this game, the first event is that they should open a screen with the game.
well, then everything is the same - any click on the client essentially just sends an event to the server about what the player wants to do. all calculations on the server and clients are already sent events to change the UI. Thus, cheating is impossible.
it is also worth explaining how the moves are made (namely, the animation of the moves). if you watch the video - then there the player selects a card - he walks and then the attack with his creatures begins. here it is done like this - the name of the thrown card goes to the server, the server completely calculates all the actions per turn (and the interactions there are quite complicated - each card has its own unique abilities and they can be combined with other cards). then it generates a list of visual events, for example - call a card, an attack with a card, a creature takes damage, the power of the element has increased, and so on. this list goes to the client and he starts to play them one by one, like a script, so that the players can follow step by step and understand what is happening.
in general, everything is simple
1) on the server and the client there are event handlers from each other.
2) events come from the client - player commands (create a game, join, make a move, surrender, skip a move)
3) commands come from the server to change the UI (add a game to the list, open the game UI, start the animation of the turn script)
I think I could and to do it in php, experience with it > 5 years, and I haven’t done anything particularly serious with the node yet, although sometimes I use it. but with php it will not be the best solution.
besides, with a node, it is possible to write part of the code that will be executed both on the client and on the server, since the language is one. for example, for now, these are only descriptions of maps, but in general, the perspective is also interesting.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question