Project/LOATODO

[LOATODO] 로스트아크 오픈 API - 오레하 제작(낚시) 효율 계산

마볼링 2022. 12. 18. 02:09

1. 서론

API 데이터 활용하는거 연습해볼겸
평소에 낚시 재료로 오레하를 만들었을 때
"어떤 오레하를 만들어서 파는 것이 이득인가?"
"낚시 재료로 파는 것과 만들어서 파는 것중에 어떤게 더 골드를 더벌까?"
두 가지 생각을 바탕으로 엑셀에 적당히 만들어서 계산을 하였다.

 

 

이 표에서 필요한 데이터는 "낚시 재료 가격"과 "오레하 가격"이다
이 두 가지 데이터를 페이지 접속시 API로 가져와서 계산 후 보여줄 것이다.

 

2. 개발

2 - 1. 낚시 재료 가격 호출

public JSONObject CallMarketCategories(int CategoryCode) {
   try {
      URL url = new URL("https://developer-lostark.game.onstove.com/markets/items/");
      HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); // 서버 연결
      httpURLConnection.setRequestMethod("POST");
      httpURLConnection.setRequestProperty("authorization", "Bearer "+LostarkApiKey);
      httpURLConnection.setRequestProperty("accept","application/json");
      httpURLConnection.setRequestProperty("content-Type","application/json");
      httpURLConnection.setDoOutput(true);

      String parameter = "{\n"
         + "  \"Sort\": \"GRADE\",\n"
         + "  \"CategoryCode\": "+CategoryCode+",\n"
         + "  \"PageNo\": 1,\n"
         + "  \"SortCondition\": \"DESC\"\n"
         + "}";
      byte[] out = parameter.getBytes(StandardCharsets.UTF_8);

      OutputStream stream = httpURLConnection.getOutputStream();
      stream.write(out);

      int result = httpURLConnection.getResponseCode();

      InputStream inputStream;
      if(result == 200) {
         inputStream = httpURLConnection.getInputStream();
      } else {
         inputStream = httpURLConnection.getErrorStream();
      }
      InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

      JSONParser parser = new JSONParser();
      JSONObject object = (JSONObject) parser.parse(inputStreamReader);
      httpURLConnection.disconnect();
      return object;
   } catch (MalformedURLException e) {
      throw new RuntimeException(e);
   } catch (ProtocolException e) {
      throw new RuntimeException(e);
   } catch (IOException e) {
      throw new RuntimeException(e);
   } catch (ParseException e) {
      throw new RuntimeException(e);
   }
}

 

카테고리 코드를 매개변수로 넣으면 거래소에서 카테고리에 맞는 데이터를 출력해주는 메소드이다.
낚시 전리품의 카테고리 코드는 90600 이다.

 

 

2 - 2. 데이터 변환

거래소에서 받아온 데이터에는 묶음 별 가격으로 되있기 때문에 원할한 계산을 위해
1개당 가격을 만들어서 JSONObject에 넣어준다

public JSONObject CompareFishing() {
   JSONObject result = marketApiService.CallMarketCategories(90600);
   JSONArray resultJsonArray = new JSONArray();
   resultJsonArray = (JSONArray) result.get("Items");
   JSONObject tempJson = new JSONObject();
   for(int i = 0; i < resultJsonArray.size(); i++) {
      tempJson = (JSONObject) resultJsonArray.get(i);
      double RecentPrice = Double.parseDouble(tempJson.get("RecentPrice").toString());
      double BundleCount = Double.parseDouble(tempJson.get("BundleCount").toString());
      double one = RecentPrice/BundleCount;
      tempJson.put("one",one);
      ((JSONObject) resultJsonArray.get(i)).put("one", one);
   }
   System.out.println(resultJsonArray.toString());
   result.put("Items",resultJsonArray);
   return result;
}

 

2 - 3. 출력

 

2 - 4. 오레하 제작 비용 계산

public double GetMakePrice(JSONObject result, String makeName) {
   JSONArray jsonArray = new JSONArray();
   jsonArray = (JSONArray) result.get("Items");
   double makePrice = 0.0;
   int carp = 0;
   int pearl = 0;
   int fish = 0;
   int price = 0;
   if(makeName.equals("최상급")) {
      carp = 52;
      pearl = 96;
      fish = 142;
      price = 285;
   }
   if(makeName.equals("상급")) {
      carp = 16;
      pearl = 64;
      fish = 128;
      price = 237;
   }
   if(makeName.equals("중급")) {
      carp = 10;
      pearl = 40;
      fish = 80;
      price = 194;
   }
   JSONObject tempJson = new JSONObject();
   for (int i=0; i <jsonArray.size(); i++) {
      tempJson = (JSONObject) jsonArray.get(i);
      String Name = tempJson.get("Name").toString();
      double one = Double.parseDouble(tempJson.get("one").toString());
      switch (Name) {
         case "오레하 태양 잉어" :
            makePrice += one * carp;
            break;
         case "자연산 진주" :
            makePrice += one * pearl;
            break;
         case "생선" :
            makePrice += one * fish;
            break;
      }
   }
   makePrice += price;
   makePrice = Math.round(makePrice * 100.0) / 100.0;
   //System.out.println(makeName+" 제작 비용 = "+makePrice);
   return makePrice;
}

 

앞서 얻은 낚시 재료 JSONObject 데이터와 제작 이름을 매개변수로 넣으면
총 제작 비용을 출력하는 메서드 이다.

 

 

 

2 - 5. 오레하 가격 호출

다음은 거래소에서 각각의 오레하 가격을 가져온다.

public double GetMarketItem(String itemId) {
   try {
      URL url = new URL("https://developer-lostark.game.onstove.com/markets/items/"+itemId);
      HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); // 서버 연결
      httpURLConnection.setRequestMethod("GET");
      httpURLConnection.setRequestProperty("authorization", "Bearer "+LostarkApiKey);
      httpURLConnection.setRequestProperty("accept","application/json");
      httpURLConnection.setRequestProperty("content-Type","application/json");
      httpURLConnection.setDoOutput(true);
      int result = httpURLConnection.getResponseCode();

      InputStream inputStream;
      if(result == 200) {
         inputStream = httpURLConnection.getInputStream();
      } else {
         inputStream = httpURLConnection.getErrorStream();
      }
      InputStreamReader inputStreamReader = new InputStreamReader(inputStream);

      JSONParser parser = new JSONParser();
      JSONArray jsonArray = new JSONArray();
      jsonArray = (JSONArray) parser.parse(inputStreamReader);
      JSONObject object = (JSONObject) jsonArray.get(0);
      jsonArray = (JSONArray) object.get("Stats");
      object = (JSONObject) jsonArray.get(0);
      httpURLConnection.disconnect();
      return Double.parseDouble(object.get("AvgPrice").toString());
   } catch (MalformedURLException e) {
      throw new RuntimeException(e);
   } catch (IOException | ParseException e) {
      throw new RuntimeException(e);
   }
}

 

날짜별로 데이터가 정렬되어 있는데 편의상 가장 최근(오늘) 날짜의 평균가격을 가져온다.
제작 비용과 비교해야 하기 때문에 각각에 데이터에 제작 갯수를 곱해서 화면에 출력한다.

 

 

 

3. 결과 및 후기

프론트단의 전체 화면은 아래와 같다.

 

  • 대성공이 뜨면 다 이득이긴하다.
  • 최상급 오레하는 사는 것보다 제작해서 쓰는게 이득이다.
  • 상급과 중급은 만들어서 쓰는 것보단 사는게 이득이고 만들어서 파는 것보단 재료자체를 파는게 더 낫다.

Spring은 아직 익숙치않아서 코드가 난잡해졌는데 계속 개선을 해봐야겠다.