var MSG_TYPE_POP=0; var MSG_TYPE_PUSH=1; var MSG_TYPE_INVITE=2; var MSG_TYPE_CANCEL=3; var MSG_TYPE_ACCEPT=4; var MSG_TYPE_REFUSE=5; var MSG_TYPE_QUIT_SESSION=6; var PLAYER_STATUS_FREE=0; var PLAYER_STATUS_INVITED=1; var PLAYER_STATUS_INVITING=2; var PLAYER_STATUS_PLAYING=3; var EVENT_TYPE_NONE=0; var EVENT_TYPE_INFO=1; var EVENT_TYPE_ACCEPT=2; var EVENT_TYPE_INVITE=3; var EVENT_TYPE_CANCEL=4; var EVENT_TYPE_QUIT_SESSION=5; var SQUARE_W=40; var SQUARE_H=40; function rpc_decode(text){ return unescape(text); } function main_receive_data(data){ //alert(data); if(data['action']=='process_room'){ var i=1; var html=''; while(typeof(data['pid_'+i])!='undefined'){ html+=''; html+=''; html+=''; html+=''; html+=''; i++; } html+='
'; if(data['pst_'+i]==PLAYER_STATUS_PLAYING){ html+=''; }else if(data['pst_'+i]==PLAYER_STATUS_INVITING){ html+=''; }else if(data['pst_'+i]==PLAYER_STATUS_INVITED){ html+=''; }else{ html+=''; } html+=''; if(data['pbn_'+i]=='freebox'){ html+=''; }else if(data['pbn_'+i]=='mobile'){ html+=''; }else{ html+=''; } html+=''; if(data['pid_'+i]==from_player_id){ html+=''+rpc_decode(data['psn_'+i])+''; }else if(data['pst_'+i]==PLAYER_STATUS_PLAYING){ html+=''+rpc_decode(data['psn_'+i])+''; }else{ html+=''+rpc_decode(data['psn_'+i])+''; } html+='
' document.getElementById('players_list_content').innerHTML=html; var i=1; while(typeof(data['mid_'+i])!='undefined'){ var n_div=document.createElement("DIV"); n_div.className='chat_tmsg'; if(data['mpi_'+i]==from_player_id){ n_div.innerHTML=''+rpc_decode(data['msg_'+i])+''; }else if(data['mpi_'+i]==0){ n_div.innerHTML=''+rpc_decode(data['msg_'+i])+''; }else{ n_div.innerHTML=rpc_decode(data['msg_'+i]); } document.getElementById('message_zone_content').appendChild(n_div); document.getElementById('message_zone_container').scrollTop+=n_div.offsetHeight+1; i++; } if(typeof(data['last_message_id'])!='undefined' && data['last_message_id']!=''){ last_message_id=data['last_message_id']; } GLOBAL['event_type']=data['event_type']; GLOBAL['event_dest_player_id']=data['event_dest_player_id']; GLOBAL['event_session_id']=data['event_session_id']; GLOBAL['event_message']=data['event_message']; if(GLOBAL['event_type']!=EVENT_TYPE_QUIT_SESSION){ main_set_game_info(GLOBAL['event_type'],GLOBAL['event_dest_player_id'],GLOBAL['event_session_id'],rpc_decode(GLOBAL['event_message'])); } GLOBAL['current_session_id']=data['current_session_id']; GLOBAL['from_info']=data['from_info']; GLOBAL['dest_info']=data['dest_info']; if(typeof(data['position_set'])!='undefined' && data['position_set']!=GLOBAL['position_set']){ GLOBAL['position_set']=data['position_set']; if(typeof(data['position_move'])!='undefined' && data['position_move']!='' && typeof(data['next_player_id'])!='undefined' && data['next_player_id']!=GLOBAL['next_player_id']){ GLOBAL['position_move']=data['position_move']; main_animation_init(); }else{ main_set_game_info(GLOBAL['event_type'],GLOBAL['event_dest_player_id'],GLOBAL['event_session_id'],rpc_decode(GLOBAL['event_message'])); main_get_area_content(1,1,GLOBAL['position_set'],GLOBAL['current_session_id'],GLOBAL['init'],GLOBAL['step'],GLOBAL['hist'],rpc_decode(GLOBAL['from_info']),rpc_decode(GLOBAL['dest_info']),GLOBAL['next_player_id']); } } GLOBAL['next_player_id']=data['next_player_id']; if(GLOBAL['next_player_id']!=from_player_id){ GLOBAL['step']=0; GLOBAL['hist']=''; } }else if(data['action']=='get_area_content'){ var row=data['row']; var col=data['col']; document.getElementById('c_'+row+'_'+col).innerHTML=rpc_decode(data['area_content']); c_def[row][col][CELL_LOADED]=1; } } function main_animation_init(){ if(room_timer_id!=0){ clearTimeout(room_timer_id); room_timer_id=0; } GLOBAL['animation_step']=0; if(GLOBAL['completed_timer']!=0){ GLOBAL['completed_timer']=0; clearTimeout(GLOBAL['completed_timer']); } GLOBAL['completed_timer']=setTimeout('main_animation_complete()',10000); main_animation_next_step(); } function main_animation_next_step(){ var step=GLOBAL['animation_step']; var move=GLOBAL['position_move']; var path=move.split(';'); GLOBAL['animation_length']=path.length; if(step>=(path.length-1)){ main_animation_complete(); return false; } var from_cell=path[step].split(','); var from_row=from_cell[0]; var from_col=from_cell[1]; if(step==0){ var init_row=from_row; var init_col=from_col; var init_square=document.getElementById('s_'+init_row+'_'+init_col); if(init_square!=null && typeof(init_square)!='undefined'){ init_square.style.zIndex=21; } GLOBAL['animation_init_row']=init_row; GLOBAL['animation_init_col']=init_col; GLOBAL['animation_init_x']=0; GLOBAL['animation_init_y']=0; }else{ GLOBAL['animation_init_x']+=GLOBAL['animation_dest_x']; GLOBAL['animation_init_y']+=GLOBAL['animation_dest_y']; } var dest_cell=path[step+1].split(','); var dest_row=dest_cell[0]; var dest_col=dest_cell[1]; GLOBAL['animation_dest_x']=(dest_col-from_col)*SQUARE_W; GLOBAL['animation_dest_y']=(dest_row-from_row)*SQUARE_H; main_animation_move_it(); } function main_animation_move_it(){ var init_row=GLOBAL['animation_init_row']; var init_col=GLOBAL['animation_init_col']; var init_obj=document.getElementById('p_'+init_row+'_'+init_col); if(init_obj!=null && (typeof(init_obj)!='undefined')){ var curr_x=parseInt(init_obj.style.left); var curr_y=parseInt(init_obj.style.top); var init_x=GLOBAL['animation_init_x']; var init_y=GLOBAL['animation_init_y']; var dest_x=GLOBAL['animation_dest_x']; var dest_y=GLOBAL['animation_dest_y']; var step_x=5; if(dest_x>0){ if((curr_x+step_x)<(init_x+dest_x)){ curr_x+=step_x; }else{ curr_x=init_x+dest_x; } }else{ if((curr_x-step_x)>(init_x+dest_x)){ curr_x-=step_x; }else{ curr_x=init_x+dest_x; } } var step_y=5; if(dest_y>0){ if((curr_y+step_y)<(init_y+dest_y)){ curr_y+=step_y; }else{ curr_y=init_y+dest_y; } }else{ if((curr_y-step_y)>(init_y+dest_y)){ curr_y-=step_y; }else{ curr_y=init_y+dest_y; } } init_obj.style.left=curr_x+'px'; init_obj.style.top=curr_y+'px'; }else{ curr_x=init_x+dest_x; curr_y=init_y+dest_y; } if(GLOBAL['animation_timer']!=0){ GLOBAL['animation_timer']=0; clearTimeout(GLOBAL['animation_timer']); } if(curr_x==(init_x+dest_x) && curr_y==(init_y+dest_y)){ GLOBAL['animation_step']++; if(GLOBAL['animation_step']>=(GLOBAL['animation_length']-1)){ main_animation_complete(); }else{ GLOBAL['animation_timer']=setTimeout('main_animation_next_step()',250); } }else{ GLOBAL['animation_timer']=setTimeout('main_animation_move_it()',25); } } function main_animation_complete(){ GLOBAL['position_move']=''; GLOBAL['animation_length']=0; if(GLOBAL['animation_timer']!=0){ GLOBAL['animation_timer']=0; clearTimeout(GLOBAL['animation_timer']); } if(GLOBAL['completed_timer']!=0){ GLOBAL['completed_timer']=0; clearTimeout(GLOBAL['completed_timer']); } main_set_game_info(GLOBAL['event_type'],GLOBAL['event_dest_player_id'],GLOBAL['event_session_id'],rpc_decode(GLOBAL['event_message'])); main_get_area_content(1,1,GLOBAL['position_set'],GLOBAL['current_session_id'],GLOBAL['init'],GLOBAL['step'],GLOBAL['hist'],rpc_decode(GLOBAL['from_info']),rpc_decode(GLOBAL['dest_info']),GLOBAL['next_player_id']); if(room_timer_id!=0){ clearTimeout(room_timer_id); room_timer_id=0; } room_timer_id=setTimeout('main_pop_msg()',10000); } function main_set_game_info(event_type,event_dest_player_id,event_session_id,event_message){ var html; if((event_type==EVENT_TYPE_NONE)||(event_type==EVENT_TYPE_INFO)){ html='
'+event_message; }else{ html=event_message+'

'; if(event_type==EVENT_TYPE_QUIT_SESSION){ html+=''; }else if(event_type==EVENT_TYPE_ACCEPT){ html+=' '; html+=''; }else if(event_type==EVENT_TYPE_INVITE){ html+=''; }else if(event_type==EVENT_TYPE_CANCEL){ html+=''; } } document.getElementById('game_info').innerHTML=html; return true; } function main_enter_room(){ player_name=document.getElementById('player_name').value; if(player_name==''){ alert('Indiquez un nom de joueur pour pouvoir rejoindre le salon'); }else{ player_active=1; last_message_id=0; main_update_player(from_player_id,player_name); main_get_area_content(1,0,'',0,'',0,'','','',0); room_timer_id=setTimeout('main_pop_msg()',250); } return false; } function main_quit_room(){ if(room_timer_id!=0){ clearTimeout(room_timer_id); room_timer_id=0; } GLOBAL['step']=0; GLOBAL['hist']==''; player_active=0; main_set_player_active(from_player_id,0); main_get_area_content(1,0,'',0,'',0,'','','',0); return false; } function main_push_msg(){ var msg=document.getElementById('msg').value; document.getElementById('msg').value=''; msg=escape(msg); msg=msg.replace('+','%2b'); return main_process_room(dest_player_id,0,0,MSG_TYPE_PUSH,msg,'','',0,GLOBAL['hist']); } function main_pop_msg(){ /* if(room_timer_id!=0){ clearTimeout(room_timer_id); room_timer_id=0; } */ room_timer_id=setTimeout('main_pop_msg()',10000); return main_process_room(0,0,0,MSG_TYPE_POP,'',GLOBAL['init'],GLOBAL['play'],GLOBAL['step'],GLOBAL['hist']); } function main_process_room(dest_player_id,focused_player_id,session_id,msg_type,msg,init,play,step,hist){ if(session_id!=0){ GLOBAL['session_id']=session_id; } if(msg_type==MSG_TYPE_ACCEPT || msg_type==MSG_TYPE_INVITE || msg_type==MSG_TYPE_QUIT_SESSION){ GLOBAL['next_player_id']=0; GLOBAL['position_set']=''; GLOBAL['step']=0; GLOBAL['hist']=''; }else{ GLOBAL['init']=init; GLOBAL['play']=play; GLOBAL['step']=step; GLOBAL['hist']=hist; } var query='action=process_room&from_player_id='+from_player_id+'&dest_player_id='+dest_player_id+'&focused_player_id='+focused_player_id+'&session_id='+GLOBAL['session_id']+'&msg_type='+msg_type+'&msg='+msg+'&last_message_id='+last_message_id+'&init='+GLOBAL['init']+'&play='+GLOBAL['play']+'&step='+GLOBAL['step']+'&hist='+GLOBAL['hist']; ajax_send('GET','inc/ajax_service.php',query); return false; } function main_set_player_active(player_id,player_active){ var query='action=set_player_active&player_id='+player_id+'&player_active='+player_active; ajax_send('GET','inc/ajax_service.php',query); return false; } function main_update_player(player_id,player_name){ var query='action=update_player&player_id='+player_id+'&player_name='+escape(player_name); ajax_send('GET','inc/ajax_service.php',query); return false; } function main_get_area_content(row,col,position_set,current_session_id,init,step,hist,from_info,dest_info,next_player_id){ var query='action=get_area_content&row='+row+'&col='+col+'&c_w='+c_dyn_def[row][col][CELL_CLIP_WIDTH]+'&c_h='+c_dyn_def[row][col][CELL_CLIP_HEIGHT]+'&player_id='+from_player_id+'&player_name='+escape(player_name)+'&player_active='+player_active+'&position_set='+position_set+'¤t_session_id='+current_session_id+'&init='+init+'&step='+step+'&hist='+hist+'&from_info='+escape(from_info)+'&dest_info='+escape(dest_info)+'&next_player_id='+next_player_id; ajax_send('GET','inc/ajax_service.php',query); return false; } var room_timer_id=0; var GLOBAL={};