Skip to content

Commit

Permalink
Merge pull request #74 from doveeeee/master
Browse files Browse the repository at this point in the history
重构代码 && 增加一些功能
  • Loading branch information
ainilili committed Jul 19, 2021
2 parents 19797fd + c35acba commit 1e3889d
Show file tree
Hide file tree
Showing 100 changed files with 2,891 additions and 2,551 deletions.
4 changes: 2 additions & 2 deletions landlords-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
<parent>
<groupId>com.smallnico.ratel</groupId>
<artifactId>landlords</artifactId>
<version>1.2.7</version>
<version>1.3.0</version>
</parent>

<dependencies>
<dependency>
<groupId>com.smallnico.ratel</groupId>
<artifactId>landlords-common</artifactId>
<version>1.2.7</version>
<version>1.3.0</version>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.nico.noson.entity.NoType;
import org.nico.ratel.landlords.client.proxy.ProtobufProxy;
import org.nico.ratel.landlords.client.proxy.WebsocketProxy;
import org.nico.ratel.landlords.features.Features;
import org.nico.ratel.landlords.print.SimplePrinter;
import org.nico.ratel.landlords.print.SimpleWriter;
import org.nico.ratel.landlords.utils.StreamUtils;
Expand All @@ -18,70 +19,72 @@ public class SimpleClient {

public static int id = -1;

public final static String VERSION = Features.VERSION_1_3_0;

public static String serverAddress;

public static int port = 1024;

public static String protocol = "pb";

private static String[] serverAddressSource = new String[] {
private final static String[] serverAddressSource = new String[]{
"https://raw.githubusercontent.com/ainilili/ratel/master/serverlist.json",
"https://gitee.com/ainilili/ratel/raw/master/serverlist.json"
};

public static void main(String[] args) throws InterruptedException, IOException, URISyntaxException {
if(args != null && args.length > 0) {
for(int index = 0; index < args.length; index = index + 2) {
if(index + 1 < args.length) {
if(args[index].equalsIgnoreCase("-p") || args[index].equalsIgnoreCase("-port")) {
if (args != null && args.length > 0) {
for (int index = 0; index < args.length; index = index + 2) {
if (index + 1 < args.length) {
if (args[index].equalsIgnoreCase("-p") || args[index].equalsIgnoreCase("-port")) {
port = Integer.parseInt(args[index + 1]);
}
if(args[index].equalsIgnoreCase("-h") || args[index].equalsIgnoreCase("-host")) {
if (args[index].equalsIgnoreCase("-h") || args[index].equalsIgnoreCase("-host")) {
serverAddress = args[index + 1];
}
if(args[index].equalsIgnoreCase("-ptl") || args[index].equalsIgnoreCase("-protocol")) {
if (args[index].equalsIgnoreCase("-ptl") || args[index].equalsIgnoreCase("-protocol")) {
protocol = args[index + 1];
}
}
}
}
if(serverAddress == null){
if (serverAddress == null) {
List<String> serverAddressList = getServerAddressList();
if(serverAddressList == null || serverAddressList.size() == 0) {
if (serverAddressList == null || serverAddressList.size() == 0) {
throw new RuntimeException("Please use '-host' to setting server address.");
}

SimplePrinter.printNotice("Please select a server:");
for(int i = 0; i < serverAddressList.size(); i++) {
SimplePrinter.printNotice((i+1) + ". " + serverAddressList.get(i));
for (int i = 0; i < serverAddressList.size(); i++) {
SimplePrinter.printNotice((i + 1) + ". " + serverAddressList.get(i));
}
int serverPick = Integer.parseInt(SimpleWriter.write("option"));
while(serverPick < 1 || serverPick > serverAddressList.size()){
while (serverPick < 1 || serverPick > serverAddressList.size()) {
try {
SimplePrinter.printNotice("The server address does not exist!");
serverPick = Integer.parseInt(SimpleWriter.write("option"));
}catch(NumberFormatException e){}
} catch (NumberFormatException ignore) {}
}
serverAddress = serverAddressList.get(serverPick-1);
serverAddress = serverAddressList.get(serverPick - 1);
String[] elements = serverAddress.split(":");
serverAddress = elements[0];
port = Integer.parseInt(elements[1]);
}

if (Objects.equals(protocol, "pb")){
if (Objects.equals(protocol, "pb")) {
new ProtobufProxy().connect(serverAddress, port);
}else if (Objects.equals(protocol, "ws")){
} else if (Objects.equals(protocol, "ws")) {
new WebsocketProxy().connect(serverAddress, port + 1);
}else{
} else {
throw new UnsupportedOperationException("Unsupported protocol " + protocol);
}
}

private static List<String> getServerAddressList(){
for(String serverAddressSource: serverAddressSource) {
private static List<String> getServerAddressList() {
for (String serverAddressSource : serverAddressSource) {
try {
String serverInfo = StreamUtils.convertToString(new URL(serverAddressSource));
return Noson.convert(serverInfo, new NoType<List<String>>() {});
return Noson.convert(serverInfo, new NoType<List<String>>() {});
} catch (IOException e) {
SimplePrinter.printNotice("Try connected " + serverAddressSource + " failed: " + e.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,43 +17,43 @@ public abstract class ClientEventListener {
public abstract void call(Channel channel, String data);

public final static Map<ClientEventCode, ClientEventListener> LISTENER_MAP = new HashMap<>();

private final static String LISTENER_PREFIX = "org.nico.ratel.landlords.client.event.ClientEventListener_";

protected static List<Poker> lastPokers = null;
protected static String lastSellClientNickname = null;
protected static String lastSellClientType = null;

protected static void initLastSellInfo() {
lastPokers = null;
lastSellClientNickname = null;
lastSellClientType = null;
}

@SuppressWarnings("unchecked")
public static ClientEventListener get(ClientEventCode code){
ClientEventListener listener = null;
public static ClientEventListener get(ClientEventCode code) {
ClientEventListener listener;
try {
if(ClientEventListener.LISTENER_MAP.containsKey(code)){
if (ClientEventListener.LISTENER_MAP.containsKey(code)) {
listener = ClientEventListener.LISTENER_MAP.get(code);
}else{
} else {
String eventListener = LISTENER_PREFIX + code.name();
Class<ClientEventListener> listenerClass = (Class<ClientEventListener>) Class.forName(eventListener);
listener = listenerClass.newInstance();
ClientEventListener.LISTENER_MAP.put(code, listener);
}
return listener;
}catch(ClassNotFoundException | InstantiationException | IllegalAccessException e) {
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
return listener;
return null;
}
protected ChannelFuture pushToServer(Channel channel, ServerEventCode code, String datas){

protected ChannelFuture pushToServer(Channel channel, ServerEventCode code, String datas) {
return ChannelUtils.pushToServer(channel, code, datas);
}
protected ChannelFuture pushToServer(Channel channel, ServerEventCode code){

protected ChannelFuture pushToServer(Channel channel, ServerEventCode code) {
return pushToServer(channel, code, null);
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
package org.nico.ratel.landlords.client.event;

import org.nico.noson.Noson;
import org.nico.ratel.landlords.client.SimpleClient;
import org.nico.ratel.landlords.enums.ServerEventCode;
import org.nico.ratel.landlords.features.Features;
import org.nico.ratel.landlords.helper.MapHelper;
import org.nico.ratel.landlords.print.SimplePrinter;

import io.netty.channel.Channel;
import org.nico.ratel.landlords.utils.JsonUtils;

public class ClientEventListener_CODE_CLIENT_CONNECT extends ClientEventListener{
import java.util.HashMap;
import java.util.Map;

public class ClientEventListener_CODE_CLIENT_CONNECT extends ClientEventListener {

@Override
public void call(Channel channel, String data) {
SimplePrinter.printNotice("Connected to server. Welcome to ratel!");
SimpleClient.id = Integer.parseInt(data);

Map<String, Object> infos = new HashMap<>();
infos.put("version", SimpleClient.VERSION);
pushToServer(channel, ServerEventCode.CODE_CLIENT_INFO_SET, Noson.reversal(infos));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,22 @@

import io.netty.channel.Channel;

public class ClientEventListener_CODE_CLIENT_EXIT extends ClientEventListener{
public class ClientEventListener_CODE_CLIENT_EXIT extends ClientEventListener {

@Override
public void call(Channel channel, String data) {
Map<String, Object> map = MapHelper.parser(data);

Integer exitClientId = (Integer) map.get("exitClientId");
String role = null;
if(exitClientId == SimpleClient.id) {

String role;
if (exitClientId == SimpleClient.id) {
role = "You";
}else {
} else {
role = String.valueOf(map.get("exitClientNickname"));
}
SimplePrinter.printNotice(role + " left the room. Room disbanded!\n");

get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

import io.netty.channel.Channel;

public class ClientEventListener_CODE_CLIENT_KICK extends ClientEventListener{
public class ClientEventListener_CODE_CLIENT_KICK extends ClientEventListener {

@Override
public void call(Channel channel, String data) {

SimplePrinter.printNotice("You have been kicked from the room for being idle.\n");

get(ClientEventCode.CODE_SHOW_OPTIONS).call(channel, data);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

import io.netty.channel.Channel;

public class ClientEventListener_CODE_CLIENT_NICKNAME_SET extends ClientEventListener{
public class ClientEventListener_CODE_CLIENT_NICKNAME_SET extends ClientEventListener {

public static final int NICKNAME_MAX_LENGTH = 10;

@Override
public void call(Channel channel, String data) {

// If it is not the first time that the user is prompted to enter nickname
// If first time, data = null or "" otherwise not empty
if (StringUtils.isNotBlank(data)) {
Expand All @@ -28,16 +28,15 @@ public void call(Channel channel, String data) {
}
SimplePrinter.printNotice("Please set your nickname (upto " + NICKNAME_MAX_LENGTH + " characters)");
String nickname = SimpleWriter.write("nickname");

// If the length of nickname is more that NICKNAME_MAX_LENGTH
if (nickname.trim().length() > NICKNAME_MAX_LENGTH) {
String result = MapHelper.newInstance().put("invalidLength", nickname.trim().length()).json();
get(ClientEventCode.CODE_CLIENT_NICKNAME_SET).call(channel, result);
}else{
} else {
pushToServer(channel, ServerEventCode.CODE_CLIENT_NICKNAME_SET, nickname);
}
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@

import io.netty.channel.Channel;

public class ClientEventListener_CODE_GAME_LANDLORD_CONFIRM extends ClientEventListener{
public class ClientEventListener_CODE_GAME_LANDLORD_CONFIRM extends ClientEventListener {

@Override
public void call(Channel channel, String data) {
Map<String, Object> map = MapHelper.parser(data);

String landlordNickname = String.valueOf(map.get("landlordNickname"));

SimplePrinter.printNotice(landlordNickname + " has become the landlord and gotten three extra cards");

List<Poker> additionalPokers = Noson.convert(map.get("additionalPokers"), new NoType<List<Poker>>() {});
SimplePrinter.printPokers(additionalPokers);

pushToServer(channel, ServerEventCode.CODE_GAME_POKER_PLAY_REDIRECT);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

import io.netty.channel.Channel;

public class ClientEventListener_CODE_GAME_LANDLORD_CYCLE extends ClientEventListener{
public class ClientEventListener_CODE_GAME_LANDLORD_CYCLE extends ClientEventListener {

@Override
public void call(Channel channel, String data) {
SimplePrinter.printNotice("No player takes the landlord, so redealing cards.");

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,34 @@

import io.netty.channel.Channel;

public class ClientEventListener_CODE_GAME_LANDLORD_ELECT extends ClientEventListener{
public class ClientEventListener_CODE_GAME_LANDLORD_ELECT extends ClientEventListener {

@Override
public void call(Channel channel, String data) {
Map<String, Object> map = MapHelper.parser(data);
int turnClientId = (int) map.get("nextClientId");
if(map.containsKey("preClientNickname")) {

if (map.containsKey("preClientNickname")) {
SimplePrinter.printNotice(map.get("preClientNickname") + " don't rob the landlord!");
}

if(turnClientId == SimpleClient.id) {
SimplePrinter.printNotice("It's your turn. Do you want to rob the landlord? [Y/N] (enter [exit|e] to exit current room)");
String line = SimpleWriter.write("Y/N");
if(line.equalsIgnoreCase("exit") || line.equalsIgnoreCase("e")) {
if (line.equalsIgnoreCase("exit") || line.equalsIgnoreCase("e")) {
pushToServer(channel, ServerEventCode.CODE_CLIENT_EXIT);
}else if(line.equalsIgnoreCase("Y")){
} else if (line.equalsIgnoreCase("Y")) {
pushToServer(channel, ServerEventCode.CODE_GAME_LANDLORD_ELECT, "TRUE");
}else if(line.equalsIgnoreCase("N")){
} else if (line.equalsIgnoreCase("N")) {
pushToServer(channel, ServerEventCode.CODE_GAME_LANDLORD_ELECT, "FALSE");
}else{
} else {
SimplePrinter.printNotice("Invalid options");
call(channel, data);
}
}else {
} else {
SimplePrinter.printNotice("It's " + map.get("nextClientNickname") + "'s turn. Please wait patiently for his/her confirmation !");
}

}

}
Loading

0 comments on commit 1e3889d

Please sign in to comment.