0
\$\begingroup\$

I'm intermediate in Java but novice in everything which is on the server-side. I've set myself a task to rewrite an old game, so learning Java is more fun. It is a space turn-based strategy. The original was called Stars! (I heard that Master of Orion is somewhat similar). Basically players assign their orders to ships and planets and then submit a turn to server, where a new turn is generated.

I suppose, I will have a lot of objects (ships, fleets, planets and so on), each of them will have some properties (XY position, orders, HP etc.). When generating a turn, the server will have to cycle through all the objects and perform tasks with each one (move a ship, unload a cargo for instance).

The question is: How should I organize entities, ships for example, so it is easy to process them. Should I store them in a database? Perhaps there is some sort of a best practice for tasks like this?

If I assume, that ships will be stored in a database. Then, each player will send a file with orders to the server. The server will put everything into a database. When all the players have submitted their turns, the server will cycle through everything in the database and generate a turn. Will this be a good way to do this?

\$\endgroup\$
0
\$\begingroup\$

Since in order to take his turn the player needs to know the outcome of other players actions, one way to solve this is a message consumer/producer pattern.

First, you need to define a queue structure with objects sharing a common functionality; this can be as simple as just List of Unit objects where Unit is a class providing an unique identifier for each unit.

A thread on the server would cycle through the list looking for the next unit and querying a database (or in memory) for the last message provided for that unit, and wait if none is found. This is the first part, the message consumer.

The second part is the message producer, where each player would somehow put a new message for a given unit via whatever interface you define. If it's a turn based game it makes sense that only a message per unit should go there (so if there's a second message the first one gets overwritten). Ideally the interface would prevent the player from adding a message unless it's his turn (whose turn it is is an attribute of the consumer, the unit after the last one that acted)

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.