pioche/défausse
This commit is contained in:
parent
35fada4c09
commit
c1f4dfcd23
3 changed files with 123 additions and 9 deletions
|
|
@ -10,6 +10,7 @@ app = Flask(__name__)
|
||||||
CORS(app)
|
CORS(app)
|
||||||
|
|
||||||
tiles = Tile.generateAll()
|
tiles = Tile.generateAll()
|
||||||
|
current_game = None # Stockage de la partie actuelle
|
||||||
|
|
||||||
# ============= GET ================
|
# ============= GET ================
|
||||||
@app.route('/api/tiles', methods=['GET'])
|
@app.route('/api/tiles', methods=['GET'])
|
||||||
|
|
@ -58,8 +59,9 @@ def get_random_hand():
|
||||||
@app.route('/api/hand_with_draw', methods=['GET'])
|
@app.route('/api/hand_with_draw', methods=['GET'])
|
||||||
def get_hand_with_draw():
|
def get_hand_with_draw():
|
||||||
"""Crée une partie et retourne le hand_player avec une tuile piochée"""
|
"""Crée une partie et retourne le hand_player avec une tuile piochée"""
|
||||||
game = Game()
|
global current_game
|
||||||
game.draw_tile(game.hand_player)
|
current_game = Game()
|
||||||
|
current_game.draw_tile(current_game.hand_player)
|
||||||
|
|
||||||
hand_data = [
|
hand_data = [
|
||||||
{
|
{
|
||||||
|
|
@ -68,15 +70,51 @@ def get_hand_with_draw():
|
||||||
'value': str(tile.value),
|
'value': str(tile.value),
|
||||||
'svg': tile.get_img()
|
'svg': tile.get_img()
|
||||||
}
|
}
|
||||||
for tile in game.hand_player.tiles
|
for tile in current_game.hand_player.tiles
|
||||||
]
|
]
|
||||||
|
|
||||||
draw_data = {
|
draw_data = {
|
||||||
'id': game.hand_player.draw.id,
|
'id': current_game.hand_player.draw.id,
|
||||||
'family': str(game.hand_player.draw.family),
|
'family': str(current_game.hand_player.draw.family),
|
||||||
'value': str(game.hand_player.draw.value),
|
'value': str(current_game.hand_player.draw.value),
|
||||||
'svg': game.hand_player.draw.get_img()
|
'svg': current_game.hand_player.draw.get_img()
|
||||||
} if game.hand_player.draw else None
|
} if current_game.hand_player.draw else None
|
||||||
|
|
||||||
|
return jsonify({'hand': hand_data, 'draw': draw_data})
|
||||||
|
|
||||||
|
@app.route('/api/discard', methods=['POST'])
|
||||||
|
def discard_tile():
|
||||||
|
"""Défausse une tuile de la main du joueur et pioche une nouvelle"""
|
||||||
|
global current_game
|
||||||
|
if current_game is None:
|
||||||
|
return jsonify({'error': 'Aucune partie en cours'}), 400
|
||||||
|
|
||||||
|
data = request.json
|
||||||
|
tile_id = data.get('tile_id')
|
||||||
|
|
||||||
|
if tile_id is None:
|
||||||
|
return jsonify({'error': 'tile_id manquant'}), 400
|
||||||
|
|
||||||
|
current_game.put_tile(current_game.hand_player, tile_id)
|
||||||
|
current_game.draw_tile(current_game.hand_player)
|
||||||
|
|
||||||
|
# Retourner la main mise à jour
|
||||||
|
hand_data = [
|
||||||
|
{
|
||||||
|
'id': tile.id,
|
||||||
|
'family': str(tile.family),
|
||||||
|
'value': str(tile.value),
|
||||||
|
'svg': tile.get_img()
|
||||||
|
}
|
||||||
|
for tile in current_game.hand_player.tiles
|
||||||
|
]
|
||||||
|
|
||||||
|
draw_data = {
|
||||||
|
'id': current_game.hand_player.draw.id,
|
||||||
|
'family': str(current_game.hand_player.draw.family),
|
||||||
|
'value': str(current_game.hand_player.draw.value),
|
||||||
|
'svg': current_game.hand_player.draw.get_img()
|
||||||
|
} if current_game.hand_player.draw else None
|
||||||
|
|
||||||
return jsonify({'hand': hand_data, 'draw': draw_data})
|
return jsonify({'hand': hand_data, 'draw': draw_data})
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -23,10 +23,16 @@ class Hand:
|
||||||
if self.draw is not None and self.draw.id == id:
|
if self.draw is not None and self.draw.id == id:
|
||||||
draw = self.draw
|
draw = self.draw
|
||||||
self.draw = None
|
self.draw = None
|
||||||
|
self.fold()
|
||||||
|
self.sort()
|
||||||
return draw
|
return draw
|
||||||
for i, tile in enumerate(self.tiles):
|
for i, tile in enumerate(self.tiles):
|
||||||
if tile.id == id:
|
if tile.id == id:
|
||||||
return self.tiles.pop(i)
|
t = self.tiles.pop(i)
|
||||||
|
self.fold()
|
||||||
|
self.sort()
|
||||||
|
return t
|
||||||
|
self.sort()
|
||||||
return None
|
return None
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|
|
||||||
|
|
@ -244,6 +244,9 @@
|
||||||
const tileDiv = document.createElement('div');
|
const tileDiv = document.createElement('div');
|
||||||
tileDiv.className = 'hand-tile-item';
|
tileDiv.className = 'hand-tile-item';
|
||||||
tileDiv.innerHTML = tile.svg;
|
tileDiv.innerHTML = tile.svg;
|
||||||
|
tileDiv.dataset.tileId = tile.id;
|
||||||
|
tileDiv.style.cursor = 'pointer';
|
||||||
|
tileDiv.addEventListener('click', () => discardTile(tile.id, tileDiv));
|
||||||
handTiles.appendChild(tileDiv);
|
handTiles.appendChild(tileDiv);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -252,12 +255,79 @@
|
||||||
const drawTileDiv = document.createElement('div');
|
const drawTileDiv = document.createElement('div');
|
||||||
drawTileDiv.className = 'hand-tile-item draw-tile';
|
drawTileDiv.className = 'hand-tile-item draw-tile';
|
||||||
drawTileDiv.innerHTML = data.draw.svg;
|
drawTileDiv.innerHTML = data.draw.svg;
|
||||||
|
drawTileDiv.dataset.tileId = data.draw.id;
|
||||||
|
drawTileDiv.style.cursor = 'pointer';
|
||||||
|
drawTileDiv.addEventListener('click', () => discardTile(data.draw.id, drawTileDiv));
|
||||||
container.appendChild(drawTileDiv);
|
container.appendChild(drawTileDiv);
|
||||||
|
|
||||||
handDiv.appendChild(container);
|
handDiv.appendChild(container);
|
||||||
tilesDisplay.appendChild(handDiv);
|
tilesDisplay.appendChild(handDiv);
|
||||||
})
|
})
|
||||||
.catch(error => console.error('Erreur:', error));
|
.catch(error => console.error('Erreur:', error));
|
||||||
|
|
||||||
|
// Fonction pour défausser une tuile
|
||||||
|
async function discardTile(tileId, tileElement) {
|
||||||
|
try {
|
||||||
|
const response = await fetch('http://localhost:5000/api/discard', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ tile_id: tileId })
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
console.error('Erreur lors de la défausse');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
// Ajouter une animation de suppression
|
||||||
|
tileElement.style.opacity = '0';
|
||||||
|
tileElement.style.transition = 'opacity 0.3s ease';
|
||||||
|
|
||||||
|
setTimeout(() => {
|
||||||
|
// Mettre à jour la main interactive avec la nouvelle main
|
||||||
|
updateInteractiveHand(data.hand, data.draw);
|
||||||
|
}, 300);
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Erreur:', error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fonction pour mettre à jour la main interactive
|
||||||
|
function updateInteractiveHand(handData, drawData) {
|
||||||
|
const handTiles = document.getElementById('interactive-tiles');
|
||||||
|
handTiles.innerHTML = '';
|
||||||
|
|
||||||
|
handData.forEach(tile => {
|
||||||
|
const tileDiv = document.createElement('div');
|
||||||
|
tileDiv.className = 'hand-tile-item';
|
||||||
|
tileDiv.innerHTML = tile.svg;
|
||||||
|
tileDiv.dataset.tileId = tile.id;
|
||||||
|
tileDiv.style.cursor = 'pointer';
|
||||||
|
tileDiv.addEventListener('click', () => discardTile(tile.id, tileDiv));
|
||||||
|
handTiles.appendChild(tileDiv);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Mettre à jour le draw
|
||||||
|
const container = handTiles.parentElement;
|
||||||
|
const oldDrawTile = container.querySelector('.draw-tile');
|
||||||
|
if (oldDrawTile) {
|
||||||
|
oldDrawTile.remove();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (drawData) {
|
||||||
|
const drawTileDiv = document.createElement('div');
|
||||||
|
drawTileDiv.className = 'hand-tile-item draw-tile';
|
||||||
|
drawTileDiv.innerHTML = drawData.svg;
|
||||||
|
drawTileDiv.dataset.tileId = drawData.id;
|
||||||
|
drawTileDiv.style.cursor = 'pointer';
|
||||||
|
drawTileDiv.addEventListener('click', () => discardTile(drawData.id, drawTileDiv));
|
||||||
|
container.appendChild(drawTileDiv);
|
||||||
|
}
|
||||||
|
}
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
Loading…
Reference in a new issue