2 * Copyright 2022 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * https://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 package com.ranfa.lib.calc;
20 import java.io.IOException;
21 import java.math.BigDecimal;
22 import java.math.RoundingMode;
23 import java.nio.file.Files;
24 import java.nio.file.Paths;
25 import java.util.List;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 import com.fasterxml.jackson.core.type.TypeReference;
31 import com.fasterxml.jackson.databind.ObjectMapper;
41 public class PRPCalc {
44 private final Logger logger = LoggerFactory.getLogger(PRPCalc.class);
49 private static final String PRP_STORAGE_FILE_PATH = "generated/prp.json";
54 private static final BigDecimal THAUSAND = BigDecimal.valueOf(1000);
57 * 合計PRP値を計算するために保管しているPRPのList
59 private List<Integer> TotalPRPList;
68 * 合計PRP値の算出に必要な値を読み出し、Listへ詰め込むまでを行います。
71 if(Files.notExists(Paths.get(PRP_STORAGE_FILE_PATH)))
72 generateEmptyPRPFile();
73 TypeReference<List<Integer>> typeref = new TypeReference<List<Integer>>() {};
75 TotalPRPList = new ObjectMapper().readValue(Paths.get(PRP_STORAGE_FILE_PATH).toFile(), typeref);
76 } catch (IOException e) {
77 logger.error("Couldn't read prp file from disk.", e);
79 TotalPRP = calcCurrentTotal();
83 * 内容が空のPRP保管ファイルを生成します。
85 * ファイルを生成するのみで内容の書き込みはしません。
87 * @return ファイルの生成に成功した場合は<code>true</code>、それ以外は<code>false</code>
89 public boolean generateEmptyPRPFile() {
90 if(Files.notExists(Paths.get("generated")))
92 Files.createDirectory(Paths.get("generated"));
93 Files.createFile(Paths.get(PRP_STORAGE_FILE_PATH));
94 } catch (IOException e) {
95 logger.error("cannot make prp file.", e);
102 * {@link #TotalPRPList} を参照して現在時点の合計を算出します。
105 public int calcCurrentTotal() {
107 for(int val : TotalPRPList)
113 * {@link #TotalPRP} を返します
114 * @return {@link #TotalPRP}の値
116 public int getTotalPRP() {
121 * 入力されたスコアからPRPを計算します。
123 * PRPは「端数切捨て(スコア×0.001)」になります
124 * @param score 計算するスコア
125 * @return 入力から計算したPRP値
127 public static int calcPRPFromScore(int score) {
128 BigDecimal scoreDecimal = BigDecimal.valueOf(score);
129 scoreDecimal.divide(THAUSAND, RoundingMode.DOWN);
130 return scoreDecimal.intValueExact();