Skip to content

Commit

Permalink
Merge pull request #66 from marmot-z/master
Browse files Browse the repository at this point in the history
新增观战模式退出功能
  • Loading branch information
ainilili committed Mar 25, 2021
2 parents b32dcea + ca6d2b7 commit 438b79c
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ public class User {
public static final User INSTANCE = new User();

/** 是否游戏中 */
private boolean isPlaying = false;
private volatile boolean isPlaying = false;

/** 是否观战中 */
private boolean isWatching = false;
private volatile boolean isWatching = false;

private User() {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public class ClientEventListener_CODE_GAME_WATCH extends ClientEventListener {

@Override
public void call(Channel channel, String wrapData) {
// 退出观战模式后不处理观战请求
if (!User.INSTANCE.isWatching()) {
return;
}

Map<String, Object> wrapMap = MapHelper.parser(wrapData);
ClientEventCode rawCode = ClientEventCode.valueOf(wrapMap.get("code").toString());
Object rawData = wrapMap.get("data");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package org.nico.ratel.landlords.client.event;

import io.netty.channel.Channel;
import org.nico.noson.Noson;
import org.nico.ratel.landlords.client.entity.User;
import org.nico.ratel.landlords.enums.ClientEventCode;
import org.nico.ratel.landlords.enums.ServerEventCode;
import org.nico.ratel.landlords.helper.MapHelper;
import org.nico.ratel.landlords.print.SimplePrinter;
import org.nico.ratel.landlords.print.SimpleWriter;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;

public class ClientEventListener_CODE_GAME_WATCH_SUCCESSFUL extends ClientEventListener {
Expand All @@ -14,6 +20,7 @@ public class ClientEventListener_CODE_GAME_WATCH_SUCCESSFUL extends ClientEventL
"|You are already watching the game. \n" +
"|Room owner: %s. Room current status: %s.\n" +
"+------------------------------------------------\n" +
"(Hint: enter [exit|e] to exit.) \n" +
" ";

@Override
Expand All @@ -26,5 +33,39 @@ public void call(Channel channel, String data) {
// Enter spectator mode
Map<String, Object> map = MapHelper.parser(data);
SimplePrinter.printNotice(String.format(WATCH_SUCCESSFUL_TIPS, map.get("owner"), map.get("status")));

// 监听输入用于退出
// Listen enter event to exit spectator mode
new Thread(() -> registerExitEvent(channel), "exit-spectator-input-thread").start();
}

private void registerExitEvent(Channel channel) {
String enter = SimpleWriter.write("watch");
if ("exit".equalsIgnoreCase(enter) || "e".equalsIgnoreCase(enter)) {
quitWatch(channel);
return;
}

printCommandUsage();
registerExitEvent(channel);
}

private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

private void quitWatch(Channel channel) {
SimplePrinter.printNotice(FORMATTER.format(LocalDateTime.now()) + " Spectating ended. Bye.");
SimplePrinter.printNotice("");
SimplePrinter.printNotice("");

// 修改玩家是否观战状态
User.INSTANCE.setWatching(false);

// 退出观战模式
pushToServer(channel, ServerEventCode.CODE_GAME_WATCH_EXIT);
get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, "");
}

private void printCommandUsage() {
SimplePrinter.printNotice("Enter [exit|e] to exit");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ public enum ServerEventCode implements Serializable{

CODE_GAME_POKER_PLAY_PASS("不出"),

CODE_GAME_WATCH("观战");
CODE_GAME_WATCH("观战"),

CODE_GAME_WATCH_EXIT("退出观战");

private String msg;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.nico.ratel.landlords.server.event;

import org.nico.ratel.landlords.entity.ClientSide;
import org.nico.ratel.landlords.entity.Room;
import org.nico.ratel.landlords.print.SimplePrinter;
import org.nico.ratel.landlords.server.ServerContains;

public class ServerEventListener_CODE_GAME_WATCH_EXIT implements ServerEventListener {

@Override
public void call(ClientSide clientSide, String data) {
Room room = ServerContains.getRoom(clientSide.getRoomId());

if (room != null) {
// 房间如果存在,则将观战者从房间观战列表中移除
clientSide.setRoomId(room.getId());
boolean successful = room.getWatcherList().remove(clientSide);
if (successful) {
SimplePrinter.serverLog(clientSide.getNickname() + " exit room " + room.getId());
}
}
}
}

0 comments on commit 438b79c

Please sign in to comment.