Skip to content

Commit

Permalink
[VoteUp 2] 完成初步重写.
Browse files Browse the repository at this point in the history
本次 commit 共包含以下内容:
- 修复了一些 Bug。
- 完成了发布前的 TODO 清单。
- 优化代码。
  • Loading branch information
Polar-Pumpkin committed May 7, 2020
1 parent 17b2263 commit adfde17
Show file tree
Hide file tree
Showing 13 changed files with 117 additions and 71 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion VoteUp/VoteUp.iml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<orderEntry type="library" scope="PROVIDED" name="Maven: net.md-5:bungeecord-chat:1.13-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.yaml:snakeyaml:1.23" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: org.projectlombok:lombok:1.18.8" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.github.Polar-Pumpkin:ParrotX:462e78c3d2" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.github.Polar-Pumpkin:ParrotX:f0f4340df0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.scireum:parsii:1.5" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Maven: com.github.WesJD.AnvilGUI:anvilgui:99a504a3bb" level="project" />
</component>
Expand Down
2 changes: 1 addition & 1 deletion VoteUp/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
<dependency>
<groupId>com.github.Polar-Pumpkin</groupId>
<artifactId>ParrotX</artifactId>
<version>462e78c3d2</version>
<version>f0f4340df0</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
1 change: 1 addition & 0 deletions VoteUp/src/main/java/net/shoal/sir/voteup/VoteUp.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public void load() {
@Override
public void onDisable() {
VoteUpAPI.VOTE_MANAGER.saveAll();
VoteUpAPI.CACHE_MANAGER.save();
super.onDisable();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,14 @@ public void save() {
super.save();
}

public void log(Notice.Type type, String voteID, Map<String, Object> params) {
public Notice log(Notice.Type type, String voteID, Map<String, Object> params) {
Map<Integer, Notice> noticeMap = notices.getOrDefault(voteID, new HashMap<>());
int number = noticeMap.size() + 1;
while (noticeMap.containsKey(number)) number++;
noticeMap.put(number, new Notice(type, voteID, number, params));
Notice notice = new Notice(type, voteID, number, params);
noticeMap.put(number, notice);
notices.put(voteID, noticeMap);
return notice;
}

public void report(Notice.Type type, @NonNull Player user) {
Expand All @@ -85,14 +87,17 @@ public void report(Notice.Type type, @NonNull Player user) {
(number, notice) -> {
String append = notice.announce(user.getUniqueId());
if (append != null) content.add(append);
if (notice.isOver()) map.remove(number);
}
)
);
if (content.isEmpty()) return;

TextComponent text = JsonChatUtil.getFromLegacy(
plugin.lang.get(plugin.localeKey, I18n.Type.INFO, "Vote", "Notice." + type.name() + ".Head")
.replace("%amount%", String.valueOf(content.size()))
);

Iterator<String> iterator = content.iterator();
while (iterator.hasNext()) {
hover.append(iterator.next());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public enum Path {
AUTOCAST_BLACKLIST("Autocast.Blacklist"),
AUTOCAST_LIST("Autocast.List"),
ADMIN("Admin"),
SETTINGS_PASSLEAST_AGREE("Settings.PassLeast.Agree"),
SETTINGS_PASSLEAST_REFUSE("Settings.PassLeast.Refuse"),
SETTINGS_PARTICIPANT_LEAST("Settings.ParticipantLeast"),
SETTINGS_BROADCAST_TITLE_VOTESTART("Settings.Broadcast.Title.VoteStart"),
SETTINGS_BROADCAST_TITLE_VOTEEND("Settings.Broadcast.Title.VoteEnd"),
SETTINGS_BROADCAST_TITLE_FADEIN("Settings.Broadcast.Title.FadeIn"),
Expand All @@ -38,6 +37,7 @@ public enum Path {
;

public final String path;

Path(String path) {
this.path = path;
}
Expand Down
71 changes: 39 additions & 32 deletions VoteUp/src/main/java/net/shoal/sir/voteup/config/VoteManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public void load(@NonNull File file) {
}

private void startCountdown(String voteID) {
// TODO 这里有一个 Bug, 重载配置文件会立刻结束所有投票, 目测是时长解析出了问题
Vote data = voteMap.getOrDefault(voteID, null);
if (data == null) return;
if (!data.open) return;
Expand All @@ -67,7 +66,7 @@ public Vote getVote(String id) {
}

public Vote create(UUID uuid) {
Vote vote = new Vote(plugin.pConfig.getConfig().getInt(ConfigManager.Path.SETTINGS_PASSLEAST_AGREE.path, 3), uuid, "1d");
Vote vote = new Vote(plugin.pConfig.getConfig().getInt(ConfigManager.Path.SETTINGS_PARTICIPANT_LEAST.path, 3), uuid, "1d");
voteMap.put(vote.voteID, vote);
return vote;
}
Expand Down Expand Up @@ -106,13 +105,14 @@ public void start(@NonNull Player user) {
vote.isDraft = false;

VoteUpAPI.SOUND.voteEvent(true);
BasicUtil.broadcastTitle(
"",
VoteUpPlaceholder.parse(vote, plugin.lang.getRaw(plugin.localeKey, "Vote", "Event.Start.Subtitle")),
plugin.pConfig.getConfig().getInt(ConfigManager.Path.SETTINGS_BROADCAST_TITLE_FADEIN.path, 5),
plugin.pConfig.getConfig().getInt(ConfigManager.Path.SETTINGS_BROADCAST_TITLE_STAY.path, 10),
plugin.pConfig.getConfig().getInt(ConfigManager.Path.SETTINGS_BROADCAST_TITLE_FADEOUT.path, 7)
);
if (plugin.pConfig.getConfig().getBoolean(ConfigManager.Path.SETTINGS_BROADCAST_TITLE_VOTESTART.path, true))
BasicUtil.broadcastTitle(
"",
VoteUpPlaceholder.parse(vote, plugin.lang.getRaw(plugin.localeKey, "Vote", "Event.Start.Subtitle")),
plugin.pConfig.getConfig().getInt(ConfigManager.Path.SETTINGS_BROADCAST_TITLE_FADEIN.path, 5),
plugin.pConfig.getConfig().getInt(ConfigManager.Path.SETTINGS_BROADCAST_TITLE_STAY.path, 10),
plugin.pConfig.getConfig().getInt(ConfigManager.Path.SETTINGS_BROADCAST_TITLE_FADEOUT.path, 7)
);
Bukkit.getOnlinePlayers().forEach(player -> player.spigot().sendMessage(JsonChatUtil.buildClickText(
VoteUpPlaceholder.parse(vote, plugin.lang.get(plugin.localeKey, I18n.Type.INFO, "Vote", "Event.Start.Broadcast")),
new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/vote view " + vote.voteID),
Expand Down Expand Up @@ -144,31 +144,27 @@ public void vote(String voteID, @NonNull Player user, Vote.Choice choice, String
I18n.send(user, plugin.lang.get(plugin.localeKey, I18n.Type.INFO, "Vote", "Vote." + choice.name()));

Player starter = Bukkit.getPlayer(vote.owner);
if (starter != null && starter.isOnline()) {
String noticeMsg = plugin.lang.get(plugin.localeKey, I18n.Type.INFO, "Vote", "Voted.Starter")
.replace("%Voter%", user.getName())
.replace("%Choice%", I18n.color(vote.choices.getOrDefault(choice, BuiltinMsg.ERROR_GET_CHOICE.msg)))
.replace("%Reason%", I18n.color(reason));
// TODO 修复这个提示信息无法发出的问题
I18n.send(starter, VoteUpPlaceholder.parse(vote, noticeMsg));
} else VoteUpAPI.CACHE_MANAGER.log(Notice.Type.VOTE, voteID, new HashMap<String, Object>() {
Notice notice = VoteUpAPI.CACHE_MANAGER.log(Notice.Type.VOTE, voteID, new HashMap<String, Object>() {
{
put("Voter", user.getName());
put("Choice", choice.name());
put("Reason", reason);
}
});

// TODO 管理员不在线时的提醒挂起规则
if (starter != null && starter.isOnline()) {
String announce = notice.announce(user.getUniqueId());
if (announce != null)
I18n.send(starter, VoteUpPlaceholder.parse(vote, announce));
}

plugin.pConfig.getConfig().getStringList(ConfigManager.Path.ADMIN.path).forEach(
adminID -> {
Player admin = Bukkit.getPlayer(UUID.fromString(adminID));
if (admin != null) {
String noticeMsg = plugin.lang.get(plugin.localeKey, I18n.Type.INFO, "Vote", "Voted.Noticer")
.replace("%Voter%", user.getName())
.replace("%Choice%", I18n.color(vote.choices.getOrDefault(choice, BuiltinMsg.ERROR_GET_CHOICE.msg)))
.replace("%Reason%", I18n.color(reason));
I18n.send(admin, VoteUpPlaceholder.parse(vote, noticeMsg));
String announce = notice.announce(admin.getUniqueId());
if (announce != null)
I18n.send(admin, VoteUpPlaceholder.parse(vote, announce));
}
}
);
Expand All @@ -183,16 +179,27 @@ public void endVote(String voteID) {

if (vote.isPassed()) vote.autocast.forEach(cmd -> Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd));
VoteUpAPI.SOUND.voteEvent(false);
BasicUtil.broadcastTitle(
"",
VoteUpPlaceholder.parse(vote, plugin.lang.getRaw(plugin.localeKey, "Vote", "Event.End.Subtitle")),
plugin.pConfig.getConfig().getInt(ConfigManager.Path.SETTINGS_BROADCAST_TITLE_FADEIN.path, 5),
plugin.pConfig.getConfig().getInt(ConfigManager.Path.SETTINGS_BROADCAST_TITLE_STAY.path, 10),
plugin.pConfig.getConfig().getInt(ConfigManager.Path.SETTINGS_BROADCAST_TITLE_FADEOUT.path, 7)
);
if (plugin.pConfig.getConfig().getBoolean(ConfigManager.Path.SETTINGS_BROADCAST_TITLE_VOTEEND.path, false))
BasicUtil.broadcastTitle(
"",
VoteUpPlaceholder.parse(vote, plugin.lang.getRaw(plugin.localeKey, "Vote", "Event.End.Subtitle")),
plugin.pConfig.getConfig().getInt(ConfigManager.Path.SETTINGS_BROADCAST_TITLE_FADEIN.path, 5),
plugin.pConfig.getConfig().getInt(ConfigManager.Path.SETTINGS_BROADCAST_TITLE_STAY.path, 10),
plugin.pConfig.getConfig().getInt(ConfigManager.Path.SETTINGS_BROADCAST_TITLE_FADEOUT.path, 7)
);
BasicUtil.broadcast(VoteUpPlaceholder.parse(vote, plugin.lang.get(plugin.localeKey, I18n.Type.INFO, "Vote", "Event.End.Broadcast")));
// TODO 管理员不在线时的提醒挂起规则
VoteUpAPI.CACHE_MANAGER.log(Notice.Type.VOTE_END, voteID, new HashMap<>());

Notice notice = VoteUpAPI.CACHE_MANAGER.log(Notice.Type.VOTE_END, voteID, new HashMap<>());
plugin.pConfig.getConfig().getStringList(ConfigManager.Path.ADMIN.path).forEach(
adminID -> {
Player admin = Bukkit.getPlayer(UUID.fromString(adminID));
if (admin != null) {
String announce = notice.announce(admin.getUniqueId());
if (announce != null)
I18n.send(admin, VoteUpPlaceholder.parse(vote, announce));
}
}
);
}
}

Expand Down
8 changes: 8 additions & 0 deletions VoteUp/src/main/java/net/shoal/sir/voteup/data/Notice.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import net.shoal.sir.voteup.VoteUp;
import net.shoal.sir.voteup.api.VoteUpAPI;
import net.shoal.sir.voteup.api.VoteUpPlaceholder;
import net.shoal.sir.voteup.config.ConfigManager;
import org.bukkit.configuration.ConfigurationSection;
import org.serverct.parrot.parrotx.PPlugin;
import org.serverct.parrot.parrotx.data.flags.Timestamp;
Expand Down Expand Up @@ -82,6 +83,13 @@ public String announce(UUID uuid) {
return result;
}

public boolean isOver() {
List<String> admins = plugin.pConfig.getConfig().getStringList(ConfigManager.Path.ADMIN.path);
List<String> announced = new ArrayList<>();
this.announced.forEach(uuid -> announced.add(uuid.toString()));
return announced.equals(admins);
}

@Override
public long getTimestamp() {
return time;
Expand Down
52 changes: 29 additions & 23 deletions VoteUp/src/main/java/net/shoal/sir/voteup/data/Vote.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.NonNull;
import net.shoal.sir.voteup.VoteUp;
import net.shoal.sir.voteup.api.VoteUpAPI;
import net.shoal.sir.voteup.config.ConfigManager;
import net.shoal.sir.voteup.enums.BuiltinMsg;
import org.bukkit.ChatColor;
import org.bukkit.configuration.ConfigurationSection;
Expand Down Expand Up @@ -89,7 +90,7 @@ public static long getDurationTimestamp(String duration) {
long result = 0;

String clone = duration.toUpperCase();
Vote.Duration durationType;
Duration durationType;
while ((durationType = getFirstIndexOf(clone)) != null) {
int index = clone.indexOf(durationType.code);
try {
Expand All @@ -105,10 +106,10 @@ public static long getDurationTimestamp(String duration) {
return result * 1000;
}

public static Vote.Duration getFirstIndexOf(String target) {
public static Duration getFirstIndexOf(String target) {
int index = Integer.MAX_VALUE;
Vote.Duration durationType = null;
for (Vote.Duration type : Vote.Duration.values()) {
Duration durationType = null;
for (Duration type : Duration.values()) {
int currentIndex = target.indexOf(type.code);
if (currentIndex == -1) continue;
if (currentIndex < index) {
Expand Down Expand Up @@ -221,25 +222,30 @@ public Choice getChoice(UUID uuid) {
}

public int getProcess() {
// TODO 考虑最低同意人数对结果的影响
int accept = participants.getOrDefault(Choice.ACCEPT, new HashMap<>()).size();
int neutral = participants.getOrDefault(Choice.NEUTRAL, new HashMap<>()).size();
int refuse = participants.getOrDefault(Choice.REFUSE, new HashMap<>()).size();
int all = accept + neutral + refuse;
int least = plugin.pConfig.getConfig().getInt(ConfigManager.Path.SETTINGS_PARTICIPANT_LEAST.path, 3);

int rate;
switch (type) {
case NORMAL:
rate = (int) ((accept / (double) (accept + neutral + refuse)) * 100);
break;
case REACHAMOUNT:
rate = (int) ((accept / (double) goal) * 100);
break;
case LEASTNOT:
rate = (int) ((Math.max(goal - refuse, 0) / (double) goal) * 100);
break;
default:
rate = 0;
break;
}
if (all >= least) {
switch (type) {
case NORMAL:
rate = (int) ((accept / (double) all) * 100);
break;
case REACHAMOUNT:
rate = (int) ((accept / (double) goal) * 100);
break;
case LEASTNOT:
rate = (int) ((Math.max(goal - refuse, 0) / (double) goal) * 100);
break;
default:
rate = 0;
break;
}
} else rate = (int) ((all / (double) least) * 100);

return rate;
}

Expand Down Expand Up @@ -321,7 +327,7 @@ public void load(@NonNull File file) {

ConfigurationSection setting = data.getConfigurationSection("Settings");
if (setting != null) {
this.title = I18n.color(setting.getString("Title", "&7" + getOwnerName() + " 的投票"));
this.title = I18n.color(setting.getString("Title", getOwnerName() + " 的投票"));
this.description = setting.getStringList("Description");
this.description.replaceAll(I18n::color);
}
Expand All @@ -338,7 +344,7 @@ public void load(@NonNull File file) {

this.autocast = setting.getStringList("Autocast");

ConfigurationSection resultSection = setting.getConfigurationSection("Results");
ConfigurationSection resultSection = setting.getConfigurationSection("Results"); // 这里有一个全都是 null 的问题
this.results = new HashMap<>();
if (resultSection != null) {
for (String resultKey : resultSection.getKeys(false)) {
Expand Down Expand Up @@ -496,10 +502,10 @@ public enum Result {

public enum Duration {
// yyyy-MM-dd HH:mm:ss
DAY('d', "天", 86400),
DAY('D', "天", 86400),
HOUR('H', "时", 3600),
MINUTE('M', "分", 60),
SECOND('s', "秒", 1);
SECOND('S', "秒", 1);

public final char code;
public final String name;
Expand Down
Loading

0 comments on commit adfde17

Please sign in to comment.