OSDN Git Service

chore: update lib
[delesterandomselector/DelesteRandomSelector.git] / src / com / ranfa / lib / calc / PRPCalc.java
1 /*
2  * Copyright 2022 the original author or authors.
3  *
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
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17
18 package com.ranfa.lib.calc;
19
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;
26
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import com.fasterxml.jackson.core.type.TypeReference;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32
33 /**
34  * PRPの計算ライブラリ。
35  * PRPの保管も兼ねています。
36  * 
37  * @author Ranfa
38  * 
39  *@since 4.0.0
40  */
41 public class PRPCalc {
42         
43         // Logger
44         private final Logger logger = LoggerFactory.getLogger(PRPCalc.class);
45         
46         /**
47          * PRPを保管しているファイルパス
48          */
49         private static final String PRP_STORAGE_FILE_PATH = "generated/prp.json";
50         
51         /**
52          * 計算用のBigDecimal値
53          */
54         private static final BigDecimal THAUSAND = BigDecimal.valueOf(1000);
55         
56         /**
57          * 合計PRP値を計算するために保管しているPRPのList
58          */
59         private List<Integer> TotalPRPList;
60         /**
61          * 合計PRP値
62          */
63         private int TotalPRP;
64
65         /**
66          * コンストラクタ。
67          * <p>
68          * 合計PRP値の算出に必要な値を読み出し、Listへ詰め込むまでを行います。
69          */
70         public PRPCalc() {
71                 if(Files.notExists(Paths.get(PRP_STORAGE_FILE_PATH)))
72                         generateEmptyPRPFile();
73                 TypeReference<List<Integer>> typeref = new TypeReference<List<Integer>>() {};
74                 try {
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);
78                 }
79                 TotalPRP = calcCurrentTotal();
80         }
81         
82         /**
83          * 内容が空のPRP保管ファイルを生成します。
84          * <p>
85          * ファイルを生成するのみで内容の書き込みはしません。
86          * 
87          * @return ファイルの生成に成功した場合は<code>true</code>、それ以外は<code>false</code>
88          */
89         public boolean generateEmptyPRPFile() {
90                 if(Files.notExists(Paths.get("generated")))
91                         try {
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);
96                                 return false;
97                         }
98                 return true;
99         }
100         
101         /**
102          * {@link #TotalPRPList} を参照して現在時点の合計を算出します。
103          * @return 現在時点の合計PRP
104          */
105         public int calcCurrentTotal() {
106                 int res = 0;
107                 for(int val : TotalPRPList)
108                         res += val;
109                 return res;
110         }
111         
112         /**
113          * {@link #TotalPRP} を返します
114          * @return {@link #TotalPRP}の値
115          */
116         public int getTotalPRP() {
117                 return TotalPRP;
118         }
119         
120         /**
121          * 入力されたスコアからPRPを計算します。
122          * <p>
123          * PRPは「端数切捨て(スコア×0.001)」になります
124          * @param score 計算するスコア
125          * @return 入力から計算したPRP値
126          */
127         public static int calcPRPFromScore(int score) {
128                 BigDecimal scoreDecimal = BigDecimal.valueOf(score);
129                 scoreDecimal.divide(THAUSAND, RoundingMode.DOWN);
130                 return scoreDecimal.intValueExact();
131         }
132 }