Skip to content

Commit

Permalink
- 修复同步游戏的混乱情况发生
Browse files Browse the repository at this point in the history
- 现在welcomeMsg很明显可以使用了,你可以使用如下tag来自定义你的欢迎信息:
{playerName}: 玩家名称
{simpleUUID}: 玩家UUID前八位
{packageName}: 玩家游戏的包名
{versionCode}: 玩家的游戏版本
- 更新相关依赖并更新 yaml 有关内容
- 修复启动时读取lang时造成的启动失败问题
- 修复 TestPlugin 中的单位生成指令的 bug
- 修复同步游戏同步到重连游戏的玩家导致整盘游戏重开的问题
- 修复房间玩家地图信息的错乱问题
- 增加房间玩家发送 info 不同步消息时自动进行同步的功能
- 修复共享控制功能,增加玩家离线启用共享控制功能
- 增加 API: NetworkPlayer.sendTeamMessage(String msg)
- 调整部分日志级别至 trace 以简化输出
- 修复 isDebug 为 true 时日志不输出 debug 级别日志的问题
- Fixed the sync game problem.
- welcomeMsg in rukkit.yml can be used, you can use this following tags to customize your server's welcome message.
 {playerName}, {simpleUUID}(first 8 degit of player's uuid), {packageName}(Player client's package name), {versionCode}(Player client's version code)
 - updated dependence, and ported the new version of SnakeYaml.
 - Fixed when starting the server, server will be broken due to the bad lang format in rukkit.yml.
 - Fixed the sharing control not working problem.
 - Fixed the conflict round info between the room.
 - Fixed when a player reconnected the game, the game have the possibility the sync with the reconnecting player's save, which lead to restart the game.
 - Fixed the summon command bugs.
 - Added the ability when player send a info, the server will start sync automatically.
 - Add API NetworkPlayer.sendTeamMessage
 - Changed some log level to trace
 - Fixed when isDebug = true, the server would not enable the debug log.
  • Loading branch information
DroppingStar committed Jun 21, 2024
1 parent 200418c commit dc26190
Show file tree
Hide file tree
Showing 23 changed files with 216 additions and 112 deletions.
24 changes: 19 additions & 5 deletions src/main/java/cn/rukkit/Rukkit.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

package cn.rukkit;
import ch.qos.logback.classic.Level;
import cn.rukkit.command.*;
import cn.rukkit.config.*;
import cn.rukkit.game.NetworkPlayer;
Expand All @@ -23,13 +24,14 @@
import cn.rukkit.plugin.internal.*;
import cn.rukkit.service.*;
import cn.rukkit.game.SaveManager;
import org.yaml.snakeyaml.nodes.Tag;

import java.util.Locale;
import java.util.UUID;

public class Rukkit {
private static boolean isStarted = false;
public static final String RUKKIT_VERSION = "0.9.3-dev";
public static final String RUKKIT_VERSION = "0.9.4-dev";
public static final int SUPPORT_GAME_VERSION = 176;
private static final Logger log = LoggerFactory.getLogger(Rukkit.class);
private static RoundConfig round;
Expand Down Expand Up @@ -157,7 +159,7 @@ public static void loadRukkitConfig() throws IOException {
FileWriter writer = new FileWriter(confFile);
RukkitConfig conf = new RukkitConfig();
conf.UUID = UUID.randomUUID().toString();
writer.write(yaml.dumpAs(conf, null, DumperOptions.FlowStyle.BLOCK));
writer.write(yaml.dumpAs(conf, Tag.MAP, DumperOptions.FlowStyle.BLOCK));
writer.flush();
writer.close();
}
Expand All @@ -175,7 +177,7 @@ public static final void loadRoundConfig() throws IOException {
confFile.delete();
confFile.createNewFile();
FileWriter writer = new FileWriter(confFile);
writer.write(yaml.dumpAs(new RoundConfig(), null, DumperOptions.FlowStyle.BLOCK));
writer.write(yaml.dumpAs(new RoundConfig(), Tag.MAP, DumperOptions.FlowStyle.BLOCK));
writer.flush();
writer.close();
}
Expand All @@ -192,7 +194,7 @@ public static final <T> T getConfig(String path, Class<T> cls) throws FileNotFou
confFile.delete();
confFile.createNewFile();
FileWriter writer = new FileWriter(confFile);
writer.write(yaml.dumpAs(cls, null, DumperOptions.FlowStyle.BLOCK));
writer.write(yaml.dumpAs(cls, Tag.MAP, DumperOptions.FlowStyle.BLOCK));
writer.flush();
writer.close();
}
Expand Down Expand Up @@ -244,10 +246,22 @@ public static final void startServer() throws IOException, InterruptedException

log.info("load::RukkitConfig..."); // 加载配置文件
loadRukkitConfig();
if (config.isDebug) {
log.info("debug::Change log level to debug...");
ch.qos.logback.classic.Logger root = (ch.qos.logback.classic.Logger) org.slf4j.LoggerFactory
.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
root.setLevel(Level.DEBUG);

}
log.info("load::RoundConfig...");
loadRoundConfig();
log.info("load::Language..."); // 加载语言文件
LangUtil.lc = new Locale(getConfig().lang.split("_")[0], getConfig().lang.split("_")[1]);
String[] lang_format = getConfig().lang.split("_");
if (lang_format.length < 2) {
log.warn("Invalid Language configuration {} detected, we will use system default language. Please check your rukkit.yml.", getConfig().lang);
} else {
LangUtil.lc = new Locale(lang_format[0], lang_format[1]);
}
log.info("Current Language: {}", LangUtil.lc);
//init SaveManager.
log.info("load::DefaultSaveData..."); // 加载保存文件
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/cn/rukkit/RukkitLauncher.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,14 @@ public static void main(String args[]){
Rukkit.startServer();
while (isTerminalRunning) {
try {
String str = lineReader.readLine(PATTERN);
Thread.sleep(1); // 不知道为什么反正要这个东西
if (Rukkit.getCommandManager() == null) continue;
if (Rukkit.isStarted()) {
serverCommandCompleter.setCommandCompleteVars(Rukkit.getCommandManager().getLoadedServerCommandStringList());
} else {
continue;
}
String str = lineReader.readLine(PATTERN);
Rukkit.getCommandManager().executeServerCommand(str);
}
catch (UserInterruptException e) {
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/cn/rukkit/command/completer/PlayerCompleter.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,19 @@

package cn.rukkit.command.completer;

import org.jline.reader.Candidate;
import org.jline.reader.Completer;
import org.jline.reader.LineReader;
import org.jline.reader.ParsedLine;
import org.jline.reader.impl.completer.StringsCompleter;

import java.util.List;

public class PlayerCompleter extends StringsCompleter {

Completer completer;

@Override
public void complete(LineReader reader, ParsedLine commandLine, List<Candidate> candidates) {
StringsCompleter stringsCompleter = new StringsCompleter();
}
}
3 changes: 2 additions & 1 deletion src/main/java/cn/rukkit/config/BaseConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.yaml.snakeyaml.*;
//import java.util.logging.*;
import org.slf4j.*;
import org.yaml.snakeyaml.nodes.Tag;

public abstract class BaseConfig
{
Expand All @@ -32,7 +33,7 @@ public BaseConfig loadConfig() throws IllegalAccessException, InstantiationExcep
confFile.createNewFile();
BaseConfig cfg = this.getClass().newInstance();
FileWriter writer = new FileWriter(confFile);
writer.write(new Yaml().dumpAs(cfg, null, DumperOptions.FlowStyle.BLOCK));
writer.write(new Yaml().dumpAs(cfg, Tag.MAP, DumperOptions.FlowStyle.BLOCK));
writer.flush();
writer.close();
return cfg;
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/cn/rukkit/config/RukkitConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class RukkitConfig extends BaseConfig
{
public String serverUser = "RUKKIT";
public String welcomeMsg = "Welcome to Rukkit server, {user}!";
public String welcomeMsg = "Welcome to Rukkit server, {playerName}!";
public String serverMotd = "My Rukkit server";
public int serverPort = 5123;
public int maxPlayer = 10;
Expand All @@ -30,6 +30,10 @@ public class RukkitConfig extends BaseConfig
public String lang = Locale.getDefault().toString();
//max threads in manager.Default = 8;
public int threadPoolCount = 8;
// max unit in per player 单玩家最大单位
public int maxUnitsPerPlayer = 250;
// using question system to vote 投票系统使用提示框模式实现
// public boolean usingPopupInVote = false;

//Ping packet receive timeout.default = 8000 (ms)
public int pingTimeout = 8000;
Expand Down
36 changes: 26 additions & 10 deletions src/main/java/cn/rukkit/game/NetworkPlayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@
import cn.rukkit.*;
import cn.rukkit.network.*;
import cn.rukkit.network.packet.Packet;
import cn.rukkit.util.LangUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;
import org.yaml.snakeyaml.nodes.Tag;

import java.io.*;
import java.nio.charset.StandardCharsets;
Expand Down Expand Up @@ -47,6 +50,9 @@ public class NetworkPlayer
public boolean isSharingControl = false;

public boolean isSurrounded = false;
public boolean isDisconnected = false;

public boolean isAfk = false;
private NetworkRoom room;

public NetworkPlayer(RoomConnection connection) {
Expand Down Expand Up @@ -126,10 +132,10 @@ public void putExtraData(String key, Object value) {
* Save player data.
*/
public void savePlayerData() {
Yaml yaml = new Yaml(new Constructor(NetworkPlayerData.class));
Yaml yaml = new Yaml(new Constructor(NetworkPlayerData.class, new LoaderOptions()));
try {
FileWriter writer = new FileWriter(Rukkit.getEnvPath() + "/data/player/" + uuid + ".yaml");
writer.write(yaml.dumpAs(data, null, DumperOptions.FlowStyle.BLOCK));
writer.write(yaml.dumpAs(data, Tag.MAP, DumperOptions.FlowStyle.BLOCK));
writer.flush();
writer.close();
} catch (FileNotFoundException e) {
Expand All @@ -143,8 +149,8 @@ public void writePlayer(DataOutputStream stream, boolean simpleMode) throws IOEx
if (simpleMode) {
stream.writeByte(0);
stream.writeInt(ping);
stream.writeBoolean(true);
stream.writeBoolean(true);
stream.writeBoolean(isSharingControl);
stream.writeBoolean(isDisconnected || isAfk);
} else {
//玩家位置
stream.writeByte(playerIndex);
Expand Down Expand Up @@ -238,6 +244,16 @@ public void updateServerInfo() {
} catch (IOException e) {}
}

public void sendTeamMessage(String message) {
for (RoomConnection conn: room.connectionManager.getConnections()) {
if (team == conn.player.team) {
conn.sendMessage(name,
LangUtil.getString("chat.teamMsg") + " " + message,
playerIndex);
}
}
}

@Override
public String toString() {
return "NetworkPlayer{" +
Expand Down Expand Up @@ -271,30 +287,30 @@ public static final void initPlayerDataDir() {

public void loadPlayerData() {
Logger log = LoggerFactory.getLogger("PlayerData");
log.info("Load player infomation.");
Yaml yaml = new Yaml(new Constructor(NetworkPlayerData.class));
log.debug("Load player infomation.");
Yaml yaml = new Yaml(new Constructor(NetworkPlayerData.class, new LoaderOptions()));
File dataFile = new File(Rukkit.getEnvPath() + "/data/player/" + uuid + ".yaml");
try {
if (dataFile.exists()) {
log.info("Player exists.Loading...");
log.debug("Player exists.Loading...");
data = yaml.load(new FileInputStream(dataFile));
data.lastUsedName = name;
data.lastConnectedTime = new Date().toString();
data.lastConnectedAddress = connection.handler.ctx.channel().remoteAddress().toString();
Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dataFile), StandardCharsets.UTF_8));
writer.write(yaml.dumpAs(data, null, DumperOptions.FlowStyle.BLOCK));
writer.write(yaml.dumpAs(data, Tag.MAP, DumperOptions.FlowStyle.BLOCK));
writer.flush();
writer.close();
} else {
log.info("New player.Creating file...");
log.info("New player.Creating data file...");
dataFile.createNewFile();
data = new NetworkPlayerData();
data.uuid = uuid;
data.lastUsedName = name;
data.lastConnectedTime = new Date().toString();
data.lastConnectedAddress = connection.handler.ctx.channel().remoteAddress().toString();
Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dataFile), StandardCharsets.UTF_8));
writer.write(yaml.dumpAs(data, null, DumperOptions.FlowStyle.BLOCK));
writer.write(yaml.dumpAs(data, Tag.MAP, DumperOptions.FlowStyle.BLOCK));
writer.flush();
writer.close();
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/cn/rukkit/game/PlayerManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public PlayerManager(NetworkRoom room, int maxPlayer) {
reset();
}

private NetworkPlayer[] players;
private volatile NetworkPlayer[] players;
//private static Player[] inGamePlayers = new Player[ServerProperties.maxPlayer];

/**
Expand Down Expand Up @@ -80,6 +80,7 @@ public void remove(int index){
// }
if(currentRoom.isGaming()){
players[index].ping = -1;
players[index].isDisconnected = true;
return;
}
players[index] = new NetworkPlayer();
Expand Down
Loading

0 comments on commit dc26190

Please sign in to comment.