Just looking for a bit if theory regarding maintaining state of something in a spring application.
For example imagine a game of poker running as a web app. There are a number of things to consider. Such as maintaining the amount of poker ch开发者_如何学Pythonips a player has in a hand, who is involved in a hand, the card values per player as well as the shared community cards. The amounts bet, pot totals etc.
Other things to consider are the time allocations a player has to act before being folded.
Regardless of the logic for performing the game loop, what would be the best way to keep track of the state of a hand of poker and also a complete game of poker using spring.
Thanks in advance.
Same way as you keep track of any data in a web app; write a DAO interface then create an implementation. For example;
public class PokerHand {
//just a POJO
}
public interface PokerHandDAO {
public PokerHand getPokerHand(long id);
public void updatePokerHand(PokerHand hand);
}
public class InMenoryPokerHandDAO implements PokerHandDAO {
private static Map<Long,PokerHand> hands = new HashMap<Long,PokerHand>();
@Override
public PokerHand getPokerHand(long id) {
return hands.get(id);
}
@Override
public void updatePokerHand(PokerHand hand) {
hands.put(hand.getId(), hand);
}
}
Whenever a player takes an action to change they make a request to the web app. You're going to have to keep track of the state of several things, and you're obviously going to have to worry about syncronising the state amongst players.
精彩评论