J2EE Game Implementation Help

Started by
3 comments, last by pek 16 years, 11 months ago
Important ---------- This is big! :S If you are familiar with J2EE, Statefull/Stateless Beans, Entity Beans, Hibernate please read and reply (anything!). Intro ---------- I am currently developing a card game with J2EE using Eclipse, JBoss and PostgresSQL. I have a very weird problem and have been looking for an answer more than a month. This is probably the fifth post in a group. I would be really appreciated if anyone could have some time to deal with my problem. Anyway, this is it. Structure ---------- The Game is divided into 3 parts (obviously) Client-Side: A very basic java2d gui Server-Side: There is currently two Session Beans that manage all (the project isn't finished so there would be more once I fix this problem) - DomainManager: A Stateless Bean which manages the member's registration/login to the game - GameManager: A Statefull Bean which manages all the game actions (draw card, advance turn etc.) Data: All the Entities that are needed for the Game (mostly for sharing between players). The most important are: - Game: It has a list of Teams, a centerDeck and some other information - Team: It has a list of Players and some other information - Player: It has a Member, the players hand cards and some other information - Member: Username, password etc. - Card: A simple card class This is the basic structure so far. The client will follow the scenario: Register/Login using the DomainManager Create/Join a Game using the GameManager Do some actions using the GameManager Inside ---------- When a member creates a Game using the GameManager.createGame method the Game is in the Waiting status which means it is waiting for all the seats to be filled (can be 2,4,6 and 8 players). The GameManager has 3 fields: the persistentManager, the Game and the Player (which is created in the method GameManager.createGame()) When a member uses GameManager.joinGame(int gameId) method the method looks if all members are filled and if true calls the "private void GameManager.startGame()" method. This method creates a stack of 108 cards in the center, then removes 11 cards from that stack to add them to a players hand. It does this for all players. Then it adds 2 stacks of 11 cards each to the side. After GameManager.startGame() ends all the transactions to the database are done automatically, that is, I don't explicitly invoke a entityManager.merge method. Problem ---------- Strangely when startGame method ends and all the database is updated, each Team has the same reference of the player 11 times! For example, if a Game had 2 players in 2 different teams and startGame method ended, then each team had 11 Players (exactly the same team1.getPlayers().get(0)==team1.getPlayers().get(1)). The most head scratching of all though is that in the database all the data is correctly added (each team has only one record of a player, not 11). Want to here something funny? Well, the game starts, then a player uses GameManager.drawCard(). This method should remove a card from the centerStack and add it to the players hand. But guess what! Since there are 11 identical reference of the player, 11 cards are removed from the stack! Help -------- I am really desperate in this. I am looking around everything. My thoughts are that something is going wrong with Hibernate. I have read somewhere that Hibernate tries to "smartly" create the steps of the database transaction to optimize the database queries. So I believe this probably is the problem when startGame method is called. Because a lot of "remove from here and add it here" is going on. But I cannot find a solution. I am also trying trying to find a J2EE example that uses collections in collections etc. (like Game has teams and team has players) but nothing. Another thought is that since GameManager has a player reference (which is not detached) I use it to add a card to the player without using the game.getTeams().get(0).getPlayers().get(playerIndex). So I think that when the GameManager.drawCard() method ends, something is going wrong with the reference and it is created (and added) to the Team again. More -------- For some source code, I have uploaded the essential classes here (I have changed it more than a million times by now but the idea is the same, and sadly, the results too): http://pek.ekei.com/misc/help/cards/source.html I have also posted on the GougleGroups here: http://groups.google.com/group/comp.lang.java.programmer/browse_thread/thread/1e19456dbd5b08c2 Apologies --------- Sorry for the length of this. As I said, I am looking everyone to solve this with no results. Thank you for taking the time. All responses would be greatly appreciated (given the time a user took to read this). Even if a user takes the time to read this and reply "Shut up" would be also appreciated! One Million Thank Yous (http://www.yonkis.com/mediaflash/unmillon.htm) -pek
Advertisement
Write a wrapper/accessor to your Team list.

Each time an object is added, print out stack trace.

You'll see exactly where it's called.
Quote:Original post by Antheus
Write a wrapper/accessor to your Team list.

Each time an object is added, print out stack trace.

You'll see exactly where it's called.


Good plan. Log4j can be used to print files and line numbers of logging calls.

Omae Wa Mou Shindeiru

Quote:Original post by LorenzoGatti
Quote:Original post by Antheus
Write a wrapper/accessor to your Team list.

Each time an object is added, print out stack trace.

You'll see exactly where it's called.


Good plan. Log4j can be used to print files and line numbers of logging calls.


(new Throwable()).printStackTrace(); works just as well for debugging since it gives you a complete stack trace.

We already know where the problem is, it's how we get there that's unknown.
I printed out every step of the list. The problem occurs right after the method is over and the transaction processed. So I think there is something going wrong with Hibernate and the support for lists. Is there anywhere I can see and example that a class has a list of classes which in turn have also a list of classes (all entities in the database)?

This topic is closed to new replies.

Advertisement