Ce serveur Gitlab sera éteint le 30 juin 2020, pensez à migrer vos projets vers les serveurs gitlab-research.centralesupelec.fr et gitlab-student.centralesupelec.fr !

Commit 2858d615 authored by Arnaud Chevalier's avatar Arnaud Chevalier

timer

parent 1bb32c18
......@@ -259,12 +259,12 @@ function catchUp(tableState) {
//targetSeat = (targetSeat + 1) % 6;
moveActive((tableState["activePlayer"]+1));
setActionButtons(tableState);
setTimerOrigin(tableState["remainingTime"].toFixed(0))
}
function setActionButtons(tableState) {
var call = $("#call-button");
call.text("Call : " + tableState["amountToCall"].toString() + "$");
call.text("Call : $" + tableState["amountToCall"].toString());
var amount = document.getElementById("amount-bet");
amount.setAttribute("value", tableState["bettingRange"][0]);
amount.setAttribute("min", tableState["bettingRange"][0]);
......@@ -336,7 +336,7 @@ function updateGameState() {
// });
}
function setTimer(val) {
function setTimerOrigin(val) {
var timer = $('#game-timer');
timer.removeClass();
if (val < 10)
......@@ -350,6 +350,16 @@ function setTimer(val) {
timer.text(val + " seconds remaining.");
timer.show();
}
function decreaseTimer() {
var timer = $('#game-timer');
var timerText = timer.text();
var val = parseInt(timerText.substring(0,2).replace(/\s/g,''));
val = val - 1;
if (val < 0)
val = 0;
timer.text(val + " seconds remaining.");
timer.show();
}
function hideTimer() {
$('#game-timer').hide();
}
......@@ -633,3 +643,8 @@ $(document).everyTime(5000,
function (i) {
updateGameState();
}, 0);
$(document).everyTime(1000,
function () {
decreaseTimer();
}, 0);
......@@ -40,7 +40,7 @@ class Player:
return self.__cards
@property
def getRemainingChips(self):
def remainingChips(self):
return self.__chips
@property
......
......@@ -16,6 +16,7 @@ from card import Card
from hand import Hand
from deck import Deck
from player.player import Player
from tableTimer import TableTimer
import json
from enum import Enum
......@@ -30,6 +31,7 @@ class GameState(Enum):
class Table(object):
def __init__(self, maxPlayers):
self.uniqueId = str(uuid4())
self.maxPlayers = maxPlayers
......@@ -50,6 +52,7 @@ class Table(object):
self.currentBet = 0
self.minRaise = 0
self.handNbr = 0
self.timer = TableTimer(30, print)
def addPlayer(self, p):
p.receiveChip(self.buyIn)
......@@ -68,13 +71,18 @@ class Table(object):
playerFound = True
print("Action to player {}.".format(self.players[self.activePlayer].name))
print("Pot {}, min raise {}".format(self.pot, self.minRaise))
# start timer
print(self.timer.remaining())
self.timer = TableTimer(30, self.__defaultActionCallback)
self.timer.start()
for p in self.players:
if p.isPlaying:
print("Player {} : {} ({} chips)".format(p.name, p.getChipsOnTable, p.getRemainingChips))
print("Player {} : {} ({} chips)".format(p.name, p.getChipsOnTable, p.remainingChips))
else:
print("Player {} : out ({} chips)".format(p.name, p.getRemainingChips))
print("Player {} : out ({} chips)".format(p.name, p.remainingChips))
return self.activePlayer
def startHand(self):
self.deck.reset()
self.boardCards = []
......@@ -84,7 +92,7 @@ class Table(object):
self.players.extend(self.waitingPlayers)
self.waitingPlayers = []
for i in range(len(self.players)):
if self.players[i].getRemainingChips <= 0:
if self.players[i].remainingChips <= 0:
del self.players[i]
assert len(self.players) >= 2
self.nbrPlayersInHand = len(self.players)
......@@ -98,8 +106,12 @@ class Table(object):
self.gameState = GameState.PREFLOP
print("Starting hand {0} with {1} players.".format(self.handNbr, len(self.players)))
for p in self.players:
print("Player {} : {} chips".format(p.name, p.getRemainingChips))
print("Player {} : {} chips".format(p.name, p.remainingChips))
print("Action to player {}.".format(self.players[self.activePlayer].name))
# start timer
self.timer = TableTimer(30, self.__defaultActionCallback)
self.timer.start()
def __nextStep(self):
for p in self.players:
......@@ -180,6 +192,8 @@ class Table(object):
return True
def fold(self, playerId):
# timer management
self.timer.cancel()
p = self.players[playerId]
if self.activePlayer != playerId:
print("Player {} cant fold, action currently to player {}".format(p.name, self.activePlayer))
......@@ -196,6 +210,8 @@ class Table(object):
return True
def check(self, playerId):
# timer management
self.timer.cancel()
p = self.players[playerId]
if self.activePlayer != playerId:
print("Player {} cant check, action currently to player {}".format(p.name, self.activePlayer))
......@@ -214,6 +230,8 @@ class Table(object):
return True
def call(self, playerId):
# timer management
self.timer.cancel()
p = self.players[playerId]
if self.activePlayer != playerId:
print("Player {} cant call, action currently to player {}".format(p.name, self.activePlayer))
......@@ -230,6 +248,8 @@ class Table(object):
return True
def bet(self, playerId, amount): # bet_by
# timer management
self.timer.cancel()
p = self.players[playerId]
if self.activePlayer != playerId:
print("Player {} cant bet, action currently to player {}".format(p.name, self.activePlayer))
......@@ -237,8 +257,8 @@ class Table(object):
if amount + p.getChipsOnTable < self.currentBet + self.minRaise:
print("Player {} cant bet, not adding enough money (would be {}/{})".format(p.name, amount + p.getChipsOnTable, self.currentBet + self.minRaise))
return False
if amount > p.getRemainingChips:
print("Player {} cant bet, not enough money ({}/{})".format(p.name, amount, self.players[playerId].getRemainingChips))
if amount > p.remainingChips:
print("Player {} cant bet, not enough money ({}/{})".format(p.name, amount, self.players[playerId].remainingChips))
return False
if not p.bet(amount):
print("Player {} cant bet, betting failed (unexpected).".format(p.name))
......@@ -269,14 +289,15 @@ class Table(object):
"gameState": self.gameState.name,
"players": [{
"name": player.name,
"chips": player.getRemainingChips,
"chips": player.remainingChips,
"isActive": player.isPlaying,
"chipsOnTable": player.getChipsOnTable,
} for player in self.players],
"holeCards": self._getHoleCards(userId),
"availableActions": self._availableActions(userId),
"amountToCall": self._getAmountToCall(userId),
"bettingRange": self._getBettingRange(userId)
"bettingRange": self._getBettingRange(userId),
"remainingTime": self.timer.remaining(),
})
def getPlayerIndex(self, userId):
......@@ -302,7 +323,7 @@ class Table(object):
if userIndex == -1:
return []
p = self.players[userIndex]
return [self.currentBet + self.minRaise - p.getChipsOnTable, p.getRemainingChips]
return [self.currentBet + self.minRaise - p.getChipsOnTable, p.remainingChips]
def _availableActions(self, userId):
userIndex = self.getPlayerIndex(userId)
......@@ -327,3 +348,10 @@ class Table(object):
return 0
p = self.players[userIndex]
return self.currentBet - p.getChipsOnTable
def __defaultActionCallback(self):
activePlayerId = self.players[self.activePlayer].uniqueId
if "check" in self._availableActions(activePlayerId):
self.check(self.activePlayer)
elif "call" in self._availableActions(activePlayerId):
self.call(self.activePlayer)
import threading
import time
class TableTimer(threading.Timer):
started_at = None
def start(self):
self.started_at = time.time()
threading.Timer.start(self)
def elapsed(self):
return time.time() - self.started_at
def remaining(self):
return self.interval - self.elapsed()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment