Skip to content

Commit

Permalink
Time API
Browse files Browse the repository at this point in the history
  • Loading branch information
rfresh2 committed Jan 16, 2025
1 parent 3f0bcb0 commit ba0e6da
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 1 deletion.
59 changes: 59 additions & 0 deletions src/main/java/vc/controller/TimeController.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package vc.controller;

import io.github.resilience4j.ratelimiter.annotation.RateLimiter;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.tags.Tags;
import org.jooq.DSLContext;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.OffsetDateTime;

import static vc.data.dto.tables.Worldtime.WORLDTIME;

@Tags({@Tag(name = "Time")})
@RestController
public class TimeController {
private final DSLContext dsl;

public TimeController(final DSLContext dsl) {
this.dsl = dsl;
}

public record TimeResponse(OffsetDateTime lastUpdated, int worldTime) {}

@GetMapping("/time")
@RateLimiter(name = "main")
@Cacheable("worldtime")
@ApiResponses(value = {
@ApiResponse(
responseCode = "200",
description = """
Returns the 2b2t world time in ticks (0 - 24000).
To determine the real-time 2b2t world time, interpolate between lastUpdated and the current real world time.
MC and real world time conversion help: https://minecraft.wiki/w/Daylight_cycle#Conversions
1 minecraft tick = 50 ms real world time
Daytime starts at 6:00 (0 ticks) and night begins at 18:00 (12000 ticks)
"""
),
@ApiResponse(responseCode = "204", description = "No world time data available")
})
public ResponseEntity<TimeResponse> getWorldTime() {
var record = dsl.selectFrom(WORLDTIME)
.orderBy(WORLDTIME.TIME.desc())
.limit(1)
.fetchOne();
if (record == null) {
return ResponseEntity.noContent().build();
}
int hour = (int) (record.getWorldtime() % 24000L);
return ResponseEntity.ok(new TimeResponse(record.getTime(), hour));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17323,6 +17323,10 @@
"name": "tablistInfoApiTest",
"parameterTypes": []
},
{
"name": "timeTest",
"parameterTypes": []
},
{
"name": "wordCountApiTest",
"parameterTypes": []
Expand Down Expand Up @@ -18977,6 +18981,54 @@
}
]
},
{
"type": "vc.controller.TimeController",
"allDeclaredFields": true,
"methods": [
{
"name": "<init>",
"parameterTypes": [
"org.jooq.DSLContext"
]
},
{
"name": "getWorldTime",
"parameterTypes": []
}
]
},
{
"type": "vc.controller.TimeController$$SpringCGLIB$$0",
"fields": [
{
"name": "CGLIB$CALLBACK_FILTER"
},
{
"name": "CGLIB$FACTORY_DATA"
}
]
},
{
"type": "vc.controller.TimeController$TimeResponse",
"allDeclaredFields": true,
"methods": [
{
"name": "<init>",
"parameterTypes": [
"java.time.OffsetDateTime",
"int"
]
},
{
"name": "lastUpdated",
"parameterTypes": []
},
{
"name": "worldTime",
"parameterTypes": []
}
]
},
{
"type": "vc.data.dto.enums.Connectiontype",
"allDeclaredFields": true,
Expand Down Expand Up @@ -19218,6 +19270,15 @@
}
]
},
{
"type": "vc.data.dto.tables.records.WorldtimeRecord",
"methods": [
{
"name": "<init>",
"parameterTypes": []
}
]
},
{
"type": "vc.translators.CustomExceptionHandler",
"allDeclaredFields": true,
Expand Down

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/test/java/vc/ApiTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,15 @@ public void playerLookupCraftheadTest() {
assertEquals("rfresh2", response.name());
}

@Test
public void timeTest() {
var response = httpRequest("/time",
TimeController.TimeResponse.class);
assertNotNull(response);
assertTrue(response.worldTime() > 0);
assertNotNull(response.lastUpdated());
}

private <T> T httpRequest(String url, Class<T> responseType) {
return restTemplate.getForObject("http://localhost:" + port + url, responseType);
}
Expand Down

0 comments on commit ba0e6da

Please sign in to comment.