Exemplo n.º 1
0
int gameOne(int index){
    int win = 0;
    int action1 = (int)gamings[index].action1 - 48;
    int action2 = (int)gamings[index].action2 - 48;
    if (action1 == action2) win = 0;
    else if (action1 % 3 + 1 == action2) win = 1;
    else win = 2;
    if (win == 1) gamings[index].status2--;else
    if (win == 2) gamings[index].status1--;
    gamings[index].action1 = '0';
    gamings[index].action2 = '0'; 
    //send package;
    struct Package package;
    package.type = '4';
    struct Gameaction *action = (struct Gameaction*)package.content;
    action->type = '2'; 
    action->action1 = gamings[index].action1;
    action->action2 = gamings[index].action2;
    action->status1[0] = gamings[index].status1;
    action->status2[0] = gamings[index].status2;
    strncpy(action->username1,gamings[index].username1,16);
    strncpy(action->username2,gamings[index].username2,16);
    if (stcp_server_send(threads[index].connsocket,(char*)&package,sizeof(struct Package))<=0){
        printf("send action from server to client failed inf gameone.\n");
    }
    if (gamings[index].status1 == '0' || gamings[index].status2 == '0'){ 
        endgame(index);
        userlistchanged();

    }
    return 0;
}
Exemplo n.º 2
0
void *sendUserlist(){
    while(1){
        pthread_mutex_lock(&mutexuserlist);
        pthread_mutex_lock(&mutex);
        if (userlistChanged == 0){
            pthread_mutex_unlock(&mutex);
            continue;
        }
        //send userlist to everyone
        int i,j=0;
        struct Package package;
        package.type = '3';
        struct Userlist* userlist = (struct Userlist*)package.content;
        userlist->N = '0';
        for(i=0; i<MAX_THREADS;i++)if(threads[i].valid == 1 && threads[i].logined == 1){
            strncpy(&(userlist->content[j*16]),threads[i].user.username,16);
            userlist->status[j] = threads[i].gaming;
            userlist->N++;
            j++;           
        }
        for(i=0; i<MAX_THREADS;i++)if(threads[i].valid == 1 && threads[i].logined == 1){
            j = stcp_server_send(threads[i].connsocket,(char *)&package,sizeof(struct Package));
            if (j<=0){ 
                printf("send userlist occurs error! to %s\n",threads[i].user.username);
            }
            printf("send userlist.\n");
        }
        userlistChanged = 0; 
        pthread_mutex_unlock(&mutex);
    }
    return NULL;
}
Exemplo n.º 3
0
int sendhandshake4(int index,char status){
    struct Package package;
    package.type = '5';
    struct Gameconnect *conn = (struct Gameconnect*)package.content;
    conn->type = '4';
    conn->status = status;
    conn->game = gamings[index].game;
    printf("game is %c\n",conn->game);
    conn->first = (index > threads[index].pkwith?'1':'0');
    
    int ans = stcp_server_send(threads[index].connsocket,(char *)&package,sizeof(struct Package));
    return ans;
}
Exemplo n.º 4
0
int gameTwo(int index){
    int action1 = (int)gamings[index].action1 - 48;
    int action2 = (int)gamings[index].action2 - 48;
    gamings[index].action1 = '0';
    gamings[index].action2 = '0';

    struct Package package;
    package.type = '4';
    struct Gameaction *action = (struct Gameaction*)package.content;
    action->type = '2';
    action->status1[0] = gamings[index].status1;
    action->status2[0] = gamings[index].status2;

    if (action1 == 9){
        action->action1 = '1';
        gamings[index].sum += action2;
        if (gamings[index].sum == 19){
            action->status1[0] = '0';
            action->action1 = '0';
            endgame(index);
            userlistchanged();
        }
        if (gamings[index].sum > 19){
            action->status2[0] = '0';
            action->action1 = '0'; 
            endgame(index);
            userlistchanged();
        }
    }else{
        action->action1 = '0';
        gamings[index].sum += action1; 
        if (gamings[index].sum == 19){
            action->status2[0] = '0';
            endgame(index);
            userlistchanged();
        }
        if (gamings[index].sum > 19){
            action->status1[0] = '0';
            endgame(index);
            userlistchanged();
        }
    }
    

    strncpy(action->username1,gamings[index].username1,16);
    strncpy(action->username2,gamings[index].username2,16);
    if (stcp_server_send(threads[index].connsocket,(char*)&package,sizeof(struct Package))<=0){
        printf("send action from server to client failed in gametwo.\n");
    }
    return 0; 
}
Exemplo n.º 5
0
int exitcurrentgame(int index){
    threads[index].gaming = '0';
    threads[index].pkwith = -1;
    gamings[index].valid = 0;
    gamings[index].game = '0';
    gamings[index].pkwith = -1;
    
    struct Package acklogin;
    acklogin.type = Tacklogin;
    struct Acklogin* ack = (struct Acklogin*)acklogin.content;
    ack->ack = '1';
    
    if (stcp_server_send(threads[index].connsocket,(char *)&acklogin,sizeof(struct Package))<0){
        printf("send ack login failed -- anotheruseralive\n");
    }
    return 0;
}
Exemplo n.º 6
0
/*when this user want to send message,get info frome this user*/
void *GetTask(void *arg)
{
	user* usr=(user *)arg;
	char sendbuf[MAXLINE];
	char temp[MAXLINE];
	int n;
	int state;
	while(1)
	{
		memset(sendbuf,0,sizeof(sendbuf));
		memset(temp,0,sizeof(temp));
		n = stcp_server_recv(usr->sock,sendbuf,MAXLINE);		//get the message
		if(n<0)					//the user want to send
		{
			perror("ERROR reading from socket");
			return NULL;	
		}
		pthread_mutex_lock(&(usr->usrlock));
		state=usr->state;
		pthread_mutex_unlock(&(usr->usrlock));
		printf("state: %d  :buf: %s\n",state,sendbuf);
		switch(state){
			case 1:			//get name and come in
				if(sendbuf[0]=='1'){
					strcpy(usr->name,&sendbuf[1]);
					user* p=head;
					while(1){	//wait for all user get the old msg 
						//then update
						pthread_mutex_lock(&lock);
						if(count>0)
						{
							pthread_mutex_unlock(&lock);
							sleep(1);
						}
						else
							break;
					}	

					while(p!=NULL)		//if this name has been used
					{
						if(strcmp(p->name,usr->name)==0)
						{
							pthread_mutex_unlock(&lock);
							break;
						}
						p=p->next;
					}
					if(p==NULL)
					{
						usr->next=head;
						head=usr;
						count=userCount;
						userCount++;
						usr->fcount=0;
						msgid=usr->id;
						strcpy(msgbuf,"&&&&& ");
						strcat(msgbuf,usr->name);
						strcat(msgbuf," come in &&&&\n");
						pthread_cond_broadcast(&has_message);
						pthread_mutex_unlock(&lock);
						pthread_mutex_lock(&(usr->usrlock));
						usr->state=2;		//read for send msg to others
						pthread_mutex_unlock(&(usr->usrlock));
						sendbuf[0]='2';		
						stcp_server_send(usr->sock,sendbuf,MAXLINE);//tell user he has log in
						break;
					}
					pthread_mutex_unlock(&lock);
					sendbuf[0]='1';
					stcp_server_send(usr->sock,sendbuf,MAXLINE);
					//printf("1-2%s\n",sendbuf);
					break;
				}
				else if(sendbuf[0]=='4')
				{
					pthread_mutex_lock(&(usr->usrlock));
					usr->state=5;
					pthread_mutex_unlock(&(usr->usrlock));
					return NULL;	
				}
				break;

			case 2:			//the user want to send this msg
				if(sendbuf[0]=='2')
				{
					while(1){	//wait for all user get the old msg 
						//then update
						pthread_mutex_lock(&lock);
						if(count>0)
						{
							pthread_mutex_unlock(&lock);
							sleep(1);
						}
						else
							break;
					}	
					strcpy(msgbuf,sendbuf);
					count=userCount-1;
					msgid=usr->id;
					pthread_cond_broadcast(&has_message); //let the waiters know it
					printf("id:%d   msgbuf: %s",(int)msgid,msgbuf);
					pthread_mutex_unlock(&lock);
				}
				else if(sendbuf[0]=='3')	//user want to list all members
				{
					//printf("list 2-3\n");
					memset(sendbuf,0,sizeof(sendbuf));
					sendbuf[0]='3';
					pthread_mutex_lock(&lock);
					user* p=head;
					while(p!=NULL){
						if(strlen(sendbuf)+strlen(p->name)>MAXLINE)break;
						strcat(sendbuf,p->name);
						strcat(sendbuf," |*| ");
						p=p->next;
					}
					pthread_mutex_unlock(&lock);
					strcat(sendbuf,"\n");
					stcp_server_send(usr->sock,sendbuf,MAXLINE);
				}
				else if(sendbuf[0]=='@')	//send to the one
				{
					strcpy(temp,sendbuf);
					char *delim="@";
					strtok(temp,delim);
					strcpy(temp,&temp[1]);
					char *p=strtok(NULL,delim);
					//printf("%s,%s\n",temp,p);
					memset(sendbuf,0,sizeof(sendbuf));
					strcpy(sendbuf,"2");
					strcat(sendbuf,usr->name);
					//printf("%s\n",sendbuf);
					strcat(sendbuf, " (private): ");
					strcat(sendbuf,p);
					pthread_mutex_lock(&lock);
					user *q=head;
					while(q!=NULL){
						if(strcmp(q->name,temp)==0)			//find this gay
						{
							stcp_server_send(q->sock,sendbuf,MAXLINE);
							break;
						}
						q=q->next;
					}
					pthread_mutex_unlock(&lock);
				}
				else if(sendbuf[0]=='$')			//make friend
				{
					strcpy(temp,&sendbuf[1]);
					int l=strlen(temp);
					if(temp[l-1]=='\n')temp[l-1]=0;
					if(strcmp(temp,usr->name)==0)break;		//cannot be friend with yourself
					memset(sendbuf,0,sizeof(sendbuf));
					strcpy(sendbuf,"$");
					strcat(sendbuf,usr->name);
					pthread_mutex_lock(&lock);
					user *q=head;
					while(q!=NULL){
						if(strcmp(q->name,temp)==0)			//find this gay
						{
							printf("$$$$sendbuf:%s\n",sendbuf);
							int i=0;
							int flag=1;
							for(;i<LISTENQ;i++)
							{
								if(usr->fri[i]==q)flag=0;
							}
							if(flag)
								stcp_server_send(q->sock,sendbuf,MAXLINE);
							break;
						}
						q=q->next;
					}
					pthread_mutex_unlock(&lock);

				}
				else if(sendbuf[0]=='y'||sendbuf[0]=='n'){   //yes or no  be friends
					strcpy(temp,&sendbuf[1]);
					if(sendbuf[0]=='y')
					{
						memset(sendbuf,0,sizeof(sendbuf));
						strcpy(sendbuf,"y");
						strcat(sendbuf,usr->name);
					}
					else
					{
						memset(sendbuf,0,sizeof(sendbuf));
						strcpy(sendbuf,"n");
						strcat(sendbuf,usr->name);

					}
					pthread_mutex_lock(&lock);
					user *q=head;
					while(q!=NULL){
						if(strcmp(q->name,temp)==0)			//find this gay
						{
							printf("y/n temp:%s\n",temp);
							if(sendbuf[0]=='y')
							{
								q->fri[q->fcount]=usr;
								q->fcount=(q->fcount+1)%LISTENQ;
								usr->fri[usr->fcount]=q;
								usr->fcount=(usr->fcount+1)%LISTENQ;
							}
							stcp_server_send(q->sock,sendbuf,MAXLINE);
							printf("y/n sendbuf:%s\n",sendbuf);
							break;
						}
						q=q->next;
					}
					pthread_mutex_unlock(&lock);

				}
				else if(sendbuf[0]=='!')
				{
					printf("1!!!sendbuf: %s\n",sendbuf);
					memset(sendbuf,0,sizeof(sendbuf));
					pthread_mutex_lock(&lock);
					int i=0;
					for(;i<LISTENQ;i++)
					{
						if(usr->fri[i]!=NULL)
						{
							strcat(sendbuf,"!");
							strcat(sendbuf,usr->fri[i]->name);
							strcat(sendbuf," || ");
						}
					}
					printf("!!!sendbuf: %s\n",sendbuf);
					pthread_mutex_unlock(&lock);
					stcp_server_send(usr->sock,sendbuf,MAXLINE);
					break;
				}
				else if(sendbuf[0]=='4')		//user quit
				{
					while(1){	//wait for all user get the old msg 
						//then update
						pthread_mutex_lock(&lock);
						if(count>0)
						{
							pthread_mutex_unlock(&lock);
							sleep(1);
						}
						else
							break;
					}
					userCount--;
					pthread_mutex_lock(&(usr->usrlock));
					usr->state=4;
					pthread_mutex_unlock(&(usr->usrlock));
					count=userCount;
					msgid=usr->id;
					strcpy(msgbuf,"@@@@@ ");
					strcat(msgbuf,usr->name);
					strcat(msgbuf," out of room @@@@\n");
					pthread_cond_broadcast(&has_message);
					pthread_mutex_unlock(&lock);
					return NULL;	
				}
				break;

		}
	}
}
Exemplo n.º 7
0
/*when this user get some message from others*/
void *GiveTask(void *arg)
{
	user* usr=(user*)arg;
	char temp[MAXLINE];
	int n;
	int state;
	while(1)
	{
		memset(temp,0,MAXLINE);
		pthread_mutex_lock(&(usr->usrlock));
		state=usr->state;
		pthread_mutex_unlock(&(usr->usrlock));
		switch(state){
			case 2:			//可以发送消息
				pthread_mutex_lock(&lock);
				pthread_cond_wait(&has_message,&lock);
				printf("wake send:%s\n",msgbuf);
				if(msgid!=usr->id){
					n=stcp_server_send(usr->sock,msgbuf,MAXLINE);
					if(n<0)
					{
						perror("EEEOR writing to socket");
						return NULL;
					}
					count--;
				}
				pthread_mutex_unlock(&lock);
				break;
			case 4:		//let recvtask of this user know it 
				temp[0]='q';
				stcp_server_send(usr->sock,temp,MAXLINE);
				pthread_mutex_lock(&lock);
				int i=0;
				for(;i<LISTENQ;i++)			//cancel friend line
				{
					if(usr->fri[i]!=NULL)
					{
						user *f=usr->fri[i];
						int j=0;
						for(;j<LISTENQ;j++)
						{
							printf("quit :%d\n",j);
							if(f->fri[j]==usr)f->fri[j]=NULL;
						}
					}
				}
				user*p=head;		//free the memory
				if(p->id==usr->id)
				{
					head=p->next;
					free(p);
					p=NULL;
				}
				else{
					while(p->next!=NULL)
					{
						if(p->next->id==usr->id)	
							break;
						p=p->next;
					}
					user*q=p->next;
					if(q!=NULL)
					{
						p->next=q->next;
						free(q);
						q=NULL;
					}
					else
					{
						free(usr);
					}
				}
				pthread_mutex_unlock(&lock);
				return NULL;	
			case 5:				//quit without login 
				temp[0]='q';
				stcp_server_send(usr->sock,temp,MAXLINE);
				free(usr);
				return NULL;
			default:sleep(1);
		}
	}
}