应用场景:
- 有很多社区服会对玩家真实游戏时长有要求,那么如何才能获取到这个信息呢?
代码分享:
下面的代码就可以实现类似的功能,注要依靠SteamWorks拓展以及steamAPI来完成这样的功能
void RequestHours(int client, char[] auth) {
Handle request = CreateRequest_RequestHours(client, auth);
SteamWorks_SendHTTPRequest(request);
}
Handle CreateRequest_RequestHours(int client, char[] auth) {
char request_url[512];
char s_Steamapi[256];
g_cSteamAPI.GetString(s_Steamapi, sizeof(s_Steamapi));
Format(request_url, sizeof(request_url), "http://api.steampowered.com/IPlayerService/GetOwnedGames/v0001/?key=%s&include_played_free_games=1&appids_filter[0]=730i&steamid=%s&format=json", s_Steamapi, auth);
Handle request = SteamWorks_CreateHTTPRequest(k_EHTTPMethodGET, request_url);
SteamWorks_SetHTTPRequestContextValue(request, GetClientUserId(client));
SteamWorks_SetHTTPCallbacks(request, RequestHours_OnHTTPResponse);
return request;
}
public int RequestHours_OnHTTPResponse(Handle request, bool bFailure, bool bRequestSuccessful, EHTTPStatusCode eStatusCode, int userid) {
if (!bRequestSuccessful || eStatusCode != k_EHTTPStatusCode200OK) {
PrintToServer("[SUMMER-SOLDIER, NonPrimeKicker]==> HTTP Request to fetch Client's Profile data failed!");
delete request;
return;
}
int client = GetClientOfUserId(userid);
if (!client) {
delete request;
return;
}
int bufferSize;
SteamWorks_GetHTTPResponseBodySize(request, bufferSize);
char[] responseBody = new char[bufferSize];
SteamWorks_GetHTTPResponseBodyData(request, responseBody, bufferSize);
delete request;
int playedTime = GetPlayerHours(responseBody);
int totalPlayedTime = playedTime / 60;
PrintToServer("[SUMMER-SOLDIER, NonPrimeKicker]==> Total CSGO play hours of the user %d", totalPlayedTime);
int minimumhours = g_cMinimumHours.IntValue;
char message[512];
g_cKickMsg.GetString(message, 512);
if (!totalPlayedTime) {
PrintToServer("[SUMMER-SOLDIER, NonPrimeKicker]==> Play hours of the user are Invisible");
KickClient(client, "%s, You have Invisible Play hours, Try making your profile public and then retry",message);
LogToFile(g_sLogs, "%L does not have the CSGO Licence and Unable to fetch CSGO play hours.", client);
return;
}
if (minimumhours != 0) {
if (totalPlayedTime < minimumhours) {
PrintToServer("[SUMMER-SOLDIER, NonPrimeKicker]==> Unlicenced Client and Also not have enough CSGO play hours, Kicked");
KickClient(client, "%s, You do not have enough CSGO Play hours to enter this server", message);
LogToFile(g_sLogs, "[SUMMER-SOLDIER, NonPrimeKicker]==> %L does not have the CSGO Licence and Not enough CSGO play hours.", client);
return;
}
}
int tagEnabled = g_cTagEnabled.IntValue;
char tag[128];
g_cTag.GetString(tag, 128);
if (tagEnabled) {
PrintToServer("[SUMMER-SOLDIER, NonPrimeKicker]==> Unlicenced Client but has enough play hours, Allowed in server with non prime tag");
CS_SetClientClanTag(client, tag);
}
}
stock int GetPlayerHours(const char[] responseBody) {
char str[8][64];
ExplodeString(responseBody, ",", str, sizeof(str), sizeof(str[]));
for (int i = 0; i < 8; i++) {
if (StrContains(str[i], "playtime_forever") != -1) {
char str2[2][32];
ExplodeString(str[i], ":", str2, sizeof(str2), sizeof(str2[]));
return StringToInt((str2[1]));
}
}
return -1;
}
其中,需要你填写steamapiKey:
获取方式:Steam 社区 :: Steam Web API 密钥 (steamcommunity.com)
在一个就是对应玩家的steamID 64位。
最终你解析的会是一个json文件,解析的data是playertime_forever:
参考: