@ -1,11 +1,17 @@
package app.services.db ;
import app.entities.Stats ;
import app.entities.db.Gametime ;
import app.entities.killfeed.KillsInFeed ;
import app.entities.other.SteamID ;
import app.repositories.GametimeRepository ;
import app.repositories.KillfeedRepository ;
import app.services.StatsService ;
import lombok.Data ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Qualifier ;
import org.springframework.data.domain.Page ;
import org.springframework.data.domain.Pageable ;
import org.springframework.jdbc.core.JdbcTemplate ;
import org.springframework.jdbc.core.RowMapper ;
import org.springframework.stereotype.Service ;
@ -16,6 +22,9 @@ import java.time.Instant;
import java.util.HashMap ;
import java.util.List ;
import java.util.Map ;
import java.util.Objects ;
import java.util.function.Function ;
import java.util.stream.Collectors ;
@Service
public class KillfeedService {
@ -23,29 +32,24 @@ public class KillfeedService {
@Autowired
@Qualifier ( "jt_ro" )
private JdbcTemplate jdbcTemplate ;
private Stats stats ;
private NicknameService nicknameService ;
private StatsService statsService ;
@Autowired
KillfeedService ( Stats stats , NicknameService nicknameService , StatsService statsService ) {
this . stats = stats ;
this . nicknameService = nicknameService ;
private GametimeRepository gametimeRepository ;
@Autowired
private KillfeedRepository killfeedRepository ;
@Autowired
KillfeedService ( StatsService statsService ) {
this . statsService = statsService ;
}
public Long getKills ( SteamID steamID , String server_id , Long utime ) {
return jdbcTemplate . query ( "SELECT COUNT(*) as c " +
"FROM user_killfeed " +
"WHERE utime > ? AND attacker_id = ? AND attacker_id != victim_id AND (? = 0 or server_id like ?)" ,
new Object [ ] {
utime = = null ? 0 : utime ,
steamID . account_id ,
server_id = = null | | server_id . isEmpty ( ) ? 0 : 1 ,
server_id = = null | | server_id . isEmpty ( ) ? "%" : server_id } ,
( rs , n ) - > rs . getLong ( "c" ) )
. stream ( ) . findFirst ( ) . orElse ( 0L ) ;
}
private final Function < Long , String > accountId2Nickname = ( account_id ) - > {
if ( account_id = = null | | account_id < 0 ) return "" ;
Gametime gametime = gametimeRepository . searchGametimeByAccountId ( account_id ) ;
return gametime = = null ? "Unknown" : gametime . getPlayer_name ( ) ;
} ;
public List < TopInFeed > getTopKills ( String server_id ) {
return jdbcTemplate . query ( "SELECT attacker_id, count(*) as c, server_id " +
@ -56,20 +60,9 @@ public class KillfeedService {
new Object [ ] {
server_id = = null | | server_id . isEmpty ( ) ? 0 : 1 ,
server_id = = null | | server_id . isEmpty ( ) ? "%" : server_id } ,
( rs , n ) - > new TopInFeed ( rs ) . setNicknames ( nicknameService ) ) ;
}
public Long getDeads ( SteamID steamID , String server_id , Long utime ) {
return jdbcTemplate . query ( "SELECT COUNT(*) as c " +
"FROM user_killfeed " +
"WHERE utime > ? AND victim_id = ? AND attacker_id != victim_id AND (? =0 or server_id like ?)" ,
new Object [ ] {
utime = = null ? 0 : utime ,
steamID . account_id ,
server_id = = null | | server_id . isEmpty ( ) ? 0 : 1 ,
server_id = = null | | server_id . isEmpty ( ) ? "%" : server_id } ,
( rs , n ) - > rs . getLong ( "c" ) )
. stream ( ) . findFirst ( ) . orElse ( 0L ) ;
( rs , n ) - > new TopInFeed ( rs ) ) . stream ( ) . peek ( t - > {
t . name = accountId2Nickname . apply ( t . uid ) ;
} ) . toList ( ) ;
}
public List < TopInFeed > getTopDeads ( String server_id ) {
@ -81,33 +74,9 @@ public class KillfeedService {
new Object [ ] {
server_id = = null | | server_id . isEmpty ( ) ? 0 : 1 ,
server_id = = null | | server_id . isEmpty ( ) ? "%" : server_id } ,
( rs , n ) - > new TopInFeed ( rs ) . setNicknames ( nicknameService ) ) ;
}
public Long getSuicides ( SteamID steamID , String server_id , Long utime ) {
return jdbcTemplate . query ( "SELECT COUNT(*) as c " +
"FROM user_killfeed " +
"WHERE utime > ? AND victim_id = ? AND attacker_id = victim_id AND (? =0 or server_id like ?)" ,
new Object [ ] {
utime = = null ? 0 : utime ,
steamID . account_id ,
server_id = = null | | server_id . isEmpty ( ) ? 0 : 1 ,
server_id = = null | | server_id . isEmpty ( ) ? "%" : server_id } ,
( rs , n ) - > rs . getLong ( "c" ) )
. stream ( ) . findFirst ( ) . orElse ( 0L ) ;
}
public Long getAssists ( SteamID steamID , String server_id , Long utime ) {
return jdbcTemplate . query ( "SELECT COUNT(*) as c " +
"FROM user_killfeed " +
"WHERE utime > ? AND assister_id = ? AND (? =0 or server_id like ?)" ,
new Object [ ] {
utime = = null ? 0 : utime ,
steamID . account_id ,
server_id = = null | | server_id . isEmpty ( ) ? 0 : 1 ,
server_id = = null | | server_id . isEmpty ( ) ? "%" : server_id } ,
( rs , n ) - > rs . getLong ( "c" ) )
. stream ( ) . findFirst ( ) . orElse ( 0L ) ;
( rs , n ) - > new TopInFeed ( rs ) ) . stream ( ) . peek ( t - > {
t . name = accountId2Nickname . apply ( t . uid ) ;
} ) . toList ( ) ;
}
public Map getPopulateWeapons ( SteamID steamID , String server_id , int offset , int limit ) {
@ -132,111 +101,65 @@ public class KillfeedService {
return Map . of ( "result" , result , "count" , - 1 ) ;
}
public Map getKills ( SteamID steamID , String server_id , int offset , int limit , boolean current ) {
long current_utime = 0L ;
if ( current ) {
Long currentPlayerSesTime = statsService . searchCurrentPlayerStartTime ( steamID ) ;
if ( currentPlayerSesTime ! = null ) current_utime = Instant . now ( ) . getEpochSecond ( ) - currentPlayerSesTime ;
else current_utime = Instant . now ( ) . getEpochSecond ( ) ;
}
List < KillsInFeed > result = jdbcTemplate . query ( "SELECT -1, u.victim_id, u.assister_id, u.utime, i.name, u.server_id, u.weapon_classname " +
"FROM user_killfeed as u " +
"INNER JOIN tf2idb.tf2idb_item as i ON u.weapon_index = i.id " +
"WHERE u.utime > ? AND u.attacker_id = ? AND u.attacker_id != u.victim_id AND (? =0 or u.server_id like ?) " +
"ORDER BY u.id DESC LIMIT ? OFFSET ?" ,
new Object [ ] {
current_utime ,
steamID . account_id ,
server_id = = null | | server_id . isEmpty ( ) ? 0 : 1 ,
server_id = = null | | server_id . isEmpty ( ) ? "%" : server_id ,
limit ,
offset } ,
( rs , n ) - > new KillsInFeed ( rs ) . setNicknames ( nicknameService ) ) ;
Long count = jdbcTemplate . query ( "SELECT COUNT(*) as c " +
"FROM user_killfeed " +
"WHERE utime > ? AND attacker_id = ? AND attacker_id != victim_id AND (? =0 or server_id like ?)" ,
new Object [ ] {
current_utime ,
steamID . account_id ,
server_id = = null | | server_id . isEmpty ( ) ? 0 : 1 ,
server_id = = null | | server_id . isEmpty ( ) ? "%" : server_id } ,
( rs , n ) - > rs . getLong ( "c" ) )
. stream ( ) . findFirst ( ) . orElse ( 0L ) ;
return Map . of ( "result" , result , "count" , count , "type" , FeedType . KILL . ordinal ( ) ) ;
public Long getKills ( SteamID steamID , String server_id , Long utime ) {
return killfeedRepository . getKills ( steamID . account_id , server_id , utime ) ;
}
public Map getDeads ( SteamID steamID , String server_id , int offset , int limit , boolean current ) {
long current_utime = 0L ;
if ( current ) {
Long currentPlayerSesTime = statsService . searchCurrentPlayerStartTime ( steamID ) ;
if ( currentPlayerSesTime ! = null ) current_utime = Instant . now ( ) . getEpochSecond ( ) - currentPlayerSesTime ;
else current_utime = Instant . now ( ) . getEpochSecond ( ) ;
}
List < KillsInFeed > result = jdbcTemplate . query ( "SELECT u.attacker_id, -1, u.assister_id, u.utime, i.name, u.server_id, u.weapon_classname " +
"FROM user_killfeed as u " +
"INNER JOIN tf2idb.tf2idb_item as i ON u.weapon_index = i.id " +
"WHERE u.utime > ? AND u.victim_id = ? AND u.attacker_id != u.victim_id AND (? =0 or u.server_id like ?) " +
"ORDER BY u.id DESC LIMIT ? OFFSET ?" ,
new Object [ ] {
current_utime ,
steamID . account_id ,
server_id = = null | | server_id . isEmpty ( ) ? 0 : 1 ,
server_id = = null | | server_id . isEmpty ( ) ? "%" : server_id ,
limit ,
offset } ,
( rs , n ) - > new KillsInFeed ( rs ) . setNicknames ( nicknameService ) ) ;
Long count = jdbcTemplate . query ( "SELECT COUNT(*) as c " +
"FROM user_killfeed " +
"WHERE utime > ? AND victim_id = ? AND attacker_id != victim_id AND (? =0 or server_id like ?)" ,
new Object [ ] {
current_utime ,
steamID . account_id ,
server_id = = null | | server_id . isEmpty ( ) ? 0 : 1 ,
server_id = = null | | server_id . isEmpty ( ) ? "%" : server_id } ,
( rs , n ) - > rs . getLong ( "c" ) ) . stream ( ) . findFirst ( ) . orElse ( 0L ) ;
public Long getDeads ( SteamID steamID , String server_id , Long utime ) {
return killfeedRepository . getDeads ( steamID . account_id , server_id , utime ) ;
}
return Map . of ( "result" , result , "count" , count , "type" , FeedType . DEAD . ordinal ( ) ) ;
public Long getSuicides ( SteamID steamID , String server_id , Long utime ) {
return killfeedRepository . getSuicides ( steamID . account_id , server_id , utime ) ;
}
public Map getAssists ( SteamID steamID , String server_id , int offset , int limit , boolean current ) {
long current_utime = 0L ;
if ( current ) {
Long currentPlayerSesTime = statsService . searchCurrentPlayerStartTime ( steamID ) ;
if ( currentPlayerSesTime ! = null ) current_utime = Instant . now ( ) . getEpochSecond ( ) - currentPlayerSesTime ;
else current_utime = Instant . now ( ) . getEpochSecond ( ) ;
}
public Long getAssists ( SteamID steamID , String server_id , Long utime ) {
return killfeedRepository . getAssists ( steamID . account_id , server_id , utime ) ;
}
List < KillsInFeed > result = jdbcTemplate . query ( "SELECT u.attacker_id, u.victim_id, -1, u.utime, i.name, u.server_id, u.weapon_classname " +
"FROM user_killfeed as u " +
"INNER JOIN tf2idb.tf2idb_item as i ON u.weapon_index = i.id " +
"WHERE u.utime > ? AND u.assister_id = ? AND (? =0 or u.server_id like ?) " +
"ORDER BY u.id DESC LIMIT ? OFFSET ?" ,
new Object [ ] {
current_utime ,
steamID . account_id ,
server_id = = null | | server_id . isEmpty ( ) ? 0 : 1 ,
server_id = = null | | server_id . isEmpty ( ) ? "%" : server_id ,
limit ,
offset } ,
( rs , n ) - > new KillsInFeed ( rs ) . setNicknames ( nicknameService ) ) ;
public Page < KillsInFeed > getKills ( Pageable pageable , List < SteamID > accounts , String server_id , boolean current ) {
Page < KillsInFeed > result = killfeedRepository . getKills (
pageable ,
accounts . stream ( )
. filter ( Objects : : nonNull )
. map ( s - > s . account_id ) . toList ( ) ,
server_id , current ? Instant . now ( ) . getEpochSecond ( ) : null ) ;
result . getContent ( ) . forEach ( k - > {
k . setAssister_name ( accountId2Nickname . apply ( k . getAssister_id ( ) ) ) ;
k . setVictim_name ( accountId2Nickname . apply ( k . getVictim_id ( ) ) ) ;
k . setAttacker_name ( accountId2Nickname . apply ( k . getAttacker_id ( ) ) ) ;
} ) ;
return result ;
}
Long count = jdbcTemplate . query ( "SELECT COUNT(*) as c " +
"FROM user_killfeed " +
"WHERE utime > ? AND assister_id = ? AND (? =0 or server_id like ?)" ,
new Object [ ] {
current_utime ,
steamID . account_id ,
server_id = = null | | server_id . isEmpty ( ) ? 0 : 1 ,
server_id = = null | | server_id . isEmpty ( ) ? "%" : server_id } ,
( rs , n ) - > rs . getLong ( "c" ) )
. stream ( ) . findFirst ( ) . orElse ( 0L ) ;
public Page < KillsInFeed > getDeads ( Pageable pageable , List < SteamID > accounts , String server_id , boolean current ) {
Page < KillsInFeed > result = killfeedRepository . getDeads (
pageable ,
accounts . stream ( )
. filter ( Objects : : nonNull )
. map ( s - > s . account_id ) . toList ( ) ,
server_id , current ? Instant . now ( ) . getEpochSecond ( ) : null ) ;
result . getContent ( ) . forEach ( k - > {
k . setAssister_name ( accountId2Nickname . apply ( k . getAssister_id ( ) ) ) ;
k . setVictim_name ( accountId2Nickname . apply ( k . getVictim_id ( ) ) ) ;
k . setAttacker_name ( accountId2Nickname . apply ( k . getAttacker_id ( ) ) ) ;
} ) ;
return result ;
}
return Map . of ( "result" , result , "count" , count , "type" , FeedType . ASSIST . ordinal ( ) ) ;
public Page < KillsInFeed > getAssists ( Pageable pageable , List < SteamID > accounts , String server_id , boolean current ) {
Page < KillsInFeed > result = killfeedRepository . getAssists (
pageable ,
accounts . stream ( )
. filter ( Objects : : nonNull )
. map ( s - > s . account_id ) . toList ( ) ,
server_id , current ? Instant . now ( ) . getEpochSecond ( ) : null ) ;
result . getContent ( ) . forEach ( k - > {
k . setAssister_name ( accountId2Nickname . apply ( k . getAssister_id ( ) ) ) ;
k . setVictim_name ( accountId2Nickname . apply ( k . getVictim_id ( ) ) ) ;
k . setAttacker_name ( accountId2Nickname . apply ( k . getAttacker_id ( ) ) ) ;
} ) ;
return result ;
}
@Data
@ -257,95 +180,25 @@ public class KillfeedService {
}
}
enum FeedType {
KILL , DEAD , ASSIST
}
@Data
class TopInFeed {
int uid ;
Long uid ;
String name ;
long count ;
String server_id ;
TopInFeed ( Object obj ) {
this . uid = ( int ) ( ( Object [ ] ) obj ) [ 0 ] ;
this . uid = ( long ) ( ( Object [ ] ) obj ) [ 0 ] ;
this . count = ( long ) ( ( Object [ ] ) obj ) [ 1 ] ;
this . server_id = ( String ) ( ( Object [ ] ) obj ) [ 2 ] ;
}
//attacker_id, count(*) as c, server_id
TopInFeed ( ResultSet resultSet ) throws SQLException {
uid = resultSet . getInt ( 1 ) ;
uid = resultSet . getLong ( 1 ) ;
count = resultSet . getLong ( "c" ) ;
server_id = resultSet . getString ( "server_id" ) ;
}
public TopInFeed setNicknames ( NicknameService nicknameService ) {
this . name = uid < = 0 | | server_id . isEmpty ( ) | | server_id = = null ? "" : nicknameService . grabNickname ( uid ) ;
return this ;
}
}
/ *
CREATE TABLE user_killfeed (
id bigint ( 11 ) NOT NULL ,
attacker_id int ( 11 ) NOT NULL ,
victim_id int ( 11 ) NOT NULL ,
assister_id int ( 11 ) NOT NULL ,
utime bigint ( 20 ) NOT NULL ,
weapon_name varchar ( 128 ) NOT NULL ,
weapon_id int ( 11 ) NOT NULL ,
weapon_classname varchar ( 128 ) NOT NULL ,
weapon_index int ( 11 ) NOT NULL ,
custom_kill int ( 11 ) NOT NULL ,
crit_type int ( 11 ) NOT NULL ,
server_id varchar ( 32 ) NOT NULL
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci ;
* /
@Data
class KillsInFeed {
int attacker_id ;
String attacker_name ;
int victim_id ;
String victim_name ;
int assister_id ;
String assister_name ;
long utime ;
private String weapon_name ;
private String weapon_classname ;
String server_id ;
KillsInFeed ( Object obj ) {
this . attacker_id = ( int ) ( ( Object [ ] ) obj ) [ 0 ] ;
this . victim_id = ( int ) ( ( Object [ ] ) obj ) [ 1 ] ;
this . assister_id = ( int ) ( ( Object [ ] ) obj ) [ 2 ] ;
this . utime = ( long ) ( ( Object [ ] ) obj ) [ 3 ] ;
this . weapon_name = ( String ) ( ( Object [ ] ) obj ) [ 4 ] ;
this . server_id = ( String ) ( ( Object [ ] ) obj ) [ 5 ] ;
this . weapon_classname = ( String ) ( ( Object [ ] ) obj ) [ 6 ] ;
}
KillsInFeed ( ResultSet rs ) throws SQLException {
assister_id = rs . getInt ( 1 ) ;
victim_id = rs . getInt ( 2 ) ;
assister_id = rs . getInt ( 3 ) ;
utime = rs . getLong ( 4 ) ;
weapon_name = rs . getString ( 5 ) ;
server_id = rs . getString ( 6 ) ;
weapon_classname = rs . getString ( 7 ) ;
}
public KillsInFeed setNicknames ( NicknameService nicknameService ) {
this . attacker_name = attacker_id = = - 1 | | server_id . isEmpty ( ) | | server_id = = null ? "" : nicknameService . grabNickname ( attacker_id ) ;
this . victim_name = victim_id = = - 1 | | server_id . isEmpty ( ) | | server_id = = null ? "" : nicknameService . grabNickname ( victim_id ) ;
this . assister_name = assister_id = = - 1 | | server_id . isEmpty ( ) | | server_id = = null ? "" : nicknameService . grabNickname ( assister_id ) ;
return this ;
}
public String getWeapon_name ( ) {
//obj_sentrygun
if ( this . weapon_classname . contains ( "obj_sentrygun" ) )
return "Турель " + ( this . weapon_classname . replace ( "obj_sentrygun" , "" ) . isEmpty ( ) ? "1" : this . weapon_classname . replace ( "obj_sentrygun" , "" ) ) + " уровня" ;
return this . weapon_name ;
}
}
}