Belote/static/game_script.js

279 lines
8.4 KiB
JavaScript
Raw Normal View History

2020-04-25 10:08:19 +00:00
var socket = null;
var sendtext = function(){
var text = $('#texte-msg').val();
if (text.length > 1){
$('#texte-msg').val('');
socket.emit('text', {'text':text});
}
};
var choixcouleur = function(coul){
socket.emit('choose_color', {'atout':coul});
}
var jouer = function(carte, no){
socket.emit('play', {'card':carte});
}
var restart_jeu = function(){
socket.emit('restart', {});
}
$.fn.pressEnter = function (fnc) {
return this.each(function () {
$(this).keypress(function (ev) {
var keycode = (ev.keyCode ? ev.keyCode : ev.which);
if (keycode == '13') {
fnc.call(this, ev);
}
})
})
}
var set_points = function(){
$('#nous-tot').html( jeu['cumul'][jeu['nr']%2] );
$('#nous-part').html(jeu['points'][jeu['nr']%2] );
$('#eux-tot').html( jeu['cumul'][(jeu['nr']+1)%2] );
$('#eux-part').html( jeu['points'][(jeu['nr']+1)%2]);
$('#nopart').html('Partie '+jeu['partie']);
}
var set_atout = function(){
if(jeu['atout'] == null || jeu['atout']==''){
var coulprop = jeu['played'][0];
$('#atout').html(`<img src="/static/cards/${coulprop}.png" width=100% />`);
if (jeu['turn']<-4 && (jeu['turn']+jeu['first_player']+8)%4 == jeu['nr']){
2020-04-26 09:32:30 +00:00
$('#preneur').html(`<input type="submit" onclick="javascript:choixcouleur('${coulprop[1]}')" value="Prendre" />`);
$('#prispar').html(`<input type="submit" onclick="javascript:choixcouleur(null)" value="Passer" />`);
2020-04-27 09:26:17 +00:00
}else if((jeu['turn']+jeu['first_player']+8)%4 == jeu['nr'] && jeu['turn']<0){
2020-04-26 09:32:30 +00:00
$('#preneur').html(`<input type="submit" onclick="javascript:choixcouleur('P')" value="Pique" />
2020-04-25 10:08:19 +00:00
<input type="submit" onclick="javascript:choixcouleur('C')" value="Coeur" />
<br />
<input type="submit" onclick="javascript:choixcouleur('T')" value="Trèfle" />
<input type="submit" onclick="javascript:choixcouleur('F')" value="Carreau" />
`);
2020-04-26 09:32:30 +00:00
$('#prispar').html(`<input type="submit" onclick="javascript:choixcouleur(null)" value="Passer" />`);
2020-04-25 10:08:19 +00:00
}else{
$('#prispar').html(`Pris par`);
$('#preneur').html(`----`);
}
}else{
$('#atout').html(`<img src="/static/cards/${jeu['atout']}.png" width=100% />`);
$('#prispar').html(`Pris par`);
$('#preneur').html(`${jeu['playersinfo'][jeu['preneur']]}`);
}
}
jeu['monjeu'] = Array.from(jeu['cards']);
var order = ['A', '0', 'R', 'D', 'V', '9', '8', '7'];
var order_atout = ['V', '9', 'A', '0', 'R', 'D', '8', '7'];
var set_jeu = function(){
var order_colors = ['P', 'C', 'T', 'F'];
if (jeu['atout'] == 'C'){
order_colors = ['C', 'T', 'F', 'P'];
}else if (jeu['atout'] == 'T'){
order_colors = ['T', 'C', 'P', 'F'];
}else if (jeu['atout'] == 'F'){
order_colors = ['F', 'T', 'C', 'P'];
}
jeu['monjeu'].sort(function(a,b){
if (a[1]==jeu['atout']){
x = order_atout.indexOf(a[0]);
}else{
x = order.indexOf(a[0]);
}
if (b[1]==jeu['atout']){
y = order_atout.indexOf(b[0]);
}else{
y = order.indexOf(b[0]);
}
return x+15*order_colors.indexOf(a[1]) > y+15*order_colors.indexOf(b[1]) ? 1 : -1;
});
console.log(jeu['monjeu']);
for(i=0;i<jeu['monjeu'].length;i++){
2020-04-26 09:32:30 +00:00
if (jeu['monjeu'][i]==null || jeu['monjeu'][i]==''){
2020-04-25 10:08:19 +00:00
$('#carte-'+i).html('');
}else{
$('#carte-'+i).html(`<a href="javascript:jouer('${jeu['monjeu'][i]}', ${i})"><img src="/static/cards/${jeu['monjeu'][i]}.png" width=100% /></a>`);
}
}
}
var set_cartes = function(){
first_player = jeu['first_player']
2020-04-26 09:32:30 +00:00
if (jeu['turn']%4==0 && 'lastfirstplayer' in jeu && jeu['turn']>0){
2020-04-25 10:08:19 +00:00
first_player = jeu['lastfirstplayer']
}
2020-04-26 09:32:30 +00:00
if (jeu['atout'] == null || jeu['atout']==''){
$('#jeu-0').html('');
2020-04-25 10:08:19 +00:00
$('#jeu-1').html('');
$('#jeu-2').html('');
$('#jeu-3').html('');
}else{
for(i=first_player;i<first_player+4;i++){
j = i-first_player;
k = (i+6-jeu['nr'])%4;
2020-04-25 10:08:19 +00:00
if (jeu['played'].length > j && jeu['played'][j]!= ''){
$('#jeu-'+ k).html(`<img src="/static/cards/${jeu['played'][j]}.png" width=100% />`);
}else{
$('#jeu-'+ k).html('');
}
}
}
}
var set_derpli = function(){
2020-04-26 09:32:30 +00:00
if(jeu['last_played'].length >0 && jeu['turn']>0){
2020-04-25 10:08:19 +00:00
for(i=0;i<4;i++){
$('#der-pli-'+i).html(`<img src="/static/cards/${jeu['last_played'][i]}.png" width=100% />`);
}
}
}
var set_players = function(){
for(i=0;i<4;i++){
name = jeu['playersinfo'][jeu['players'][i]];
if(jeu['players'][i]==jeu['admin']){
name = name + '&nbsp;*';
}
k = (i+6-jeu['nr'])%4;
$('#joueur-'+k).html(name);
2020-04-25 10:08:19 +00:00
}
}
var set_current_player = function(){
for(i=0;i<4;i++){
$('#joueur-'+i).css('font-weight', 'normal');
$('#joueur-'+i).css('border-width', '0px');
$('#joueur-'+i).css('background', 'none');
2020-04-25 10:08:19 +00:00
}
i = (jeu['turn']+jeu['first_player']-jeu['nr']+18)%4;
2020-04-25 10:08:19 +00:00
$('#joueur-'+i).css('font-weight', 'bold');
$('#joueur-'+i).css('border-width', '1px 0px 1px 0px');
$('#joueur-'+i).css('background', '#55B78D');
2020-04-25 10:08:19 +00:00
if((jeu['turn']+jeu['first_player']+8)%4 == jeu['nr']){
$('#a-vous').css('visibility', 'visible');
}else{
$('#a-vous').css('visibility', 'hidden');
}
if(jeu['turn']==32 && jeu['admin']==jeu['players'][jeu['nr']]){
$('#suivant').show();
}
2020-04-26 18:10:43 +00:00
else if(jeu['turn']==0 && (jeu['atout'] == null||jeu['atout']=='') && jeu['admin']==jeu['players'][jeu['nr']]){
$('#suivant').show();
}
else
{
2020-04-27 09:26:17 +00:00
$('#suivant').hide();
2020-04-26 09:32:30 +00:00
}
2020-04-25 10:08:19 +00:00
}
$(document).ready(function() {
socket = io(window.location.pathname);
$('#suivant').hide()
set_points();
set_atout();
set_jeu();
set_cartes();
set_derpli();
set_players();
set_current_player();
if((jeu['turn']+jeu['first_player'] + 8)%4 == jeu['nr']){
$('#a-vous').css('visibility', 'visible');
}
socket.on('connect', function() {
socket.emit('join', {data: 'I\'m connected!'});
});
socket.on('text', function(data) {
textr = data['text'];
name = data['name'];
$('#chat').prepend(`<b>${name}</b> : ${textr}<br />`);
});
socket.on('restart', function(data) {
for(d in data){
jeu[d] = data[d]
}
set_points();
set_atout();
set_cartes();
set_derpli();
set_players();
set_current_player();
socket.emit('monjeu', {});
});
socket.on('choose_color', function(data){
console.log(data)
jeu['turn'] = data['turn']
jeu['preneur'] = data['preneur']
jeu['atout'] = data['atout']
set_atout();
set_current_player();
if(jeu['turn']>=0){
socket.emit('monjeu', {});
}
});
socket.on('monjeu', function(data){
jeu['cards'] = data['cards'];
jeu['monjeu'] = Array.from(data['cards']);
set_jeu();
});
socket.on('play', function(data){
console.log(data)
jeu['turn'] = data['turn'];
jeu['lastfirstplayer'] = jeu['first_player']
jeu['first_player'] = data['first_player'];
jeu['last_played'] = data['last_played'];
jeu['points'] = data['points'];
if(data['played'] == null || data['played'].length == 0){
jeu['played'] = data['last_played'];
console.log(jeu['played'])
set_cartes();
set_derpli();
set_points();
}
else{
jeu['played'] = data['played'];
console.log(jeu['played'])
set_cartes();
set_points();
}
var first_player = jeu['first_player'];
if (jeu['turn']%4==0 && 'lastfirstplayer' in jeu){
first_player = jeu['lastfirstplayer']
}
if((jeu['turn']+first_player+7)%4 == jeu['nr']){
console.log("Je viens de jouer")
i = jeu['monjeu'].indexOf(jeu['played'][jeu['played'].length-1]);
if (jeu['played'].length>0 && i >=0){
console.log(i)
jeu['monjeu'][i] = null;
$('#carte-'+i).html('')
}
}
set_current_player();
});
2020-04-26 09:32:30 +00:00
socket.on('disconnect', function() {
2020-04-27 18:13:26 +00:00
$('#chat').prepend('<b>Souci technique</b> : Vous avez été déconnecté. Veuillez recharger la page.<br />');
2020-04-26 09:32:30 +00:00
});
2020-04-25 10:08:19 +00:00
$('#texte-msg').pressEnter(function(e){
sendtext();
});
});