#[derive(Debug, Clone, Deserialize)]
pub enum Scope {
Global,
- Game { id: u32 },
- Hand { id: u32 },
+ Game { id: i64 },
+ Hand { id: i64 },
Player { username: String },
}
Logout,
CreateGame { settings: GameSettings },
JoinLobby { filter: String },
- JoinGame { id: u32 },
+ JoinGame { id: i64 },
TakeAction { action: Action },
SendMessage { scope: Scope, message: String },
LeaveGame,
ChangeNicknameSuccess,
ChangeNicknameFailure { reason: String },
LogoutSuccess,
- CreateGameSuccess { id: u32 },
+ CreateGameSuccess { id: i64 },
CreateGameFailure { reason: String },
JoinLobbySuccess { games: Vec<GameSummary> },
JoinLobbyFailure { reason: String },
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ClientInterest {
GameList,
- Game { id: u32 },
+ Game { id: i64 },
User { username: Username },
}
}
impl Dealer {
- pub async fn new(mut server: ServerState, id: u32) -> RedisResult<Self> {
+ pub async fn new(mut server: ServerState, id: i64) -> RedisResult<Self> {
let mut interests = HashSet::new();
interests.insert(ClientInterest::Game{id});
server.register_interests(interests).await;
#[derive(Debug, Clone)]
pub struct Chatroom {
- id: u32,
+ id: i64,
settings: ChatroomSettings,
messages: Vec<(Username, ChatroomAction)>,
users: HashSet<Username>,
}
impl Chatroom {
- pub fn new(id: u32, settings: ChatroomSettings) -> Self {
+ pub fn new(id: i64, settings: ChatroomSettings) -> Self {
Chatroom {
id,
settings,
}
impl Game for Chatroom {
- fn id(&self) -> u32 {
+ fn id(&self) -> i64 {
self.id
}
pub use self::action::{Action, ActionError, UserAction, ValidatedUserAction};
pub trait Game : Debug + CloneBox + Send + Sync {
- fn id(&self) -> u32;
+ fn id(&self) -> i64;
fn players(&self) -> HashSet<Username>;
fn actions_len(&self) -> usize;
fn validate_action(&self, action: UserAction) -> Result<ValidatedUserAction, ActionError>;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GameSummary {
- id: u32,
+ id: i64,
settings: GameSettings,
}
impl GameSummary {
- pub fn new(id: u32, settings: GameSettings) -> Self {
+ pub fn new(id: i64, settings: GameSettings) -> Self {
Self{id, settings}
}
- pub fn id(&self) -> u32 {
+ pub fn id(&self) -> i64 {
self.id
}
}
-pub enum TexasHoldEm {
- NotYetStarted {
- seats: Seats,
- stacks: HashMap<Username, u32>,
- }
- Dealing {
- dealer: Username,
- hands: HashMap<Username, HashSet<Card>>,
- deck: HashSet<Card>,
- seats: Seats,
- stacks: HashMap<Username, u32>,
- }
- PostSmallBlind {
- dealer: Username,
- action: Username,
- hands: HashMap<Username, HashSet<Card>>,
- deck: HashSet<Card>,
- seats: Seats,
- bets: HashMap<Username, u32>,
- players: HashSet<Username>,
- stacks: HashMap<Username, u32>,
- }
- PostBigBlind {
- dealer: Username,
- action: Username,
- hands: HashMap<Username, HashSet<Card>>,
- deck: HashSet<Card>,
- seats: Seats,
- bets: HashMap<Username, u32>,
- players: HashSet<Username>,
- stacks: HashMap<Username, u32>,
- }
- PreFlopBetting {
- dealer: Username,
- action: Username,
- hands: HashMap<Username, HashSet<Card>>,
- deck: HashSet<Card>,
- seats: Seats,
- pot: u32,
- bets: HashMap<Username, u32>,
- players: HashSet<Username>,
- stacks: HashMap<Username, u32>,
- }
- DealFirstFlopCard {
- dealer: Username,
- action: Username,
- flop: [Card; 1],
- hands: HashMap<Username, HashSet<Card>>,
- deck: HashSet<Card>,
- seats: Seats,
- pot: u32,
- players: HashSet<Username>,
- stacks: HashMap<Username, u32>,
- }
- DealSecondFlopCard {
- dealer: Username,
- action: Username,
- flop: [Card; 2],
- hands: HashMap<Username, HashSet<Card>>,
- deck: HashSet<Card>,
- seats: Seats,
- pot: u32,
- players: HashSet<Username>,
- stacks: HashMap<Username, u32>,
- }
- DealThirdFlopCard {
- dealer: Username,
- action: Username,
- flop: [Card; 3],
- hands: HashMap<Username, HashSet<Card>>,
- deck: HashSet<Card>,
- seats: Seats,
- pot: u32,
- players: HashSet<Username>,
- stacks: HashMap<Username, u32>,
- }
- PostFlopBetting {
- }
+pub enum TexasHoldEmState {
+ NotStarted,
+ Dealing,
+ PostingSmallBlind,
+ PostingBigBlind,
+ PreFlopBetting,
+ DealingFlop,
+ PostFlopBetting,
+ DealingTurn,
+ TurnBetting,
+ DealingRiver,
+ RiverBetting,
+ Completed,
+}
+
+pub struct TexasHoldEmSettings {
+ title: String,
+ max_players: u32,
+ blinds: u64,
+ starting_stack: u64,
+}
+
+pub struct TexasHoldEm {
+ id: i64,
+ settings: TexasHoldEmSettings,
+ actions_len: usize,
+ state: TexasHoldEmState,
+ seats: Seats,
+ stacks: HashMap<Username, u64>,
+ hands: HashMap<Username, HashSet<Card>>,
+ deck: HashSet<Card>,
+ dealer: Username,
+ action: Username,
+ bets: HashMap<Username, u64>,
+ players: HashSet<Username>,
+ pot: u64,
+ community: HashSet<Card>,
}
#[derive(Clone, Debug)]
pub struct KnockOutWhist {
- id: u32,
+ id: i64,
settings: KnockOutWhistSettings,
actions_len: usize,
state: KnockOutWhistState,
}
impl KnockOutWhist {
- pub fn new(id: u32, settings: KnockOutWhistSettings) -> Self {
+ pub fn new(id: i64, settings: KnockOutWhistSettings) -> Self {
Self {
id,
settings,
}
impl Game for KnockOutWhist {
- fn id(&self) -> u32 {
+ fn id(&self) -> i64 {
self.id
}
format!("user:{}", username)
}
-fn game_settings_key(id: u32) -> String {
+fn game_settings_key(id: i64) -> String {
format!("game:{}:settings", id)
}
-fn game_actions_key(id: u32) -> String {
+fn game_actions_key(id: i64) -> String {
format!("game:{}:actions", id)
}
}
}
- pub async fn create_game(&mut self, settings: GameSettings) -> RedisResult<u32> {
- let id = self.redis.incr("game:next_id", 1u32).await?;
+ pub async fn create_game(&mut self, settings: GameSettings) -> RedisResult<i64> {
+ let id = self.redis.incr("game:next_id", 1).await?;
let key = game_settings_key(id);
let () = self.redis.set(key, AsJson(settings)).await?;
let () = self.redis.rpush("game:list", id).await?;
pub async fn game_list(&mut self, from: usize) -> RedisResult<Vec<GameSummary>> {
debug!("game_list(from: {})", from);
- let games: Vec<u32> = self.redis.lrange("game:list", from as isize, -1).await?;
+ let games: Vec<i64> = self.redis.lrange("game:list", from as isize, -1).await?;
let mut summaries = Vec::with_capacity(games.len());
for id in games {
match self.game_summary(id).await {
Ok(summaries)
}
- pub async fn game_state(&mut self, id: u32, from: usize) -> RedisResult<Vec<ValidatedUserAction>> {
+ pub async fn game_state(&mut self, id: i64, from: usize) -> RedisResult<Vec<ValidatedUserAction>> {
let key = game_actions_key(id);
let actions: Vec<AsJson<ValidatedUserAction>> = self.redis.lrange(&key, from as isize, -1).await?;
Ok(actions.into_iter().map(AsJson::get).collect())
}
- pub async fn game_summary(&mut self, id: u32) -> RedisResult<GameSummary> {
+ pub async fn game_summary(&mut self, id: i64) -> RedisResult<GameSummary> {
let key = game_settings_key(id);
self.redis.get(key).await.map(AsJson::get).map(|settings| GameSummary::new(id, settings))
}
- pub async fn take_action(&mut self, id: u32, len: usize, action: &ValidatedUserAction) -> RedisResult<ActionStatus> {
+ pub async fn take_action(&mut self, id: i64, len: usize, action: &ValidatedUserAction) -> RedisResult<ActionStatus> {
let key = game_actions_key(id);
debug!("take_action: EVAL {{TAKE_ACTION_LUA_SCRIPT}} 1 {} {} {:?}", key, len, action);
self.take_action_script.key(key).arg(len).arg(AsJson(action)).invoke_async(&mut self.redis).await