View Javadoc

1   package net.sourceforge.queried.gametypes;
2   
3   import java.io.FileOutputStream;
4   import java.io.IOException;
5   import java.io.UnsupportedEncodingException;
6   import java.net.InetAddress;
7   import java.net.UnknownHostException;
8   import java.util.ArrayList;
9   import java.util.Collections;
10  
11  import net.sourceforge.queried.KillsComparator;
12  import net.sourceforge.queried.PlayerInfo;
13  import net.sourceforge.queried.ServerInfo;
14  import net.sourceforge.queried.Util;
15  
16  
17  public class SourceServerInfo {
18  
19  	public static ArrayList getPlayers(int localPort, String ipStr, int port, int infoType, int queryType,
20          int gameType) {
21              
22          byte[] buf = null;
23          try {
24              buf = Util.getInfo(localPort, ipStr, port, "U", infoType, queryType, gameType).getBytes("ISO-8859-1");
25          } catch (UnsupportedEncodingException ex) {
26              ex.printStackTrace();
27          }
28  
29          if(buf == null || buf.length == 0) {
30              return null;            
31          } else if(buf[0] != buf[1] || buf[1] != buf[2] || buf[2] != buf[3] || buf[4] != 'D') { 
32              return null;            
33          }
34          
35          ArrayList sorted = new ArrayList();
36                 
37  		int playerCount = buf[5] & 255;
38  		String[] playerNames = new String[playerCount];
39  		int[] playerFrags = new int[playerCount];
40  		int off = 7;
41  
42          while(off < buf.length) {
43              PlayerInfo playerInfo = new PlayerInfo();
44  			StringBuffer playerName = new StringBuffer(20);
45  			while(buf[off] != 0) {
46  				playerName.append((char)(buf[off++] & 255));
47  			}
48  			off++;
49              playerInfo.setName(playerName.toString().trim());
50              playerInfo.setKills((buf[off] & 255) | ((buf[off+1] & 255) << 8) | 
51                  ((buf[off+2] & 255) << 16) | ((buf[off+3] & 255) << 24));
52              sorted.add(playerInfo);
53  			off += 9;
54  		}
55  		Collections.sort(sorted, new KillsComparator());
56          
57          return sorted;
58  	}
59  
60  	public static ServerInfo getDetails(int localPort, String ipStr, int port, int infoType, int queryType,
61          int gameType) {
62              
63          byte[] buf = null;
64          try {
65              buf = Util.getInfo(localPort, ipStr, port, "TSource Engine Query", infoType, queryType, gameType).getBytes("ISO-8859-1");
66          } catch (UnsupportedEncodingException ex) {
67              ex.printStackTrace();
68          }
69  
70          if(buf == null || buf.length == 0) {
71              return null;            
72          } else if(buf[0] != buf[1] || buf[1] != buf[2] || buf[2] != buf[3] || buf[4] != 'I') { 
73              return null;            
74  		}
75  
76          ServerInfo serverInfo = new ServerInfo();
77          
78          InetAddress inettst;
79          try {
80              inettst = InetAddress.getByName(ipStr);
81              serverInfo.setIp(inettst.getHostAddress());
82          } catch (UnknownHostException e) {
83              serverInfo.setIp(ipStr);
84          }
85  
86          serverInfo.setPort(port +"");
87  
88      	int off = 6;
89  
90          StringBuffer netName = new StringBuffer(20);
91          while(buf[off] != 0) {
92              netName.append((char)(buf[off++] & 255));
93          }
94          serverInfo.setName(netName.toString());
95  
96          off++;
97                  
98          StringBuffer mapName = new StringBuffer(20);
99          while(buf[off] != 0) {
100             mapName.append((char)(buf[off++] & 255));
101         }
102         serverInfo.setMap(mapName.toString());
103         
104         off++;
105 
106 		// skip game directory
107 		while(buf[off] != 0) {
108 			off++;
109 		}
110 		
111 		off++;
112 
113 		StringBuffer gameDesc = new StringBuffer(20);
114 		while(buf[off] != 0) {
115 			gameDesc.append((char)(buf[off++] & 255));
116 		}
117         serverInfo.setGame(gameDesc.toString());
118 
119         off++;
120         
121         // skip app id
122         off++;
123         
124         off++;
125 		
126 		int playerCount = buf[off] & 255;
127         serverInfo.setPlayerCount(playerCount +"");
128 		
129 		off++;
130 		
131 		int maxPlayerCount = buf[off] & 255;
132         serverInfo.setMaxPlayers(maxPlayerCount +"");
133         serverInfo.setFullResponse(new String(buf));
134 
135 		return serverInfo;
136 	}
137 	
138 }