Author Topic: Java Lobby Parser  (Read 1645 times)

0 Members and 1 Guest are viewing this topic.

Offline vEsP@

  • Major(1)
  • Posts: 14
Java Lobby Parser
« on: September 01, 2008, 01:15:22 pm »
Synce i´ve had reply on how to connect to lobby server i could done the lobby string parser...


here´s the code

Code: [Select]
/*
 * Lobby Received String Parser
 */
package lobby;

/**
 *
 * @author vEsP@
 *
 */
public class Parser {
    private static int token ;
    public static ServerObject makeServer(String serverInfo) {
        token = 0 ;
        ServerObject server = null;
        if (serverInfo.charAt(0) == 'g') {
            // version parser
            String version = getNext(token+2,serverInfo) ;
            String ip = getNext(token+2, serverInfo) ;
            String port = getNext(token+2, serverInfo) ;
            String gamestyle = getNext(token+2, serverInfo) ;
            String players = getNext(token+2, serverInfo) ;
            String maxPlayers = getNext(token+2, serverInfo) ;
            String map = getNext(token+2, serverInfo) ;
            String name = getNext(token+2, serverInfo) ;
            String bots = getNext(token+2, serverInfo) ;
            String bonusFreq = getNext(token+2, serverInfo) ;
            String respawn = getNext(token+2, serverInfo) ;
            String connection = getNext(token+2, serverInfo) ;
            String survival = getNext(token+2, serverInfo) ;
            String realistic = getNext(token+2, serverInfo) ;
            String dedicated = getNext(token+2, serverInfo) ;
            String os = getNext(token+2, serverInfo) ;
            String passworded = getNext(token+2, serverInfo) ;
            String be = getNext(token+2, serverInfo) ;
            String country = getNext(token+2, serverInfo) ;
            server = new ServerObject
                    (version, ip, port, gamestyle, players, maxPlayers, map,
                    name, bots, bonusFreq, respawn, connection, survival,
                    realistic, dedicated, os, passworded, be, country) ;
        }
        return server ;
    }
   
    public static String getNext(int begin, String str) {
        String result = "" ;
        for (int i = begin; i<= str.length()-1; i++) {
            token++ ;
            if (str.charAt(i) == '©')
                break ;
            result += str.charAt(i) ;
        }
        return result ;
    }
}


Offline FliesLikeABrick

  • Administrator
  • Flamebow Warrior
  • *****
  • Posts: 6144
    • Ultimate 13 Soldat
Re: Java Lobby Parser
« Reply #1 on: September 01, 2008, 01:35:58 pm »
Out of curiosity. what did someone tell you that helped you figure it out?

Offline vEsP@

  • Major(1)
  • Posts: 14
Re: Java Lobby Parser
« Reply #2 on: September 01, 2008, 08:25:47 pm »
i was trying to connect to lobby from a single terminal program just to "look and feel" the string it could send from the base request...

i was asking that someone do this and send me a printscreen containing a request string to be sent and another for the reply...

someone (i don´t remember the nickname) posted here a simple pascal program...
i was able then to compile it and figure it out that the escape characters © in linux have diferent understanding´s by the OS´s

my problem was really easy to solve, and a little shame is that i´ve doubted of the documentation on devs.soldat/wiki/ .... the real problem was in the manner i was sending the scape characters...

i will post the java lobby connector here too
Code: [Select]
/*
 * Lobby TCP Connection
 */
package lobby;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.SocketException;

/**
 *
 * @author vEsP@
 */
public class TCPLobby {

    public static void getRequest(String request) {
        try {
            Socket socket = new Socket("rr.soldat.pl", 13073);
           
            BufferedReader buffer =
                    new BufferedReader(
                    new InputStreamReader(socket.getInputStream())) ;
           
            PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
            // STREAMS READY
            String line ;
            StringBuffer answer = new StringBuffer() ;
            request = "e©42©0©0©0©0©0©0©0©0©0©0©-1©1©#10
" ; // base req
            out.write(request);
            out.flush();
            // REQUEST SENT
            do {
                line = buffer.readLine() ;
                answer.append(line + "
") ;
            } while (!line.equals("h©©©©©")) ;
            // REQUEST RECEIVED...
            writeFile( answer.toString() ) ;
           
           
        } catch (SocketException se) {
            se.printStackTrace();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
    }

    public static void writeFile(String file) {
        LobbyFile.write(file);
    }
}


Date Posted: September 01, 2008, 09:17:58 pm
this class describes the socket and the streams to send and receive data...

that writeFile() method is not necessary for others... this was just my need of the data

thanks