Пример #1
0
//handles msg written by client clientQueue[index]
void handleIncomingMsg(struct clientMsg data, int index){
	//int i;
	char buf[MSGTXT_SIZE];
	struct gameData newGame;

	newGame.valid = 1;
	newGame.msg = 1;
	//newGame.playing = ClientsQueue[index].isPlayer;

	strncpy(newGame.msgTxt, data.msgTxt, strlen(data.msgTxt));
	newGame.msgTxt[strlen(data.msgTxt)] = '\0';

	createGameDataBuff(newGame, buf);
	index == 0 ? strcat(ClientsQueue[1].writeBuf, buf) : strcat(ClientsQueue[0].writeBuf, buf);
	//if (data.recp == -1){
	//	// send to all
	//	for (i = 0; i<conViewers + conPlayers; i++) strcat(ClientsQueue[i].writeBuf, buf);
	//}
	//else{
	//	//sent to specific client. we will search for him
	//	for (i = 0; i < conViewers + conPlayers; i++){
	//		if (ClientsQueue[i].clientNum == data.recp) strcat(ClientsQueue[i].writeBuf, buf);
	//	}
	//}
}
Пример #2
0
/**
	handles msg written by client clientQueue[index] 
*/
void handleIncomingMsg(struct clientMsg data,int index){
	int i;
	char buf[MSG_SIZE];
	struct gameData newGame;

	newGame.valid =1;
	newGame.msg = ClientsQueue[index].clientNum;
	newGame.playing = ClientsQueue[index].isPlayer;

	strncpy(newGame.msgTxt, data.msgTxt, strlen(data.msgTxt));
	newGame.msgTxt[strlen(data.msgTxt)] = '\0';

	createGameDataBuff(newGame, buf);
	//printf("gamebuf is %s\n", buf);

	if(data.recp == -1){
		// send to all
		for(i=0;i<conViewers+conPlayers; i++){
			strcat(ClientsQueue[i].writeBuf, buf);
		}
	}
	else{
		//sent to specific client. we will search for him
		for(i=0;i<conViewers+conPlayers; i++)
			if(ClientsQueue[i].clientNum == data.recp){
				strcat(ClientsQueue[i].writeBuf, buf);
			}
	}
}
Пример #3
0
/**
 newFd - new client's fd returned from select
 isPlayer - 1 for player, 0 for viewer
*/
void addClientToQueue(int newFd, int isPlayer){
	struct clientData newClient;
	int newClientIndex;
	struct gameData newGame;
	int i;

	// handling queue
	newClientIndex = conPlayers+conViewers; 
	newClient.fd = newFd;
	newClient.clientNum = minFreeClientNum;
	newClient.isPlayer = isPlayer;
	ClientsQueue[newClientIndex] = newClient;

	// handling globals
	if(isPlayer){
		conPlayers++;
	}
	else{
		conViewers++;
	}

	// finding new MinFreeClientNum
	for(minFreeClientNum = 1; minFreeClientNum<100; minFreeClientNum++){
		for(i=0; i<conPlayers+conViewers; i++){
			if(minFreeClientNum == ClientsQueue[i].clientNum){
				// we found a client with the same number. need to continue to next outside iteration
				break;
			}
		}
		if(minFreeClientNum != ClientsQueue[i].clientNum){
			// kind of nasty code, but should work.
			// we are exiting main loop because we have found our number (inner loop finished)
			break;
		}
	}


	// handling writeBuf
	newGame.valid = 1;
	newGame.msg=0;
	newGame.isMyTurn = (conViewers+conPlayers == 1) ? 1 : 0; // this is new client turn only if he is the only one here
	newGame.win =-1;
	newGame.numOfPlayers = game.numOfPlayers;
	newGame.myPlayerId = newClient.clientNum;
	newGame.playing = newClient.isPlayer;
	newGame.isMisere = game.isMisere;
	newGame.heapA = game.heapA;
	newGame.heapB = game.heapB;
	newGame.heapC = game.heapC;
	newGame.heapD = game.heapD;
	newGame.moveCount = game.moveCount;

	// first write to writeBuf. no worries of ruining previous data
	createGameDataBuff(newGame, ClientsQueue[newClientIndex].writeBuf);
}
Пример #4
0
void updateEveryoneOnMove(int index){
	int i;
	char buf[MSG_SIZE];

	game.myPlayerId = ClientsQueue[index].clientNum;
	for(i=0; i<conPlayers+conViewers; i++){
		game.playing = ClientsQueue[i].isPlayer;
		createGameDataBuff(game, buf);
		strcat(ClientsQueue[i].writeBuf, buf);
	}
}
Пример #5
0
void sendInvalidMoveToPlayer(int index){
	char buf[MSGTXT_SIZE];
	game.valid = 0;
	//game.playing = ClientsQueue[index].isPlayer;

	strcpy(game.msgTxt, "");
	createGameDataBuff(game, buf);

	// restore value
	game.valid = 1;
	strcat(ClientsQueue[index].writeBuf, buf);
}
Пример #6
0
void SendCantConnectToClient(int fd){
	int errorIndicator;
	char buf[MSGTXT_SIZE];
	struct gameData newGame;
	int msgtxt_size = MSGTXT_SIZE;
	// -1 stands for too many clients connected
	newGame.valid = 0;
	createGameDataBuff(newGame, buf);

	errorIndicator = sendAll(fd, buf, &msgtxt_size);
	checkForNegativeValue(errorIndicator, "send", fd);

	close(fd);
}
Пример #7
0
void notifyOnTurningToPlayer(){
	char buf[MSG_SIZE];
	int index;

	index = conPlayers;
	ClientsQueue[index].isPlayer = 1;

	game.isMyTurn = 0;
	game.valid=1;
	game.playing=1;

	createGameDataBuff(game, buf);
	strcat(ClientsQueue[index].writeBuf, buf);
}
Пример #8
0
void notifyOnDisconnectionToPlayer(int index){
	char buf[MSGTXT_SIZE];
	//int index;

	//index = conPlayers;
	//ClientsQueue[index].isPlayer = 1;

	game.isMyTurn = 0;
	game.valid = 1;
	game.win = 2; // (index == 0) ? 2 : 1;
	//game.playing = 1;

	createGameDataBuff(game, buf);
	strcat(ClientsQueue[(index + 1)%2].writeBuf, buf);
}
Пример #9
0
void notifyOnWinningToAll(int index){
	int i;
	char buf[MSGTXT_SIZE];

	game.myPlayerId = index; // ClientsQueue[index].clientNum;
	game.win = index + 1;

	for (i = 0; i < conPlayers; i++){
		createGameDataBuff(game, buf);
		strcat(ClientsQueue[i].writeBuf, buf);
	}
	//	game.playing = ClientsQueue[i].isPlayer;
	//	createGameDataBuff(game, buf);
	//	strcat(ClientsQueue[i].writeBuf, buf);
	//}
}
Пример #10
0
void notifyOnTurn(){
	char buf[MSG_SIZE];
	struct gameData newGame;

	newGame.isMyTurn =1;
	newGame.valid=1;
	newGame.playing=1;
	newGame.msg = 0;
	newGame.win = game.win;
	newGame.numOfPlayers = game.numOfPlayers;
	newGame.isMisere = game.isMisere;
	newGame.heapA = game.heapA;
	newGame.heapB = game.heapB;
	newGame.heapC = game.heapC;
	newGame.heapD = game.heapD;
	game.moveCount++;
	newGame.moveCount = game.moveCount;

	createGameDataBuff(newGame, buf);
	//printf("buf:%s\n",buf );
	strcat(ClientsQueue[clientIndexTurn].writeBuf, buf);
}
Пример #11
0
void notifyOnTurn(){
	char buf[MSGTXT_SIZE];
	struct gameData newGame;
	int i;

	newGame.isMyTurn = 1;
	newGame.valid = 1;
	//newGame.playing = 1;
	newGame.msg = 0;
	newGame.win = game.win;
	newGame.myPlayerId = clientIndexTurn;
	//newGame.numOfPlayers = game.numOfPlayers;
	//newGame.isMisere = game.isMisere;
	for (i = 0; i < HEAPS_NUM; i++) newGame.heaps[i] = game.heaps[i];
	//newGame.heapA = game.heapA;
	//newGame.heapB = game.heapB;
	//newGame.heapC = game.heapC;
	//newGame.heapD = game.heapD;
	//game.moveCount++;
	//newGame.moveCount = game.moveCount;

	createGameDataBuff(newGame, buf);
	strcat(ClientsQueue[clientIndexTurn].writeBuf, buf);
}