123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- # -*- coding: utf-8 -*-
- import flask_socketio as fio
- from flask_login import current_user
- from db import Game, Player, User
- from belote_jeu import translator, legal_play, colors, N_PLAYERS, N_TURN
- NAME_ARBITRE = 'Arbitre'
- NAME_BONJOUR = 'Portier'
- class CustomNamespacePlay(fio.Namespace):
- def __init__(self, id):
- self._id = id
- self.users = []
- fio.Namespace.__init__(self, '/game/{}'.format(id))
- def on_connect(self):
- if current_user is None or not self.game().authorized(current_user):
- fio.disconnect()
- if current_user.name not in self.users:
- if len(self.users)==1:
- fio.emit('text', {'text': '{} est là !'.format(self.users[0]), 'username':None, 'name':NAME_BONJOUR})
- elif len(self.users)>1:
- fio.emit('text', {'text': '{} sont là !'.format(', '.join(self.users)), 'username':None, 'name':NAME_BONJOUR})
- self.users.append(current_user.name)
- fio.emit('text', {'text': '{} vient d\'arriver !'.format(current_user.name), 'username':None, 'name':NAME_BONJOUR}, broadcast=True)
- pass
- def on_disconnect(self):
- if current_user.name in self.users:
- self.users.remove(current_user.name)
- fio.emit('text', {'text': '{} est parti.e.'.format(current_user.name), 'username':None, 'name':NAME_BONJOUR}, broadcast=True)
- pass
- def on_join(self, data):
- pass
- def on_choose_color(self, data):
- if 'atout' not in data:
- return
- atout = data['atout']
- game = self.game()
- if not game.turn<0:
- fio.emit('text', {'text': "Choix d'atout indisponible", 'username':None, 'name':NAME_ARBITRE})
- return
- # C'est mon tour ?
- if game.get_nr(current_user) != (game.turn + game.first_player)%4:
- fio.emit('text', {'text': "Ce n'est pas votre tour...", 'username':None, 'name':NAME_ARBITRE})
- return
- # Choix légal pour ce tour ?
- if atout is not None and game.turn < -4 and game.played[1] != atout:
- fio.emit('text', {'text': "Pour l'instant on prend ou pas, mais on ne peut pas choisir l'atout.", 'username':None, 'name':NAME_ARBITRE})
- return
- if atout is not None and not atout in colors:
- fio.emit('text', {'text': "Couleur d'atout inconnue ({})".format(atout), 'username':None, 'name':NAME_ARBITRE})
- return
- # OK !
- if(game.tour_choix(atout, current_user)):
- fio.emit('choose_color', {'turn':game.turn, 'first_player':game.first_player, 'atout':game.atout, 'preneur':game.preneur if game.preneur is not None else current_user.login}, broadcast=True)
- else:
- fio.emit('text', {'text': "Erreur E0101", 'username':None, 'name':NAME_ARBITRE})
-
- def on_play(self, data):
- if 'card' not in data:
- return
- card = data['card']
- game = self.game()
- if game.turn<0 or game.atout is None or game.atout =='':
- fio.emit('text', {'text': "Jeu pas encore disponible. Il faut choisir l'atout !", 'username':None, 'name':NAME_ARBITRE})
- return
- # C'est mon tour ?
- if game.get_nr(current_user) != (game.turn + game.first_player)%4:
- fio.emit('text', {'text': "Ce n'est pas votre tour...", 'username':None, 'name':NAME_ARBITRE})
- return
- # Choix légal pour ce tour ?
- p = game.get_player(current_user)
- if legal_play(game.played, game.atout, card, p.cards):
- ok, winnr = game.tour_jeu(card, current_user)
- if ok:
- fio.emit('play', {'turn':game.turn, 'played':game.played.split(',') if game.played is not None and game.played !='' else [], \
- 'last_played':game.last_played.split(',') if game.last_played is not None else [] , 'first_player': game.first_player, 'points':[game.points_0, game.points_1]}, broadcast=True)
- else:
- fio.emit('text', {'text': "Vous ne pouvez pas jouer cette carte ({})".format(translator(card)), 'username':None, 'name':NAME_ARBITRE})
- return
- def on_text(self, data):
- if 'text' in data and len(data['text'])>0:
- fio.emit('text', {'text':data['text'], 'username':current_user.login, 'name':current_user.name}, broadcast=True);
- def on_monjeu(self, data):
- cards = self.game().get_player(current_user).cards
- cards = cards.split(',') if cards is not None and cards != '' else []
- fio.emit('monjeu', {'cards':cards});
- def on_restart(self, data):
- game = self.game()
- if game.isadmin(current_user):
- if game.turn==N_TURN*N_PLAYERS or (game.turn==0 and (game.atout is None or game.atout == '')):
- game.restart_jeu()
- fio.emit('restart', game.serialize_state_anonymous(), broadcast=True)
- else:
- fio.emit('text', {'text': "Finissez la partie avant d'en recommencer une.", 'username':None, 'name':NAME_ARBITRE})
- else:
- fio.emit('text', {'text': "Demandez au maitre du jeu pour reprendre une partie.", 'username':None, 'name':NAME_ARBITRE})
- def on_state_game(self, data):
- game = self.game()
- fio.emit('state_game', game.serialize_state_anonymous())
- def game(self):
- return Game.query.get(self._id)
- class CustomNamespaceJoin(fio.Namespace):
- def __init__(self, id):
- self._id = id
- fio.Namespace.__init__(self, '/game/{}/join'.format(id))
- def on_connect(self):
- pass
- def on_disconnect(self):
- pass
- def on_join(self, data):
- if self.game().can_join(current_user):
- if not self.game().authorized(current_user):
- self.game().join(current_user)
- fio.emit('join', {'username':current_user.login, 'name': current_user.name}, broadcast=True)
- def on_add_player(self, data):
- if self.game().isadmin(current_user):
- if 'username' in data:
- user = User.query.get(data['username'])
- if user is not None and self.game().can_join(user):
- if not self.game().authorized(user):
- self.game().join(user)
- fio.emit('join', {'username':user.login, 'name': user.name}, broadcast=True)
- def on_leave(self, data):
- if self.game().leave(current_user):
- fio.emit('leave', {'username':current_user.login}, broadcast=True)
- def on_ban(self, data):
- if 'username' in data:
- username = data['username']
- if self.game().isadmin(current_user):
- if 'username' is not None:
- if self.game().leave(username):
- fio.emit('leave', {'username':username}, broadcast=True)
- def on_fixplayers(self, data):
- if self.game().isadmin(current_user):
- if self.game().can_start():
- self.game().fix_players()
- fio.emit('fixplayers', {})
- def on_start(self, data):
- if 'order' in data:
- order = data['order']
- else:
- order = None
- if self.game().isadmin(current_user):
- if self.game().start_game(order):
- fio.emit('start', {}, broadcast=True)
- def game(self):
- return Game.query.get(self._id)
-
|