简体   繁体   English

在 spring 应用程序中维护 state?

[英]Maintaining state in a spring application?

Just looking for a bit if theory regarding maintaining state of something in a spring application.只是寻找一些关于在 spring 应用程序中维护 state 的理论。

For example imagine a game of poker running as a web app.例如,想象一个扑克游戏作为 web 应用程序运行。 There are a number of things to consider.有很多事情需要考虑。 Such as maintaining the amount of poker chips 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.不管执行游戏循环的逻辑如何,跟踪一手扑克牌的 state 以及使用 spring 的完整扑克游戏的最佳方法是什么。

Thanks in advance.提前致谢。

Same way as you keep track of any data in a web app;与跟踪 web 应用程序中的任何数据的方式相同; write a DAO interface then create an implementation.编写一个 DAO 接口然后创建一个实现。 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.每当玩家采取行动进行更改时,他们都会向 web 应用程序发出请求。 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.您将不得不跟踪 state 的几件事,显然您将不得不担心在玩家之间同步 state。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM