본문 바로가기

TFT Support - 2 본문

개인 프로젝트 공부

TFT Support - 2

Seongjun_You 2024. 6. 26. 20:21

API의 JSON데이터를 정리해 주었다.

 

 

 

#ifndef APIMANAGER_H
#define APIMANAGER_H

#include <string>
#include <nlohmann/json.hpp>
#include <vector>

class APIManager{
    private:

        std::string apiEndpoint;
        std::string api_Data;
        static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void* userp);

    public:
        APIManager(const std::string& endpoint);
        void FetchData();

        void SET_api_data(const std::string& data);
        nlohmann::json GET_api_data();

};

#endif // APIMANAGER_H

 

먼저 APIManager.h이다.

 

SET_api_data() 함수를 이용해

api 데이터를 string api_Data에 저장해 두었다가

 

GET_api_data() 함수를 이용해

api_Data를 리턴해준다.

 

main함수에서 json데이터를 리턴 받아 속성별로 정리해 주는 작업을 해주었다.

 

 

 

 

#ifndef JSONPARSING_H
#define JSONPARSING_H

#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <nlohmann/json.hpp>


class MatchID{
    private:
        std::vector<std::string> matchids;
    
    public:
        MatchID(const nlohmann::json& json);
        std::vector<std::string> GET_match_id();
};



class Companion {
public:
    std::string content_ID;
    int item_ID;
    int skin_ID;
    std::string species;

    void from_json(const nlohmann::json& json);
};

class Trait {
public:
    std::string name;
    int num_units;
    int style;
    int tier_current;
    int tier_total;

    void from_json(const nlohmann::json& json);
};

class Unit {
public:
    std::string character_id;
    std::vector<std::string> itemNames;
    std::string name;
    int rarity;
    int tier;

    void from_json(const nlohmann::json& json);
};

// Participant 클래스
class Participant {
public:
    std::vector<std::string> augments;
    Companion companion;
    int gold_left;
    int last_round;
    int level;
    int missions_PlayerScore2;
    int placement;
    int players_eliminated;
    std::string puuid;
    double time_eliminated;
    int total_damage_to_players;
    std::vector<Trait> traits;
    std::vector<Unit> units;
    
    void from_json(const nlohmann::json& json);
};

class Info {
public:
    std::string endOfGameResult;
    long long gameCreation;
    long long gameId;
    long long game_datetime;
    double game_length;
    std::string game_version;
    int mapId;
    std::vector<Participant> participants;

    
    void from_json(nlohmann::json& json);
    void printGameDetails();
};

#endif

JSONParsing.h 코드이다.

 

클래스별로 만들어주었고

from_json() 함수를 통해 json객체를 사용할 수 있는 데이터로 변환시켜 저장한다.

Info 클래스부터 시작해서 안에 속해있는 선언된 클래스들 순서로 변수로써 저장해 준다.

 

 

 

#include "JSONParsing.h"
#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <nlohmann/json.hpp>
using json = nlohmann::json;

MatchID::MatchID(const nlohmann::json& json){
    //nlohmann::json jsonArray = nlohmann::json::parse(data);
    matchids = json.get<std::vector<std::string>>();
}

std::vector<std::string> MatchID::GET_match_id()
{
    return matchids;
}


void Companion::from_json(const nlohmann::json& json){
    content_ID = json.at("content_ID").get<std::string>();
    item_ID = json.at("item_ID").get<int>();
    skin_ID = json.at("skin_ID").get<int>();
    species = json.at("species").get<std::string>();
}

void Trait::from_json(const nlohmann::json& json){
    name = json.at("name").get<std::string>();
    num_units = json.at("num_units").get<int>();
    style = json.at("style").get<int>();
    tier_current = json.at("tier_current").get<int>();
    tier_total = json.at("tier_total").get<int>();
}

void Unit::from_json(const nlohmann::json& json){
    character_id = json.at("character_id").get<std::string>();
    itemNames = json.at("itemNames").get<std::vector<std::string>>();
    name = json.at("name").get<std::string>();
    rarity = json.at("rarity").get<int>();
    tier = json.at("tier").get<int>();
}


void Participant::from_json(const nlohmann::json& json){
    augments = json.at("augments").get<std::vector<std::string>>();
    companion.from_json(json.at("companion"));
    gold_left = json.at("gold_left").get<int>();
    last_round = json.at("last_round").get<int>();
    level = json.at("level").get<int>();
    missions_PlayerScore2 = json.at("missions").at("PlayerScore2").get<int>();
    placement = json.at("placement").get<int>();
    players_eliminated = json.at("players_eliminated").get<int>();
    puuid = json.at("puuid").get<std::string>();
    time_eliminated = json.at("time_eliminated").get<double>();
    total_damage_to_players = json.at("total_damage_to_players").get<int>();

    for (const auto& trait_json : json.at("traits")) {
        Trait trait;
        trait.from_json(trait_json);
        traits.push_back(trait);
    }

    for (const auto& unit_json : json.at("units")) {
        Unit unit;
        unit.from_json(unit_json);
        units.push_back(unit);
    }
}

void Info::from_json(nlohmann::json& json) {
    json = json.at("info");
    endOfGameResult = json.at("endOfGameResult").get<std::string>();
    gameCreation = json.at("gameCreation").get<long long>();
    gameId = json.at("gameId").get<long long>();
    game_datetime = json.at("game_datetime").get<long long>();
    game_length = json.at("game_length").get<double>();
    game_version = json.at("game_version").get<std::string>();
    mapId = json.at("mapId").get<int>();

    for(const auto& participant_json : json.at("participants")){
        Participant participant;
        participant.from_json(participant_json);
        participants.push_back(participant);
    }
}

void Info::printGameDetails(){
    std::cout << "Game ID: " << gameId << std::endl;
    std::cout << "Game date time: " << game_datetime << std::endl;
    std::cout << "number of players : " << participants.size() << std::endl;
    std::cout << "data of player1 : " << participants[0].traits[0].name << std::endl;
}

JSONParsing.cpp이다.

전부 JSON파일의 속성을 찾아내고 뽑아내는 거라 시간이 오래 걸렸다.

또한 중첩클래스가 깊게 존재하기 때문에

너무 헷갈렸다.

중첩클래스는 vector로 관리하여 삽입했다.

 

 

 

#include "APIManager.h"
#include "JSONParsing.h"
#include <iostream>
#include <vector>
#include <curl/curl.h>

using namespace std;
int main() {
    
    

    string puuid = "jzmQU3rvfYTnN7djGlqZg7GvuQISJT1iRAohHZy8PtWwTnFy5EGD8o-_2KL3iVLgEQkB2jk55p6Q_w";
    string api_key = "???";


    vector<string> match_ids;
    nlohmann::json match_data;

    APIManager get_match_id("https://asia.api.riotgames.com/tft/match/v1/matches/by-puuid/"+ puuid +"/ids?start=0&endTime=1719298646&startTime=1718841600&count=20&api_key=" + api_key); // 실제 API 엔드포인트로 변경
    get_match_id.FetchData();
    MatchID change_to_array(get_match_id.GET_api_data());
    match_ids = change_to_array.GET_match_id();
    
    
    APIManager get_match_data("https://asia.api.riotgames.com/tft/match/v1/matches/"+ match_ids[0] +"?api_key=" + api_key);
    get_match_data.FetchData();
    match_data = get_match_data.GET_api_data();

    Info info_data;
    info_data.from_json(match_data);
    info_data.printGameDetails();

    return 0;
}

메인함수에서는 클래스들을 선언해 준다.

마지막으로 데이터가 클래스들 안에 잘 저장되어 있는지 확인하기 위해

printGameDetails() 함수를 만들어서 출력하기로 했다.

 

 

 

게임아이디와 게임 시작시간

플레이어 수와 첫 번째 플레이어의 첫번째 증강체를 출력해 주었다.

 

 

이런 데이터들을 활용해서

내가 전판에 어떤 시너지가 좋았는지 나빴는지 어떤 유닛이 좋은지 나쁜지에 대해 분석할 수 있는

코드를 작성해볼까 한다.

Comments