Пример #1
0
UBool 
ICUService::isDefault() const 
{
    return countFactories() == 0;
}
Пример #2
0
 virtual UBool isDefault() const {
     return countFactories() == 1;
 }
Пример #3
0
void handleServerEvent(Message * mes)
{
	Player * sp = NULL;
	Game * sg = NULL;
	char * nick = NULL;
	Message nmes;

	if (mes->sndr_t == O_PLAYER)
	{
		sp = mes->sndr.player;
		nick = getNickname(sp);
	}
	if (mes->sndr_t == O_GAME)
		sg = mes->sndr.game;

	switch(mes->type)
	{
		case MEST_PLAYER_JOIN_SERVER:
			info("%s has joined on the server\n", nick);
			info("Players on server/in hall: %d/%d\n", srv.nPlayers, srv.hallPlayers);
			{
				Buffer * msg = newBuffer();
				Buffer * info = newBuffer();
				mPlayer * mp;
				mGame * mg;
				char * str;
				uint32_t i;

				/*  generating MES part  */
				addnStr(msg, infos[INFO_PLAYER_JOINS_SERVER], 1);
				i = htonl(sp->pid);
				addnStr(msg, &i, sizeof(i));

				/*  generating INFO part  */
				addnStr(info, answrs[ANSWR_INFO], 1);
				i = htonl(msg->count);
				addnStr(info, &i, sizeof(i));
				i = msg->count;
				str = flushBuffer(msg);
				addnStr(info, str, i);
				free(str);

				/*  sending info message  */
				i = info->count;
				str = flushBuffer(info);
				/*  players in hall  */
				for (mp = srv.players; mp != NULL; mp = mp->next)
					if (mp->player != sp)
						write(mp->player->fd, str, i);
				/*  players in games  */
				for (mg = srv.games; mg != NULL; mg = mg->next)
					for (mp = mg->game->players; mp != NULL; mp = mp->next)
						if (mp->player != sp)
							write(mp->player->fd, str, i);
				free(str);

				clearBuffer(msg);
				free(msg);
				clearBuffer(info);
				free(info);
			}
			break;

		case MEST_PLAYER_LEAVE_SERVER:
			info("%s has leaved the server\n", nick);
			{
				Buffer * msg = newBuffer();
				Buffer * info = newBuffer();
				mPlayer * mp;
				mGame * mg;
				char * str;
				uint32_t i;

				/*  generating MES part  */
				addnStr(msg, infos[INFO_PLAYER_LEAVES_SERVER], 1);
				i = htonl(sp->pid);
				addnStr(msg, &i, sizeof(i));

				/*  generating INFO part  */
				addnStr(info, answrs[ANSWR_INFO], 1);
				i = htonl(msg->count);
				addnStr(info, &i, sizeof(i));
				i = msg->count;
				str = flushBuffer(msg);
				addnStr(info, str, i);
				free(str);

				/*  sending info message  */
				i = info->count;
				str = flushBuffer(info);
				/*  players in hall  */
				for (mp = srv.players; mp != NULL; mp = mp->next)
					if (mp->player != sp)
						write(mp->player->fd, str, i);
				/*  players in games  */
				for (mg = srv.games; mg != NULL; mg = mg->next)
					for (mp = mg->game->players; mp != NULL; mp = mp->next)
						if (mp->player != sp)
							write(mp->player->fd, str, i);
				free(str);

				clearBuffer(msg);
				free(msg);
				clearBuffer(info);
				free(info);
			}
			freePlayer(sp, sp->game);
			info("Players on server/in hall: %d/%d\n", srv.nPlayers, srv.hallPlayers);
			break;

		case MEST_PLAYER_CHANGES_NICK:
			{
				Buffer * msg = newBuffer();
				Buffer * info = newBuffer();
				mPlayer * mp;
				mGame * mg;
				char * str;
				uint32_t i;

				/*  generating MES part  */
				addnStr(msg, infos[INFO_PLAYER_CHANGES_NICK], 1);
				i = htonl(sp->pid);
				addnStr(msg, &i, sizeof(i));
				i = htonl(strlen(nick));
				addnStr(msg, &i, sizeof(i));
				i = strlen(nick);
				addnStr(msg, nick, i);

				/*  generating INFO part  */
				addnStr(info, answrs[ANSWR_INFO], 1);
				i = htonl(msg->count);
				addnStr(info, &i, sizeof(i));
				i = msg->count;
				str = flushBuffer(msg);
				addnStr(info, str, i);
				free(str);

				/*  sending info message  */
				i = info->count;
				str = flushBuffer(info);
				/*  players in hall  */
				for (mp = srv.players; mp != NULL; mp = mp->next)
					if (mp->player != sp)
						write(mp->player->fd, str, i);
				/*  players in games  */
				for (mg = srv.games; mg != NULL; mg = mg->next)
					for (mp = mg->game->players; mp != NULL; mp = mp->next)
						if (mp->player != sp)
							write(mp->player->fd, str, i);
				free(str);

				clearBuffer(msg);
				free(msg);
				clearBuffer(info);
				free(info);
			}
			break;

		case MEST_COMMAND_UNKNOWN:
			info("%s send unknown command and will be disconnected\n", nick);
			{
				char err = 255;
				write(sp->fd, answrs[ANSWR_ERROR], 1);
				write(sp->fd, &err, 1);
			}
			nmes.sndr_t = O_SERVER;
			nmes.rcvr_t = O_GAME;
			nmes.rcvr.game = sp->game;
			if (sp->game != NULL)
				nmes.type = MEST_PLAYER_LEAVE_GAME;
			else
				nmes.type = MEST_PLAYER_LEAVES_HALL;
			nmes.len = 0;
			nmes.data = NULL;
			sendMessage(&nmes);
			nmes.sndr_t = O_SERVER;
			nmes.rcvr_t = O_PLAYER;
			nmes.rcvr.player = sp;
			nmes.type = MEST_PLAYER_KICKED_FROM_SERVER;
			nmes.len = 0;
			nmes.data = NULL;
			sendMessage(&nmes);
			break;

		case MEST_COMMAND_NICK:
			info("%s sets nick into %s\n", nick, mes->data);
			if (sp->nick != NULL)
				free(sp->nick);
			sp->nick = mes->data;
			write(sp->fd, answrs[ANSWR_OK], 1);
			nmes.sndr_t = O_PLAYER;
			nmes.sndr.player = sp;
			nmes.rcvr_t = O_SERVER;
			nmes.type = MEST_PLAYER_CHANGES_NICK;
			nmes.len = 0;
			nmes.data = NULL;
			sendMessage(&nmes);
			break;

		case MEST_COMMAND_ADM:
			info("%s trying to get Adm privilegies\n", nick);
			if (strcmp(cfg.passkey, mes->data) == 0)
				sp->adm = 1;

			if (sp->adm == 1)
			{
				info("%s grants its Adm privilegies\n", nick);
				write(sp->fd, answrs[ANSWR_OK], 1);
			}
			else
			{
				char err = 0;
				info("Attempt was failed\n");
				write(sp->fd, answrs[ANSWR_ERROR], 1);
				write(sp->fd, &err, 1);
			}
			free(mes->data);
			break;

		case MEST_COMMAND_JOIN:
			info("%s trying to join into game #%d\n", nick, *(uint32_t *)(mes->data));
			if (sp->game == NULL)
			{
				Game * g = findGameByGid(*(uint32_t *)(mes->data));
				if (g == NULL)
				{
					info("Not found game #%d\n", *(uint32_t *)(mes->data));
					char err = 1;
					write(sp->fd, answrs[ANSWR_ERROR], 1);
					write(sp->fd, &err, 1);
				}
				else
				{
					info("%s joins into game #%d\n", nick, *(uint32_t *)(mes->data));
					IntoGame(g, sp);
					write(sp->fd, answrs[ANSWR_OK], 1);
					nmes.sndr_t = O_PLAYER;
					nmes.sndr.player = sp;
					nmes.rcvr_t = O_GAME;
					nmes.rcvr.game = g;
					nmes.type = MEST_PLAYER_JOIN_GAME;
					nmes.len = 0;
					nmes.data = NULL;
					sendMessage(&nmes);
					nmes.sndr_t = O_PLAYER;
					nmes.sndr.player = sp;
					nmes.rcvr_t = O_GAME;
					nmes.rcvr.game = NULL;
					nmes.type = MEST_PLAYER_LEAVES_HALL;
					nmes.len = sizeof(g);
					nmes.data = g;
					sendMessage(&nmes);
					nmes.sndr_t = O_PLAYER;
					nmes.sndr.player = sp;
					nmes.rcvr_t = O_GAME;
					nmes.rcvr.game = g;
					nmes.type = MEST_PLAYERS_STAT_GAME;
					nmes.len = 0;
					nmes.data = NULL;
					sendMessage(&nmes);
				}
			}
			else
			{
				info("%s already in game #%d\n", sp->game->gid);
				char err = 0;
				write(sp->fd, answrs[ANSWR_ERROR], 1);
				write(sp->fd, &err, 1);
			}
			free(mes->data);
			break;

		case MEST_COMMAND_GAMES:
			info("%s trying to get games list\n", nick);
			{
				uint32_t i;
				mGame * mg = srv.games;
				Buffer * buf = newBuffer();
				char * str;

				i = htonl(srv.nGames);
				addnStr(buf, &i, sizeof(i));
				while (mg != NULL)
				{
					i = htonl(mg->game->gid);
					addnStr(buf, &i, sizeof(i));
					i = htonl(mg->game->nPlayers);
					addnStr(buf, &i, sizeof(i));
					mg = mg->next;
				}

				write(sp->fd, answrs[ANSWR_GAMES], 1);
				i = htonl(buf->count);
				write(sp->fd, &i, sizeof(i));
				i = buf->count;
				str = flushBuffer(buf);
				write(sp->fd, str, i);
				free(str);

				clearBuffer(buf);
				free(buf);
			}
			break;

		case MEST_COMMAND_PLAYERS:
			info("%s trying to get players list in game #%d\n", nick, *(uint32_t *)(mes->data));
			{
				int gid = *(uint32_t *)(mes->data);
				Game * g = NULL;

				if (gid != 0)
				{
					g = findGameByGid(gid);
					if (g == NULL)
					{
						char err = 0;
						info("Not found game #%d\n", *(uint32_t *)(mes->data));
						write(sp->fd, answrs[ANSWR_ERROR], 1);
						write(sp->fd, &err, 1);
					}
				}

				if (gid == 0 || g != NULL)
				{
					uint32_t i;
					char * n;
					mPlayer * mp;
					Buffer * buf = newBuffer();
					char * str;

					if (gid == 0)
						mp = srv.players;
					else
						mp = g->players;

					i = htonl(gid);
					addnStr(buf, &i, sizeof(i));
					if (gid == 0)
						i = htonl(srv.hallPlayers);
					else
						i = htonl(g->nPlayers);
					addnStr(buf, &i, sizeof(i));
					while (mp != NULL)
					{
						i = htonl(mp->player->pid);
						addnStr(buf, &i, sizeof(i));
						n = getNickname(mp->player);
						i = htonl(strlen(n));
						addnStr(buf, &i, sizeof(i));
						addnStr(buf, n, strlen(n));
						mp = mp->next;
					}

					write(sp->fd, answrs[ANSWR_PLAYERS], 1);
					i = htonl(buf->count);
					write(sp->fd, &i, sizeof(i));
					i = buf->count;
					str = flushBuffer(buf);
					write(sp->fd, str, i);
					free(str);

					clearBuffer(buf);
					free(buf);
				}
			}
			break;

		case MEST_COMMAND_CREATEGAME:
			info("%s trying to create game\n", nick);
			if (!sp->adm)
			{
				char err = 0;
				info("%s not granted to create games\n", nick);
				write(sp->fd, answrs[ANSWR_ERROR], 1);
				write(sp->fd, &err, 1);
			}
			else
			{
				Game * g = newGame();
				uint32_t i;
				g->gid = srv.nextGid++;
				addGame(g);
				i = htonl(g->gid);
				info("%s created game #%d\n", nick, g->gid);
				write(sp->fd, answrs[ANSWR_STAT], 1);
				write(sp->fd, &i, sizeof(i));
				nmes.sndr_t = O_PLAYER;
				nmes.sndr.player = sp;
				nmes.rcvr_t = O_GAME;
				nmes.rcvr.game = NULL;
				nmes.type = MEST_NEW_GAME;
				nmes.len = sizeof(g);
				nmes.data = g;
				sendMessage(&nmes);
			}
			break;

		case MEST_COMMAND_DELETEGAME:
			info("%s trying to delete game #%d\n", nick, *(uint32_t *)(mes->data));
			if (!sp->adm)
			{
				char err = 0;
				info("%s not granted to delete games\n", nick);
				write(sp->fd, answrs[ANSWR_ERROR], 1);
				write(sp->fd, &err, 1);
			}
			else
			{
				Game * g = findGameByGid(*(uint32_t *)(mes->data));
				if (g == NULL)
				{
					char err = 1;
					info("Not found game #%d\n", *(uint32_t *)(mes->data));
					write(sp->fd, answrs[ANSWR_ERROR], 1);
					write(sp->fd, &err, 1);
				}
				else
				{
					mPlayer * mp;
					info("%s deleted game #%d\n", nick, g->gid);
					write(sp->fd, answrs[ANSWR_OK], 1);

					for (mp = g->players; mp != NULL; mp = mp->next)
					{
						nmes.sndr_t = O_PLAYER;
						nmes.sndr.player = sp;
						nmes.rcvr_t = O_PLAYER;
						nmes.rcvr.player = mp->player;
						nmes.type = MEST_PLAYER_KICKED_FROM_GAME;
						nmes.len = sizeof(g);
						nmes.data = g;
						sendMessage(&nmes);
					}
					nmes.sndr_t = O_PLAYER;
					nmes.sndr.player = sp;
					nmes.rcvr_t = O_GAME;
					nmes.rcvr.game = g;
					nmes.type = MEST_GAME_OVER;
					nmes.len = 0;
					nmes.data = NULL;
					sendMessage(&nmes);
				}
			}
			break;

		case MEST_COMMAND_PLAYER:
			info("%s trying to get info about player #%d\n", nick, *(uint32_t *)(mes->data));
			{
				Player * np = findPlayerByPid(*(uint32_t *)(mes->data));
				if (np == NULL)
				{
					char err = 0;
					info("Not found player #%d\n", *(uint32_t *)(mes->data));
					write(sp->fd, answrs[ANSWR_ERROR], 1);
					write(sp->fd, &err, 1);
				}
				else
				{
					uint32_t i;
					Buffer * buf = newBuffer();
					char * str;

					/*  #pid  */
					i = htonl(np->pid);
					addnStr(buf, &i, sizeof(i));

					/*  #game  */
					if (np->game == NULL)
						i = 0;
					else
						i = np->game->gid;
					i = htonl(i);
					addnStr(buf, &i, sizeof(i));

					/* game characteristics */
					if (np->game->started)
					{
						i = htonl(1);
						addnStr(buf, &i, sizeof(i));
						i = htonl(np->mat);
						addnStr(buf, &i, sizeof(i));
						i = htonl(np->prod);
						addnStr(buf, &i, sizeof(i));
						i = htonl(np->bf);
						addnStr(buf, &i, sizeof(i));
						i = htonl(countFactories(np));
						addnStr(buf, &i, sizeof(i));
						i = htonl(np->money);
						addnStr(buf, &i, sizeof(i));
					}
					else
					{
						i = htonl(0);
						addnStr(buf, &i, sizeof(i));
						addnStr(buf, &i, sizeof(i));
						addnStr(buf, &i, sizeof(i));
						addnStr(buf, &i, sizeof(i));
						addnStr(buf, &i, sizeof(i));
						addnStr(buf, &i, sizeof(i));
					}

					/*  len NICK  */
					str = getNickname(np);
					i = htonl(strlen(str));
					addnStr(buf, &i, sizeof(i));
					addnStr(buf, str, strlen(str));

					/*  player len MES */
					write(sp->fd, answrs[ANSWR_PLAYER], 1);
					i = htonl(buf->count);
					write(sp->fd, &i, sizeof(i));
					i = buf->count;
					str = flushBuffer(buf);
					write(sp->fd, str, i);
					free(str);

					clearBuffer(buf);
					free(buf);
				}
			}
			break;
	}
}