-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Modularize the server; Add Player and BSTree classes #184
Conversation
This is the kind of commit we need to make the project shine! |
@jniles Bug with joining players, if someone join and after first player join another player the first player see his nickname his colour of character and his how he move the cell. Waiting for the update :) |
Bug is now fixed, as far as I can tell. |
FIX: ensure player id is unique
@igorantun , @huytd Would you mind taking a look at these ideas? There are lots of PRs, and it would be nice to clear them so we can start working on stability. |
Is it a bit overkill? Number of players for this clone is never above 10. Even number player in agar io in each match is not over 200. Building a BST to improve from O(10) to O(log(10)) does not contribute much to this current situation and may cause the program slower when building a tree. |
@giongto35 , the important aspect of this PR is the modularization and separation of concerns into a player class, and a container for players, moving that logic out of the server. Once an API is established to communicate between those entities, they can be implemented as trees, maps, etc. I'm not sure overkill is a good reason to discard a data structure. A hash map is a perfectly acceptable alternative. The reasoning behind using a tree was to maintain a relative ordering between elements, hopefully taking advantage of mass or some other attribute, so we could quickly find the top 5 players in rapid time (see #119). This obviously isn't implemented in this PR, but I don't want to get too far ahead of the master branch and what other devs are doing. |
Why has this been closed? |
I've reopened it, apologies. |
Could you please fix the merge conflicts so I can merge this? Thanks in advance :) |
I am closing this one, and will break these changes up into separate pull requests. |
Sure! Thanks :) |
UPDATE
@hunterpl and I found a bug in this PR. I'll let you know when we've written a patch and is safe to merge.Bug is now fixed. This should be safe to merge.
This PR tries to modularize the server so that we can test it properly. It contains major changes.
Players are now encapsulated in the
Player
object. It holds player properties and state. When a new player joins, it is instantiated with a callnew Player({ /* ... */ });
. Players only have two methods currently:player.move(target)
moves the player toward the target (derived from legacymovePlayer()
function).player.serialize()
provides a hook for the developers working on binary serialization of player state. See issue Binary Protocol #177 . It is currently unused, but potentially useful.Players reside in a PlayerTree, which is an implementation of the Binary Search Tree. Unlike a regular array, it provides guarantees of
O(log n)
search speed in the best case andO(n)
in the worst case. See the filebstree.js
for full analysis. In the future, we will want to use a Red-Black Tree, a binary search tree which guarantees aO(log n)
time for insertion, removal, and search in the worst case. The API will be identical, so we can transparently drop it in when it is complete.Tree API:
Both the PlayerTree and the Players directly address issue #36.
This PR may break a few things, but it has many benefits including modular development on the server, hooks for player serialization, and is test-ready. It isn't perfect, but it's a start.
What do you all think?