Storing all players

Hi guys, I’m developing an mmorpg and my question is: what is the better way to store all the connected players in the server and how can I rapidly find them? I’m using kryonet so I have and ID and I’m storing them in a LinkedList at the moment. So any time I receive something like a movement update I have to use a

for (Player p : connectedPlayers)

but I guess this is not the better solution.

And I have one more question, when I receive a packet, like a movement update, I’m looping between players in the same thread, this probably makes my server slower, is it a good idea to make a little thread that expires each time his little task has been handled? Or is it better to have something like a thread specialized on movement handling and each time a player moves it handles the data?
Let me know if you have any better approach, thanks.

First off, stop using that linked list. There is absolutely no point in using one. The only advantage linked lists have over array lists is the complexity of its remove operation. What you should do in the packet is store an id that corresponds to a player. When the packet is received, look up the id and call upon the correct player. Then perform whatever action you need to do.

Looping through your player list is not a complex operation at all, modern day computers have no issue looping through anything. Don’t worry about that.

So you suggest me to use arrays, ok I’ll probably do that.

I would suggest you use an array list.

Why not use a map and use the player’s id as the key?

Hi

Depending on the key, you could keep arraylists and just file the arraylist by index. Otherwise a HashMap would work.
Filing arraylist by index: [icode]put(int index, Object o)[/icode][icode]get(int index)[/icode][icode]remove(int index)[/icode]

CopyableCougar4

That’s what I was thinking about right now 8)

Anyone who say something about threads for various tasks? In other words, shall I put the login in different thread instead of kryonet’s one, right?

Hi

For me personally, it would depend how taxing/long it takes to complete a login. If it takes 1ms it’s fine in that thread, but if it takes like half a second than use a different thread. (Just my opinion)

CopyableCougar4

For login and logout I’ve already set up a specific thread because I need to read and modify the database :wink:

Don’t use an arraylist actually. If you are storing by ID then there is no reason to reason to use a list of that type. A hashmap would be your best solution. Don’t spawn a new thread every time you receive a packet, you receive thousands of packets every second. Think about all those threads, and you would eventually (or very soon) run into multithreading issues. Honestly, I think you need to do some research on how computers work first. It seems you think that small tasks will slow down computation, its not true at all. Receiving a packet and processing it shouldn’t be a complex task.

Surely the idea is to take player movements and notify all other players so they can update their local world state? In which case a loop is the only thing to do and the exact kind of list probably doesn’t make too much difference.

To temu, how big is the world, and how many players are you hoping to connect? To start with I’d just do what you posted originally, and if you start getting hundreds of users then that’s a nice problem to have :slight_smile:

At that point one way it might be adapted to improve performance is divide the world into sub-areas and when player movements come in, only notify other players in their sub-area, and also probably adjacent sub-areas. When a player moves between areas, shift their “Player” record to the list for the new subarea. In that kind of scheme it could make sense to have movements in each area monitored by a separate thread, effectively treating each area as a separate small world. Potentially more scalable that way too as you might eventually run each area on a different machine entirely.