> ## Documentation Index
> Fetch the complete documentation index at: https://devdocs.xbox.com/llms.txt
> Use this file to discover all available pages before exploring further.

# 이벤트 기반 통계 가져오기

> 서비스 호출을 최소화하기 위해 사용자 및 SCID 전반에서 일괄 쿼리를 포함하여 XBOX Live 이벤트 기반 통계를 읽기 위한 XSAPI 예제 코드.

<a id="top" />

이 항목에서는 이벤트 기반 통계를 가져오는 예제 코드를 제공합니다.

API는 이벤트 기반 통계를 가져오기 위해 다양한 수준의 일괄 처리를 제공합니다.
필요에 맞는 배치 API를 사용하여 가능한 한 적은 호출을 수행하는 것이 모범 사례입니다.

이 항목에서는 다음 내용을 다룹니다.

* [단일 사용자와 단일 SCID에 대한 단일 통계 가져오기](#ID4EM1)
* [단일 사용자와 단일 SCID에 대한 여러 통계 가져오기](#ID4EM2)
* [여러 사용자와 단일 SCID에 대한 여러 통계 가져오기](#ID4EM3)
* [여러 SCID 전반에서 여러 사용자에 대한 여러 통계 가져오기](#ID4EM4)

또한 [통계 변경 구독](/services/xbox-services/player-data/stats-leaderboards/event-based/how-to/live-handling-eb-stat-change)도 가능합니다.

<a id="ID4EM1" />

## 단일 사용자와 단일 SCID에 대한 단일 통계 가져오기

다음 예제 코드는 단일 사용자 통계를 읽는 방법을 보여 줍니다.

#### 플랫 C API

```cpp theme={null}
auto asyncBlock = std::make_unique<XAsyncBlock>();
asyncBlock->queue = queue;
asyncBlock->callback = [](XAsyncBlock* asyncBlock)
{
    std::unique_ptr<XAsyncBlock> asyncBlockPtr{ asyncBlock }; // Take over ownership of the XAsyncBlock*.
    size_t resultSize;
    HRESULT hr = XblUserStatisticsGetSingleUserStatisticResultSize(asyncBlock, &resultSize);

    if (SUCCEEDED(hr))
    {
        std::vector<char> buffer(resultSize, 0);
        XblUserStatisticsResult* result{};

        hr = XblUserStatisticsGetSingleUserStatisticResult(asyncBlock, resultSize, buffer.data(), &result, nullptr);

        if (SUCCEEDED(hr) && result->serviceConfigStatisticsCount > 0 && result->serviceConfigStatistics->statisticsCount > 0)
        {
            int64_t userStatValue = atoll(result->serviceConfigStatistics[0].statistics[0].value);
            // You can now show or store userStatValue.
        }
    }
};

HRESULT hr = XblUserStatisticsGetSingleUserStatisticAsync(
    xboxLiveContext,
    xboxUserId,
    scid,
    statisticName.c_str(),
    asyncBlock.get()
);
if (SUCCEEDED(hr))
{
    // The call succeeded, so release the std::unique_ptr ownership of XAsyncBlock* because the callback will take over ownership.
    // If the call fails, the std::unique_ptr will keep ownership and delete the XAsyncBlock*.
    asyncBlock.release();
}
```

자세한 내용은 다음을 참조하세요.

* [XAsyncBlock](/reference/system/xasync/structs/xasyncblock)
* [XblUserStatisticsGetSingleUserStatisticAsync](/reference/live/xsapi-c/user_statistics_c/functions/xbluserstatisticsgetsingleuserstatisticasync)
* [XblUserStatisticsGetSingleUserStatisticResult](/reference/live/xsapi-c/user_statistics_c/functions/xbluserstatisticsgetsingleuserstatisticresult)
* [XblUserStatisticsGetSingleUserStatisticResultSize](/reference/live/xsapi-c/user_statistics_c/functions/xbluserstatisticsgetsingleuserstatisticresultsize)
* [XblUserStatisticsResult](/reference/live/xsapi-c/user_statistics_c/structs/xbluserstatisticsresult)

[이 항목의 맨 위로 돌아가기.](#top)

<a id="ID4EM2" />

## 단일 사용자와 단일 SCID에 대한 여러 통계 가져오기

다음 예제 코드는 하나의 HTTP 호출로 단일 사용자에 대한 여러 통계를 읽는 방법을 보여 줍니다.

#### 플랫 C API

```cpp theme={null}
auto asyncBlock = std::make_unique<XAsyncBlock>();
asyncBlock->queue = queue;
asyncBlock->callback = [](XAsyncBlock* asyncBlock)
{
    std::unique_ptr<XAsyncBlock> asyncBlockPtr{ asyncBlock }; // Take over ownership of the XAsyncBlock*.
    size_t resultSize;
    HRESULT hr = XblUserStatisticsGetSingleUserStatisticsResultSize(asyncBlock, &resultSize);

    if (SUCCEEDED(hr))
    {
        std::vector<char> buffer(resultSize, 0);
        XblUserStatisticsResult* results{};

        hr = XblUserStatisticsGetSingleUserStatisticsResult(asyncBlock, resultSize, buffer.data(), &results, nullptr);
        if (SUCCEEDED(hr))
        {
            // You can now use the XblUserStatisticsResult array in results.
        }
    }

};

const char* statisticNames[2] = {};
statisticNames[0] = statisticName1.c_str();
statisticNames[1] = statisticName2.c_str();

HRESULT hr = XblUserStatisticsGetSingleUserStatisticsAsync(
    xboxLiveContext,
    xboxUserId,
    scid,
    statisticNames, 2, 
    asyncBlock.get()
);
if (SUCCEEDED(hr))
{
    // The call succeeded, so release the std::unique_ptr ownership of XAsyncBlock* because the callback will take over ownership.
    // If the call fails, the std::unique_ptr will keep ownership and delete the XAsyncBlock*.
    asyncBlock.release();
}

```

자세한 내용은 다음을 참조하세요.

* [XAsyncBlock](/reference/system/xasync/structs/xasyncblock)
* [XblUserStatisticsGetSingleUserStatisticAsync](/reference/live/xsapi-c/user_statistics_c/functions/xbluserstatisticsgetsingleuserstatisticasync)
* [XblUserStatisticsGetSingleUserStatisticResult](/reference/live/xsapi-c/user_statistics_c/functions/xbluserstatisticsgetsingleuserstatisticresult)
* [XblUserStatisticsGetSingleUserStatisticResultSize](/reference/live/xsapi-c/user_statistics_c/functions/xbluserstatisticsgetsingleuserstatisticresultsize)
* [XblUserStatisticsResult](/reference/live/xsapi-c/user_statistics_c/structs/xbluserstatisticsresult)

[이 항목의 맨 위로 돌아가기.](#top)

<a id="ID4EM3" />

## 여러 사용자와 단일 SCID에 대한 여러 통계 가져오기

다음 예제 코드는 하나의 HTTP 호출로 여러 사용자에 대한 여러 통계를 읽는 방법을 보여 줍니다.

#### 플랫 C API

```cpp theme={null}
auto asyncBlock = std::make_unique<XAsyncBlock>();
asyncBlock->queue = queue;
asyncBlock->callback = [](XAsyncBlock* asyncBlock)
{
    std::unique_ptr<XAsyncBlock> asyncBlockPtr{ asyncBlock }; // Take over ownership of the XAsyncBlock*.
    size_t resultSize;
    HRESULT hr = XblUserStatisticsGetMultipleUserStatisticsResultSize(asyncBlock, &resultSize);

    if (SUCCEEDED(hr))
    {
        std::vector<char> buffer(resultSize, 0);
        XblUserStatisticsResult* results{};
        size_t resultsCount = 0;

        hr = XblUserStatisticsGetMultipleUserStatisticsResult(asyncBlock, resultSize, buffer.data(), &results, &resultsCount, nullptr);

        // Process results array to read the user stats data.
        for (size_t iResult = 0; iResult < resultsCount; iResult++)
        {
            LogToFile("%d", results[iResult].xboxUserId);
            for (size_t iScid = 0; iScid < results[iResult].serviceConfigStatisticsCount; iScid++)
            {
                LogToFile("SCID: %s", results[iResult].serviceConfigStatistics[iScid].serviceConfigurationId);
                for (size_t iStat = 0; iStat < results[iResult].serviceConfigStatistics[iScid].statisticsCount; iStat++)
                {
                    LogToFile("Stat %d: name:%s value:%s type:%s", iResult,
                        results[iResult].serviceConfigStatistics[iScid].statistics[iStat].statisticName,
                        results[iResult].serviceConfigStatistics[iScid].statistics[iStat].value,
                        results[iResult].serviceConfigStatistics[iScid].statistics[iStat].statisticType );
                }
            }
        }
    }

};

const char* statisticNames[2] = {};
statisticNames[0] = statisticName1.c_str();
statisticNames[1] = statisticName2.c_str();

uint64_t xuids[2] = {};
xuids[0] = xuid1;
xuids[1] = xuid2;

HRESULT hr = XblUserStatisticsGetMultipleUserStatisticsAsync(
    xboxLiveContext,
    xuids, 2,
    scid,
    statisticNames, 2,
    asyncBlock.get()
);
if (SUCCEEDED(hr))
{
    // The call succeeded, so release the std::unique_ptr ownership of XAsyncBlock* because the callback will take over ownership.
    // If the call fails, the std::unique_ptr will keep ownership and delete the XAsyncBlock*.
    asyncBlock.release();
}
```

자세한 내용은 다음을 참조하세요.

* [XAsyncBlock](/reference/system/xasync/structs/xasyncblock)
* [XblUserStatisticsGetMultipleUserStatisticsAsync](/reference/live/xsapi-c/user_statistics_c/functions/xbluserstatisticsgetmultipleuserstatisticsasync)
* [XblUserStatisticsGetMultipleUserStatisticsResult](/reference/live/xsapi-c/user_statistics_c/functions/xbluserstatisticsgetmultipleuserstatisticsresult)
* [XblUserStatisticsGetMultipleUserStatisticsResultSize](/reference/live/xsapi-c/user_statistics_c/functions/xbluserstatisticsgetmultipleuserstatisticsresultsize)
* [XblUserStatisticsResult](/reference/live/xsapi-c/user_statistics_c/structs/xbluserstatisticsresult)

[이 항목의 맨 위로 돌아가기.](#top)

<a id="ID4EM4" />

## 여러 SCID 전반에서 여러 사용자에 대한 여러 통계 가져오기

다음 예제 코드는 XBOX Services API(XSAPI)를 사용하여 단 한 번의 호출로 여러 사용자에 대한 단일 서비스 구성에서 여러 통계를 읽는 방법을 보여 줍니다.

#### 플랫 C API

```cpp theme={null}
auto asyncBlock = std::make_unique<XAsyncBlock>();
asyncBlock->queue = queue;
asyncBlock->callback = [](XAsyncBlock* asyncBlock)
{
    std::unique_ptr<XAsyncBlock> asyncBlockPtr{ asyncBlock }; // Take over ownership of the XAsyncBlock*.
    size_t resultSize;
    HRESULT hr = XblUserStatisticsGetMultipleUserStatisticsForMultipleServiceConfigurationsResultSize(asyncBlock, &resultSize);

    if (SUCCEEDED(hr))
    {
        std::vector<char> buffer(resultSize, 0);
        XblUserStatisticsResult* results{};
        size_t resultsCount = 0;

        hr = XblUserStatisticsGetMultipleUserStatisticsForMultipleServiceConfigurationsResult(asyncBlock, resultSize, buffer.data(), &results, &resultsCount, nullptr);

        if (SUCCEEDED(hr))
        {
            for (size_t i = 0; i < resultsCount; i++)
            {
                // Log results.
                std::stringstream stream;
                stream << "XUID: " << results[i].xboxUserId << std::endl;;

                for (size_t j = 0; j < results[i].serviceConfigStatisticsCount; j++)
                {
                    stream << " " << results[i].serviceConfigStatistics[j].serviceConfigurationId << ": " << std::endl;
                    for (size_t k = 0; k < results[i].serviceConfigStatistics[j].statisticsCount; k++)
                    {
                        stream << " " << results[i].serviceConfigStatistics[j].statistics[k].statisticName << "=" << results[i].serviceConfigStatistics[j].statistics[k].value << std::endl;
                    }
                }
            }
        }
    }

};

const char* requestedStatsNames1[2] = {};
requestedStatsNames1[0] = statisticName1.c_str();
requestedStatsNames1[1] = statisticName2.c_str();

XblRequestedStatistics requestedStats[1] = {};
pal::strcpy(requestedStats[0].serviceConfigurationId, sizeof(XblRequestedStatistics::serviceConfigurationId), scid);
requestedStats[0].statistics = requestedStatsNames1;
requestedStats[0].statisticsCount = 2;

uint64_t xuids[2] = {};
xuids[0] = xuid1;
xuids[1] = xuid2;
    
HRESULT hr = XblUserStatisticsGetMultipleUserStatisticsForMultipleServiceConfigurationsAsync(
    xboxLiveContext,
    xuids, 2,
    requestedStats, 1,
    asyncBlock.get()
);
if (SUCCEEDED(hr))
{
    // The call succeeded, so release the std::unique_ptr ownership of XAsyncBlock* because the callback will take over ownership.
    // If the call fails, the std::unique_ptr will keep ownership and delete the XAsyncBlock*.
    asyncBlock.release();
}
```

자세한 내용은 다음을 참조하세요.

* [XAsyncBlock](/reference/system/xasync/structs/xasyncblock)
* [XblRequestedStatistics](/reference/live/xsapi-c/user_statistics_c/structs/xblrequestedstatistics)
* [XblUserStatisticsGetMultipleUserStatisticsForMultipleServiceConfigurationsAsync](/reference/live/xsapi-c/user_statistics_c/functions/xbluserstatisticsgetmultipleuserstatisticsformultipleserviceconfigurationsasync)
* [XblUserStatisticsGetMultipleUserStatisticsForMultipleServiceConfigurationsResult](/reference/live/xsapi-c/user_statistics_c/functions/xbluserstatisticsgetmultipleuserstatisticsformultipleserviceconfigurationsresult)
* [XblUserStatisticsGetMultipleUserStatisticsForMultipleServiceConfigurationsResultSize](/reference/live/xsapi-c/user_statistics_c/functions/xbluserstatisticsgetmultipleuserstatisticsformultipleserviceconfigurationsresultsize)
* [XblUserStatisticsResult](/reference/live/xsapi-c/user_statistics_c/structs/xbluserstatisticsresult)

## 참고 항목

[이벤트 전송](/services/xbox-services/player-data/stats-leaderboards/event-based/events/how-to/live-sending-an-event)
[user\_statistics\_c 헤더](/reference/live/xsapi-c/user_statistics_c/user_statistics_c_members)

[이 항목의 맨 위로 돌아가기.](#top)


## Related topics

- [이벤트 기반 통계 및 순위표 예제 코드](/ko/services/xbox-services/player-data/stats-leaderboards/event-based/how-to/live-statslb-eb-howto-nav.md)
- [이벤트 기반 업적 가져오기](/ko/services/xbox-services/player-data/achievements/event-based/how-to/live-getting-event-based-achievements.md)
- [RTA 서비스 프로그래밍](/ko/services/xbox-services/fundamentals/rta/how-to/live-programming-rta.md)
- [이벤트 기반 통계 개요](/ko/services/xbox-services/player-data/stats-leaderboards/event-based/live-stats-eb-overview.md)
- [이벤트 기반 통계 규칙 개요](/ko/services/xbox-services/player-data/stats-leaderboards/event-based/config/live-user-stat-rules.md)
