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 public class BF2ServerInfo {
12
13 public static ServerInfo getDetails(int localPort, String ipStr, int port, int infoType, int queryType,
14 int gameType) {
15
16 String queryResult = Util.getInfo(localPort, ipStr, port, infoType, queryType, gameType);
17
18 ServerInfo serverInfo = null;
19 if(queryResult != null && queryResult.length() > 0) {
20 serverInfo = new ServerInfo();
21 serverInfo.setGame(Util.getPartGS2(queryResult, "gamename"));
22 serverInfo.setGameVersion(Util.getPartGS2(queryResult, "gamever"));
23 serverInfo.setIp(ipStr);
24 serverInfo.setPort(Util.getPartGS2(queryResult, "hostport"));
25 serverInfo.setName(Util.getPartGS2(queryResult, "hostname"));
26 serverInfo.setMap(Util.getPartGS2(queryResult, "mapname"));
27 serverInfo.setPlayerCount(Util.getPartGS2(queryResult, "numplayers"));
28 serverInfo.setMaxPlayers(Util.getPartGS2(queryResult, "maxplayers"));
29 serverInfo.setFullResponse(queryResult);
30 }
31
32 return serverInfo;
33 }
34
35 public static ArrayList getPlayers(int localPort, String ipStr, int port, int infoType, int queryType,
36 int gameType) {
37
38 String recStr = Util.getInfo(localPort, ipStr, port, infoType, queryType, gameType);
39
40 ArrayList playerInfo = new ArrayList();
41
42 String[] players = assembleParts(recStr, "player_");
43
44
45 if(players == null || players.length == 0) {
46 return playerInfo;
47 }
48
49 String[] scores = assembleParts(recStr, "score_");
50
51 String[] deaths = assembleParts(recStr, "deaths_");
52
53 String[] skills = assembleParts(recStr, "skill_");
54
55 try {
56 for(int i=0; i < players.length; i++) {
57 PlayerInfo player = new PlayerInfo();
58
59 player.setName(players[i]);
60
61 player.setScore(Integer.valueOf(scores[i]).intValue());
62
63 player.setDeaths(Integer.valueOf(deaths[i]).intValue());
64
65 player.setKills(Integer.valueOf(skills[i]).intValue());
66 playerInfo.add(player);
67 }
68 } catch (ArrayIndexOutOfBoundsException aiex) {
69
70 } catch (NumberFormatException nfex) {
71
72 } catch (StringIndexOutOfBoundsException sobex) {
73
74 }
75 Collections.sort(playerInfo, new ScoreComparator());
76
77 return playerInfo;
78 }
79
80 private static String[] assembleParts(String recStr, String markerString) {
81 char chr = 00;
82 String marker = markerString + chr;
83 boolean search = true;
84 int start = 0;
85 int end = 0;
86 String chunk = "";
87 String[] retArray = null;
88 while(search) {
89 start = recStr.indexOf(marker, start) + marker.length() + 1;
90 end = recStr.indexOf(chr +""+ chr, start);
91 if(end <= 0) {
92 end = recStr.length();
93 }
94 if(start == marker.length()) {
95 search = false;
96 }
97 if(search) {
98 chunk = recStr.substring(start, end);
99 if(retArray == null || retArray.length == 0) {
100 retArray = chunk.split(chr +"");
101 } else {
102 String[] tmpArray = chunk.split(chr +"");
103 String[] copyArray = (String[]) retArray.clone();
104 retArray = new String[tmpArray.length + copyArray.length - 1];
105
106 System.arraycopy(copyArray, 0, retArray, 0, copyArray.length - 1);
107 System.arraycopy(tmpArray, 0, retArray, copyArray.length -1, tmpArray.length);
108 }
109 start = end;
110 }
111 }
112
113 if(retArray == null) {
114 return null;
115 }
116
117 return (String[]) retArray.clone();
118 }
119 }