Exemplo n.º 1
0
void	ManageRoom::handleConnectGame(void *buffer, Client *cl)
{
	t_TCPConnection *res;
	ISocket *sock;
	std::string name;
	res = (t_TCPConnection *)buffer;
	cl->setName(res->name);
	std::cout << res->name << std::endl;
	sock = cl->getSocket();
	cl->setIp(sock->getIp());
	if (!cl->getName().empty())
	{
		t_TCPHeader Header;
		Header.type = ESTABLISHED;
		Header.packetSize = sizeof(Header);
		sock->sendBinary(&Header, sizeof(Header));
	}
	else
	{
		t_TCPError Header;
		Header.header.type = PACKET_ERROR;
		Header.header.packetSize = sizeof(Header);
		Header.error = LOGIN;
		sock->sendBinary(&Header, sizeof(Header));
	}
}
Exemplo n.º 2
0
void	ManageRoom::handleListGames(void *buffer, Client *cl)
{
	t_TCPGame	Game;
	int			len;
	ISocket *sock;
	sock = cl->getSocket();

	Game.header.packetSize = sizeof(Game);
	Game.header.type = GAME; 
	for (std::map<short, Room *>::iterator it = this->listRoom.begin(); it != this->listRoom.end(); ++it)
	{
		Game.nb_ingame = ((*it).second)->getNbIngame();
		Game.nb_max = ((*it).second)->getNbMax();
		Game.id_game = ((*it).second)->getGameId();
		if ((len = sock->sendBinary(&Game, sizeof(Game))) < 0)
			std::cout << "Error while sending Game" << std::endl;
	}
	Game.header.packetSize = sizeof(Game.header);
	Game.header.type = END_LIST_GAMES;
	if ((len = sock->sendBinary(&Game, sizeof(Game.header))) < 0)
			std::cout << "Error while sending End Game" << std::endl;
}
Exemplo n.º 3
0
void	ManageRoom::handleCreateGame(void *buffer, Client *cl)
{
	t_TCPCreate *create;
	create = (t_TCPCreate *)buffer;

	Room *room = new Room;

	room->setGameId(this->currentGameId);
	room->setNbMax(create->nb_max);
	room->setName(create->name_game);
	std::cout << "Creating room=" << room->getName() << std::endl;
	char id = room->addClient(cl);
	std::cout << (int)id  << std::endl;
	this->listRoom[this->currentGameId++] = room;
	int i = 0;
	int len;
	const char *str = cl->getName().c_str();
	ISocket *sock;
	t_TCPPlayer Player;

	//On prépare le packet pour prévenir le joueur de sa connection à la partie créée

	while (str[i])
		Player.name[i] = str[i++];
	Player.name[i] = '\0';
	Player.player_id = id;
	Player.status = NOT_READY;
	Player.header.packetSize = sizeof(Player);
	Player.header.type = PLAYER;

	sock = cl->getSocket();
	this->_access[sock->getIp()] = room;
	IThread *th = new WinThread;

	std::cout <<"LOOOOOOOOOOOL" << std::endl;
	th->create(initGame, room);
	std::cout << "LOOOL2"<< std::endl;
	if ((len = sock->sendBinary(&Player, sizeof(Player))) < 0)
		std::cout << "Error while sending Create" << std::endl;
}