Browse Source

new db update

master
gsd 1 year ago
parent
commit
dc2845b140
  1. 9
      src/main/java/app/annotations/impl/WebAccessAspect.java
  2. 5
      src/main/java/app/entities/PlayerProfile.java
  3. 14
      src/main/java/app/entities/db/Killfeed.java
  4. 26
      src/main/java/app/services/ProfileService.java
  5. 115
      src/main/java/app/services/db/KillfeedService.java
  6. 60
      src/main/java/app/services/db/MessageService.java
  7. 52
      src/main/java/app/services/db/NicknameService.java

9
src/main/java/app/annotations/impl/WebAccessAspect.java

@ -16,6 +16,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
@Aspect
@Configuration
@ -23,14 +24,22 @@ public class WebAccessAspect {
SaltedCookie saltedCookie;
private final Logger logger = LoggerFactory.getLogger(WebAccessAspect.class);
private boolean enabled = true;
@Autowired
public WebAccessAspect(SaltedCookie saltedCookie) {
this.saltedCookie = saltedCookie;
if (System.getenv("DISABLE_AUTH") != null)
this.enabled = !System.getenv("DISABLE_AUTH").equals("true");
if (!this.enabled) {
logger.warn("!!!AUTH DISABLED!!!");
}
}
@Before("@annotation(app.annotations.interfaces.CheckWebAccess) && args(request,..)")
public void before(JoinPoint joinPoint, HttpServletRequest request){
if (!enabled) return;
AuthMethod auth_method = ((MethodSignature) joinPoint.getSignature()).getMethod().getAnnotation(CheckWebAccess.class).auth_method();
logger.info("check web access");
if(!(request instanceof HttpServletRequest)) {

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

@ -2,10 +2,13 @@ package app.entities;
import app.entities.db.Ban;
import app.entities.db.DonateStat;
import app.entities.db.Killfeed;
import app.entities.db.Permition;
import app.entities.other.SteamID;
import app.entities.server.PlayOn;
import app.entities.steam.SteamData;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Builder;
import lombok.Data;
import java.math.BigDecimal;
@ -25,4 +28,6 @@ public class PlayerProfile {
List attached_discords;
List<DonateStat> donates;
List<Ban> ban_list;
Killfeed killfeed;
Long messages;
}

14
src/main/java/app/entities/db/Killfeed.java

@ -0,0 +1,14 @@
package app.entities.db;
import lombok.Data;
@Data
public class Killfeed {
long kills;
long deads;
long assists;
public Killfeed setKills(Long count) {this.kills = count; return this;}
public Killfeed setDeads(Long count) {this.deads = count; return this;}
public Killfeed setAssists(Long count) {this.assists = count; return this;}
}

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

@ -3,6 +3,7 @@ package app.services;
import app.entities.PlayerProfile;
import app.entities.Stats;
import app.entities.db.Ban;
import app.entities.db.Killfeed;
import app.entities.other.SteamID;
import app.services.db.*;
import app.services.steam.SteamWebApi;
@ -25,6 +26,8 @@ public class ProfileService {
DiscordAuthService discordAuthService;
DetectService detectService;
DonateService donateService;
MessageService messageService;
KillfeedService killfeedService;
@Autowired
public ProfileService(SteamWebApi steamWebApi,
@ -34,7 +37,9 @@ public class ProfileService {
BanService banService,
DiscordAuthService discordAuthService,
DetectService detectService,
DonateService donateService) {
DonateService donateService,
MessageService messageService,
KillfeedService killfeedService) {
this.steamWebApi = steamWebApi;
this.usertimeService = usertimeService;
this.permitionService = permitionService;
@ -43,6 +48,8 @@ public class ProfileService {
this.discordAuthService = discordAuthService;
this.detectService = detectService;
this.donateService = donateService;
this.messageService = messageService;
this.killfeedService = killfeedService;
}
public PlayerProfile GetProfile(SteamID steamID, List<String> requests) {
@ -128,6 +135,23 @@ public class ProfileService {
profile.getResponse_time().put("ban_list", Double.valueOf(end_time) / 1000);
}
//if(requests.contains("killfeed")){
start_time = Instant.now().toEpochMilli();
profile.setKillfeed((new Killfeed())
.setKills(killfeedService.getKills(steamID, null))
.setDeads(killfeedService.getDeads(steamID, null))
.setAssists(killfeedService.getAssists(steamID, null)));
end_time = Instant.now().toEpochMilli() - start_time;
profile.getResponse_time().put("killfeed", Double.valueOf(end_time) / 1000);
//}
//if(requests.contains("messages")){
start_time = Instant.now().toEpochMilli();
profile.setMessages(messageService.getMessageCount(steamID, null));
end_time = Instant.now().toEpochMilli() - start_time;
profile.getResponse_time().put("messages", Double.valueOf(end_time) / 1000);
//}
return profile;
}

115
src/main/java/app/services/db/KillfeedService.java

@ -0,0 +1,115 @@
package app.services.db;
import app.entities.Stats;
import app.entities.other.SteamID;
import app.utils.SteamIDConverter;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import jakarta.persistence.EntityManager;
import lombok.Data;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional("RoTransactionManager")
public class KillfeedService {
private EntityManager entityManager;
private Stats stats;
private NicknameService nicknameService;
KillfeedService(Stats stats, @Qualifier(value = "RoEntityManager") EntityManager entityManager, NicknameService nicknameService) {
this.entityManager = entityManager;
this.stats = stats;
this.nicknameService = nicknameService;
}
//
public List<HypeWeapons> getPopulalatedWeapons(SteamID steamID, String server_id) {
return entityManager.createNativeQuery("SELECT COUNT(u.`weapon_index`) as c, i.`name` FROM `user_killfeed` as u INNER JOIN `tf2idb`.`tf2idb_item` as i ON u.`weapon_index` = i.`id` WHERE u.`attacker_id` = ?1 AND u.server_id like ?2 GROUP BY u.`weapon_index` DESC ORDER BY `c` DESC")
.setParameter(1, steamID.account_id)
.setParameter(2, server_id==null?'%':server_id)
.getResultStream().map(obj -> new HypeWeapons(obj)).toList();
}
public Long getKills(SteamID steamID, String server_id) {
return (Long) entityManager.createNativeQuery("SELECT COUNT(*) FROM `user_killfeed` WHERE `attacker_id` = ?1 AND `attacker_id` != `victim_id` AND `server_id` like ?2")
.setParameter(1, steamID.account_id)
.setParameter(2, server_id==null?'%':server_id)
.getSingleResult();
}
public Long getDeads(SteamID steamID, String server_id) {
return (Long) entityManager.createNativeQuery("SELECT COUNT(*) FROM `user_killfeed` WHERE `victim_id` = ?1 AND `attacker_id` != `victim_id` AND `server_id` like ?2")
.setParameter(1, steamID.account_id)
.setParameter(2, server_id==null?'%':server_id)
.getSingleResult();
}
public Long getAssists(SteamID steamID, String server_id) {
return (Long) entityManager.createNativeQuery("SELECT COUNT(*) FROM `user_killfeed` WHERE `assister_id` = ?1 AND `server_id` like ?2")
.setParameter(1, steamID.account_id)
.setParameter(2, server_id==null?'%':server_id)
.getSingleResult();
}
//;
public List<KillsInFeed> getKills(SteamID steamID, String server_id, int offset, int limit) {
List<KillsInFeed> result = entityManager.createNativeQuery("SELECT u.victim_id, u.assister_id, u.utime, i.name, u.server_id FROM `user_killfeed` as u INNER JOIN `tf2idb`.`tf2idb_item` as i ON u.`weapon_index` = i.`id` WHERE `attacker_id` = ?1 AND `attacker_id` != `victim_id` AND `server_id` like ?2 ORDER BY u.`id` DESC LIMIT ?3 OFFSET ?4")
.setParameter(1, steamID.account_id)
.setParameter(2, server_id==null?'%':server_id)
.setParameter(3, limit)
.setParameter(4, offset)
.getResultStream().map(obj -> new KillsInFeed(obj)).toList();
return result.stream().peek(kif -> kif.setNicknames(nicknameService)).toList();
}
@Data
class HypeWeapons {
long count;
String name;
HypeWeapons(Object obj) {
this.count = (long) ((Object[]) obj)[0];
this.name = (String) ((Object[]) obj)[1];
}
}
@Data
class KillsInFeed {
int victim_id;
String victim_name;
int assister_id;
String assister_name;
long utime;
String weapon_name;
private String server_id;
KillsInFeed(Object obj) {
this.victim_id = (int) ((Object[]) obj)[0];
this.assister_id = (int) ((Object[]) obj)[1];
this.utime = (long) ((Object[]) obj)[2];
this.weapon_name = (String) ((Object[]) obj)[3];
this.server_id = (String) ((Object[]) obj)[4];
}
public void setNicknames(NicknameService nicknameService) {
this.victim_name = nicknameService.grabNickname(victim_id, server_id);
this.assister_name = assister_id == -1?"":nicknameService.grabNickname(assister_id, server_id);
}
@JsonValue
@JsonSerialize(using = ToStringSerializer.class)
public long victim_steam64() {
return SteamIDConverter.getSteamID(victim_id).steam64;
}
@JsonValue
@JsonSerialize(using = ToStringSerializer.class)
public long assister_steam64() {
return assister_id == -1?-1:SteamIDConverter.getSteamID(assister_id).steam64;
}
}
}

60
src/main/java/app/services/db/MessageService.java

@ -0,0 +1,60 @@
package app.services.db;
import app.entities.Stats;
import app.entities.other.SteamID;
import jakarta.persistence.EntityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
@Transactional("RoTransactionManager")
public class MessageService {
private EntityManager entityManager;
private Stats stats;
private NicknameService nicknameService;
MessageService(Stats stats, @Qualifier(value = "RoEntityManager") EntityManager entityManager, NicknameService nicknameService) {
this.entityManager = entityManager;
this.stats = stats;
this.nicknameService = nicknameService;
}
public List<Message> getMessages(String server_id, int offset, int limit) {
List<Message> result = entityManager.createNativeQuery("SELECT `account_id`,`utime`,`message`,`server_id` FROM `user_messages` WHERE `server_id` like ?1 ORDER BY `user_messages`.`id` DESC LIMIT ?2 OFFSET ?3")
.setParameter(1, server_id==null?'%':server_id)
.setParameter(2, limit)
.setParameter(3, offset)
.getResultStream().map(obj -> new Message(obj)).toList();
return result.stream().peek(message -> message.setAccount_name(nicknameService)).toList();
}
public Long getMessageCount(SteamID steamID, String server_id) {
return (Long) entityManager.createNativeQuery("SELECT COUNT(*) FROM `user_messages` WHERE `account_id` = ?1 AND `server_id` like ?2")
.setParameter(1, steamID.account_id)
.setParameter(2, server_id==null?"%":server_id)
.getSingleResult();
}
class Message {
int account_id;
long utime;
String message;
private String server_id;
String account_name;
Message(Object obj) {
this.account_id = (int) ((Object[]) obj)[0];
this.utime = (long) ((Object[]) obj)[1];
this.message = (String) ((Object[]) obj)[2];
this.server_id = (String) ((Object[]) obj)[3];
}
public void setAccount_name(NicknameService nicknameService) {
this.account_name = nicknameService.grabNickname(account_id, server_id);
}
}
}

52
src/main/java/app/services/db/NicknameService.java

@ -0,0 +1,52 @@
package app.services.db;
import app.entities.Stats;
import app.entities.other.SteamID;
import app.entities.server.Server;
import jakarta.persistence.EntityManager;
import org.apache.commons.math3.util.Pair;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.Instant;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Optional;
@Service
@Transactional("RoTransactionManager")
public class NicknameService {
private EntityManager entityManager;
private Stats stats;
private HashMap<Integer, Pair<String, Long>> nickname_cache = new HashMap<>();
private long refresh_after = 900;
NicknameService(Stats stats, @Qualifier(value = "RoEntityManager") EntityManager entityManager) {
this.entityManager = entityManager;
this.stats = stats;
}
public String getDB4Server(String server_id) {
return stats.getServers().entrySet().stream().filter(ent -> ent.getKey().equals(server_id)).map(ent -> ent.getValue().getDb()).findFirst().orElse(null);
}
public String grabNickname(int account_id, String server_id) {
if (nickname_cache.containsKey(account_id)) {
if (Instant.now().getEpochSecond() - nickname_cache.get(account_id).getValue() < refresh_after) {
return nickname_cache.get(account_id).getKey();
}
}
String nickname = grabNicknameFromDb(account_id, getDB4Server(server_id));
nickname_cache.put(account_id, Pair.create(nickname, Instant.now().getEpochSecond()));
return nickname;
}
private String grabNicknameFromDb(int account_id, String db) {
return (String) Optional.of(entityManager.createNativeQuery("SELECT `player_name` FROM `"+db+"`.`user_connections` WHERE `account_id` = ?1 ORDER BY `user_connections`.`id` DESC LIMIT 1;")
.setParameter(1, account_id)
.getSingleResult()).orElse("Unknown");
}
}
Loading…
Cancel
Save