hirezapi.json.Ping Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of HiRezApi-common Show documentation
Show all versions of HiRezApi-common Show documentation
Java-Based API Wrapper for Hi-Rez Studios games.
The newest version!
package hirezapi.json;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import lombok.Data;
import lombok.ToString;
@Data
@ToString(exclude = "rawMessage")
public class Ping {
private final String game;
private final String version;
private final String versionName;
private final boolean pingRecived;
private final Instant timestamp;
private final String rawMessage;
/**
* Ping message model.
* @param rawMessage RAW message
*/
public Ping(String rawMessage) {
this.rawMessage = rawMessage.replace("\"", "");
Matcher matcher = Pattern
.compile("^(?Smite|Paladins)API \\(ver (?(?:[0-9]+\\.){3}[0-9]+)\\)"
+ " \\[PATCH - (?.+)] - (?.+)\\. Server Date:(?.+)$")
.matcher(this.rawMessage);
matcher.find();
this.game = matcher.group("game");
this.version = matcher.group("version");
this.versionName = matcher.group("versionName");
this.pingRecived = matcher.group("ping").contains("successful");
this.timestamp = parse(matcher.group("timestamp"));
}
private Instant parse(String timestamp) {
if (timestamp.contains("\\/")) {
timestamp = timestamp.replace("\\/", "/");
}
try {
SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss a");
sdf.setTimeZone(TimeZone.getTimeZone(ZoneOffset.UTC));
return sdf.parse(timestamp).toInstant();
} catch (ParseException e) {
e.printStackTrace();
}
return Instant.now();
}
}