1 package net.sourceforge.queried.gametypes;
2
3 import java.util.ArrayList;
4 import java.util.Collections;
5
6 import net.sourceforge.queried.PlayerInfo;
7 import net.sourceforge.queried.ScoreComparator;
8 import net.sourceforge.queried.ServerInfo;
9 import net.sourceforge.queried.Util;
10
11
12 public class ETServerInfo {
13
14 public static ServerInfo getDetails(int localPort, String ipStr, int port, int infoType, int queryType,
15 int gameType) {
16
17 String queryResult = Util.getInfo(localPort, ipStr, port, "getstatus", infoType, queryType, gameType);
18
19 ServerInfo serverInfo = null;
20 if(queryResult != null && queryResult.length() > 0) {
21 queryResult = queryResult.substring(queryResult.indexOf("\\"));
22 queryResult = queryResult.replaceAll("\\^([0-9a-wyzA-WYZ]|x[0-9a-fA-F]{6})", "");
23 serverInfo = new ServerInfo();
24 String[] playerParts = queryResult.split("\n");
25 serverInfo.setGame(Util.getPart(queryResult, "gamename"));
26 serverInfo.setIp(ipStr);
27 serverInfo.setPort(port +"");
28 serverInfo.setName(Util.getPart(queryResult, "sv_hostname"));
29 serverInfo.setPlayerCount(playerParts.length - 1 +"");
30 serverInfo.setMaxPlayers(Util.getPart(queryResult, "sv_maxclients"));
31 serverInfo.setMap(Util.getPart(queryResult, "mapname"));
32 serverInfo.setFullResponse(queryResult);
33 }
34
35 return serverInfo;
36 }
37
38 public static ArrayList getPlayers(int localPort, String ipStr, int port, int infoType, int queryType,
39 int gameType) {
40
41 ArrayList playerInfo = new ArrayList();
42 String queryResult = Util.getInfo(localPort, ipStr, port, "getstatus", infoType, queryType, gameType);
43 if(queryResult == null || queryResult.length() < 1) {
44 return playerInfo;
45 }
46
47 queryResult = queryResult.substring(queryResult.indexOf("\\"));
48 queryResult = queryResult.replaceAll("\\^([0-9a-wyzA-WYZ]|x[0-9a-fA-F]{6})", "");
49
50 if(queryResult != null || queryResult.length() > 0) {
51
52
53 String[] plyrs = queryResult.split("\n");
54 int playerCount = plyrs.length-1;
55 for(int x=1; x < plyrs.length; x++) {
56 String plyr = plyrs[x];
57 String score = plyr.substring(0, plyr.indexOf(" "));
58 String ping = plyr.substring(plyr.indexOf(" ")+1, plyr.indexOf(" ", plyr.indexOf(" ")+1));
59 String name = plyr.substring(plyr.indexOf("\"")+1, plyr.lastIndexOf("\""));
60
61 PlayerInfo player = new PlayerInfo();
62 player.setDeaths(-9999);
63 player.setKills(Integer.valueOf(score).intValue());
64 player.setName(name);
65 player.setScore(-9999);
66 player.setPing(Integer.valueOf(ping).intValue());
67 playerInfo.add(player);
68 }
69 Collections.sort(playerInfo, new ScoreComparator());
70 }
71
72 return playerInfo;
73
74 }
75
76 }