ribasco / async-gamequery-lib

A high-performance java game query library designed for steam/source based games and others
https://ribasco.github.io/async-gamequery-lib/
Apache License 2.0
125 stars 27 forks source link
api asynchronous hl-log java java-8 library netty query rest-api source source-engine steam steam-api storefront valve valve-cs web-services

Asynchronous Game Query Library

Snapshot Builds Release Builds Site Builds Code Analysis

Maven Donate Javadocs Gitter

A game query library on steroids written for Java. It's an implementation of Valve's source Query, Rcon, Master and Steam Web API protocols. Built on top of Netty

Features

Usage

For more examples, please refer to the site docs.

Blocking Queries

public class BlockingQueryExample {

    //Use a custom executor. This is not really necessary as the library 
    //provides it's own default executor, this only serves an example.
    ExecutorService customExecutor = Executors.newCachedThreadPool();

    public static void main(String[] args) {
        // - Change rate limiting method to BURST
      // - Used a custom executor for query client. We are responsible for shutting down this executor, not the library.
      SourceQueryOptions queryOptions = SourceQueryOptions.builder()
                                                          .option(FailsafeOptions.FAILSAFE_RATELIMIT_TYPE, RateLimitType.BURST)
                                                          .option(GeneralOptions.THREAD_EXECUTOR_SERVICE, customExecutor)
                                                          .build();

        //You can instantiate the client from the try-with block as it implements the java.io.Closeable interface
        try (SourceQueryClient client = new SourceQueryClient(queryOptions)) {
            InetSocketAddress address = new InetSocketAddress("192.168.60.1", 27016);
            SourceServer info = client.getInfo(address).join().getResult();
            System.out.printf("INFO: %s\n", info);
        }
    }
}

Non-Blocking Queries

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;

public class NonBlockingQueryExample {

    //Use a custom executor. This is not required as the library 
    //provides it's own default global executor shared across all clients, this only serves an example on how you can provide your own.
    ExecutorService customExecutor = Executors.newCachedThreadPool();

    public static void main(String[] args) throws Exception {
      //Example configuration
      // - Enabled rate limiting, so we don't send too fast
      // - Change rate limiting type to SMOOTH (Two available types SMOOTH and BURST)
      // - Used a custom executor for query client. We are responsible for shutting down this executor, not the library.
      SourceQueryOptions queryOptions = SourceQueryOptions.builder()
                                                          .option(FailsafeOptions.FAILSAFE_RATELIMIT_TYPE, RateLimitType.SMOOTH)
                                                          .option(GeneralOptions.THREAD_EXECUTOR_SERVICE, customExecutor)
                                                          .build();

        //create a countdown latch with value of 1 since we are only expecting to receive 1 result
        CountDownLatch latch = new CountDownLatch(1);

        //Instantiate the client (constructor argument is optional)
        try (SourceQueryClient client = new SourceQueryClient(queryOptions)) {
            //Create a user defined object which serves as an aggregate where all the resulting data will be stored
            SourceQueryAggregate result = new SourceQueryAggregate(address);
            CompletableFuture<SourceQueryInfoResponse> infoFuture = client.getInfo(address);

            //Check if the future is complete
            if (infoFuture.isDone()) {
                try {
                    SourceQueryInfoResponse response = infoFuture.getNow(null);
                } catch (Exception e) {
                    e.printStackTrace(System.err);
                } finally {
                    latch.countDown();
                }
            }
            //If not, register a callback to handle future response
            else {
                infoFuture.whenComplete(new BiConsumer<SourceQueryInfoResponse, Throwable>() {
                    @Override
                    public void accept(SourceQueryInfoResponse response, Throwable error) {
                        try {
                            if (error != null) {
                                throw new CompletionException(error);
                            }
                            assert response != null;
                            System.out.printf("INFO: %s\n", response.getResult());
                        } finally {
                            latch.countDown();
                        }
                    }
                });
            }

          //REMEMBER: Since we are executing an asynchronous operation, 
          // we need to wait until we have received a response from the server, 
          // otherwise the program might abruptly terminate. 
          latch.await();
        }
    }
}

A slightly advanced example demonstrating how to combine all three asynchronous queries into one call. For more advanced examples (e.g. sending requests by batch, using synchronization barriers etc), please check out the examples module in the project source.

public class NonBlockingQueryExample {

    //Use a custom executor. This is not required as the library 
    //provides it's own default global executor shared across all clients, this only serves an example on how you can provide your own.
    ExecutorService customExecutor = Executors.newCachedThreadPool();

    public static void main(String[] args) {
        //Example configuration
        // - Enabled rate limiting so we don't send too fast
        // - Change rate limiting type to SMOOTH (Two available types SMOOTH and BURST)
      // - Used a custom executor for query client. We are responsible for shutting down this executor, not the library.
      SourceQueryOptions queryOptions = SourceQueryOptions.builder()
                                                          .option(SourceQueryOptions.FAILSAFE_RATELIMIT_TYPE, RateLimitType.SMOOTH)
                                                          .option(GeneralOptions.THREAD_EXECUTOR_SERVICE, customExecutor)
                                                          .build();
        //Instantiate the client (constructor argument is optional)
        SourceQueryClient client = new SourceQueryClient(queryOptions);

        //Create a user defined object which serves as an aggregate where all the resulting data will be stored
        SourceQueryAggregate result = new SourceQueryAggregate(address);

        //Combining all queries in one call
        //Note: `Functions::selectFirst` is simply a utility function which returns the first argument of the callback (Refer to the examples section for the source) 
        CompletableFuture<SourceQueryAggregate> resultFuture = CompletableFuture.completedFuture(result)
                                                                                .thenCombine(client.getInfo(address).handle(result.ofType(SourceQueryType.INFO)), Functions::selectFirst)
                                                                                .thenCombine(client.getPlayers(address).handle(result.ofType(SourceQueryType.PLAYERS)), Functions::selectFirst)
                                                                                .thenCombine(client.getRules(address).handle(result.ofType(SourceQueryType.RULES)), Functions::selectFirst);

        //Display result
        resultFuture.whenComplete(new BiConsumer<SourceQueryAggregate, Throwable>() {
            @Override
            public void accept(SourceQueryAggregate result, Throwable error) {
                if (error != null) {
                    throw new CompletionException(error);
                }
                log.info("INFO: {}, PLAYERS: {}, RULES: {}", result.getInfo(), result.getPlayers(), result.getRules());
            }
        });
    }
}

Failsafe Demo

A couple of demonstration videos showcasing the built-in failsafe features provided by this library.

Retry Feature

https://user-images.githubusercontent.com/13303385/165238011-1bbefbfc-14cd-40c8-9ab3-26f1db7c5d8f.mp4

Circuit Breaker (Fail-fast)

https://user-images.githubusercontent.com/13303385/165109735-0d96f3ca-5681-4456-977c-8cd4a5189c8b.mp4

Project Resources

Discussion Platforms

If you have any inquiries,concerns or suggestions please use one of the official communication channels for this project

Implementations

Below is the list of what is currently implemented on the library

Requirements

Installation

Just add the following dependencies to your maven pom.xml. Only include the modules you need.

Install from Maven Central

Aggregate (All modules included in this artifact)

<dependency>
    <groupId>com.ibasco.agql</groupId>
  <artifactId>agql</artifactId>
  <version>1.2.2</version>
</dependency>

Valve Master Server Query Protocol

<dependency>
    <groupId>com.ibasco.agql</groupId>
  <artifactId>agql-steam-master</artifactId>
  <version>1.2.2</version>
</dependency>

Valve Source Query Protocol

<dependency>
  <groupId>com.ibasco.agql</groupId>
  <artifactId>agql-source-query</artifactId>
  <version>1.2.2</version>
</dependency>

Valve Source Log Listener Service


<dependency>
  <groupId>com.ibasco.agql</groupId>
  <artifactId>agql-source-log</artifactId>
  <version>1.2.2</version>
</dependency>

Valve Source Rcon Protocol

<dependency>
  <groupId>com.ibasco.agql</groupId>
  <artifactId>agql-source-rcon</artifactId>
  <version>1.2.2</version>
</dependency>

Valve Steam Web API

<dependency>
  <groupId>com.ibasco.agql</groupId>
  <artifactId>agql-steam-webapi</artifactId>
  <version>1.2.2</version>
</dependency>

Valve Dota 2 Web API

<dependency>
    <groupId>com.ibasco.agql</groupId>
  <artifactId>agql-dota2-webapi</artifactId>
  <version>1.2.2</version>
</dependency>

Valve CS:GO Web API

<dependency>
    <groupId>com.ibasco.agql</groupId>
  <artifactId>agql-csgo-webapi</artifactId>
  <version>1.2.2</version>
</dependency>

Supercell Clash of Clans Web API (Deprecated)

NOTE: As of 1.0.0, this has been marked as deprecated and will be removed in the next major release

<dependency>
    <groupId>com.ibasco.agql</groupId>
  <artifactId>agql-coc-webapi</artifactId>
  <version>1.2.2</version>
</dependency>

Install from Source

Clone from remote repository then mvn install. All the modules will be installed to your local maven repository.

git clone https://github.com/ribasco/async-gamequery-lib.git
cd async-gamequery-lib
mvn install

Interactive Examples

RCON interactive example video

To run the available examples, I have included a convenience script (run-example.sh or run-example.cmd) that will allow you to pick a specific example you could run.

The script accepts a "key" that represents an example application. To get a list of keys, simply invoke the script without arguments, for example:

$ ./run-example.sh
Error: Missing Example Key. Please specify the example key. (e.g. source-query)

====================================================================
List of available examples
====================================================================
- Source Server Query Example      (key: source-query)
- Master Server Query Example      (key: master-query)
- Source Rcon Example              (key: source-rcon)
- Clash of Clans Web API Example   (key: coc-webapi)
- CS:GO Web API Example            (key: csgo-webapi)
- Steam Web API Example            (key: steam-webapi)
- Steam Storefront Web API Example (key: steam-store-webapi)
- Source Log Listener Example      (key: source-logger)
- Steam Econ Web API Example       (key: steam-econ-webapi)
- Dota2 Web API Example            (key: dota2-webapi)

If you are running a web service type example, you will be prompted with an API key. Simply copy and paste the key to the console.

$ ./run-example.sh coc-webapi
Running example for coc-webapi
$ Please input your API Token:

Note:

Protocol Specifications

References you might find helpful regarding the implementations

Demo Application

Coming soon (Currently working on a desktop application showcasing the features provided by this library). For now, refer to the interactive examples.

Contributing

Fork it and submit a pull request. Any type of contributions are welcome.

Special Thanks