7 changed files with 280 additions and 1 deletions
@ -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;} |
|||
} |
@ -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; |
|||
} |
|||
} |
|||
} |
@ -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); |
|||
} |
|||
} |
|||
} |
@ -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…
Reference in new issue