mirror of
https://github.com/HMCL-dev/HMCL.git
synced 2025-09-27 13:14:42 -04:00
带有”保底“机制的随机”小贴士“生成算法
This commit is contained in:
parent
b7d538c1e9
commit
864f38f8b5
@ -121,7 +121,7 @@ public class TaskExecutorDialogPane extends BorderPane {
|
||||
FileDownloadTask.speedEvent.channel(FetchTask.SpeedEvent.class).registerWeak(speedEventHandler);
|
||||
|
||||
onEscPressed(this, btnCancel::fire);
|
||||
|
||||
|
||||
tipTimeline = new Timeline(new KeyFrame(Duration.seconds(2), e -> nextTip()));
|
||||
tipTimeline.setCycleCount(Animation.INDEFINITE);
|
||||
tipTimeline.play();
|
||||
|
@ -13,40 +13,41 @@
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* 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;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.concurrent.ThreadLocalRandom;
|
||||
|
||||
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 int maxTipNumber = 30;
|
||||
|
||||
private static final GuaranteedRandomIndex indexGenerator;
|
||||
|
||||
static {
|
||||
// Initialization tips list
|
||||
tips = IntStream.rangeClosed(1, maxTipNumber)
|
||||
.mapToObj(i -> i18n(String.format("message.tips_%s", i)))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
indexGenerator = new GuaranteedRandomIndex(tips.size());
|
||||
}
|
||||
|
||||
public static String getRandomTip() {
|
||||
String tip = tips.get(getRandomTipIndex());
|
||||
String tip = tips.get(indexGenerator.next());
|
||||
return formatTip(tip);
|
||||
}
|
||||
|
||||
public static String getRandomTip(String previous) {
|
||||
String tip;
|
||||
do {
|
||||
tip = tips.get(getRandomTipIndex());
|
||||
} while (tips.size() > 1 && tip.equals(previous));
|
||||
return formatTip(tip);
|
||||
return getRandomTip();
|
||||
}
|
||||
|
||||
private static String formatTip(String tip) {
|
||||
@ -75,7 +76,35 @@ public class RandomTip {
|
||||
return formattedTip.toString();
|
||||
}
|
||||
|
||||
private static int getRandomTipIndex() {
|
||||
return ThreadLocalRandom.current().nextInt(tips.size());
|
||||
private static final class GuaranteedRandomIndex {
|
||||
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