Browse Source

base profile

master
gsd 2 years ago
parent
commit
4c97094709
  1. 2
      src/main/java/app/controllers/AuthController.java
  2. 35
      src/main/java/app/controllers/ProfileController.java
  3. 17
      src/main/java/app/entities/PlayerProfile.java
  4. 16
      src/main/java/app/entities/steam/SteamData.java
  5. 37
      src/main/java/app/services/ProfileService.java
  6. 2
      src/main/java/app/services/io/GeoIP.java
  7. 5
      src/main/java/app/services/io/ServersReader.java
  8. 2
      src/main/java/app/services/steam/SteamSignIn.java
  9. 35
      src/main/java/app/services/steam/SteamWebApi.java
  10. 2
      src/main/java/app/updates/CountriesUpdater.java

2
src/main/java/app/controllers/AuthController.java

@ -1,6 +1,6 @@
package app.controllers;
import app.services.SteamSignIn;
import app.services.steam.SteamSignIn;
import app.utils.SaltedCookie;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletResponse;

35
src/main/java/app/controllers/ProfileController.java

@ -0,0 +1,35 @@
package app.controllers;
import app.services.ProfileService;
import app.utils.SaltedCookie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("api/profile")
public class ProfileController {
private SaltedCookie saltedCookie;
private ProfileService profileService;
@Autowired
public ProfileController(SaltedCookie saltedCookie,
ProfileService profileService) {
this.saltedCookie = saltedCookie;
this.profileService = profileService;
}
@GetMapping("/current")
public ResponseEntity GetCurrentUser(
@CookieValue(value = "steam64", defaultValue = "") String steam64,
@CookieValue(value = "steam64_secured", defaultValue = "") String steam64_secured
){
if(!saltedCookie.Validate(steam64, steam64_secured)) return new ResponseEntity<>(HttpStatus.FORBIDDEN);
return new ResponseEntity(profileService.GetProfile(steam64), HttpStatus.OK);
}
}

17
src/main/java/app/entities/PlayerProfile.java

@ -0,0 +1,17 @@
package app.entities;
import app.entities.other.SteamID;
import app.entities.steam.SteamData;
import lombok.Data;
import java.util.HashMap;
@Data
public class PlayerProfile {
Object ban;
HashMap<String, HashMap<String, Long>> gametime;
Object permition;
HashMap<String, Long> response_time;
SteamData steam_data;
SteamID steamids;
}

16
src/main/java/app/entities/steam/SteamData.java

@ -0,0 +1,16 @@
package app.entities.steam;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.Data;
@Data
public class SteamData {
String avatar;
String nickname;
double time;
public SteamData(JsonNode player){
nickname = player.get("personaname").asText();
avatar = player.get("avatar").asText();
}
}

37
src/main/java/app/services/ProfileService.java

@ -0,0 +1,37 @@
package app.services;
import app.controllers.ProfileController;
import app.entities.PlayerProfile;
import app.entities.other.SteamID;
import app.services.steam.SteamWebApi;
import app.utils.SteamIDConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.List;
@Service
public class ProfileService {
SteamWebApi steamWebApi;
@Autowired
public ProfileService(SteamWebApi steamWebApi) {
this.steamWebApi = steamWebApi;
}
public PlayerProfile GetProfile(String steam64, List<String> requests) {
PlayerProfile profile = new PlayerProfile();
SteamID steamID = SteamIDConverter.getSteamID(steam64);
profile.setSteamids(steamID);
if(requests.contains("steam_data")){
profile.setSteam_data(steamWebApi.getSteamData(steamID.steam64));
}
return profile;
}
public PlayerProfile GetProfile(String steam64) {
return GetProfile(steam64, List.of("steam_data".split(",")));
}
}

2
src/main/java/app/services/GeoIP.java → src/main/java/app/services/io/GeoIP.java

@ -1,4 +1,4 @@
package app.services;
package app.services.io;
import com.maxmind.geoip2.DatabaseReader;
import com.maxmind.geoip2.exception.GeoIp2Exception;

5
src/main/java/app/services/ServersReader.java → src/main/java/app/services/io/ServersReader.java

@ -1,6 +1,7 @@
package app.services;
package app.services.io;
import app.entities.server.Server;
import app.services.Stats;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.PostConstruct;
@ -29,7 +30,7 @@ public class ServersReader {
Iterator<Map.Entry<String, JsonNode>> iterator = node.fields();
while (iterator.hasNext()) {
Map.Entry<String, JsonNode> server = iterator.next();
stats.servers.put(server.getKey(), this.objectMapper.treeToValue(server.getValue(), Server.class));
stats.getServers().put(server.getKey(), this.objectMapper.treeToValue(server.getValue(), Server.class));
}
} catch (IOException err) {
System.out.printf("Cannot read servers file: %s\n", servers_path);

2
src/main/java/app/services/SteamSignIn.java → src/main/java/app/services/steam/SteamSignIn.java

@ -1,4 +1,4 @@
package app.services;
package app.services.steam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

35
src/main/java/app/services/steam/SteamWebApi.java

@ -0,0 +1,35 @@
package app.services.steam;
import app.entities.steam.SteamData;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
@Service
public class SteamWebApi {
private RestTemplate restTemplate;
@Value("${backend.auth.steam_api_key}")
private String webapi_key;
@Autowired
public SteamWebApi(){
restTemplate = new RestTemplate();
}
public SteamData getSteamData(long steam64){
String url = String.format("https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v0002/?key=%s&steamids=%s", webapi_key, steam64);
try {
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);
JsonNode player = (new ObjectMapper()).readTree(response.getBody()).get("response").get("players").get(0);
return new SteamData(player);
} catch (Exception err){
return null;
}
}
}

2
src/main/java/app/updates/CountriesUpdater.java

@ -1,7 +1,7 @@
package app.updates;
import app.entities.server.Server;
import app.services.GeoIP;
import app.services.io.GeoIP;
import app.services.Stats;
import com.maxmind.geoip2.exception.GeoIp2Exception;
import jakarta.annotation.PostConstruct;

Loading…
Cancel
Save