197 lines
7.5 KiB
Java
197 lines
7.5 KiB
Java
package com.alterdekim.game.controller;
|
|
|
|
import com.alterdekim.game.component.GameServer;
|
|
import com.alterdekim.game.component.NativeDao;
|
|
import com.alterdekim.game.component.StartUpListener;
|
|
import com.alterdekim.game.controller.result.api.ApiResult;
|
|
import com.alterdekim.game.controller.result.api.PreloaderResult;
|
|
import com.alterdekim.game.controller.result.api.PromotionBannerResult;
|
|
import com.alterdekim.game.controller.result.api.PromotionResult;
|
|
import com.alterdekim.game.entity.Location;
|
|
import com.alterdekim.game.entity.Preloader;
|
|
import com.alterdekim.game.entity.Promotion;
|
|
import com.alterdekim.game.entity.PromotionBanner;
|
|
import com.alterdekim.game.service.*;
|
|
import com.alterdekim.game.utils.DateUtils;
|
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
|
import jakarta.servlet.http.HttpServletRequest;
|
|
import lombok.AllArgsConstructor;
|
|
import lombok.Getter;
|
|
import lombok.NoArgsConstructor;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.core.env.Environment;
|
|
import org.springframework.http.MediaType;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.io.IOException;
|
|
import java.io.RandomAccessFile;
|
|
import java.lang.reflect.Field;
|
|
import java.lang.reflect.InvocationTargetException;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Paths;
|
|
import java.text.SimpleDateFormat;
|
|
import java.time.Duration;
|
|
import java.time.temporal.ChronoUnit;
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Slf4j
|
|
@RestController
|
|
@RequestMapping("/api")
|
|
public class ApiController {
|
|
@Autowired
|
|
private Environment env;
|
|
|
|
@Autowired
|
|
private NativeDao nativeDao;
|
|
|
|
@Autowired
|
|
private StartUpListener startUpListener;
|
|
|
|
@Autowired
|
|
private GameServer gameServer;
|
|
|
|
@Autowired
|
|
private UserService userService;
|
|
|
|
@Autowired
|
|
private PreloaderService preloaderService;
|
|
|
|
@Autowired
|
|
private PromotionService promotionService;
|
|
|
|
@Autowired
|
|
private PromotionBannerService promotionBannerService;
|
|
|
|
@Autowired
|
|
private LocationService locationService;
|
|
|
|
@PostMapping("/delete_row")
|
|
public ResponseEntity deleteRow(@RequestParam("table") APITable table, @RequestParam("row") Long rowId) {
|
|
switch (table) {
|
|
case Preloaders -> preloaderService.removeById(rowId);
|
|
case Promotions -> promotionService.removeById(rowId);
|
|
case Banners -> promotionBannerService.removeById(rowId);
|
|
case Locations -> locationService.removeById(rowId);
|
|
}
|
|
return ResponseEntity.ok().build();
|
|
}
|
|
|
|
@PostMapping("/update_table")
|
|
public ResponseEntity updateRow(@RequestParam("table") APITable table,
|
|
@RequestParam("column") String column,
|
|
@RequestParam("id") Long id,
|
|
@RequestParam("val") Boolean val) {
|
|
if( column.matches("[^a-zA-Z]") || id < 1 ) return ResponseEntity.badRequest().build();
|
|
nativeDao.updateTable(table.getVal(), column, val, id);
|
|
return ResponseEntity.noContent().build();
|
|
}
|
|
|
|
@RequestMapping(value = "/get_entity_info", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
public ResponseEntity<Map<String, String>> getEntityInfo(@RequestParam("table") APITable table) {
|
|
Map<String, String> m = new HashMap<>();
|
|
for( Field f : table.getCl().getDeclaredFields() ) {
|
|
m.put(f.getName(), f.getType().getSimpleName());
|
|
}
|
|
return ResponseEntity.ok(m);
|
|
}
|
|
|
|
@Getter
|
|
@AllArgsConstructor
|
|
public enum APITable {
|
|
Preloaders("preloaders", Preloader.class, StaticController.PanelSection.Preloaders),
|
|
Promotions("promotions", Promotion.class, StaticController.PanelSection.Preloaders),
|
|
Banners("promotion_banners", PromotionBanner.class, StaticController.PanelSection.Preloaders),
|
|
Locations("locations", Location.class, StaticController.PanelSection.Locations);
|
|
|
|
private final String val;
|
|
private final Class<?> cl;
|
|
private final StaticController.PanelSection section;
|
|
}
|
|
|
|
@GetMapping("/get_dashboard_logs")
|
|
public ResponseEntity<String> getLogs() {
|
|
try {
|
|
Path logFile = Paths.get(env.getProperty("logging.file.name"));
|
|
RandomAccessFile file = new RandomAccessFile(logFile.toFile(), "r");
|
|
int n = 2048;
|
|
file.seek(logFile.toFile().length() - n);
|
|
byte[] b = new byte[n];
|
|
file.read(b, 0, n);
|
|
return ResponseEntity.ok(new String(b));
|
|
} catch (IOException e) {
|
|
log.error("getLogs error: {}", e.getMessage());
|
|
return ResponseEntity.badRequest().build();
|
|
}
|
|
}
|
|
|
|
@GetMapping("/get_uptime")
|
|
public ResponseEntity<String> getUpTime() {
|
|
return ResponseEntity.ok(
|
|
DateUtils.prettyPrint(Duration.of(System.currentTimeMillis() - startUpListener.getStartTime(), ChronoUnit.MILLIS))
|
|
);
|
|
}
|
|
|
|
@GetMapping("/get_players_online")
|
|
public ResponseEntity<Integer> getPlayersOnline() {
|
|
return ResponseEntity.ok(gameServer.getPlayersCount());
|
|
}
|
|
|
|
@GetMapping("/get_users_count")
|
|
public ResponseEntity<Long> getUsersCount() {
|
|
return ResponseEntity.ok(userService.getUsersCount());
|
|
}
|
|
|
|
@RequestMapping(value = "/get_entity_rows", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
public ResponseEntity<List<Object>> getEntityRows(@RequestParam("entity") APITable table) {
|
|
var r = switch (table) {
|
|
case Preloaders -> preloaderService.getAll();
|
|
case Promotions -> promotionService.getAll();
|
|
case Banners -> promotionBannerService.getAll();
|
|
case Locations -> locationService.getAll();
|
|
};
|
|
|
|
return ResponseEntity.ok(
|
|
r
|
|
.stream()
|
|
.map(ApiResult::toAPIResult)
|
|
.collect(Collectors.toList())
|
|
);
|
|
}
|
|
|
|
@RequestMapping(value = "/add_row", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
public ResponseEntity<AddEntryResponse> addSpecificRow(HttpServletRequest httpServletRequest) {
|
|
if( !httpServletRequest.getParameterMap().containsKey("table") ) return ResponseEntity.badRequest().build();
|
|
try {
|
|
APITable table = APITable.valueOf(httpServletRequest.getParameterMap().get("table")[0]);
|
|
List<Field> fields = Arrays.stream(table.getCl().getDeclaredFields())
|
|
.filter(f -> httpServletRequest.getParameterMap().containsKey(f.getName()))
|
|
.collect(Collectors.toList());
|
|
|
|
Map<String, String> vals = new HashMap<>();
|
|
for (Field f : fields) {
|
|
String val = httpServletRequest.getParameterMap().get(f.getName())[0];
|
|
val = switch (f.getType().getSimpleName()) {
|
|
case "String" -> "'" + val + "'";
|
|
case "Boolean" -> val.equals("true") ? "1" : "0";
|
|
default -> val;
|
|
};
|
|
vals.put(f.getName(), val);
|
|
}
|
|
nativeDao.insertEntity(table.getVal(), vals);
|
|
return ResponseEntity.ok(new AddEntryResponse(table.getSection().name()));
|
|
} catch (Exception e) {
|
|
log.error("addEntry error: {}", e.getMessage());
|
|
}
|
|
return ResponseEntity.internalServerError().build();
|
|
}
|
|
|
|
@AllArgsConstructor
|
|
private class AddEntryResponse {
|
|
@JsonProperty
|
|
private String section;
|
|
}
|
|
}
|