Commit 02637e1a authored by feihuaduo's avatar feihuaduo

适配决斗助手1.0卡组协议,卡组码长度优化

parent 2221b0bb
......@@ -190,14 +190,21 @@ public interface Constants {
*/
String PATH_DECK = "/deck";
String SCHEME_HTTP = "http";
String SCHEME_HTTPS = "https";
String SCHEME_APP = "ygo";
String URI_HOST = "deck";
String QUERY_YDK = "ydk";
String QUERY_NAME = "name";
String QUERY_MAIN = "main";
String QUERY_EXTRA = "extra";
String QUERY_SIDE = "side";
String QUERY_MAIN = "m";
String QUERY_EXTRA = "e";
String QUERY_SIDE = "s";
String QUERY_VERSION="v";
String QUERY_MAIN_ALL = "main";
String QUERY_EXTRA_ALL = "extra";
String QUERY_SIDE_ALL = "side";
String QUERY_YGO_TYPE="ygotype";
String ARG_DECK="deck";
String PATH_ROOM = "/room";
String QUERY_HOST = "host";
String QUERY_PORT = "port";
......@@ -214,4 +221,5 @@ public interface Constants {
String TAG = "ygo-java";
String DEF_ENCODING = "utf-8";
}
package cn.garymb.ygomobile.bean;
/**
* Create By feihua On 2021/9/25
*/
public class CardIdNum {
private String cardIdNum;
private String CardIdNumCompressed;
public CardIdNum(String cardIdNum, String cardIdNumCompressed) {
this.cardIdNum = cardIdNum;
CardIdNumCompressed = cardIdNumCompressed;
}
public String getCardIdNum() {
return cardIdNum;
}
public void setCardIdNum(String cardIdNum) {
this.cardIdNum = cardIdNum;
}
public String getCardIdNumCompressed() {
return CardIdNumCompressed;
}
public void setCardIdNumCompressed(String cardIdNumCompressed) {
CardIdNumCompressed = cardIdNumCompressed;
}
}
package cn.garymb.ygomobile.bean;
import static cn.garymb.ygomobile.Constants.ARG_DECK;
import static cn.garymb.ygomobile.Constants.QUERY_EXTRA;
import static cn.garymb.ygomobile.Constants.QUERY_EXTRA_ALL;
import static cn.garymb.ygomobile.Constants.QUERY_MAIN;
import static cn.garymb.ygomobile.Constants.QUERY_MAIN_ALL;
import static cn.garymb.ygomobile.Constants.QUERY_SIDE;
import static cn.garymb.ygomobile.Constants.QUERY_SIDE_ALL;
import static cn.garymb.ygomobile.Constants.QUERY_VERSION;
import static cn.garymb.ygomobile.Constants.QUERY_YDK;
import static cn.garymb.ygomobile.Constants.QUERY_YGO_TYPE;
import static cn.garymb.ygomobile.Constants.YDK_FILE_EX;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
......@@ -12,17 +24,86 @@ import java.util.List;
import cn.garymb.ygomobile.Constants;
import cn.garymb.ygomobile.ui.cards.deck.DeckUtils;
import static cn.garymb.ygomobile.Constants.QUERY_EXTRA;
import static cn.garymb.ygomobile.Constants.QUERY_MAIN;
import static cn.garymb.ygomobile.Constants.QUERY_SIDE;
import static cn.garymb.ygomobile.Constants.QUERY_YDK;
import static cn.garymb.ygomobile.Constants.YDK_FILE_EX;
public class Deck implements Parcelable {
private String name;
public static final Creator<Deck> CREATOR = new Creator<Deck>() {
@Override
public Deck createFromParcel(Parcel source) {
return new Deck(source);
}
@Override
public Deck[] newArray(int size) {
return new Deck[size];
}
};
private static final int YGO_PROTOCOL_0 = 0;
private static final int YGO_PROTOCOL_1 = 1;
private static final String CARD_DIVIDE_ID = "_";
private static final String CARD_DIVIDE_NUM = "*";
private static final String CARD_NUM_2 = "-";
private static final String CARD_NUM_3 = "!";
private static List<CardIdNum> cardIdNumList;
static {
cardIdNumList = new ArrayList<>();
cardIdNumList.add(new CardIdNum("10", "a"));
cardIdNumList.add(new CardIdNum("11", "b"));
cardIdNumList.add(new CardIdNum("12", "c"));
cardIdNumList.add(new CardIdNum("13", "d"));
cardIdNumList.add(new CardIdNum("14", "e"));
cardIdNumList.add(new CardIdNum("15", "f"));
cardIdNumList.add(new CardIdNum("16", "g"));
cardIdNumList.add(new CardIdNum("17", "h"));
cardIdNumList.add(new CardIdNum("18", "i"));
cardIdNumList.add(new CardIdNum("19", "j"));
cardIdNumList.add(new CardIdNum("20", "k"));
cardIdNumList.add(new CardIdNum("21", "l"));
cardIdNumList.add(new CardIdNum("22", "m"));
cardIdNumList.add(new CardIdNum("23", "n"));
cardIdNumList.add(new CardIdNum("24", "o"));
cardIdNumList.add(new CardIdNum("25", "p"));
cardIdNumList.add(new CardIdNum("26", "q"));
cardIdNumList.add(new CardIdNum("27", "r"));
cardIdNumList.add(new CardIdNum("28", "s"));
cardIdNumList.add(new CardIdNum("29", "t"));
cardIdNumList.add(new CardIdNum("30", "u"));
cardIdNumList.add(new CardIdNum("31", "v"));
cardIdNumList.add(new CardIdNum("32", "w"));
cardIdNumList.add(new CardIdNum("33", "x"));
cardIdNumList.add(new CardIdNum("34", "y"));
cardIdNumList.add(new CardIdNum("35", "z"));
cardIdNumList.add(new CardIdNum("36", "A"));
cardIdNumList.add(new CardIdNum("37", "B"));
cardIdNumList.add(new CardIdNum("38", "C"));
cardIdNumList.add(new CardIdNum("39", "D"));
cardIdNumList.add(new CardIdNum("40", "E"));
cardIdNumList.add(new CardIdNum("41", "F"));
cardIdNumList.add(new CardIdNum("42", "G"));
cardIdNumList.add(new CardIdNum("43", "H"));
cardIdNumList.add(new CardIdNum("44", "I"));
cardIdNumList.add(new CardIdNum("45", "J"));
cardIdNumList.add(new CardIdNum("46", "K"));
cardIdNumList.add(new CardIdNum("47", "L"));
cardIdNumList.add(new CardIdNum("48", "M"));
cardIdNumList.add(new CardIdNum("49", "N"));
cardIdNumList.add(new CardIdNum("50", "O"));
cardIdNumList.add(new CardIdNum("51", "P"));
cardIdNumList.add(new CardIdNum("52", "Q"));
cardIdNumList.add(new CardIdNum("53", "R"));
cardIdNumList.add(new CardIdNum("54", "S"));
cardIdNumList.add(new CardIdNum("55", "T"));
cardIdNumList.add(new CardIdNum("56", "U"));
cardIdNumList.add(new CardIdNum("57", "V"));
cardIdNumList.add(new CardIdNum("58", "W"));
cardIdNumList.add(new CardIdNum("59", "X"));
cardIdNumList.add(new CardIdNum("60", "Y"));
cardIdNumList.add(new CardIdNum("61", "Z"));
}
private final ArrayList<Integer> mainlist;
private final ArrayList<Integer> extraList;
private final ArrayList<Integer> sideList;
private String name;
public Deck() {
mainlist = new ArrayList<>();
......@@ -32,37 +113,87 @@ public class Deck implements Parcelable {
public Deck(String name, Uri uri) {
this(name);
String main = uri.getQueryParameter(QUERY_MAIN);
String extra = uri.getQueryParameter(QUERY_EXTRA);
String side = uri.getQueryParameter(QUERY_SIDE);
int version = YGO_PROTOCOL_0;
try {
String ygoType = uri.getQueryParameter(QUERY_YGO_TYPE);
if (ygoType.equals(ARG_DECK)) {
version = Integer.parseInt(uri.getQueryParameter(QUERY_VERSION));
}
} catch (Exception exception) {
version = YGO_PROTOCOL_0;
}
String main=null, extra=null, side=null;
switch (version) {
case YGO_PROTOCOL_0:
try {
main = uri.getQueryParameter(QUERY_MAIN_ALL);
}catch (Exception e){}
try {
extra = uri.getQueryParameter(QUERY_EXTRA_ALL);
}catch (Exception e){}
try {
side = uri.getQueryParameter(QUERY_SIDE_ALL);
}catch (Exception e){}
break;
case YGO_PROTOCOL_1:
try {
main = uri.getQueryParameter(QUERY_MAIN);
}catch (Exception e){}
try {
extra = uri.getQueryParameter(QUERY_EXTRA);
}catch (Exception e){}
try {
side = uri.getQueryParameter(QUERY_SIDE);
}catch (Exception e){}
break;
default:
try {
main = uri.getQueryParameter(QUERY_MAIN_ALL);
}catch (Exception e){}
try {
extra = uri.getQueryParameter(QUERY_EXTRA_ALL);
}catch (Exception e){}
try {
side = uri.getQueryParameter(QUERY_SIDE_ALL);
}catch (Exception e){}
}
if (!TextUtils.isEmpty(main)) {
String[] mains = main.split("_");
String[] mains = main.split(CARD_DIVIDE_ID);
for (String m : mains) {
int []idNum=toIdAndNum(m);
int[] idNum = toIdAndNum(m, version);
if (idNum[0] > 0) {
for (int i=0;i<idNum[1];i++){
for (int i = 0; i < idNum[1]; i++) {
mainlist.add(idNum[0]);
}
}
}
}
if (!TextUtils.isEmpty(extra)) {
String[] extras = extra.split("_");
String[] extras = extra.split(CARD_DIVIDE_ID);
for (String m : extras) {
int []idNum=toIdAndNum(m);
int[] idNum = toIdAndNum(m, version);
if (idNum[0] > 0) {
for (int i=0;i<idNum[1];i++){
for (int i = 0; i < idNum[1]; i++) {
extraList.add(idNum[0]);
}
}
}
}
if (!TextUtils.isEmpty(side)) {
String[] sides = side.split("_");
String[] sides = side.split(CARD_DIVIDE_ID);
for (String m : sides) {
int []idNum=toIdAndNum(m);
int[] idNum = toIdAndNum(m, version);
if (idNum[0] > 0) {
for (int i=0;i<idNum[1];i++){
for (int i = 0; i < idNum[1]; i++) {
sideList.add(idNum[0]);
}
}
......@@ -70,24 +201,76 @@ public class Deck implements Parcelable {
}
}
private int[] toIdAndNum(String m) {
int[] idNum={0,1};
if (m.contains("*")){
try{
idNum[1]=Integer.parseInt(m.substring(m.length()-1));
}catch (Exception e){
public Deck(Uri uri) {
this(uri.getQueryParameter(QUERY_YDK), uri);
}
public Deck(String name) {
this();
this.name = name;
}
idNum[0]=toId(m.substring(0,m.length()-2));
}else {
idNum[0]=toId(m);
protected Deck(Parcel in) {
this.name = in.readString();
this.mainlist = new ArrayList<Integer>();
in.readList(this.mainlist, Integer.class.getClassLoader());
this.extraList = new ArrayList<Integer>();
in.readList(this.extraList, Integer.class.getClassLoader());
this.sideList = new ArrayList<Integer>();
in.readList(this.sideList, Integer.class.getClassLoader());
}
private int[] toIdAndNum(String m, int protocol) {
//元素0为卡密,元素1为卡片数量
int[] idNum;
switch (protocol) {
case YGO_PROTOCOL_0:
idNum=toIdAndNum0(m);
break;
case YGO_PROTOCOL_1:
idNum=toIdAndNum1(m);
break;
default:
idNum=toIdAndNum0(m);
break;
}
return idNum;
}
public Deck(Uri uri) {
this(uri.getQueryParameter(QUERY_YDK), uri);
private int[] toIdAndNum1(String m) {
//元素0为卡密,元素1为卡片数量
int[] idNum = {0, 1};
if (m.contains(CARD_NUM_2)) {
idNum[0] = toId(m.substring(0, m.length() - 1));
idNum[1] = 2;
} else if (m.contains(CARD_NUM_3)) {
idNum[0] = toId(m.substring(0, m.length() - 1));
idNum[1] = 3;
} else {
idNum[0] = toId(m);
}
return idNum;
}
private int[] toIdAndNum0(String m) {
//元素0为卡密,元素1为卡片数量
int[] idNum = {0, 1};
if (m.contains(CARD_DIVIDE_NUM)) {
try {
idNum[1] = Integer.parseInt(m.substring(m.length() - 1));
} catch (Exception e) {
}
idNum[0] = toId(m.substring(0, m.length() - 2));
} else {
idNum[0] = toId(m);
}
return idNum;
}
public Uri toAppUri() {
......@@ -106,9 +289,13 @@ public class Deck implements Parcelable {
if (!TextUtils.isEmpty(name)) {
uri.appendQueryParameter(Constants.QUERY_NAME, name);
}
uri.appendQueryParameter(Constants.QUERY_MAIN, toString(mainlist))
.appendQueryParameter(Constants.QUERY_EXTRA, toString(extraList))
.appendQueryParameter(Constants.QUERY_SIDE, toString(sideList));
uri.appendQueryParameter(Constants.QUERY_VERSION, "1");
if (mainlist.size() != 0)
uri.appendQueryParameter(Constants.QUERY_MAIN, toString(mainlist));
if (extraList.size() != 0)
uri.appendQueryParameter(Constants.QUERY_EXTRA, toString(extraList));
if (sideList.size() != 0)
uri.appendQueryParameter(Constants.QUERY_SIDE, toString(sideList));
return uri.build();
}
......@@ -117,13 +304,14 @@ public class Deck implements Parcelable {
for (int i = 0; i < ids.size(); i++) {
Integer id = ids.get(i);
if (i > 0) {
builder.append("_");
builder.append(CARD_DIVIDE_ID);
}
if (id > 0) {
//如果需要使用十六进制码:builder.append(compressedId(id));
builder.append(id);
//如果需要使用十六进制码:
builder.append(compressedId(id));
// builder.append(id);
//如果是最后一张就不用对比下张卡
if(i!=ids.size()-1) {
if (i != ids.size() - 1) {
int id1 = ids.get(i + 1);
//同名卡张数
int tNum = 1;
......@@ -131,7 +319,7 @@ public class Deck implements Parcelable {
if (id1 == id) {
tNum++;
//如果是倒数第二张就不用对比下下张卡
if(i!=ids.size()-2) {
if (i != ids.size() - 2) {
id1 = ids.get(i + 2);
//如果下下张是同名卡
if (id1 == id) {
......@@ -143,7 +331,12 @@ public class Deck implements Parcelable {
}
//如果有同名卡
if (tNum > 1) {
builder.append("*" + tNum);
if (tNum == 2) {
builder.append(CARD_NUM_2);
} else {
builder.append(CARD_NUM_3);
}
}
}
}
......@@ -152,13 +345,73 @@ public class Deck implements Parcelable {
}
//压缩卡密,目前直接转换为16进制
private String compressedId(int id){
return Integer.toHexString(id);
private String compressedId(int id) {
StringBuilder compressedId1 = new StringBuilder();
StringBuilder compressedId2 = new StringBuilder();
String ids = id + "";
while (ids.startsWith("0")) {
ids = ids.substring(1);
}
int lenght = ids.length();
for (int i = 0; i < lenght / 2; i++) {
int start = i * 2;
int end = Math.min(start + 2, lenght);
compressedId1.append(getCardIdCompressedId(ids.substring(start, end)));
}
int currentPosition = 0;
while (currentPosition < lenght) {
int start = currentPosition;
int end = Math.min(start + 2, lenght);
String message = ids.substring(start, end);
String result = getCardIdCompressedId(message);
if (message.equals(result)) {
compressedId2.append(ids.charAt(start));
currentPosition++;
} else {
compressedId2.append(result);
currentPosition = currentPosition + 2;
}
}
return compressedId2.length() < compressedId1.length() ? compressedId2.toString() : compressedId1.toString();
}
private String getCardIdCompressedId(String idNum) {
for (CardIdNum cardIdNum : cardIdNumList) {
if (cardIdNum.getCardIdNum().equals(idNum)) {
return cardIdNum.getCardIdNumCompressed();
}
}
return idNum;
}
private String getCardIdUnCompressedId(String compressedNum) {
for (CardIdNum cardIdNum : cardIdNumList) {
if (cardIdNum.getCardIdNumCompressed().equals(compressedNum)) {
return cardIdNum.getCardIdNum();
}
}
return compressedNum;
}
//解析卡密,目前直接16进制转换为10进制
private int unId(String id){
return Integer.parseInt(id,16);
private int unId(String id) {
StringBuilder compressedId = new StringBuilder();
String[] sList = id.split("");
for (String s : sList)
compressedId.append(getCardIdUnCompressedId(s));
int cardId;
try {
cardId = Integer.parseInt(compressedId.toString());
} catch (Exception e) {
cardId = 0;
}
return cardId;
}
public String getName() {
......@@ -196,18 +449,14 @@ public class Deck implements Parcelable {
private int toId(String str) {
if (TextUtils.isEmpty(str)) return 0;
try {
//如果需要返回16进制码:return unId(str)
return Integer.parseInt(str);
//如果需要返回16进制码:
return unId(str);
// return Integer.parseInt(str);
} catch (Exception e) {
return 0;
}
}
public Deck(String name) {
this();
this.name = name;
}
public List<Integer> getSideList() {
return sideList;
}
......@@ -253,26 +502,4 @@ public class Deck implements Parcelable {
dest.writeList(this.extraList);
dest.writeList(this.sideList);
}
protected Deck(Parcel in) {
this.name = in.readString();
this.mainlist = new ArrayList<Integer>();
in.readList(this.mainlist, Integer.class.getClassLoader());
this.extraList = new ArrayList<Integer>();
in.readList(this.extraList, Integer.class.getClassLoader());
this.sideList = new ArrayList<Integer>();
in.readList(this.sideList, Integer.class.getClassLoader());
}
public static final Creator<Deck> CREATOR = new Creator<Deck>() {
@Override
public Deck createFromParcel(Parcel source) {
return new Deck(source);
}
@Override
public Deck[] newArray(int size) {
return new Deck[size];
}
};
}
......@@ -804,7 +804,7 @@ public class DeckManagerActivity extends BaseCardsActivity implements RecyclerVi
BitmapUtil.saveBitmap(bitmap, savePath, 50);
builderShareLoading.dismiss();
DialogUtils du = DialogUtils.getdx(this);
View viewDialog = du.dialogBottomSheet(R.layout.dialog_deck_share, 0);
View viewDialog = du.dialogBottomSheet(R.layout.dialog_deck_share,0);
ImageView iv_image = viewDialog.findViewById(R.id.iv_image);
Button bt_image_share = viewDialog.findViewById(R.id.bt_image_share);
Button bt_code_share = viewDialog.findViewById(R.id.bt_code_share);
......
......@@ -3,12 +3,10 @@ package com.ourygo.assistant.util;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import com.ourygo.assistant.base.listener.OnClipChangedListener;
import com.ourygo.assistant.base.listener.OnDuelAssistantListener;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
......@@ -87,6 +85,19 @@ public class DuelAssistantManagement implements OnClipChangedListener {
if (deckStart != -1) {
onSaveDeck(message.substring(deckStart + Record.DECK_URL_PREFIX.length()), true, id);
return true;
} else if (message.contains("?" + Record.ARG_YGO_TYPE + "=" + Record.ARG_DECK) || message.contains("&" + Record.ARG_YGO_TYPE + "=" + Record.ARG_DECK)) {
String m1 = "?" + Record.ARG_YGO_TYPE + "=" + Record.ARG_DECK;
String m2 = "&" + Record.ARG_YGO_TYPE + "=" + Record.ARG_DECK;
int s1 = message.indexOf(m1);
if (s1 == -1)
s1 = message.indexOf(m2);
int start=message.lastIndexOf(Record.DECK_URL_PREFIX,s1);
if (start==-1)
start=message.lastIndexOf(Record.HTTP_URL_PREFIX,s1);
if (start==-1)
start=message.lastIndexOf(Record.HTTPS_URL_PREFIX,s1);
onSaveDeck(message.substring(start + Record.DECK_URL_PREFIX.length()), true, id);
return true;
}
return false;
}
......@@ -94,17 +105,18 @@ public class DuelAssistantManagement implements OnClipChangedListener {
public boolean roomCheck(String message, int id) {
int start = -1;
int end = -1;
start=message.indexOf(Record.ROOM_PREFIX);
if (start!=-1){
end=message.indexOf(Record.ROOM_END,start);
if (end!=-1){
message=message.substring(start,end);
JSONObject jsonObject= null;
start = message.indexOf(Record.ROOM_PREFIX);
if (start != -1) {
end = message.indexOf(Record.ROOM_END, start);
if (end != -1) {
message = message.substring(start, end);
JSONObject jsonObject = null;
try {
jsonObject = new JSONObject(message);
onJoinRoom(jsonObject.getString(Record.ARG_HOST),jsonObject.getInt(Record.ARG_PORT),jsonObject.getString(Record.ARG_PASSWORD),id);
onJoinRoom(jsonObject.getString(Record.ARG_HOST), jsonObject.getInt(Record.ARG_PORT), jsonObject.getString(Record.ARG_PASSWORD), id);
return true;
} catch (JSONException e) { e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
......@@ -131,7 +143,7 @@ public class DuelAssistantManagement implements OnClipChangedListener {
if (end - start == passwordPrefixKey.length())
return false;
}
onJoinRoom(null,0,message.substring(start, end), id);
onJoinRoom(null, 0, message.substring(start, end), id);
return true;
}
return false;
......@@ -160,12 +172,12 @@ public class DuelAssistantManagement implements OnClipChangedListener {
return false;
}
private void onJoinRoom(String host,int port,String password, int id) {
private void onJoinRoom(String host, int port, String password, int id) {
int i = 0;
while (i < onDuelAssistantListenerList.size()) {
OnDuelAssistantListener onDuelAssistantListener = onDuelAssistantListenerList.get(i);
if (onDuelAssistantListener.isListenerEffective()) {
onDuelAssistantListener.onJoinRoom(host,port,password, id);
onDuelAssistantListener.onJoinRoom(host, port, password, id);
i++;
} else {
onDuelAssistantListenerList.remove(i);
......
......@@ -32,7 +32,12 @@ public class Record {
//卡组url前缀
public static final String DECK_URL_PREFIX = "ygo://deck";
public static final String HTTP_URL_PREFIX = "http://";
public static final String HTTPS_URL_PREFIX = "https://";
public static final String ARG_PORT = "port";
public static final String ARG_HOST = "host";
public static final String ARG_PASSWORD = "password";
public static final String ARG_YGO_TYPE="ygotype";
public static final String ARG_DECK="deck";
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment