机器人优化

master
fy 2026-01-30 16:04:50 +08:00
parent 254fd9ba86
commit 9d38ac2c61
8 changed files with 1858 additions and 1257 deletions

View File

@ -11,6 +11,7 @@
<maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target> <maven.compiler.target>1.8</maven.compiler.target>
<build.type>pro</build.type> <build.type>pro</build.type>
<kotlin.version>1.5.10</kotlin.version>
</properties> </properties>
<dependencies> <dependencies>
@ -80,7 +81,7 @@
<dependency> <dependency>
<groupId>com.robot</groupId> <groupId>com.robot</groupId>
<artifactId>robot_common</artifactId> <artifactId>robot_common</artifactId>
<version>1.0.7</version> <version>1.0.8</version>
</dependency> </dependency>
<!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-webapp --> <!-- https://mvnrepository.com/artifact/org.eclipse.jetty/jetty-webapp -->
@ -96,6 +97,17 @@
<version>2.2.3</version> <version>2.2.3</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
@ -118,6 +130,50 @@
</webResources> </webResources>
</configuration> </configuration>
</plugin> </plugin>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins> </plugins>
</build> </build>

View File

@ -2,11 +2,12 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>com.robot</groupId> <groupId>com.robot</groupId>
<artifactId>robot_common</artifactId> <artifactId>robot_common</artifactId>
<version>1.0.7</version> <version>1.0.8</version>
<packaging>jar</packaging> <packaging>jar</packaging>
<name>robot_common</name> <name>robot_common</name>
<properties> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<kotlin.version>1.5.10</kotlin.version>
</properties> </properties>
<dependencies> <dependencies>
<!--依赖 taurus-core --> <!--依赖 taurus-core -->
@ -44,16 +45,67 @@
<artifactId>log4j</artifactId> <artifactId>log4j</artifactId>
<version>1.2.17</version> <version>1.2.17</version>
</dependency> </dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib-jdk8</artifactId>
<version>${kotlin.version}</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-test</artifactId>
<version>${kotlin.version}</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>
<plugins> <plugins>
<plugin>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-maven-plugin</artifactId>
<version>${kotlin.version}</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>test-compile</id>
<phase>test-compile</phase>
<goals>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
<configuration>
<jvmTarget>1.8</jvmTarget>
</configuration>
</plugin>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version> <version>3.6.1</version>
<executions>
<execution>
<id>compile</id>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>testCompile</id>
<phase>test-compile</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
<configuration> <configuration>
<!-- <compilerArgument>-parameters</compilerArgument> --> <!-- <compilerArgument>-parameters</compilerArgument> -->
<source>1.8</source> <source>1.8</source>

View File

@ -1,14 +1,15 @@
package hunan; package hunan;
import com.game.Global;
import com.game.Util; import com.game.Util;
import com.taurus.core.entity.ITArray; import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject; import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TArray; import com.taurus.core.entity.TArray;
import com.taurus.core.entity.TObject; import com.taurus.core.entity.TObject;
import com.taurus.core.plugin.database.DataBase; import com.taurus.core.plugin.database.DataBase;
import com.taurus.core.plugin.redis.Redis;
import com.taurus.core.util.Logger; import com.taurus.core.util.Logger;
import com.taurus.core.util.StringUtil; import com.taurus.core.util.StringUtil;
import redis.clients.jedis.Jedis;
import taurus.client.Message; import taurus.client.Message;
import taurus.client.TaurusClient; import taurus.client.TaurusClient;
import taurus.util.*; import taurus.util.*;
@ -19,15 +20,12 @@ import java.util.*;
public class HuNanChangSha { public class HuNanChangSha {
public static int changShaCard = 0; public static int changShaCard = 0;
public static boolean isTinChi = false; public static boolean isTinChi = false;
public static boolean isTinPeng = false; public static boolean isTinPeng = false;
private static final Logger log = Logger.getLogger(DoTest.class); // private static final Logger log = Logger.getLogger(DoTest.class);
//湖南长沙麻将手牌
private List<Integer> changShaCardInhand = new ArrayList<>();
private List<Integer> changShaCardInhandgang = new ArrayList<>(); private List<Integer> changShaCardInhandgang = new ArrayList<>();
@ -35,6 +33,8 @@ public class HuNanChangSha {
//长沙麻将出过的牌 //长沙麻将出过的牌
private List<Integer> changShachuguopai = new ArrayList<>(); private List<Integer> changShachuguopai = new ArrayList<>();
private List<Integer> changShaCardInhand = new ArrayList<>();
private Map<Integer, Integer> chuGuoPainum = new HashMap<>(); private Map<Integer, Integer> chuGuoPainum = new HashMap<>();
@ -53,24 +53,16 @@ public class HuNanChangSha {
// 玩家座位号 // 玩家座位号
public static int seat = 0; public static int seat = 0;
public static int playerId = 0;
public static int cardToOut1 = 0;
// 会话标识 // 会话标识
public static String session = ""; public static String session = "";
// 访问令牌 // 访问令牌
public static String token = ""; public static String token = "";
//红中麻将算法
// private static HongZhongSuanFa hongZhongSuanFa = new HongZhongSuanFa();
private static ChangShaSuanFaTest changShaSuanFaTest = new ChangShaSuanFaTest(); private static ChangShaSuanFaTest changShaSuanFaTest = new ChangShaSuanFaTest();
// 公共的getter和setter方法 // 公共的getter和setter方法
public List<Integer> getChangShaCardInhand() {
return changShaCardInhand;
}
public List<Integer> getgangdepai() { public List<Integer> getgangdepai() {
return gangdepai; return gangdepai;
@ -89,6 +81,10 @@ public class HuNanChangSha {
return chowGroup; return chowGroup;
} }
public List<Integer> getChangShaCardInhand() {
return changShaCardInhand;
}
public List<Integer> getChuGuoCardInhand() { public List<Integer> getChuGuoCardInhand() {
return changShachuguopai; return changShachuguopai;
} }
@ -125,23 +121,34 @@ public class HuNanChangSha {
* @param message * @param message
* @return * @return
*/ */
public String getCard(String command, Message message) { public String getCard(String command, Message message, TaurusClient client, Map<String, Object> mapclient) {
if (command.equalsIgnoreCase("819")) { if (command.equalsIgnoreCase("819")) {
ITObject param = message.param; ITObject param = message.param;
if (param == null) { if (param == null) {
return null; return null;
} }
Jedis jedis222 = Redis.use("group1_db2").getJedis();
if (param.getInt("player") != null) { if (param.getInt("player") != null) {
Integer player = param.getInt("player");
int drawnCard = param.getInt("card"); int drawnCard = param.getInt("card");
changShaSuanFaTest.drawnCards = drawnCard;//存储摸到的牌 changShaSuanFaTest.drawnCards = drawnCard;//存储摸到的牌
changShaCardInhand.add(drawnCard); changShaCardInhand.add(drawnCard);
changShaSuanFaTest.analyzeHand(changShaCardInhand);
if (jedis222.hget("{robortInfo}:" + player, "circleId") != null && jedis222.hget("{robortInfo}:" + player, "pid") != null) {
String circleId = jedis222.hget("{robortInfo}:" + player, "circleId");
String pid = jedis222.hget("{robortInfo}:" + player, "pid");
String getStart = "g{" + circleId + "}:play:" + pid;
if (!pid.equals("0")) {
jedis222.hset(getStart, String.valueOf(player), "2");
} }
} }
}
jedis222.close();
}
return null; return null;
} }
@ -157,7 +164,6 @@ public class HuNanChangSha {
// 直接调用hongZhongSuanFaTest中的shouldPong方法它已经包含了所有需要的规则 // 直接调用hongZhongSuanFaTest中的shouldPong方法它已经包含了所有需要的规则
return changShaSuanFaTest.shouldPong(proposedCard, changShaCardInhand); return changShaSuanFaTest.shouldPong(proposedCard, changShaCardInhand);
// return hongZhongSuanFaTest.shouldPong(proposedCard, Arrays.asList(305,304,303,207,207,204,204,208,208,201,201,412,412));
} }
@ -182,13 +188,17 @@ public class HuNanChangSha {
} }
// {bank_seat=1, laiziCard=0, laiziCard2=0, laiziCard2Before=0, jing=0, laiziCardBefore=0, card_list=[101, 103, 104, 201, 204, 207, 208, 209, 307, 309, 501, 502, 503]} // {bank_seat=1, laiziCard=0, laiziCard2=0, laiziCard2Before=0, jing=0, laiziCardBefore=0, card_list=[101, 103, 104, 201, 204, 207, 208, 209, 307, 309, 501, 502, 503]}
ITArray cardList = param.getTArray("card_list"); ITArray cardList = param.getTArray("card_list");
List<Integer> handCards = new ArrayList<>();
for (int i = 0; i < cardList.size(); i++) {
handCards.add(cardList.getInt(i));
}
for (int i = 0; i < cardList.size(); i++) { for (int i = 0; i < cardList.size(); i++) {
changShaCardInhand.add(cardList.getInt(i)); changShaCardInhand.add(cardList.getInt(i));
} }
// if (changShaCardInhand.size() > 13) {
// outCard(client);
// } System.out.println("changShaCardInhand" + changShaCardInhand);
} }
return null; return null;
@ -231,61 +241,60 @@ public class HuNanChangSha {
/** /**
* *
*
* @param param * @param param
* @param client * @param client
* @return * @return
* [TCP->814] data:{"tip_list":[{"type":1,"id":1,"opcard":[108,109],"weight":1,"card":107}]}
[TCP->814] data:{"tip_list":[{"type":1,"id":1,"opcard":[108,109],"weight":1,"card":107}]} *
* {"tip_list":[{"type":1,"id":1,"opcard":[206,205],"weight":1,"card":207},{"type":2,"id":2,"opcard":[207],"weight":2,"card":207}]}
{"tip_list":[{"type":1,"id":1,"opcard":[206,205],"weight":1,"card":207},{"type":2,"id":2,"opcard":[207],"weight":2,"card":207}]} *
* {"tip_list":[{"type":2,"id":1,"opcard":[101],"weight":2,"card":101},{"type":3,"id":2,"opcard":[101],"weight":3,"card":101}]}
{"tip_list":[{"type":2,"id":1,"opcard":[101],"weight":2,"card":101},{"type":3,"id":2,"opcard":[101],"weight":3,"card":101}]} *
* [TCP->814] data:{"tip_list":[{"type":5,"id":1,"opcard":[101],"weight":3,"card":101}]}
[TCP->814] data:{"tip_list":[{"type":5,"id":1,"opcard":[101],"weight":3,"card":101}]} *
* {"tip_list":[{"type":1,"id":1,"opcard":[206,204],"weight":1,"card":205},{"type":1,"id":2,"opcard":[206,207],"weight":1,"card":205},{"type":2,"id":3,"opcard":[205],"weight":2,"card":205},{"type":3,"id":4,"opcard":[205],"weight":3,"card":205},{"type":3,"id":5,"opcard":[205],"weight":4,"card":205}]}
{"tip_list":[{"type":1,"id":1,"opcard":[206,204],"weight":1,"card":205},{"type":1,"id":2,"opcard":[206,207],"weight":1,"card":205},{"type":2,"id":3,"opcard":[205],"weight":2,"card":205},{"type":3,"id":4,"opcard":[205],"weight":3,"card":205},{"type":3,"id":5,"opcard":[205],"weight":4,"card":205}]} *
* {"tip_list":[{"type":2,"id":1,"opcard":[207],"weight":2,"card":207}]}
{"tip_list":[{"type":2,"id":1,"opcard":[207],"weight":2,"card":207}]} *
* {"tip_list":[{"type":5,"id":1,"opcard":[207],"weight":3,"card":207},{"type":5,"id":2,"opcard":[207],"weight":4,"card":207}]}
{"tip_list":[{"type":5,"id":1,"opcard":[207],"weight":3,"card":207},{"type":5,"id":2,"opcard":[207],"weight":4,"card":207}]} * <p>
* //拟定:
//拟定: * 1
1 * boolean isTing
boolean isTing * boolean canTing
boolean canTing * int tingNum
int tingNum * int isDaHu
int isDaHu * int guzhang
int guzhang * int lastHands
int lastHands * int id tiplist id
int id tiplist id * List<Integer> opcard
List<Integer> opcard * int weight tiplist weight
int weight tiplist weight * int card tiplist card
int card tiplist card * int type tiplist type
int type tiplist type * 2
2 *
* ---> 40
---> 40 * ---> 20
---> 20 * ---> -50
---> -50 * ---> 3
---> 3 * ----> -3
----> -3 * ----> -5
----> -5 * ----> 10
----> 10 * -----> -10
-----> -10 * <p>
* 107
107 * {"tip_list":[{"type":5,"id":1,"opcard":[107],"weight":3,"card":107},{"type":5,"id":2,"opcard":[107],"weight":4,"card":107}]}
{"tip_list":[{"type":5,"id":1,"opcard":[107],"weight":3,"card":107},{"type":5,"id":2,"opcard":[107],"weight":4,"card":107}]} * <p>
* 3204
3204 * {"tip_list":[{"type":4,"id":1,"opcard":[204],"weight":3,"card":204},{"type":4,"id":2,"opcard":[204],"weight":4,"card":204}]}
{"tip_list":[{"type":4,"id":1,"opcard":[204],"weight":3,"card":204},{"type":4,"id":2,"opcard":[204],"weight":4,"card":204}]} * <p>
* 3206
3206 * {"tip_list":[{"type":3,"id":1,"opcard":[206],"weight":3,"card":206}]}
{"tip_list":[{"type":3,"id":1,"opcard":[206],"weight":3,"card":206}]}
*/ */
public String actionCard(ITObject param, TaurusClient client) { public String actionCard(ITObject param, TaurusClient client) {
ITArray tipList = param.getTArray("tip_list"); ITArray tipList = param.getTArray("tip_list");
log.info("tipList" +tipList); // log.info("tipList" +tipList);
ITObject params = TObject.newInstance(); ITObject params = TObject.newInstance();
int card = 0; int card = 0;
@ -328,8 +337,6 @@ public class HuNanChangSha {
card = firstTip.getInt("card"); card = firstTip.getInt("card");
if ((type == 5 || type == 3 || type == 4) && weight == 4) { if ((type == 5 || type == 3 || type == 4) && weight == 4) {
//判断开杠后是否能下听 //判断开杠后是否能下听
List<Integer> gangusecars = new ArrayList<>(); List<Integer> gangusecars = new ArrayList<>();
@ -376,7 +383,6 @@ public class HuNanChangSha {
if (type == 4) { if (type == 4) {
// Util.removeCard(changShaCardInhand,card,4); // Util.removeCard(changShaCardInhand,card,4);
Util.removeCard(gangusecars, card, 4); Util.removeCard(gangusecars, card, 4);
List<Integer> shifoutingpai4 = changShaSuanFaTest.handscardshifoutingpai(gangusecars, chowGroup, pongGroup, gangdepai); List<Integer> shifoutingpai4 = changShaSuanFaTest.handscardshifoutingpai(gangusecars, chowGroup, pongGroup, gangdepai);
@ -765,11 +771,9 @@ public class HuNanChangSha {
} }
public String actionCardBak2(ITObject param, TaurusClient client) { public String actionCardBak2(ITObject param, TaurusClient client) {
ITArray tipList = param.getTArray("tip_list"); ITArray tipList = param.getTArray("tip_list");
log.info("tipList" +tipList); // log.info("tipList" +tipList);
//tipList[{opcard=[207, 208], weight=1, id=1, type=1, card=206}] //tipList[{opcard=[207, 208], weight=1, id=1, type=1, card=206}]
boolean chiflag = false;//吃 boolean chiflag = false;//吃
boolean pengflag = false; //碰 boolean pengflag = false; //碰
@ -843,8 +847,6 @@ public class HuNanChangSha {
} }
/** /**
* *
* @param tipList * @param tipList
@ -1016,10 +1018,11 @@ public class HuNanChangSha {
client.send("612", params, response -> {}); client.send("612", params, response -> {});
} }
*/ */
/** /**
* *
* *
* @param param * @param
* @return * @return
*/ */
/* /*
@ -1682,8 +1685,6 @@ public class HuNanChangSha {
} }
*/ */
public static List<Integer> getOtherCards1(List<Integer> integers, int card) { public static List<Integer> getOtherCards1(List<Integer> integers, int card) {
List<Integer> result = new ArrayList<>(); List<Integer> result = new ArrayList<>();
for (Integer num : integers) { for (Integer num : integers) {
@ -1762,7 +1763,6 @@ public class HuNanChangSha {
resultList.addAll(zigang); resultList.addAll(zigang);
} }
// 长沙麻将出牌 // 长沙麻将出牌
String changShaOutCard = changShaSuanFaTest.outCardSuanFa(changShaCardInhand, pongGroup, chowGroup, gangdepai, resultList); String changShaOutCard = changShaSuanFaTest.outCardSuanFa(changShaCardInhand, pongGroup, chowGroup, gangdepai, resultList);
// String changShaOutCard = changShaSuanFaTest.outCardSuanFa(list, changShaCard,pongGroup); // String changShaOutCard = changShaSuanFaTest.outCardSuanFa(list, changShaCard,pongGroup);
@ -1791,12 +1791,12 @@ public class HuNanChangSha {
changShachuguopai.add(cardToOut); changShachuguopai.add(cardToOut);
// 从手牌中移除 // 从手牌中移除
changShaCardInhand.remove(Integer.valueOf(cardToOut)); changShaCardInhand.remove(Integer.valueOf(cardToOut));
params.putString("session", session + "," + token); params.putString("session", session + "," + token);
client.send("611", params, response -> { client.send("611", params, response -> {
}); });
return null; return null;
} }
@ -1903,7 +1903,6 @@ public class HuNanChangSha {
tiplist.addTObject(tob3); tiplist.addTObject(tob3);
params.putTArray("tip_list", tiplist); params.putTArray("tip_list", tiplist);
System.out.println(params); System.out.println(params);
//已经吃掉数据 //已经吃掉数据

View File

@ -957,7 +957,8 @@ public class JiQiRens {
//[TCP->822] data:{"tip_list":[{"type":8,"id":1,"opcard":[],"weight":8,"card":0}],"types":[{"type":21,"value":1}]} //[TCP->822] data:{"tip_list":[{"type":8,"id":1,"opcard":[],"weight":8,"card":0}],"types":[{"type":21,"value":1}]}
//板胡Event [TCP->823] data:{"type":8,"seat":1,"data":[{"opcard":[204,204,204,108,108,108],"type":21,"value":1}]} //板胡Event [TCP->823] data:{"type":8,"seat":1,"data":[{"opcard":[204,204,204,108,108,108],"type":21,"value":1}]}
if ("811".equalsIgnoreCase(command)) {//初始化收手牌 if ("811".equalsIgnoreCase(command)) {//初始化收手牌
huNanChangSha.cardInHead(command, message, client); Map<String, Object> d = null;
// huNanChangSha.cardInHead(command, message, client);
} else if ("812".equalsIgnoreCase(command)) {//出牌广播 } else if ("812".equalsIgnoreCase(command)) {//出牌广播
//{"opzicards":[{"opzicards":[],"playerId":101555}],"opmingcards":[{"opmingcards":[],"playerId":101555}],"outcard_map":[{"outcards":[209,205],"playerId":101555},{"outcards":[],"playerId":112233}],"card":205,"opchicards":[{"opchicards":[105,103],"playerId":101555}],"oppengcards":[{"oppengcards":[],"playerId":101555}],"seat":1} //{"opzicards":[{"opzicards":[],"playerId":101555}],"opmingcards":[{"opmingcards":[],"playerId":101555}],"outcard_map":[{"outcards":[209,205],"playerId":101555},{"outcards":[],"playerId":112233}],"card":205,"opchicards":[{"opchicards":[105,103],"playerId":101555}],"oppengcards":[{"oppengcards":[],"playerId":101555}],"seat":1}
ITArray outcard_map = param.getTArray("outcard_map"); ITArray outcard_map = param.getTArray("outcard_map");
@ -1054,9 +1055,10 @@ public class JiQiRens {
HuNanChangSha.drawCard(command, message); HuNanChangSha.drawCard(command, message);
} else if ("819".equalsIgnoreCase(command)) { } else if ("819".equalsIgnoreCase(command)) {
//摸牌 //摸牌
huNanChangSha.getCard(command, message); // huNanChangSha.getCard(command, message);
} else if ("813".equalsIgnoreCase(command)) {//出牌提示 } else if ("813".equalsIgnoreCase(command)) {//出牌提示
huNanChangSha.outCard(client,playerOutcardsMap,playerchisMap,playerpengsMap,playermingsMap,playerzisMap);
// huNanChangSha.outCard(client,playerOutcardsMap,playerchisMap,playerpengsMap,playermingsMap,playerzisMap,null);
} else if ("814".equalsIgnoreCase(command)) { } else if ("814".equalsIgnoreCase(command)) {
//放招提示 //放招提示
huNanChangSha.actionCard(param, client); huNanChangSha.actionCard(param, client);

View File

@ -0,0 +1,609 @@
package taurus.newRobot;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TObject;
import com.taurus.core.events.Event;
import com.taurus.core.events.IEventListener;
import com.taurus.core.plugin.redis.Redis;
import com.taurus.core.util.ICallback;
import com.taurus.core.util.StringUtil;
import hunan.HuNanChangSha;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Pipeline;
import redis.clients.jedis.Response;
import taurus.client.Message;
import taurus.client.MessageResponse;
import taurus.client.SocketCode;
import taurus.client.TaurusClient;
import taurus.client.business.AccountBusiness;
import taurus.client.business.GroupRoomBusiness;
import taurus.util.ChangShaSuanFaTest;
import taurus.util.TinHuChi;
import java.util.*;
import java.util.stream.Collectors;
public class jiqiren {
public String wanfaId = "";
// 会话标识
private String session = "";
// 访问令牌
public String token = "";
// 客户端连接对象
private TaurusClient client = null;
private Map<String, Object> mapclient = new HashMap<>();
public TaurusClient getClient() {
return client;
}
public void setClient(TaurusClient client) {
this.client = client;
}
//进入真人房间
public void tcp_jiqirenzhenren(String id) {
Jedis jedis0 = Redis.use().getJedis();
Jedis jedis11 = Redis.use("group1_db11").getJedis();
Jedis jedis2 = Redis.use("group1_db2").getJedis();
String roomKey = "";
String roomId = id.substring(id.indexOf(":") + 1);
roomKey = "room:" + roomId;
//玩法id
String gpid = jedis0.hget(roomKey, "gpid");
//圈子id
String group = jedis0.hget(roomKey, "group");
//获取玩法对应的机器人 g{330800}:play:10
String playKey = "g{" + group + "}:play:" + gpid;
String players1 = jedis0.hget(roomKey, "players");
if (jedis11.hget(playKey, "leftover_robot") != null) {
//玩法配置的机器人数量
String leftover_robot = jedis11.hget(playKey, "leftover_robot");
//机器人主动找真人
if (players1 != null && !players1.equals("[]")) {
List<String> availableRobots1 = getAvailableRobots1();
String players = jedis0.hget(roomKey, "players");
players = players.substring(1, players.length() - 1);
String[] playerIds1 = players.split(",");
String maxPlayers = jedis0.hget(roomKey, "maxPlayers");
if (Integer.parseInt(maxPlayers) == 2) {
if (playerIds1.length == 1) {
//循环playerIds
for (int i = 0; i < playerIds1.length; i++) {
String playeid = playerIds1[i];
if (!availableRobots1.contains(playeid) && Integer.parseInt(leftover_robot) > 0) {
Map<String, String> stringStringMap = jedis2.hgetAll(playKey);
List<String> zeroValueKeys = getKeysWithValueZero(stringStringMap);
System.out.println("值为0的机器人ID: " + zeroValueKeys);
String randomKeyFromList = getRandomKeyFromList(zeroValueKeys);
System.out.println("随机给的用户id----" + randomKeyFromList);
wanfaId = jedis0.hget(roomKey, "game");
if ("22".equalsIgnoreCase(wanfaId)) { //湖南红中麻将
//client = new TaurusClient("8.138.242.190:6421", "game", TaurusClient.ConnectionProtocol.Tcp);
}
if ("10".equalsIgnoreCase(wanfaId)) {
client = new TaurusClient("127.0.0.1:6311", "game", TaurusClient.ConnectionProtocol.Tcp);
//TaurusClient client1=getClient();
client.connect();
mapclient.put(randomKeyFromList, client);
}
if ("66".equalsIgnoreCase(wanfaId)) {
// client = new TaurusClient("8.138.242.190:6841", "game", TaurusClient.ConnectionProtocol.Tcp);
}
ITObject object = null;
AccountBusiness accountBusiness = null;
accountBusiness = new AccountBusiness();
// 登录获取session跟token
if (jedis0.keys("{user}:" + randomKeyFromList + "_token").size() != 0) {
Set<String> keys1 = jedis0.keys("{user}:" + randomKeyFromList + "_token");
if (keys1 == null) {
try {
object = accountBusiness.idPasswordLogin(Integer.parseInt(randomKeyFromList), "123456");
} catch (Exception e) {
e.printStackTrace();
}
ITObject validate = TObject.newInstance();
validate.putString("token", object.getString("token"));
token = object.getString("token");
session = accountBusiness.getSession();
} else {
Set<String> keys = jedis0.smembers("{user}:" + randomKeyFromList + "_token");
List<String> keyList = new ArrayList<>(keys);
session = "{user}:" + randomKeyFromList;
token = keyList.get(0);
}
} else {
try {
object = accountBusiness.idPasswordLogin(Integer.parseInt(randomKeyFromList), "123456");
} catch (Exception e) {
e.printStackTrace();
}
ITObject validate = TObject.newInstance();
validate.putString("token", object.getString("token"));
token = object.getString("token");
session = accountBusiness.getSession();
}
String roomKey1 = "room:" + roomId;
try {
ITObject roomInfos = GroupRoomBusiness.joinRoom(Integer.parseInt(group), roomKey1, session, null);
} catch (Exception e) {
e.printStackTrace();
}
joinRoom(Integer.parseInt(randomKeyFromList)); //进入房间
jedis2.hset(playKey, randomKeyFromList, "1");
String robortInfoKey = "{robortInfo}:" + randomKeyFromList;
jedis2.hset(robortInfoKey, "circleId", group);
jedis2.hset(robortInfoKey, "pid", gpid);
jedis2.hset(robortInfoKey, "room_id", roomId);
ready(); //准备
jedis11.hincrBy(playKey, "leftover_robot", -1);
lianjiejiqiren lianjiejiqiren = new lianjiejiqiren();
lianjiejiqiren.token = token;
lianjiejiqiren.session = session;
lianjiejiqiren.lianjie(client, wanfaId, mapclient);
}
}
}
}
}
}
System.out.println(roomKey);
}
//进入中括号的房间
public void tcp_jiqirezhongkouhao(String id) {
Jedis jedis0 = Redis.use().getJedis();
Jedis jedis11 = Redis.use("group1_db11").getJedis();
Jedis jedis2 = Redis.use("group1_db2").getJedis();
String roomKey = "";
String roomId = id.substring(id.indexOf(":") + 1);
roomKey = "room:" + roomId;
//玩法id
String gpid = jedis0.hget(roomKey, "gpid");
//圈子id
String group = jedis0.hget(roomKey, "group");
//获取玩法对应的机器人 g{330800}:play:10
String playKey = "g{" + group + "}:play:" + gpid;
String players1 = jedis0.hget(roomKey, "players");
if (jedis11.hget(playKey, "leftover_robot") != null && players1 != null) {
//玩法配置的机器人数量
String leftover_robot = jedis11.hget(playKey, "leftover_robot");
//机器人进入退出的房间
if (players1.equals("[]")) {
String maxPlayers = jedis0.hget(roomKey, "maxPlayers");
if (Integer.parseInt(maxPlayers) == 2) {
if (Integer.parseInt(leftover_robot) > 0) {
int count = countValuesEqualToOne(jedis2, playKey);
System.out.println("count 数量 +++++++++++++++++++++" + count);
if (count < 2) {
Map<String, String> stringStringMap = jedis2.hgetAll(playKey);
List<String> zeroValueKeys = getKeysWithValueZero(stringStringMap);
System.out.println("值为0的机器人ID: " + zeroValueKeys);
String randomKeyFromList = getRandomKeyFromList(zeroValueKeys);
System.out.println("随机给的用户id----" + randomKeyFromList);
wanfaId = jedis0.hget(roomKey, "game");
if ("22".equalsIgnoreCase(wanfaId)) { //湖南红中麻将
// client = new TaurusClient("8.138.242.190:6421", "game", TaurusClient.ConnectionProtocol.Tcp);
}
if ("10".equalsIgnoreCase(wanfaId)) {
client = new TaurusClient("127.0.0.1:6311", "game", TaurusClient.ConnectionProtocol.Tcp);
client.connect();
mapclient.put(randomKeyFromList, client);
}
if ("66".equalsIgnoreCase(wanfaId)) {
// client = new TaurusClient("8.138.242.190:6841", "game", TaurusClient.ConnectionProtocol.Tcp);
}
ITObject object = null;
AccountBusiness accountBusiness = null;
accountBusiness = new AccountBusiness();
// 登录获取session跟token
if (jedis0.keys("{user}:" + randomKeyFromList + "_token").size() != 0) {
Set<String> keys1 = jedis0.keys("{user}:" + randomKeyFromList + "_token");
if (keys1 == null) {
try {
object = accountBusiness.idPasswordLogin(Integer.parseInt(randomKeyFromList), "123456");
} catch (Exception e) {
e.printStackTrace();
}
ITObject validate = TObject.newInstance();
validate.putString("token", object.getString("token"));
token = object.getString("token");
session = accountBusiness.getSession();
} else {
Set<String> keys = jedis0.smembers("{user}:" + randomKeyFromList + "_token");
List<String> keyList = new ArrayList<>(keys);
session = "{user}:" + randomKeyFromList;
token = keyList.get(0);
}
} else {
try {
object = accountBusiness.idPasswordLogin(Integer.parseInt(randomKeyFromList), "123456");
} catch (Exception e) {
e.printStackTrace();
}
ITObject validate = TObject.newInstance();
validate.putString("token", object.getString("token"));
token = object.getString("token");
session = accountBusiness.getSession();
}
String roomKey1 = "room:" + roomId;
try {
ITObject roomInfos = GroupRoomBusiness.joinRoom(Integer.parseInt(group), roomKey1, session, null);
} catch (Exception e) {
e.printStackTrace();
}
joinRoom(Integer.parseInt(randomKeyFromList)); //进入房间
jedis2.hset(playKey, randomKeyFromList, "1");
String robortInfoKey = "{robortInfo}:" + randomKeyFromList;
jedis2.hset(robortInfoKey, "circleId", group);
jedis2.hset(robortInfoKey, "pid", gpid);
jedis2.hset(robortInfoKey, "room_id", roomId);
ready(); //准备
jedis11.hincrBy(playKey, "leftover_robot", -1);
lianjiejiqiren lianjiejiqiren = new lianjiejiqiren();
lianjiejiqiren.token = token;
lianjiejiqiren.session = session;
lianjiejiqiren.lianjie(client, wanfaId, mapclient);
}
}
}
}
}
jedis0.close();
jedis2.close();
jedis11.close();
}
//创建房间,进入房间
public void createRoomsForCircle(String circleId, Map<String, Integer> playRoomMap) {
Jedis jedis2 = Redis.use("group1_db2").getJedis();
Jedis jedis11 = Redis.use("group1_db11").getJedis();
Jedis jedis0 = Redis.use().getJedis();
// 按玩法ID排序处理
List<Map.Entry<String, Integer>> sortedPlays = playRoomMap.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.collect(Collectors.toList());
for (Map.Entry<String, Integer> entry : sortedPlays) {
String playId = entry.getKey();
int requiredRooms = entry.getValue();
if (requiredRooms <= 0) {
System.out.println("玩法 " + playId + ": 跳过创建 (需求数=" + requiredRooms + ")");
continue;
}
boolean isPlaying = true;
//获取所有房间
Set<String> roomIds = jedis0.keys("room:*");
for (String id : roomIds) {
String roomId = id.substring(id.indexOf(":") + 1);
String roomKey = "room:" + roomId;
String players = jedis0.hget(roomKey, "players");
if (players != null) {
if (players.equals("[]")) {
isPlaying = false;
}
}
}
System.out.println("玩法 " + playId + ": 创建 " + requiredRooms + " 个房间");
List<Object> roomsForPlay = new ArrayList<>();
for (int i = 1; i <= requiredRooms; i++) {
try {
String keys = "g{" + circleId + "}:play:" + playId;
if (jedis11.hget(keys, "leftover_robot") != null && isPlaying) {
String leftover_robot = jedis11.hget(keys, "leftover_robot");
if (Integer.parseInt(leftover_robot) > 0) {
Map<String, String> stringStringMap = jedis2.hgetAll(keys);
List<String> zeroValueKeys = getKeysWithValueZero(stringStringMap);
System.out.println("值为0的机器人ID: " + zeroValueKeys);
String randomKeyFromList = getRandomKeyFromList(zeroValueKeys);
System.out.println("随机给的用户id----" + randomKeyFromList);
jedis2.hset(keys, randomKeyFromList, "1");
ITObject object = null;
AccountBusiness accountBusiness = null;
accountBusiness = new AccountBusiness();
// 登录获取session跟token
if (jedis0.keys("{user}:" + randomKeyFromList + "_token").size() != 0) {
Set<String> keys1 = jedis0.keys("{user}:" + randomKeyFromList + "_token");
if (keys1 == null) {
object = accountBusiness.idPasswordLogin(Integer.parseInt(randomKeyFromList), "123456");
ITObject validate = TObject.newInstance();
validate.putString("token", object.getString("token"));
token = object.getString("token");
session = accountBusiness.getSession();
} else {
Set<String> keys2 = jedis0.smembers("{user}:" + randomKeyFromList + "_token");
List<String> keyList = new ArrayList<>(keys2);
session = "{user}:" + randomKeyFromList;
token = keyList.get(0);
}
} else {
object = accountBusiness.idPasswordLogin(Integer.parseInt(randomKeyFromList), "123456");
ITObject validate = TObject.newInstance();
validate.putString("token", object.getString("token"));
token = object.getString("token");
session = accountBusiness.getSession();
}
// 调用原有的房间匹配逻辑
Object room = GroupRoomBusiness.matchRoom(Integer.parseInt(circleId), Integer.parseInt(playId), session, "Android", false);
String game = jedis0.hget(String.valueOf(room), "game");
wanfaId = String.valueOf(game);
String roomKey = String.valueOf(room);
System.out.println("roomkey--------" + roomKey);
client = null;
System.out.println("wanfaId" + wanfaId);
if ("22".equalsIgnoreCase(wanfaId)) { //湖南红中麻将
client = new TaurusClient("8.138.242.190:6421", "game", TaurusClient.ConnectionProtocol.Tcp);
}
if ("10".equalsIgnoreCase(wanfaId)) {
client = new TaurusClient("127.0.0.1:6311", "game", TaurusClient.ConnectionProtocol.Tcp);
// client.setSession(session);
client.connect();
mapclient.put(randomKeyFromList, client);
}
System.out.println("clinet 111 " + client);
if ("66".equalsIgnoreCase(wanfaId)) {
client = new TaurusClient("8.138.242.190:6841", "game", TaurusClient.ConnectionProtocol.Tcp);
}
System.out.println("id ++++++" + mapclient);
System.out.println("mapclient----" + mapclient);
ITObject roomInfos = GroupRoomBusiness.joinRoom(Integer.parseInt(circleId), roomKey, this.session, null);
String roomId = roomInfos.getString("room_id");
String robort = "{robortInfo}:" + randomKeyFromList;
jedis2.hset(robort, "room_id", roomId);
jedis2.hset(robort, "circleId", circleId);
jedis2.hset(robort, "pid", playId);
joinRoom(Integer.parseInt(randomKeyFromList)); //进入房间
String playKey1 = "g{" + circleId + "}:play:" + playId;
jedis11.hincrBy(playKey1, "leftover_robot", -1);
lianjiejiqiren lianjiejiqiren = new lianjiejiqiren();
lianjiejiqiren.token = token;
lianjiejiqiren.session = session;
lianjiejiqiren.lianjie(client, wanfaId, mapclient);
ready(); //准备
if (room != null) {
roomsForPlay.add(room);
System.out.println(" 第 " + i + " 个房间创建成功");
} else {
System.out.println(" 第 " + i + " 个房间创建失败");
}
}
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
System.err.println("创建过程被中断");
break;
} catch (Exception e) {
System.err.println("创建房间时出错: " + e.getMessage());
}
}
jedis2.close();
jedis11.close();
jedis0.close();
}
}
/**
* Hash"1"
*/
public static int countValuesEqualToOne(Jedis jedis, String key) {
int count = 0;
Map<String, String> hashData = jedis.hgetAll(key);
for (Map.Entry<String, String> entry : hashData.entrySet()) {
if ("1".equals(entry.getValue())) {
count++;
System.out.println("找到字段: " + entry.getKey() + " = " + entry.getValue());
}
}
return count;
}
//获取所有机器人
public List<String> getAvailableRobots1() {
Jedis jedis2 = Redis.use("group1_db2").getJedis();
Set<String> allRobotKeys = jedis2.keys("{robot}:*");
List<String> availableRobots = new ArrayList<>();
// 使用Pipeline提高效率
Pipeline pipeline = jedis2.pipelined();
List<Response<String>> responses = new ArrayList<>();
for (String robotKey : allRobotKeys) {
responses.add(pipeline.hget(robotKey, "start"));
}
pipeline.sync();
// 提取机器人ID并筛选
int index = 0;
for (String robotKey : allRobotKeys) {
String startStatus = responses.get(index).get();
String robotId = robotKey.substring(robotKey.indexOf(":") + 1);
availableRobots.add(robotId);
index++;
}
jedis2.close();
return availableRobots;
}
//获取状态为0的机器人
public static List<String> getKeysWithValueZero(Map<String, String> robotData) {
List<String> result = new ArrayList<>();
for (Map.Entry<String, String> entry : robotData.entrySet()) {
if ("0".equals(entry.getValue())) {
result.add(entry.getKey());
}
}
return result;
}
//随机分配机器人
public String getRandomKeyFromList(List<String> zeroValueKeys) {
if (zeroValueKeys == null || zeroValueKeys.isEmpty()) {
return null;
}
Random random = new Random();
int randomIndex = random.nextInt(zeroValueKeys.size());
return zeroValueKeys.get(randomIndex);
}
//监听
/**
*
*
* @return
*/
public String joinRoom(int playerId) {
try {
// 添加2秒延迟
Thread.sleep(5000);
ITObject params = TObject.newInstance();
params.putString("session", session + "," + token);
client.send("1002", params, response -> {
ITObject obj = response.messageData.param.getTObject("tableInfo");
});
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
/**
* 线
*
* @param time ()
*/
public static void sleepTime(int time) {
try {
// 添加延迟
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
/**
*
*
* @return
*/
public String ready() {
try {
// 添加1秒延迟
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ITObject params = TObject.newInstance();
params.putString("session", session + "," + token);
client.send("1003", params, new ICallback<MessageResponse>() {
@Override
public void action(MessageResponse messageResponse) {
}
});
return null;
}
}

View File

@ -0,0 +1,852 @@
package taurus.newRobot;
import com.taurus.core.entity.ITArray;
import com.taurus.core.entity.ITObject;
import com.taurus.core.entity.TObject;
import com.taurus.core.events.Event;
import com.taurus.core.events.IEventListener;
import com.taurus.core.plugin.redis.Redis;
import com.taurus.core.util.ICallback;
import com.taurus.core.util.StringUtil;
import hunan.HuNanChangSha;
import redis.clients.jedis.Jedis;
import taurus.client.Message;
import taurus.client.MessageResponse;
import taurus.client.SocketCode;
import taurus.client.TaurusClient;
import taurus.util.ChangShaSuanFaTest;
import taurus.util.TinHuChi;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class lianjiejiqiren {
// 会话标识
public String session = "";
// 访问令牌
public String token = "";
private HuNanChangSha huNanChangSha = new HuNanChangSha();
private Map<Integer, List<Integer>> playerOutcardsMap = new HashMap<>();
private Map<Integer, List<Integer>> playerchisMap = new HashMap<>();
private Map<Integer, List<Integer>> playerpengsMap = new HashMap<>();
private Map<Integer, List<Integer>> playermingsMap = new HashMap<>();
private Map<Integer, List<Integer>> playerzisMap = new HashMap<>();
public void lianjie(TaurusClient client, String wanfaId, Map<String, Object> mapclient) {
// 添加事件监听器处理网络消息
client.addEventListener(TaurusClient.NetClientEvent.OnEvent, new IEventListener() {
@Override
public void handleEvent(Event event) {
// 获取 msg
Message message = (Message) event.getParameter("msg");
if (message == null) {
return;
}
ITObject param = message.param;
//回调协议号
String command = message.command;
//根据玩法id 调用不同的回调
if (StringUtil.isNotEmpty(command)) {
if ("66".equalsIgnoreCase(wanfaId)) {//跑的快
// if ("2011".equalsIgnoreCase(command)) { //跑的快 初始化手牌
// huNanPaoDeKuai.paoDeKuaiCardInHead(param, client);
// } else if ("2021".equalsIgnoreCase(command)) { //出牌广播
// huNanPaoDeKuai.paoDekuaiChupaiGuangBo(param);
// } else if ("2004".equalsIgnoreCase(command)) { //出牌提示事件,牌权
// Integer player = param.getInt("player");
// Integer seat1 = param.getInt("seat");
//
// if (seat1 != null) {
// huNanPaoDeKuai.seat = seat1;
// }
//
// //出牌
// huNanPaoDeKuai.outCard(client);
// } else if ("2007".equalsIgnoreCase(command)) { //结算准备
// //type为1 为大结算 type为0为小结算
// Integer type = param.getInt("type");
//
// if (type == 0) {
// huNanPaoDeKuai.getSeatRemainHistory().clear();
// huNanPaoDeKuai.getPaoDekuaiCardInhand().clear();
// ITArray card_list = huNanPaoDeKuai.getCard_list().getTArray("card_list");
// card_list.clear();
// ready();
// }
//
// if (type == 1) {
// Jedis jedis11s = Redis.use("group1_db11").getJedis();
// try {
// String key = "g{" + groupId + "}:play:" + pid;
// jedis11s.hincrBy(key, "leftover_robot", 1);
// String sql = String.format("UPDATE `account` SET start = %d WHERE id = %d", 0, playerId);
// try {
// DataBase.use().executeUpdate(sql);
// } catch (SQLException e) {
// e.printStackTrace();
// }
// } catch (Exception e) {
// e.printStackTrace();
// } finally {
// jedis11s.close();
// }
// }
//
// } else if ("2009".equalsIgnoreCase(command)) {
// Jedis jedis21 = Redis.use().getJedis();
// sleepTime(3000);
//
// ITObject params = TObject.newInstance();
// String[] playerIds2 = null;
//
//
// Set<String> roomIds = jedis21.keys("room:*");
// String roomKey = "";
// //拿到所有的机器人
// List<Integer> robotIdsList = new ArrayList<>();
// String sql2 = "SELECT id FROM `account` WHERE jiqiren=9998";
// ITArray robotId2 = null;
// try {
// robotId2 = DataBase.use().executeQueryByTArray(sql2);
// } catch (SQLException e) {
// e.printStackTrace();
// }
// for (int j = 0; j < Objects.requireNonNull(robotId2).size(); j++) {
// robotIdsList.add(robotId2.getTObject(j).getInt("id"));
// }
//
//
// for (String roomId : roomIds) {
// String roomId1 = roomId.substring(roomId.indexOf(":") + 1);
// roomKey = "room:" + roomId1;
//
// if (jedis21.hget(roomKey, "players") != null) {
// String players = jedis21.hget(roomKey, "players");
// if (!players.equals("[]")) {
// players = players.substring(1, players.length() - 1);
// playerIds2 = players.split(",");
//
//
// if (playerIds2.length == 1) {
// for (String s : playerIds2) {
// if (robotIdsList.contains(Integer.parseInt(s))) { //房间里的人是机器人
// String gpid = jedis21.hget(roomKey, "gpid");
// String gpId = jedis21.hget(roomKey, "group");
// String key = "g{" + gpId + "}:play:" + gpid;
// if (!players.equals("[]") && pid == Integer.parseInt(gpid)) {
//
//
// if (JiQiRens.count != null && JiQiRens.count.containsKey(Integer.parseInt(gpid))) {
// Integer currentValue = JiQiRens.count.get(Integer.parseInt(gpid));
// if (currentValue > 0) {
// JiQiRens.count.put(Integer.parseInt(gpid), currentValue - 1);
// }
// }
//
//
// Jedis jedis20 = Redis.use("group1_db11").getJedis();
//
// jedis20.hincrBy(key, "leftover_robot", 1);
// jedis20.close();
// String sql = String.format("UPDATE `account` SET start = %d WHERE id = %d", 0, Integer.parseInt(s));
//
// try {
// DataBase.use().executeUpdate(sql);
// } catch (SQLException e) {
// e.printStackTrace();
// }
//
// client.send("1005", params, response -> {
//
// });
//
// }
// }
// }
// }
// }
// }
// }
//
// jedis21.close();
//
// } else if ("2008".equalsIgnoreCase(command)) { //解散房间时候恢复机器人账号可以使用
// Set<String> roomIds = scanRooms(jedis0, "room:*");
// String[] playerIds = null;
// for (String roomId : roomIds) {
// String rid = roomId.substring(roomId.indexOf(":") + 1);
// String roomKey = "room:" + rid;
// if (jedis0.hget(roomKey, "players") != null) {
// String players = jedis0.hget(roomKey, "players");
// if (!players.equals("[]")) {
// players = players.substring(1, players.length() - 1);
// playerIds = players.split(",");
// for (String pyids : playerIds) {
// if (Integer.parseInt(pyids) == playerId) {
// jedis0.del(roomId);
// }
// }
// }
// }
// }
// } else if ("2031".equalsIgnoreCase(command)) {
// ITObject params = TObject.newInstance();
// params.putInt("id", 0);
// client.send("1015", params, response -> {
//
// });
//
// }
// } else if ("22".equalsIgnoreCase(wanfaId)) { //湖南红中麻将
//
// if ("812".equalsIgnoreCase(command)) { //出牌广播
// HuNanHongZhong.drawCard(command, message);
// } else if ("811".equalsIgnoreCase(command)) {//初始化手牌
// huNanHongZhong.cardInHead(command, message, client);
// } else if ("819".equalsIgnoreCase(command)) { //摸牌
// huNanHongZhong.getCard(command, message);
// } else if ("813".equalsIgnoreCase(command)) { //出牌,牌权
// huNanHongZhong.outCard(client);
// } else if ("817".equalsIgnoreCase(command)) {//结算
// huNanHongZhong.getHongZhongCardInhand().clear();
// huNanHongZhong.getChuGuoCardInhand().clear();
//
// Integer type = param.getInt("type");
// if (type == 1 || type == 2) { //为1为大结算 为2为解散
// Jedis jedis11s = Redis.use("group1_db11").getJedis();
// try {
// String key = "g{" + groupId + "}:play:" + pid;
// jedis11s.hincrBy(key, "leftover_robot", 1);
////
// if (count != null && count.containsKey(pid)) {
// Integer currentValue = count.get(pid);
// if (currentValue > 0) {
// count.put(pid, currentValue - 1);
// }
// }
//
//
// String sql = String.format("UPDATE `account` SET start = %d WHERE id = %d", 0, playerId);
// try {
// DataBase.use().executeUpdate(sql);
// } catch (SQLException e) {
// e.printStackTrace();
// }
// } catch (Exception e) {
// e.printStackTrace();
// } finally {
// jedis11s.close();
// }
//
// if (count != null && count.containsKey(pid)) {
// Integer value = count.get(pid);
//
// // 如果找到了对应的 pid
// String shangxianRobot = jedis11.hget(playKey, "shangxian_robot");
// String leftoverRobot = jedis11.hget(playKey, "leftover_robot");
//
// if (shangxianRobot != null && leftoverRobot != null) {
// if (value == 0) {
// jedis11.hset(playKey, "leftover_robot", shangxianRobot);
// }
// }
// }
// }
//// playerState.pongGroups.clear();;
//// playerState.handCards.clear();
//// playerState.chiGroups.clear();
//// playerState.gangGroups.clear();;
//
//
// ready();
// } else if ("2009".equalsIgnoreCase(command)) {
// Jedis jedis21 = Redis.use().getJedis();
// sleepTime(3000);
// ITObject params = TObject.newInstance();
// String[] playerIds2 = null;
//
//
// Set<String> roomIds = jedis21.keys("room:*");
// String roomKey = "";
// //拿到所有的机器人
// List<Integer> robotIdsList = new ArrayList<>();
// String sql2 = "SELECT id FROM `account` WHERE jiqiren=9998";
// ITArray robotId2 = null;
// try {
// robotId2 = DataBase.use().executeQueryByTArray(sql2);
// } catch (SQLException e) {
// e.printStackTrace();
// }
// for (int j = 0; j < Objects.requireNonNull(robotId2).size(); j++) {
// robotIdsList.add(robotId2.getTObject(j).getInt("id"));
// }
//
//
// for (String roomId : roomIds) {
// String roomId1 = roomId.substring(roomId.indexOf(":") + 1);
// roomKey = "room:" + roomId1;
//
// if (jedis21.hget(roomKey, "players") != null) {
// String players = jedis21.hget(roomKey, "players");
// if (!players.equals("[]")) {
// players = players.substring(1, players.length() - 1);
// playerIds2 = players.split(",");
//
//
// if (playerIds2.length == 1) {
// for (String s : playerIds2) {
// if (robotIdsList.contains(Integer.parseInt(s))) { //房间里的人是机器人
// String gpid = jedis21.hget(roomKey, "gpid");
// String gpId = jedis21.hget(roomKey, "group");
// String key = "g{" + gpId + "}:play:" + gpid;
// if (!players.equals("[]") && pid == Integer.parseInt(gpid)) {
//
//
// if (JiQiRens.count != null && JiQiRens.count.containsKey(Integer.parseInt(gpid))) {
// Integer currentValue = JiQiRens.count.get(Integer.parseInt(gpid));
// if (currentValue > 0) {
// JiQiRens.count.put(Integer.parseInt(gpid), currentValue - 1);
// }
// }
//
//
// Jedis jedis20 = Redis.use("group1_db11").getJedis();
//
// jedis20.hincrBy(key, "leftover_robot", 1);
// jedis20.close();
// String sql = String.format("UPDATE `account` SET start = %d WHERE id = %d", 0, Integer.parseInt(s));
//
// try {
// DataBase.use().executeUpdate(sql);
// } catch (SQLException e) {
// e.printStackTrace();
// }
//
// client.send("1005", params, response -> {
//
// });
//
// }
// }
// }
// }
// }
// }
// }
//
// jedis21.close();
//
// } else if ("814".equalsIgnoreCase(command)) {//杠碰胡通知协议
//// sleepTime(2000);
// huNanHongZhong.actionCard(param, client);
// } else if ("820".equalsIgnoreCase(command)) {
// HuNanHongZhong.changePlayer(command, message);
// } else if ("815".equalsIgnoreCase(command)) { //服务器通知客户端有玩家执行了操作
// huNanHongZhong.shanchuchuguopai(param);
// } else if ("2008".equalsIgnoreCase(command)) { //解散房间时候恢复机器人账号可以使用
//// Jedis jedis11s = Redis.use("group1_db11").getJedis();
//// try {
//// String key = "g{" + groupId + "}:play:" + pid;
//// jedis11s.hincrBy(key, "leftover_robot", 1);
//// String sql = String.format("UPDATE `account` SET start = %d WHERE id = %d", 0, playerId);
//// DataBase.use().executeUpdate(sql);
// Set<String> roomIds = scanRooms(jedis0, "room:*");
// String[] playerIds = null;
// for (String roomId : roomIds) {
// String rid = roomId.substring(roomId.indexOf(":") + 1);
// String roomKey = "room:" + rid;
// if (jedis0.hget(roomKey, "players") != null) {
// String players = jedis0.hget(roomKey, "players");
// if (!players.equals("[]")) {
// players = players.substring(1, players.length() - 1);
// playerIds = players.split(",");
// for (String pyids : playerIds) {
// if (Integer.parseInt(pyids) == playerId) {
// jedis0.del(roomId);
// }
// }
// }
// }
// }
//
// }
} else if ("17".equalsIgnoreCase(wanfaId)) { //字牌放炮罚
// if ("811".equalsIgnoreCase(command)) { //初始化手牌
// huNanFangPaoFa.cardInHead(command, message, client);
//
// } else if ("819".equalsIgnoreCase(command)) { //摸牌
// huNanFangPaoFa.getCard(command, message);
// } else if ("813".equalsIgnoreCase(command)) { //出牌 牌权
// sleepTime(1500);
// huNanFangPaoFa.outCard(client);
// } else if ("812".equalsIgnoreCase(command)) { //出牌广播
// HuNanFangPaoFa.drawCard(command, message);
// } else if ("814".equalsIgnoreCase(command)) { //可执行的 操作 吃碰 偎提胡
// huNanFangPaoFa.actionCard(param, client);
// } else if ("817".equalsIgnoreCase(command)) { //结算
// huNanFangPaoFa.getFangPaoFaCardInhand().clear();
// ready();
// } else if ("822".equalsIgnoreCase(command)) { //放跑提示
//
// HuNanFangPaoFa.fangPaoTipEvent(message, client);
// } else if ("2008".equalsIgnoreCase(command)) { //解散房间时候恢复机器人账号可以使用
// try {
// String sql = String.format("UPDATE `account` SET start = %d WHERE id = %d", 0, playerId);
// DataBase.use().executeUpdate(sql);
//
// } catch (SQLException e) {
//
// }
// }
} else if ("108".equalsIgnoreCase(wanfaId)) {//转转麻将
// if ("811".equalsIgnoreCase(command)) {//初始化收手牌
// huNanZhuanZhuan.cardInHead(command, message, client);
// } else if ("812".equalsIgnoreCase(command)) {//出牌广播
// HuNanZhuanZhuan.drawCard(command, message);
// } else if ("819".equalsIgnoreCase(command)) {//摸牌
//
// huNanZhuanZhuan.getCard(command, message);
// } else if ("813".equalsIgnoreCase(command)) {//出牌提示
//// sleepTime(1500);
// huNanZhuanZhuan.outCard(client);
// } else if ("814".equalsIgnoreCase(command)) {//放招提示
// huNanZhuanZhuan.actionCard(param, client);
// } else if ("817".equalsIgnoreCase(command)) {//结算
// huNanZhuanZhuan.getZhuanZhuanCardInhand().clear();
// huNanZhuanZhuan.getZhuanZhuanchuguopai().clear();
// ready();
// } else if ("815".equalsIgnoreCase(command)) { //服务器通知客户端有玩家执行了操作
// huNanZhuanZhuan.shanchuchuguopai(param);
// } else if ("820".equalsIgnoreCase(command)) {//换牌提示
// HuNanZhuanZhuan.changePlayer(command, message);
// } else if ("2008".equalsIgnoreCase(command)) { //解散房间时候恢复机器人账号可以使用
// try {
// String sql = String.format("UPDATE `account` SET start = %d WHERE id = %d", 0, playerId);
// DataBase.use().executeUpdate(sql);
// } catch (SQLException e) {
//
// }
// }
} else if ("10".equalsIgnoreCase(wanfaId)) {
//长沙麻将 机器人处理事件
if ("811".equalsIgnoreCase(command)) {//初始化收手牌
// System.out.println("初始化手牌" + session);
Jedis jedis222 = Redis.use("group1_db2").getJedis();
for (Map.Entry<String, Object> entry : mapclient.entrySet()) {
System.out.println("mapclient++++++++++++++++++++++++++++++++"+mapclient);
System.out.println("client++++++++++++++++++++++++++++++++++"+client);
TaurusClient taurusClient = (TaurusClient) entry.getValue();
if (client.getId() == taurusClient.getId()) {
String key = entry.getKey();
System.out.println("key+++++++++++++++++++++++++++++++++++" + key);
if (jedis222.hget("{robortInfo}:" + key, "circleId") != null && jedis222.hget("{robortInfo}:" + key, "pid") != null) {
String circleId = jedis222.hget("{robortInfo}:" + key, "circleId");
System.out.println("circleId +++++++++++++++++++++++++"+circleId);
String pid = jedis222.hget("{robortInfo}:" + key, "pid");
System.out.println("pid +++++++++++++++++++++++++"+pid);
String getStart = "g{" + circleId + "}:play:" + pid;
if (!pid.equals("0")){
jedis222.hset(getStart, key, "2");
}
}
}
}
jedis222.close();
huNanChangSha.cardInHead(command, message, client);
System.out.println("client " + client);
} else if ("812".equalsIgnoreCase(command)) {//出牌广播
//{"opzicards":[{"opzicards":[],"playerId":101555}],"opmingcards":[{"opmingcards":[],"playerId":101555}],"outcard_map":[{"outcards":[209,205],"playerId":101555},{"outcards":[],"playerId":112233}],"card":205,"opchicards":[{"opchicards":[105,103],"playerId":101555}],"oppengcards":[{"oppengcards":[],"playerId":101555}],"seat":1}
ITArray outcard_map = param.getTArray("outcard_map");
ITArray opchicards = param.getTArray("opchicards");
ITArray oppengcards = param.getTArray("oppengcards");
ITArray opmingcards = param.getTArray("opmingcards");
ITArray opzicards = param.getTArray("opzicards");
// 清空旧数据,用新数据完全覆盖
playerOutcardsMap.clear();
playerchisMap.clear();
playerpengsMap.clear();
playermingsMap.clear();
playerzisMap.clear();
//出过的牌
if (outcard_map != null) {
for (int i = 0; i < outcard_map.size(); i++) {
ITObject playerData = outcard_map.getTObject(i);
int playerId = playerData.getInt("playerId");
ITArray outcardsArray = playerData.getTArray("outcards");
// 转换为List<Integer>
List<Integer> outcardsList = new ArrayList<>();
for (int j = 0; j < outcardsArray.size(); j++) {
outcardsList.add(outcardsArray.getInt(j));
}
// 存储到Map中覆盖旧数据
playerOutcardsMap.put(playerId, outcardsList);
}
}
//吃的牌
if (opchicards != null) {
for (int i = 0; i < opchicards.size(); i++) {
ITObject playerData = opchicards.getTObject(i);
int playerId = playerData.getInt("playerId");
ITArray outchiArray = playerData.getTArray("opchicards");
List<Integer> outchiList = new ArrayList<>();
for (int j = 0; j < outchiArray.size(); j++) {
outchiList.add(outchiArray.getInt(j));
}
playerchisMap.put(playerId, outchiList);
}
}
//碰的牌
if (oppengcards != null) {
for (int i = 0; i < oppengcards.size(); i++) {
ITObject playerData = oppengcards.getTObject(i);
int playerId = playerData.getInt("playerId");
ITArray outpengArray = playerData.getTArray("oppengcards");
List<Integer> outpengList = new ArrayList<>();
for (int j = 0; j < outpengArray.size(); j++) {
outpengList.add(outpengArray.getInt(j));
}
playerpengsMap.put(playerId, outpengList);
}
}
//明杠的牌
if (opmingcards != null) {
for (int i = 0; i < opmingcards.size(); i++) {
ITObject playerData = opmingcards.getTObject(i);
int playerId = playerData.getInt("playerId");
ITArray outmingArray = playerData.getTArray("opmingcards");
List<Integer> outmingList = new ArrayList<>();
for (int j = 0; j < outmingArray.size(); j++) {
outmingList.add(outmingArray.getInt(j));
}
playermingsMap.put(playerId, outmingList);
}
}
//暗杠的牌
if (opzicards != null) {
for (int i = 0; i < opzicards.size(); i++) {
ITObject playerData = opzicards.getTObject(i);
int playerId = playerData.getInt("playerId");
ITArray outziArray = playerData.getTArray("opzicards");
List<Integer> outziList = new ArrayList<>();
for (int j = 0; j < outziArray.size(); j++) {
outziList.add(outziArray.getInt(j));
}
playerzisMap.put(playerId, outziList);
}
}
HuNanChangSha.drawCard(command, message);
} else if ("819".equalsIgnoreCase(command)) {
for (Map.Entry<String, Object> entry : mapclient.entrySet()) {
TaurusClient taurusClient = (TaurusClient) entry.getValue();
if (client == taurusClient) {
//摸牌
huNanChangSha.getCard(command, message, taurusClient,mapclient);
}
}
} else if ("813".equalsIgnoreCase(command)) {//出牌提示
System.out.println("出牌提示 ++++++++++++++++++++++++++++++++");
System.out.println("mapclient ++++++++++++++++" + mapclient);
System.out.println("进入出牌11111111111111112222222");
System.out.println("client4444444444444444" + client);
huNanChangSha.outCard(client, playerOutcardsMap, playerchisMap, playerpengsMap, playermingsMap, playerzisMap);
} else if ("814".equalsIgnoreCase(command)) {
//吃碰杠
huNanChangSha.actionCard(param, client);
} else if ("2009".equalsIgnoreCase(command)) {
//db0
Jedis jedis22 = Redis.use().getJedis();
//db2
Jedis jedis33 = Redis.use("group1_db2").getJedis();
Jedis jedis = Redis.use("group1_db0").getJedis();
Integer aid = param.getInt("aid");
String getKey = "{robortInfo}:" + aid;
System.out.println("param ++++++++++++++++ " + param);
ITObject params = TObject.newInstance();
String[] playerIds2 = null;
if (jedis33.hget(getKey, "pid") != null) {
String circleId1 = jedis33.hget(getKey, "circleId");
String pid = jedis33.hget(getKey, "pid");
String key = "g{" + circleId1 + "}:play:" + pid;
if (jedis33.hget(getKey, "room_id") != null) {
String roomid = jedis33.hget(getKey, "room_id");
String roomKey = "room:" + roomid;
if (jedis22.hget(roomKey, "players") != null) {
String players = jedis22.hget(roomKey, "players");
if (!players.equals("[]")) {
players = players.substring(1, players.length() - 1);
playerIds2 = players.split(",");
if (playerIds2.length == 1) {
if (!pid.equals("0")) {
Jedis jedis20 = Redis.use("group1_db11").getJedis();
System.out.println("进入 -------------------------");
jedis20.hincrBy(key, "leftover_robot", 1);
jedis20.close();
System.out.println("mapclient--------------" + mapclient);
String room_id = jedis33.hget(getKey, "room_id");
for (Map.Entry<String, Object> entry : mapclient.entrySet()) {
String key1 = entry.getKey();
if (Integer.parseInt(key1) == aid) {
// 获取指定field的值
String value = jedis33.hget(key, String.valueOf(aid));
if (!value.equals("2")) {
sleepTime(4000);
jedis33.hset(getKey, "circleId", "0");
jedis33.hset(getKey, "pid", "0");
jedis33.hset(getKey, "room_id", "0");
//退出修改机器人状态
jedis33.hset(key, String.valueOf(aid), "0");
TaurusClient taurusClient = (TaurusClient) entry.getValue();
taurusClient.send("1005", params, response -> {
});
}
}
}
}
}
}
}
}
jedis33.close();
jedis22.close();
jedis.close();
}
} else if ("817".equalsIgnoreCase(command)) {//结算
huNanChangSha.getChangShaCardInhand().clear();
huNanChangSha.getChuGuoCardInhand().clear();
huNanChangSha.getpongGroup().clear();
huNanChangSha.getchowGroup().clear();
TinHuChi.lastTingCount = 0;
TinHuChi.isMoreThanLast = false;
ChangShaSuanFaTest.isTin = false;
ChangShaSuanFaTest.isChi = false;
ChangShaSuanFaTest.isPeng = false;
ChangShaSuanFaTest.tinCards.clear();
Integer type = param.getInt("type");
if (type == 1 || type == 2) { //为1为大结算 为2为解散
Jedis jedis11s = Redis.use("group1_db11").getJedis();
try {
//db2
Jedis jedis21 = Redis.use("group1_db2").getJedis();
try {
for (Map.Entry<String, Object> entry : mapclient.entrySet()) {
TaurusClient taurusClient = (TaurusClient) entry.getValue();
if (client == taurusClient) {
String key = entry.getKey();
if (jedis21.hget("{robortInfo}:" + key, "circleId") != null && jedis21.hget("{robortInfo}:" + key, "pid") != null) {
String circleId = jedis21.hget("{robortInfo}:" + key, "circleId");
String pid = jedis21.hget("{robortInfo}:" + key, "pid");
String getStart = "g{" + circleId + "}:play:" + pid;
jedis21.hset(getStart, key, "0");
jedis11s.hincrBy(getStart, "leftover_robot", 1);
jedis21.hset("{robortInfo}:" + key, "circleId", "0");
jedis21.hset("{robortInfo}:" + key, "pid", "0");
jedis21.hset("{robortInfo}:" + key, "room_id", "0");
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
jedis21.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
jedis11s.close();
}
// if (count != null && count.containsKey(pid)) {
// Integer value = count.get(pid);
//
// // 如果找到了对应的 pid
// Jedis jedis12 = Redis.use("group1_db11").getJedis();
//
// String shangxianRobot = jedis12.hget(playKey, "shangxian_robot");
// String leftoverRobot = jedis12.hget(playKey, "leftover_robot");
//
// if (shangxianRobot != null && leftoverRobot != null) {
// if (value == 0) {
// jedis12.hset(playKey, "leftover_robot", shangxianRobot);
// }
// }
// jedis12.close();
// }
}
ready(client);
//写定时器
} else if ("815".equalsIgnoreCase(command)) { //服务器通知客户端有玩家执行了操作
//[TCP->815] data:{"playerid":101555,"card":104,"opcard":[105,103],"from_seat":2,"type":1,"opengang":false}
huNanChangSha.shanchuchuguopai(param);
} else if ("820".equalsIgnoreCase(command)) {//换牌提示
huNanChangSha.changePlayer(command, message);
} else if ("2008".equalsIgnoreCase(command)) { //解散房间时候恢复机器人账号可以使用
// Set<String> roomIds = scanRooms(jedis0, "room:*");
// String[] playerIds = null;
// for (String roomId : roomIds) {
// String rid = roomId.substring(roomId.indexOf(":") + 1);
// String roomKey = "room:" + rid;
//
// if (jedis0.hget(roomKey, "players") != null) {
// String players = jedis0.hget(roomKey, "players");
//
// if (!players.equals("[]")) {
// players = players.substring(1, players.length() - 1);
// playerIds = players.split(",");
//
// for (String pyids : playerIds) {
//
// if (Integer.parseInt(pyids) == playerId) {
// jedis0.del(roomId);
// }
// }
// }
// }
// }
} else if ("825".equalsIgnoreCase(command)) {
ITObject params = TObject.newInstance();
params.putInt("qi", 0);
params.putInt("id", 1);
client.send("612", params, response -> {
});
} else if ("822".equalsIgnoreCase(command)) {
ITObject params = TObject.newInstance();
//params.putInt("qi", 0);
params.putInt("id", 1);
//[TCP->822] data:{"tip_list":[{"type":8,"id":1,"opcard":[],"weight":8,"card":0}],"types":[{"type":21,"value":1}]}
//板胡Event [TCP->823] data:{"type":8,"seat":1,"data":[{"opcard":[204,204,204,108,108,108],"type":21,"value":1}]}
client.send("612", params, response -> {
});
} else if ("835".equalsIgnoreCase(command)) { //听牌天听
ITObject params = TObject.newInstance();
params.putInt("qi", 0);
params.putInt("id", 1);
client.send("612", params, response -> {
});
}
//END 长沙麻将
}
}
}
});
// 添加连接状态监听器
client.addEventListener(TaurusClient.NetClientEvent.Connect, new
IEventListener() {
@Override
public void handleEvent(Event event) {
SocketCode code = (SocketCode) event.getParameter("code");
if (code == SocketCode.Connect) {
} else {
}
}
});
}
/**
*
*
* @return
*/
public String ready(TaurusClient client) {
try {
// 添加1秒延迟
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
// cardInhand = new ArrayList<Integer>();
// HuNanHongZhong.hongZhongCardInhand();
// HuNanFangPaoFa.fangPaoFaCardInhand();
ITObject params = TObject.newInstance();
params.putString("session", session + "," + token);
client.send("1003", params, new ICallback<MessageResponse>() {
@Override
public void action(MessageResponse messageResponse) {
}
});
return null;
}
public static void sleepTime(int time) {
try {
// 添加延迟
Thread.sleep(time);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

View File

@ -1,8 +1,8 @@
import com.taurus.core.plugin.PluginService; import com.taurus.core.plugin.PluginService;
import hunan.DoTest; //import hunan.DoTest;
import hunan.HuNanChangSha; import hunan.HuNanChangSha;
import hunan.HuNanHongZhong; import hunan.HuNanHongZhong;
import hunan.JiQiRens; //import hunan.JiQiRens;
import taurus.client.NetManager; import taurus.client.NetManager;
import taurus.newRobot.AddRedisRobot; import taurus.newRobot.AddRedisRobot;
import taurus.newRobot.AddRoomRobot; import taurus.newRobot.AddRoomRobot;