mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-30 06:32:32 -04:00
带有”保底“机制的随机”小贴士“生成算法
This commit is contained in:
parent
b7d538c1e9
commit
864f38f8b5
@ -13,40 +13,41 @@
|
|||||||
* GNU General Public License for more details.
|
* GNU General Public License for more details.
|
||||||
*
|
*
|
||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* along with this program. If not, see <https://www.gnu.org/licenses/ >.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
package org.jackhuang.hmcl.util;
|
package org.jackhuang.hmcl.util;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.*;
|
||||||
|
|
||||||
import java.util.stream.IntStream;
|
import java.util.stream.IntStream;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.concurrent.ThreadLocalRandom;
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
|
import static org.jackhuang.hmcl.util.i18n.I18n.i18n;
|
||||||
|
|
||||||
public class RandomTip {
|
public final class RandomTip {
|
||||||
|
|
||||||
private static final List<String> tips;
|
private static final List<String> tips;
|
||||||
private static final int maxTipNumber = 30;
|
private static final int maxTipNumber = 30;
|
||||||
|
|
||||||
|
private static final GuaranteedRandomIndex indexGenerator;
|
||||||
|
|
||||||
static {
|
static {
|
||||||
// Initialization tips list
|
// Initialization tips list
|
||||||
tips = IntStream.rangeClosed(1, maxTipNumber)
|
tips = IntStream.rangeClosed(1, maxTipNumber)
|
||||||
.mapToObj(i -> i18n(String.format("message.tips_%s", i)))
|
.mapToObj(i -> i18n(String.format("message.tips_%s", i)))
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
indexGenerator = new GuaranteedRandomIndex(tips.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getRandomTip() {
|
public static String getRandomTip() {
|
||||||
String tip = tips.get(getRandomTipIndex());
|
String tip = tips.get(indexGenerator.next());
|
||||||
return formatTip(tip);
|
return formatTip(tip);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getRandomTip(String previous) {
|
public static String getRandomTip(String previous) {
|
||||||
String tip;
|
return getRandomTip();
|
||||||
do {
|
|
||||||
tip = tips.get(getRandomTipIndex());
|
|
||||||
} while (tips.size() > 1 && tip.equals(previous));
|
|
||||||
return formatTip(tip);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private static String formatTip(String tip) {
|
private static String formatTip(String tip) {
|
||||||
@ -75,7 +76,35 @@ public class RandomTip {
|
|||||||
return formattedTip.toString();
|
return formattedTip.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static int getRandomTipIndex() {
|
private static final class GuaranteedRandomIndex {
|
||||||
return ThreadLocalRandom.current().nextInt(tips.size());
|
private final List<Integer> indices;
|
||||||
|
private final Random random;
|
||||||
|
private int cursor;
|
||||||
|
|
||||||
|
GuaranteedRandomIndex(int size) {
|
||||||
|
this.indices = new ArrayList<>(size);
|
||||||
|
for (int i = 0; i < size; i++) {
|
||||||
|
indices.add(i);
|
||||||
}
|
}
|
||||||
|
this.random = new Random();
|
||||||
|
shuffle();
|
||||||
|
this.cursor = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void shuffle() {
|
||||||
|
for (int i = indices.size() - 1; i > 0; i--) {
|
||||||
|
int j = random.nextInt(i + 1);
|
||||||
|
Collections.swap(indices, i, j);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int next() {
|
||||||
|
if (cursor >= indices.size()) {
|
||||||
|
shuffle();
|
||||||
|
cursor = 0;
|
||||||
|
}
|
||||||
|
return indices.get(cursor++);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private RandomTip() {}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user