Esempio n. 1
0
//---------------------------------------------------------------------------------
// Purpose: called once per server frame, do recurring work here (like checking for timeouts)
//---------------------------------------------------------------------------------
void WebSpecPlugin::GameFrame( bool simulating )
{
	if (gpGlobals->curtime - g_lastUpdateTime > WEBSPEC_UPDATE_RATE_IN_SECONDS
		&& ws_spectators.Count() > 0) {
		
		char *buffer = (char *)malloc(MAX_BUFFER_SIZE);
		Vector playerOrigin;
		QAngle playerAngles;
		float playerUberCharge;
		int userid, health, playerClass;

		IPlayerInfo *playerInfo;
		CBaseEntity *playerEntity;
		int bufferLength;

		bufferLength = sprintf(buffer, "O");

		for (int i = 1; i < gpGlobals->maxClients; i++) {
			playerInfo = playerInfoManager->GetPlayerInfo(engine->PEntityOfEntIndex(i));
			if (playerInfo == NULL || !playerInfo->IsConnected() || playerInfo->IsDead()) continue;

			if (strlen(buffer) > 1)
				snprintf(buffer, MAX_BUFFER_SIZE, "%s|", buffer);

			userid = playerInfo->GetUserID();
			health = playerInfo->GetHealth();
			playerOrigin = playerInfo->GetAbsOrigin();

			playerEntity = serverGameEnts->EdictToBaseEntity(engine->PEntityOfEntIndex(i));
			playerClass = *MakePtr(int*, playerEntity, WSOffsets::pCTFPlayer__m_iClass);
			playerAngles = CBaseEntity_EyeAngles(playerEntity);

			if (playerClass == TFClass_Medic) {
				CBaseCombatCharacter *playerCombatCharacter = CBaseEntity_MyCombatCharacterPointer(playerEntity);
				CBaseCombatWeapon *slot1Weapon = CBaseCombatCharacter_Weapon_GetSlot(playerCombatCharacter, 1);
				
				playerUberCharge = *MakePtr(float*, slot1Weapon, WSOffsets::pCWeaponMedigun__m_flChargeLevel);
			} else {
//---------------------------------------------------------------------------------
// Purpose: Builds up a list of players that are 'kickable'
//---------------------------------------------------------------------------------
void ManiReservedSlot::BuildPlayerKickList( player_t *player_ptr, int *players_on_server )
{
	player_t	temp_player;
	active_player_t active_player;

	FreeList((void **) &active_player_list, &active_player_list_size);

	for (int i = 1; i <= max_players; i ++)
	{
#if defined ( GAME_CSGO )
		edict_t *pEntity = PEntityOfEntIndex(i);
#else
		edict_t *pEntity = engine->PEntityOfEntIndex(i);
#endif
		if( pEntity && !pEntity->IsFree())
		{
			if ( player_ptr && ( pEntity == player_ptr->entity ) )
				continue;

			IPlayerInfo *playerinfo = playerinfomanager->GetPlayerInfo( pEntity );
			if (playerinfo && playerinfo->IsConnected())
			{
				Q_strcpy(active_player.steam_id, playerinfo->GetNetworkIDString());
				if (FStrEq("BOT", active_player.steam_id))
				{
					continue;
				}

				INetChannelInfo *nci = engine->GetPlayerNetInfo(i);
				if (!nci)
				{
					continue;
				}

				active_player.entity = pEntity;

				active_player.ping = nci->GetAvgLatency(0);
				const char * szCmdRate = engine->GetClientConVarValue( i, "cl_cmdrate" );
				int nCmdRate = (20 > Q_atoi( szCmdRate )) ? 20 : Q_atoi(szCmdRate);
				active_player.ping -= (0.5f/nCmdRate) + TICKS_TO_TIME( 1.0f ); // correct latency

				// in GoldSrc we had a different, not fixed tickrate. so we have to adjust
				// Source pings by half a tick to match the old GoldSrc pings.
				active_player.ping -= TICKS_TO_TIME( 0.5f );
				active_player.ping = active_player.ping * 1000.0f; // as msecs
				active_player.ping = ((5 > active_player.ping) ? 5:active_player.ping); // set bounds, dont show pings under 5 msecs

				active_player.time_connected = nci->GetTimeConnected();
				Q_strcpy(active_player.ip_address, nci->GetAddress());
				if (gpManiGameType->IsSpectatorAllowed() &&
					playerinfo->GetTeamIndex () == gpManiGameType->GetSpectatorIndex())
				{
					active_player.is_spectator = true;
				}
				else
				{
					active_player.is_spectator = false;
				}
				active_player.user_id = playerinfo->GetUserID();
				Q_strcpy(active_player.name, playerinfo->GetName());

				if ( players_on_server )
					*players_on_server = *players_on_server + 1;

				active_player.kills = playerinfo->GetFragCount();
				active_player.deaths = playerinfo->GetDeathCount();
				Q_strcpy(temp_player.steam_id, active_player.steam_id);
				Q_strcpy(temp_player.ip_address, active_player.ip_address);
				Q_strcpy(temp_player.name, active_player.name);
				temp_player.is_bot = false;

				if (IsPlayerInReserveList(&temp_player))
				{
					continue;
				}

				active_player.index = i;

				if (mani_reserve_slots_include_admin.GetInt() == 1 &&
					gpManiClient->HasAccess(active_player.index, ADMIN, ADMIN_BASIC_ADMIN))
				{
					continue;
				}

				if (gpManiClient->HasAccess(active_player.index, IMMUNITY, IMMUNITY_RESERVE))
				{
					continue;
				}

				AddToList((void **) &active_player_list, sizeof(active_player_t), &active_player_list_size);
				active_player_list[active_player_list_size - 1] = active_player;
			}
		}
	}
}
Esempio n. 3
0
//=================================================================================
// Callback for the 'webspec' protocol
// Manages spectator connections & sending Initial messages to new spectators
//=================================================================================
int webspec_callback(struct libwebsocket_context *ctx, struct libwebsocket *wsi, 
	enum libwebsocket_callback_reasons reason, void *user, void *in, size_t len)
{
	switch (reason) {
		case LWS_CALLBACK_ESTABLISHED:
		{
			Msg("[WS] New connection\n");
			// New connection
			ws_spectators.AddToTail(wsi);
			
			// Send basic game info to let client set up
			// MapName, Server name (may remove), current team names
			char *buffer = (char*)malloc(MAX_BUFFER_SIZE);
			char *mapName = (char*) STRING(gpGlobals->mapname);
			ConVarRef hostNameCVar = ConVarRef("hostname");
			string_t hostname;
			if (hostNameCVar.IsValid())
				hostname = MAKE_STRING(hostNameCVar.GetString());
			else
				hostname = MAKE_STRING("WebSpec Demo Server"); //Can't imagine when hostname would be invalid, but this is Source
			int length = snprintf(buffer, MAX_BUFFER_SIZE, "%c%s:%s:%s:%s", WSPacket_Init, mapName, STRING(hostname), STRING(ws_teamName[1]), STRING(ws_teamName[0]));

			SendPacketToOne(buffer, length, wsi);
			free(buffer);

			//Send connected players
			IPlayerInfo *playerInfo;
			for (int i=1; i<=gpGlobals->maxClients; i++) {
				playerInfo = playerInfoManager->GetPlayerInfo(engine->PEntityOfEntIndex(i));
				if (playerInfo != NULL && playerInfo->IsConnected()) {
					buffer = (char *)malloc(MAX_BUFFER_SIZE);
					int userid = playerInfo->GetUserID();
					int teamid = playerInfo->GetTeamIndex();
					int health = playerInfo->GetHealth();
					int maxHealth = playerInfo->GetMaxHealth();
					bool alive = !playerInfo->IsDead();
					string_t playerName = MAKE_STRING(playerInfo->GetName());
					
					//Pointer magic to get TF2 class
					CBaseEntity *playerEntity = serverGameEnts->EdictToBaseEntity(engine->PEntityOfEntIndex(i));
					int playerClass = *MakePtr(int*, playerEntity, WSOffsets::pCTFPlayer__m_iClass);

					float uberCharge = 0.0f;
					
					if (playerClass == TFClass_Medic) { 
						//Way more pointer magic to get ubercharge from medigun
						CBaseCombatCharacter *playerCombatCharacter = CBaseEntity_MyCombatCharacterPointer(playerEntity);
						CBaseCombatWeapon *slot1Weapon = CBaseCombatCharacter_Weapon_GetSlot(playerCombatCharacter, 1);
						
						uberCharge = *MakePtr(float*, slot1Weapon, WSOffsets::pCWeaponMedigun__m_flChargeLevel);
					}

					int length = snprintf(buffer, MAX_BUFFER_SIZE, "%c%d:%d:%d:%d:%d:%d:0:%d:%s", 'C', userid, teamid, playerClass,
						health, maxHealth, alive, Round(uberCharge*100.0f), STRING(playerName));

					SendPacketToOne(buffer, length, wsi);
					free(buffer);
				}
			}

			break;
		}