Java 查询英雄联盟玩家战绩

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

先来张效果图

Java 查询英雄联盟玩家战绩_第1张图片

 

首先新建两个实体类

//玩家的信息
public class Player {
	private String id;// 用户名
	private String tier;//大段位
	private String rank;// 小段位
	private String league_points;// 胜点
	private String rank_games;// 排位场数
	private String rank_winRate;// 排位胜率
	private String general_games;// 匹配场数
	private String general_winRate;// 匹配胜率
	private String loginTime;// 最后登录时间
	private String fighting;// 战斗力
	private int  latelyGameRate;//最近几场游戏胜率
	private String assess;//评价
	
	public String getAssess() {
		return assess;
	}
	public void setAssess(String assess) {
		this.assess = assess;
	}
	public String getTier() {
		return tier;
	}
	public void setTier(String tier) {
		this.tier = tier;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getRank() {
		return rank;
	}
	public void setRank(String rank) {
		this.rank = rank;
	}
	public String getLeague_points() {
		return league_points;
	}
	public void setLeague_points(String league_points) {
		this.league_points = league_points;
	}
	public String getRank_games() {
		return rank_games;
	}
	public void setRank_games(String rank_games) {
		this.rank_games = rank_games;
	}
	public String getRank_winRate() {
		return rank_winRate;
	}
	public void setRank_winRate(String rank_winRate) {
		this.rank_winRate = rank_winRate;
	}
	public String getGeneral_games() {
		return general_games;
	}
	public void setGeneral_games(String general_games) {
		this.general_games = general_games;
	}
	public String getGeneral_winRate() {
		return general_winRate;
	}
	public void setGeneral_winRate(String general_winRate) {
		this.general_winRate = general_winRate;
	}
	public String getLoginTime() {
		return loginTime;
	}
	public void setLoginTime(String loginTime) {
		this.loginTime = loginTime;
	}
	public String getFighting() {
		return fighting;
	}
	public void setFighting(String fighting) {
		this.fighting = fighting;
	}
	public int getLatelyGameRate() {
		return latelyGameRate;
	}
	public void setLatelyGameRate(int latelyGameRate) {
		this.latelyGameRate = latelyGameRate;
	}


}
import net.sf.json.JSONArray;

//英雄信息
public class Hero {
	private String championName;
	private String championNameCN;
	private String winRate;// 胜率
	private String matchStat;// 场数
	private JSONArray averageKDA;// 场均 杀/死/助
	private String averageKDARating;// 场均KDA
	private JSONArray averageDamage;// 场均输出
	private JSONArray averageEarn;
	private String averageMinionsKilled;
	private String totalMVP;// MVP次数
	private String totalHope;
	@Override
	public String toString() {
		return "Hero [championNameCN=" + championNameCN + ", winRate="
				+ winRate + ", matchStat=" + matchStat + ", averageKDA="
				+ averageKDA + ", averageKDARating=" + averageKDARating
				+ ", totalMVP=" + totalMVP + "]";
	}

	private JSONArray championPoints;

	public String getChampionName() {
		return championName;
	}

	public void setChampionName(String championName) {
		this.championName = championName;
	}

	public String getChampionNameCN() {
		return championNameCN;
	}

	public void setChampionNameCN(String championNameCN) {
		this.championNameCN = championNameCN;
	}

	public String getWinRate() {
		return winRate;
	}

	public void setWinRate(String winRate) {
		this.winRate = winRate;
	}

	public String getMatchStat() {
		return matchStat;
	}

	public void setMatchStat(String matchStat) {
		this.matchStat = matchStat;
	}

	public JSONArray getAverageKDA() {
		return averageKDA;
	}

	public void setAverageKDA(JSONArray averageKDA) {
		this.averageKDA = averageKDA;
	}

	public String getAverageKDARating() {
		return averageKDARating;
	}

	public void setAverageKDARating(String averageKDARating) {
		this.averageKDARating = averageKDARating;
	}

	public JSONArray getAverageDamage() {
		return averageDamage;
	}

	public void setAverageDamage(JSONArray averageDamage) {
		this.averageDamage = averageDamage;
	}

	public JSONArray getAverageEarn() {
		return averageEarn;
	}

	public void setAverageEarn(JSONArray averageEarn) {
		this.averageEarn = averageEarn;
	}

	public String getAverageMinionsKilled() {
		return averageMinionsKilled;
	}

	public void setAverageMinionsKilled(String averageMinionsKilled) {
		this.averageMinionsKilled = averageMinionsKilled;
	}

	public String getTotalMVP() {
		return totalMVP;
	}

	public void setTotalMVP(String totalMVP) {
		this.totalMVP = totalMVP;
	}

	public String getTotalHope() {
		return totalHope;
	}

	public void setTotalHope(String totalHope) {
		this.totalHope = totalHope;
	}

	public JSONArray getChampionPoints() {
		return championPoints;
	}

	public void setChampionPoints(JSONArray championPoints) {
		this.championPoints = championPoints;
	}

}

再建一个枚举,用于保存和查询服务器名字


import java.util.HashMap;
import java.util.Map;

// 定义枚举类
public enum ENUMServerData {
	server1("电信一", "艾欧尼亚"), server2("网通一", "比尔吉沃特"), server3("电信二", "祖安"), server4("电信三", "诺克萨斯"), server5(
			"网通二", "德玛西亚"), server6("电信四", "班德尔城"), server7("电信五", "皮尔特沃夫"), server8("电信六", "战争学院"), server9(
			"网通三", "弗雷尔卓德"), server10("电信七", "巨神峰"), server11("电信八", "雷瑟守备"), server12("网通四",
			"无畏先锋"), server13("电信九", "裁决之地"), server14("电信十", "黑色玫瑰"), server15("电信十一", "暗影岛"), server16(
			"电信十二", "钢铁烈阳"), server17("网通五", "恕瑞玛"), server18("电信十三", "均衡教派"), server19("电信十四",
			"水晶之痕"), server20("电信十五", "影流"), server21("电信十六", "守望之海"), server22("网通六", "扭曲丛林"), server23(
			"电信十七", "征服之海"), server24("电信十八", "卡拉曼达"), server25("电信十九", "皮城警备"), server26("网通七",
			"巨龙之巢");

	private String serverNum;
	private String serverName;

	private ENUMServerData(String a, String b) {
		this.serverNum = a;
		this.serverName = b;
	}

	public String getServerName() {
		return serverName;
	}

	public String getServerNum() {
		return serverNum;
	}
    //根据残缺的大区名,智能从枚举中取完整大区名
    //如输入黑色,即可识别为黑色玫瑰
	public static Map GetServer(String s) {
		Map map = null;
		ENUMServerData[] sd = ENUMServerData.values();
		for (ENUMServerData server : sd) {
			if ((server.getServerNum() + server.getServerName()).indexOf(s) != -1) {
				map = new HashMap();
				map.put("serverName", server.getServerName());
				map.put("serverNum", server.getServerNum());
				break;
			}
		}

		return map;
	}
}


查询

import java.net.URLEncoder;
import java.util.ArrayList;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

import com.yemao.pojo.Hero;
import com.yemao.pojo.Player;
import util.WebUtil;
public class lolService {

	/**
	 * 查询
	 * 
	 * @param server
	 *            大区
	 * @param name
	 *            玩家ID
	 * @return
	 * @throws InterruptedException
	 * @throws ExecutionException
	 */
	public static Map query(String server, String name)
			throws InterruptedException, ExecutionException {
		// 从枚举中查询对应的名称
		Map smap = ENUMServerData.GetServer(server);
		String serverName = smap.get("serverName").toString();
		String serverNum = smap.get("serverNum").toString();

		// 返回的Map
		Map map = new HashMap();
		// 建立一个线程池
		ExecutorService pool = Executors.newFixedThreadPool(2);
		Callable c1 = new queryHeroData(serverName, name);
		Callable c2 = new queryUser(serverNum, name);
		// 执行线程
		Future task1 = pool.submit(c1);
		Future task2 = pool.submit(c2);
		// 等待线程执行完毕
		pool.shutdown();
		while (true) {
			if (pool.isTerminated()) {
				break;
			}
		}
		// 线程返回的对象

		map.put("heroData", task1.get());
		map.put("userData", task2.get());
		return map;
	}
}

/**
 * 线程-获取英雄数据
 * 
 * @author Administrator
 * 
 */
class queryHeroData implements Callable {
	private String name, server;

	queryHeroData(String server, String name) {
		this.server = server;//大区名,如黑色玫瑰
		this.name = name;
	}

	public Object call() throws Exception {
		try {
			server = URLEncoder.encode(server, "utf-8");
			name = URLEncoder.encode(name, "utf-8");
			String result = WebUtil.sendGet("http://lolbox.duowan.com/new/api/index.php",
					"_do=personal/championslist&serverName=" + server + "&playerName=" + name);
			if (result.equals("")) {
				return null;
			}
			// Unicode解密
			result = WebUtil.decodeUnicode(result);
			List list = new ArrayList();
			// 解析返回的json数据
			JSONObject jb = JSONObject.fromObject(result);
			JSONArray jsons = jb.getJSONArray("content");
			for (int i = 0; i < jsons.size(); i++) {
				JSONObject tempJson = JSONObject.fromObject(jsons.get(i));
				tempJson.remove("championPoints");
				Hero hero = (Hero) JSONObject.toBean(tempJson, Hero.class);
				list.add(hero);
			}
			// 根据英雄胜率排序
			Collections.sort(list, new Comparator() {
				public int compare(Hero arg0, Hero arg1) {
					int hits0 = Integer.parseInt(arg0.getMatchStat());
					int hits1 = Integer.parseInt(arg1.getMatchStat());
					if (hits1 > hits0) {
						return 1;
					} else if (hits1 == hits0) {
						return 0;
					} else {
						return -1;
					}
				}
			});
			return list;
		} catch (Exception e) {
			return null;
		}

	}
}

/**
 * 线程-获取玩家信息
 * 
 * @author Administrator
 * 
 */
class queryUser implements Callable {
	private String name, server;

	queryUser(String server, String name) {
		this.server = server;//大区,如 电信十
		this.name = name;
	}

	public Object call() throws Exception {
		server = URLEncoder.encode(server, "utf-8");
		name = URLEncoder.encode(name, "utf-8");
		Player player = new Player();
		String url = "http://lolbox.duowan.com/playerDetail.php";// 完整URL
		String submitI = "serverName=" + server + "&playerName=" + name;// 提交信息
		String result = WebUtil.sendGet(url, submitI);
		if (result.equals("")) {
			return null;
		}

		try {
			Document doc = Jsoup.parse(result, "utf-8");
			Elements links = doc.select(".fighting span");

			// 战斗力
			player.setFighting(links.get(0).text());
			// 最后登录
			player.setLoginTime(links.get(0).attr("title").split(":")[1]);
			// 玩家ID
			player.setId(doc.select("#playerNameLink").get(0).text());
			// AJAX取玩家实时信息
			String json = WebUtil.sendGet("http://lolbox.duowan.com/ajaxGetWarzone.php",
					"serverName=" + server + "&playerName=" + name);
			JSONObject jb = JSONObject.fromObject(json);
			// 大段位
			player.setTier(jb.getString("tier"));
			// 小段位
			String rank = jb.getString("rank");
			if (rank.equals("null")) {
				rank = "";// 最强王者没有小段位,设为空字符串
			}
			player.setRank(rank);

			// 胜点
			player.setLeague_points(jb.getString("league_points"));

			links = doc.select(".mod-tabs-content:eq(0) tr:eq(1) td");
			// 匹配场数
			player.setGeneral_games(links.get(1).text());
			// 匹配胜率
			player.setGeneral_winRate(links.get(2).text());

			links = doc.select(".mod-tabs-content:eq(1) tr:eq(1) td");
			// 排位场数
			player.setRank_games(links.get(3).text());
			// 排位胜率
			player.setRank_winRate(links.get(4).text());

			// 计算最近几场游戏胜率
			double rate = (double) doc.select(".green").size() / 7.0 * 100;
			player.setLatelyGameRate((int) rate);
			return calculateAssess(player);
		} catch (Exception e) {
			System.out.println("Elements Error!");
			return null;
		}
	}

	/**
	 * 战绩分析,评价
	 * 
	 * @param player
	 * @return
	 */
	private Player calculateAssess(Player player) {
		if (player.getTier() == null) {
			return player;
		}
		String TIME_ARRAY[] = "万年/资深/爬行艰难的/游刃有余的".split("/");
		String CARRY_ARRAY[] = "Carry型/稳定型/混分型/深坑型".split("/");
		String ROLE_ARRAY[] = "大神/玩家/小学生".split("/");
		String timeAccess = null;
		String carryAccess = null;
		String roleAccess = null;
		String tier = player.getTier();// 大段位
		Double rate = Double.parseDouble(player.getRank_winRate().replace("%", ""));// 排位胜率
		int games = Integer.parseInt(player.getRank_games());// 排位场数
		int limit[] = null;
		if (tier == null) {
			return player;
		}

		// 自定义规则,不同分段对应的排位场数。场数越多越坑
		switch (tier) {
		case "青铜":
			limit = new int[] { 300, 100, 50 };
			break;
		case "白银":
			limit = new int[] { 800, 500, 150 };
			break;
		case "黄金":
			limit = new int[] { 800, 500, 200 };
			break;
		case "铂金":
			limit = new int[] { 1200, 800, 300 };
			break;
		case "钻石":
			limit = new int[] { 1500, 800, 400 };
			break;
		default:
			// 大师王者
			limit = new int[] { 1800, 1000, 500 };
		}

		// 根据上方规则进行判断
		if (games >= limit[0]) {
			timeAccess = TIME_ARRAY[0];
		} else if (games < limit[0] && games >= limit[1]) {
			timeAccess = TIME_ARRAY[1];
		} else if (games < limit[1] && games >= limit[2]) {
			timeAccess = TIME_ARRAY[2];
		} else if (games <= limit[2]) {
			timeAccess = TIME_ARRAY[3];
		}

		// 根据胜率进行判断
		if (rate >= 55) {
			carryAccess = CARRY_ARRAY[0];
			roleAccess = ROLE_ARRAY[0];
		} else if (rate < 55 && rate >= 51) {// 51~54
			carryAccess = CARRY_ARRAY[1];
			roleAccess = ROLE_ARRAY[1];
		} else if (rate < 51 && rate >= 48) {// 48~50
			carryAccess = CARRY_ARRAY[2];
			roleAccess = ROLE_ARRAY[1];
		} else if (rate <= 47) {
			carryAccess = CARRY_ARRAY[3];
			roleAccess = ROLE_ARRAY[2];
		}
		// 接拼评价
		String assess = timeAccess + carryAccess + player.getTier() + roleAccess;
		// System.out.println(assess);
		player.setAssess(assess);
		return player;

	}

} 
   

测试

public static void main(String[] args) {		
	try {
			String name = "玩家ID";
            //枚举那里做了智能判断,输入大区名前几个字也可,如黑色自动识别为黑色玫瑰
			String server = "大区";

			 Map map = lolService.query(server, name);
			 System.out.println(map.get("heroData"));
             System.out.println(map.get(userData"));
		} catch (Exception e) {
			// TODO: handle exception
		}
}	

 

 

转载于:https://my.oschina.net/yejunxi/blog/700076

你可能感兴趣的:(Java 查询英雄联盟玩家战绩)