Skip to content

Commit

Permalink
Add credit pools
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianVennen committed Dec 6, 2023
1 parent 246bef9 commit b783961
Show file tree
Hide file tree
Showing 13 changed files with 554 additions and 4 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
ORG_GRADLE_PROJECT_OSSRH_PASSWORD: ${{ secrets.SONATYPE_OSSRH_PASSWORD }}
EXAROTON_API_TOKEN: ${{ secrets.EXAROTON_API_KEY }}
EXAROTON_TEST_SERVER: "WgvSsfR8ZizUO1RQ"
EXAROTON_TEST_POOL: "N2t9gWOMpzRL37FI"
run: ./gradlew publish -Prelease=${{ github.ref_name }}
- name: Create Release
uses: softprops/action-gh-release@v1
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ jobs:
run: ./gradlew build --info
env:
EXAROTON_API_TOKEN: ${{ secrets.EXAROTON_API_KEY }}
EXAROTON_TEST_SERVER: "WgvSsfR8ZizUO1RQ"
EXAROTON_TEST_SERVER: "WgvSsfR8ZizUO1RQ"
EXAROTON_TEST_POOL: "N2t9gWOMpzRL37FI"
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- Add endpoints for the server config
- Add endpoints for the server config
- Add endpoints for credit pools
38 changes: 36 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ try {
}
```

### Files
#### Files
To manage a file on your server first obtain a file Object:
```java
ExarotonClient client = new ExarotonClient("example-api-token");
Expand Down Expand Up @@ -215,7 +215,7 @@ file.delete();
file.createAsDirectory();
```

### Configs
#### Configs
Some files are special because they are parsed, validated and understood by the exaroton backend.
These files are called configs and can be managed like this:
```java
Expand Down Expand Up @@ -249,6 +249,40 @@ config.getOption("level-seed").setValue("example");
config.save();
```

#### Credit Pools
Credit pools allow you to share payments for your server with other users in a safe way.
You can view information about credit pools like this:
```java
// get all credit pools
CreditPool[] pools = client.getCreditPools();
for (CreditPool pool: pools) {
System.out.println(pool.getName() + ": " + pool.getCredits());
}

// get a single credit pool
CreditPool pool = client.getCreditPool("N2t9gWOMpzRL37FI");
pool.get(); // update pool info
System.out.println(pool.getName() + ": " + pool.getCredits());
```

The API also allows you to fetch the servers in a pool:
```java
CreditPool pool = client.getCreditPool("N2t9gWOMpzRL37FI");
Server[] servers = pool.getServers();
for (Server server: servers) {
System.out.println(server.getName() + ": " + server.getAddress());
}
```

If you have the "View members" permission, you can even get all members of a pool:
```java
CreditPool pool = client.getCreditPool("N2t9gWOMpzRL37FI");
CreditPoolMember[] members = pool.getMembers();
for (CreditPoolMember member: members) {
System.out.println(member.getName() + ": " + member.getCredits());
}
```


## Websocket API
The websocket API allows a constant connection to our websocket service to receive events in real time without polling
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/com/exaroton/api/ExarotonClient.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.exaroton.api;

import com.exaroton.api.account.Account;
import com.exaroton.api.billing.pools.CreditPool;
import com.exaroton.api.request.account.GetAccountRequest;
import com.exaroton.api.request.billing.pools.GetCreditPoolRequest;
import com.exaroton.api.request.billing.pools.GetCreditPoolsRequest;
import com.exaroton.api.request.server.GetServersRequest;
import com.exaroton.api.server.Server;
import com.exaroton.api.server.config.ConfigOption;
Expand Down Expand Up @@ -232,6 +235,29 @@ public Server getServer(String id) {
return new Server(this, id);
}

/**
* list all credit pools you have access to
* @return accessible credit pools
* @throws APIException connection and API errors
*/
public CreditPool[] getCreditPools() throws APIException {
GetCreditPoolsRequest request = new GetCreditPoolsRequest(this);
CreditPool[] pools = request.request().getData();
for (CreditPool server: pools) {
server.setClient(this).setFetched();
}
return pools;
}

/**
* get a credit pool
* @param id credit pool id
* @return empty credit pool object
*/
public CreditPool getCreditPool(String id) {
return new CreditPool(this, id);
}

/**
* Get the current exaroton server using the EXAROTON_SERVER_ID environment variable.
* If the environment variable is not set returns null
Expand Down
227 changes: 227 additions & 0 deletions src/main/java/com/exaroton/api/billing/pools/CreditPool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,227 @@
package com.exaroton.api.billing.pools;

import com.exaroton.api.APIException;
import com.exaroton.api.ExarotonClient;
import com.exaroton.api.request.billing.pools.GetCreditPoolMembersRequest;
import com.exaroton.api.request.billing.pools.GetCreditPoolRequest;
import com.exaroton.api.request.billing.pools.GetCreditPoolServersRequest;
import com.exaroton.api.server.Server;
import org.jetbrains.annotations.NotNull;

public class CreditPool {
/**
* Has this pool been fetched from the API yet
*/
private boolean fetched;

/**
* The unique id of the pool
*/
private final @NotNull String id;

/**
* The display name of the pool
*/
private String name;

/**
* The current amount of credits in the pool
*/
private double credits;

/**
* The number of servers in the pool
*/
private int servers;

/**
* The id of the user that owns the pool
*/
private String owner;

/**
* Is the current user the owner of the pool
*/
private boolean isOwner;

/**
* The number of members in the pool
*/
private int members;

/**
* The share of credits in the pool that belong to the current user
*/
private double ownShare;

/**
* The amount of credits in the pool that belong to the current user
*/
private double ownCredits;

/**
* The client used to create this pool
*/
private @NotNull transient ExarotonClient client;

public CreditPool(@NotNull ExarotonClient client, @NotNull String id) {
this.client = client;
this.id = id;
}

/**
* Get the unique id of the pool
* @return unique pool id
*/
public String getId() {
return id;
}

/**
* Get the display name of the pool
* @return pool display name
*/
public String getName() {
return name;
}

/**
* Get the current amount of credits in the pool
* @return amount of credits in the pool
*/
public double getCredits() {
return credits;
}

/**
* Get the number of servers in the pool
* @return number of servers in the pool
*/
public int getServers() {
return servers;
}

/**
* Get the id of the user that owns the pool
* @return pool owner id
*/
public String getOwner() {
return owner;
}

/**
* Is the current user the owner of the pool
* @return is the current user the owner of the pool
*/
public boolean isOwner() {
return isOwner;
}

/**
* Get the number of members in the pool
* @return number of members in the pool
*/
public int getMembers() {
return members;
}

/**
* Get the share of credits in the pool that belong to the current user
* @return share of credits in the pool that belong to the current user
*/
public double getOwnShare() {
return ownShare;
}

/**
* Get the amount of credits in the pool that belong to the current user
* @return amount of credits in the pool that belong to the current user
*/
public double getOwnCredits() {
return ownCredits;
}

/**
* Set the exaroton client used for further requests
* @param client exaroton client
* @return updated pool object
*/
public CreditPool setClient(ExarotonClient client) {
this.client = client;
return this;
}

/**
* Mark this pool as fetched from the API
* @return updated pool object
*/
public CreditPool setFetched() {
this.fetched = true;
return this;
}

/**
* Fetch the Credit Pool from the API
* @return full credit pool
* @throws APIException connection or API errors
*/
public CreditPool get() throws APIException {
this.fetched = true;
GetCreditPoolRequest request = new GetCreditPoolRequest(this.client, this.id);
return this.setFromObject(request.request().getData());
}

/**
* Fetch the Credit Pool from the API if it hasn't been fetched yet
* @return full credit pool
* @throws APIException connection or API errors
*/
public CreditPool getIfNotFetched() throws APIException {
if (!this.fetched) {
return this.get();
}
return this;
}

/**
* Get a list of members in this pool
* @return array of pool members
* @throws APIException connection or API errors
*/
public CreditPoolMember[] getMemberList() throws APIException {
GetCreditPoolMembersRequest request = new GetCreditPoolMembersRequest(this.client, this.id);
return request.request().getData();
}

/**
* Get a list of servers in this pool
* @return array of pool servers
* @throws APIException connection or API errors
*/
public Server[] getServerList() throws APIException {
GetCreditPoolServersRequest request = new GetCreditPoolServersRequest(this.client, this.id);
Server[] servers = request.request().getData();
for (Server server: servers) {
server.setClient(this.client);
server.fetched = true;
}
return servers;
}

/**
* update properties from fetched object
* @param pool pool fetched from the API
* @return updated pool object
*/
private CreditPool setFromObject(CreditPool pool) {
this.name = pool.getName();
this.credits = pool.getCredits();
this.servers = pool.getServers();
this.owner = pool.getOwner();
this.isOwner = pool.isOwner();
this.members = pool.getMembers();
this.ownShare = pool.getOwnShare();
this.ownCredits = pool.getOwnCredits();
return this;
}
}
Loading

0 comments on commit b783961

Please sign in to comment.