diff --git a/pom.xml b/pom.xml index 547d619..aed25af 100644 --- a/pom.xml +++ b/pom.xml @@ -53,10 +53,11 @@ 3.0.2 - com.mysql - mysql-connector-j - 8.0.32 + org.postgresql + postgresql + 42.6.0 + org.springframework.boot spring-boot-starter-actuator diff --git a/src/main/java/app/services/db/BanService.java b/src/main/java/app/services/db/BanService.java index eaa6d78..aacc812 100644 --- a/src/main/java/app/services/db/BanService.java +++ b/src/main/java/app/services/db/BanService.java @@ -33,7 +33,7 @@ public class BanService { } public Ban getBan(SteamID steamID) { - return jdbcTemplate.query("SELECT * FROM light_bans WHERE account_id = ? AND active = 1", + return jdbcTemplate.query("SELECT * FROM light_bans WHERE account_id = ? AND active = true", new Object[]{ steamID.account_id }, (rs, n) -> new Ban(rs)) .stream() @@ -55,7 +55,7 @@ public class BanService { } public List getBans(SteamID steamID) { - return jdbcTemplate.query("SELECT * FROM light_bans WHERE account_id = ? AND active = 0", + return jdbcTemplate.query("SELECT * FROM light_bans WHERE account_id = ? AND active = false", new Object[]{steamID.account_id}, (rs, n) -> new Ban(rs)) .stream() @@ -65,8 +65,8 @@ public class BanService { } public List getLastBans(Integer limit, Integer offset) { - return jdbcTemplate.query("SELECT * FROM `light_bans` WHERE `active` = ? ORDER BY `light_bans`.`id` DESC LIMIT ? OFFSET ?", - new Object[]{1, limit, offset}, + return jdbcTemplate.query("SELECT * FROM light_bans WHERE active = ? ORDER BY light_bans.id DESC LIMIT ? OFFSET ?", + new Object[]{true, limit, offset}, (rs, n) -> new Ban(rs)) .stream() .peek(ban -> ban.cryptIP(cryptedCookie)) @@ -75,7 +75,7 @@ public class BanService { } public Long getBansCount() { - return jdbcTemplate.query("SELECT COUNT(*) as c FROM `light_bans` WHERE active = 1", + return jdbcTemplate.query("SELECT COUNT(*) as c FROM light_bans WHERE active = true", (rs, n) -> rs.getLong("c")).stream().findFirst().orElse(0L); } @@ -83,7 +83,7 @@ public class BanService { Ban ban = getBan(user.getSteamids()); if (ban == null) return false; return jdbcTemplate.update("UPDATE light_bans SET active = ?, unbanned_by_id = ?, unbanned_timestamp = CURRENT_TIMESTAMP WHERE id = ?", - 0, admin.getSteamids().steam2, ban.getId()) > 0; + false, admin.getSteamids().steam2, ban.getId()) > 0; } public int addBan(PlayerProfile user, PlayerProfile admin, int ban_length, String ban_reason) { @@ -99,8 +99,8 @@ public class BanService { } public List getUsersDiscordWithBanOnServers() { - return jdbcTemplate.query("SELECT `discord_id` FROM `light_bans` INNER JOIN `steam2discord` ON `light_bans`.`active` = ? AND `light_bans`.`steam_id` COLLATE utf8mb4_unicode_ci LIKE `steam2discord`.`steam_id`", - new Object[]{1}, + return jdbcTemplate.query("SELECT discord_id FROM light_bans INNER JOIN steam2discord ON light_bans.active = ? AND light_bans.steam_id LIKE steam2discord.steam_id", + new Object[]{true}, (rs, n) -> rs.getLong("discord_id")); } diff --git a/src/main/java/app/services/db/DBService.java b/src/main/java/app/services/db/DBService.java index d5a26f0..7d8da2c 100644 --- a/src/main/java/app/services/db/DBService.java +++ b/src/main/java/app/services/db/DBService.java @@ -31,13 +31,13 @@ public class DBService { } public Long getMainServerTime() { - return jdbcTemplate_rw.query("SELECT UNIX_TIMESTAMP() as u", + return jdbcTemplate_rw.query("select CAST(extract(epoch from now()) as BIGINT) as u", (rs, n) -> rs.getLong("u")) .stream().findFirst().orElse(0L); } public Long getReplicaServerTime() { - return jdbcTemplate_rw.query("SELECT UNIX_TIMESTAMP() as u", + return jdbcTemplate_rw.query("select CAST(extract(epoch from now()) as BIGINT) as u", (rs, n) -> rs.getLong("u")) .stream().findFirst().orElse(0L); } diff --git a/src/main/java/app/services/db/DetectService.java b/src/main/java/app/services/db/DetectService.java index d7934bc..28a3242 100644 --- a/src/main/java/app/services/db/DetectService.java +++ b/src/main/java/app/services/db/DetectService.java @@ -40,11 +40,11 @@ public class DetectService { public List getAccountsPerSteamID(SteamID steamID) { List founded_steamid = new ArrayList<>(); for (Map.Entry stringServerEntry : stats.getServers().entrySet()) { - founded_steamid.addAll(jdbcTemplate_ro.query("SELECT `steam_id` FROM `" + stringServerEntry.getValue().getDb() + "`." + "`user_connections` WHERE `connect_ip` in (SELECT `connect_ip` FROM `" + stringServerEntry.getValue().getDb() + "`." + "`user_connections` WHERE `steam_id` LIKE ? AND `connect_ip` NOT LIKE '10.%' GROUP BY `connect_ip` ORDER BY `id` DESC) GROUP BY `steam_id`", + founded_steamid.addAll(jdbcTemplate_ro.query("SELECT steam_id FROM " + stringServerEntry.getValue().getDb() + "." + "user_connections WHERE connect_ip in (SELECT connect_ip FROM " + stringServerEntry.getValue().getDb() + "." + "user_connections WHERE steam_id LIKE ? AND connect_ip NOT LIKE '10.%' GROUP BY connect_ip ORDER BY id DESC) GROUP BY steam_id", new Object[]{steamID.steam2}, (rs, n) -> rs.getString("steam_id"))); } - //SELECT `steam_id` FROM `tf2.facti13.five`.`user_connections` WHERE `connect_ip` in (SELECT `connect_ip` FROM `tf2.facti13.five`.`user_connections` WHERE `steam_id` LIKE 'STEAM_0:0:63666481' AND `connect_ip` NOT LIKE '10.%' GROUP BY `connect_ip` ORDER BY `id` DESC) GROUP BY `steam_id`; + //SELECT steam_id FROM tf2.facti13.five.user_connections WHERE connect_ip in (SELECT connect_ip FROM tf2.facti13.five.user_connections WHERE steam_id LIKE 'STEAM_0:0:63666481' AND connect_ip NOT LIKE '10.%' GROUP BY connect_ip ORDER BY id DESC) GROUP BY steam_id; return founded_steamid.stream().distinct().map(SteamIDConverter::getSteamID).filter(Objects::nonNull).toList(); } @@ -61,7 +61,7 @@ public class DetectService { em_selector.set(0); } - String sql = "SELECT `steam_id` FROM `"+stringServerEntry.getValue().getDb()+"`."+"`user_connections` WHERE `connect_ip` in (SELECT `connect_ip` FROM `" + stringServerEntry.getValue().getDb() + "`." +"`user_connections` WHERE `steam_id` LIKE ? AND `connect_ip` NOT LIKE '10.%' GROUP BY `connect_ip` ORDER BY `id` DESC) GROUP BY `steam_id`"; + String sql = "SELECT steam_id FROM "+stringServerEntry.getValue().getDb()+"."+"user_connections WHERE connect_ip in (SELECT connect_ip FROM " + stringServerEntry.getValue().getDb() + "." +"user_connections WHERE steam_id LIKE ? AND connect_ip NOT LIKE '10.%' GROUP BY connect_ip ORDER BY id DESC) GROUP BY steam_id"; switch (em_selector.get()) { case 0:return jdbcTemplate_ro.query(sql, new Object[]{steamID.steam2}, (rs, n) -> rs.getString("steam_id")); @@ -81,9 +81,9 @@ public class DetectService { } public List> getAttachedDiscordAccountPerSteam(SteamID steamID) { - return jdbcTemplate_ro.query("SELECT `discord_id`, UNIX_TIMESTAMP(`timestamp`) as utime, `active` FROM `steam2discord` WHERE `steam_id` LIKE ? GROUP BY `discord_id`", + return jdbcTemplate_ro.query("SELECT discord_id, extract(epoch from timestamp) as utime, active FROM steam2discord WHERE steam_id LIKE ? GROUP BY discord_id", new Object[]{ steamID.steam2 }, (rs, n) -> Map.of("discord_id", rs.getObject("discord_id"), "utime", rs.getObject("utime"), "active", rs.getObject("active"))); } - //SELECT CONCAT(SUBSTRING_INDEX(connect_ip,'.', 1),'.',SUBSTRING_INDEX(SUBSTRING_INDEX(connect_ip,'.', 2),'.',-1),'.',SUBSTRING_INDEX(SUBSTRING_INDEX(connect_ip,'.', 3),'.',-1),'.','%') FROM `all_users_con_2` WHERE `steam_id` LIKE 'STEAM_0:1:768781968'; + //SELECT CONCAT(SUBSTRING_INDEX(connect_ip,'.', 1),'.',SUBSTRING_INDEX(SUBSTRING_INDEX(connect_ip,'.', 2),'.',-1),'.',SUBSTRING_INDEX(SUBSTRING_INDEX(connect_ip,'.', 3),'.',-1),'.','%') FROM all_users_con_2 WHERE steam_id LIKE 'STEAM_0:1:768781968'; } diff --git a/src/main/java/app/services/db/DiscordAuthService.java b/src/main/java/app/services/db/DiscordAuthService.java index b9242e2..2304977 100644 --- a/src/main/java/app/services/db/DiscordAuthService.java +++ b/src/main/java/app/services/db/DiscordAuthService.java @@ -23,32 +23,32 @@ public class DiscordAuthService { private HashMap> processing_accounts = new HashMap<>(); public SteamID getSteamIDofDiscordID(String discord_id) { - return jdbcTemplate.query("SELECT steam_id FROM steam2discord WHERE discord_id = ? AND active = 1 LIMIT 1", + return jdbcTemplate.query("SELECT steam_id FROM steam2discord WHERE discord_id = ? AND active = true LIMIT 1", new Object[]{ discord_id }, (rs, n) -> SteamIDConverter.getSteamID(rs.getString("steam_id"))) .stream().findFirst().orElse(null); } public String getDiscordIDofSteamID(SteamID steamID) { - return jdbcTemplate.query("SELECT discord_id FROM steam2discord WHERE steam_id = ? AND active = 1 LIMIT 1", + return jdbcTemplate.query("SELECT discord_id FROM steam2discord WHERE steam_id = ? AND active = true LIMIT 1", new Object[]{ steamID.steam2 }, (rs, n) -> rs.getString("discord_id")) .stream().findFirst().orElse(null); } public boolean setSteamIDofDiscordID(SteamID steamID, String discord_id) { - return jdbcTemplate.update("INSERT INTO `steam2discord` (`id`, `steam_id`, `discord_id`, `timestamp`, `active`) VALUES (NULL, ?, ?, current_timestamp(), '1')", + return jdbcTemplate.update("INSERT INTO steam2discord (steam_id, discord_id, timestamp, active) VALUES (?, ?, current_timestamp, true)", steamID.steam2, discord_id) > 0; } public boolean removeSteamIDofDiscordID(SteamID steamID) { - return jdbcTemplate.update("UPDATE `steam2discord` SET `active` = '0' WHERE `steam_id` LIKE ?", + return jdbcTemplate.update("UPDATE steam2discord SET active = false WHERE steam_id LIKE ?", steamID.steam2) > 0; } public List getAccountsNotInList(List discord_accounts) { String inSql = String.join(",", Collections.nCopies(discord_accounts.size(), "?")); - return jdbcTemplate.query("SELECT `discord_id` FROM `steam2discord` WHERE `active` = 1 AND `discord_id` in (" + inSql + ")", + return jdbcTemplate.query("SELECT discord_id FROM steam2discord WHERE active = true AND discord_id in (" + inSql + ")", discord_accounts.toArray(), (rs, n) -> rs.getString("discord_id")); } diff --git a/src/main/java/app/services/db/DonateService.java b/src/main/java/app/services/db/DonateService.java index 735fb37..b6ac59e 100644 --- a/src/main/java/app/services/db/DonateService.java +++ b/src/main/java/app/services/db/DonateService.java @@ -39,9 +39,9 @@ public class DonateService { //KEY, METAL public Pair getSummarySteamDonateValue(StatisticRange statisticRange) { - return jdbcTemplate.query("SELECT SUM(CAST(REPLACE(REPLACE(substring_index(`reserved`,';',-2), 'metal=',''), ';','') AS INT)) as r," + - " SUM(CAST(REPLACE(REPLACE(substring_index(`reserved`,';',1), 'keys=',''), ';','') as INT)) as k " + - "FROM `gived_vip` WHERE `givemethod` = ? AND `timestamp` > CAST(DATE_FORMAT(NOW() ,?) as DATE)", + return jdbcTemplate.query("SELECT SUM(CAST(REPLACE(REPLACE(substring_index(reserved,';',-2), 'metal=',''), ';','') AS INT)) as r," + + " SUM(CAST(REPLACE(REPLACE(substring_index(reserved,';',1), 'keys=',''), ';','') as INT)) as k " + + "FROM gived_vip WHERE givemethod = ? AND timestamp > CAST(DATE_FORMAT(NOW() ,?) as DATE)", new Object[]{VipGiveMethod.STEAM.ordinal(), StatisticRange.cast(statisticRange)}, (rs, n) -> Pair.of( Optional.of(rs.getInt("k")).orElse(0), @@ -51,29 +51,29 @@ public class DonateService { } public Integer getSummaryQiwiDonateValue(StatisticRange statisticRange) { - return jdbcTemplate.query("SELECT SUM(CAST(REPLACE(REPLACE(substring_index(`reserved`,';',1), 'rub=',''), ';','') as INT)) as k " + - "FROM `gived_vip` WHERE `givemethod` = ? AND `timestamp` > CAST(DATE_FORMAT(NOW() ,?) as DATE)", + return jdbcTemplate.query("SELECT SUM(CAST(REPLACE(REPLACE(substring_index(reserved,';',1), 'rub=',''), ';','') as INT)) as k " + + "FROM gived_vip WHERE givemethod = ? AND timestamp > CAST(DATE_FORMAT(NOW() ,?) as DATE)", new Object[]{ VipGiveMethod.QIWI.ordinal(), StatisticRange.cast(statisticRange)}, (rs, n) -> rs.getInt("k")).stream().findFirst().orElse(0); } public Integer getSummaryDonationAlertsDonateValue(StatisticRange statisticRange) { - return jdbcTemplate.query("SELECT SUM(CAST(REPLACE(REPLACE(substring_index(`reserved`,';',1), 'rub=',''), ';','') as INT)) as k " + - "FROM `gived_vip` WHERE `givemethod` = ? AND `timestamp` > CAST(DATE_FORMAT(NOW() ,?) as DATE)", + return jdbcTemplate.query("SELECT SUM(CAST(REPLACE(REPLACE(substring_index(reserved,';',1), 'rub=',''), ';','') as INT)) as k " + + "FROM gived_vip WHERE givemethod = ? AND timestamp > CAST(DATE_FORMAT(NOW() ,?) as DATE)", new Object[]{ VipGiveMethod.DONATIONALERTS.ordinal(), StatisticRange.cast(statisticRange)}, (rs, n) -> rs.getInt("k")).stream().findFirst().orElse(0); } //no need if use getGivedVipStatistic public Long getSummaryGivedFreeVip(StatisticRange statisticRange) { - return jdbcTemplate.query("SELECT COUNT(*) as count FROM `gived_vip` WHERE `givemethod` = ? AND `timestamp` > CAST(DATE_FORMAT(NOW() ,?) as DATE)", + return jdbcTemplate.query("SELECT COUNT(*) as count FROM gived_vip WHERE givemethod = ? AND timestamp > CAST(DATE_FORMAT(NOW() ,?) as DATE)", new Object[]{ VipGiveMethod.FREE.ordinal(), StatisticRange.cast(statisticRange) }, (rs, n) -> rs.getLong("count")) .stream().findFirst().orElse(0L); } public HashMap getGivedVipStatistic(StatisticRange statisticRange) { - List donateStatistics = jdbcTemplate.query("SELECT `givemethod`,COUNT(*) as count,`reserved`, `amount` FROM `gived_vip` WHERE `givemethod` IN (0,1,2,6) AND `timestamp` > CAST(DATE_FORMAT(NOW() ,?) as DATE) GROUP BY `givemethod`, `amount`", + List donateStatistics = jdbcTemplate.query("SELECT givemethod,COUNT(*) as count,reserved, amount FROM gived_vip WHERE givemethod IN (0,1,2,6) AND timestamp > CAST(DATE_FORMAT(NOW() ,?) as DATE) GROUP BY givemethod, amount", new Object[]{StatisticRange.cast(statisticRange)}, (rs, n) -> new DonateStatistic(rs)); @@ -113,7 +113,7 @@ public class DonateService { } public List getDonateStatistic(SteamID steamID) { - return jdbcTemplate.query("SELECT *, UNIX_TIMESTAMP(`timestamp`) as utime FROM `gived_vip` WHERE `steam2` LIKE ?", + return jdbcTemplate.query("SELECT *, extract(epoch from timestamp) as utime FROM gived_vip WHERE steam2 LIKE ?", new Object[]{steamID.steam2}, (rs, n) -> new DonateStat(rs)); } @@ -136,10 +136,10 @@ public class DonateService { @PostConstruct public void UpdateStatistic() { - stats.getDonate().put("day", getDonateStatistic(StatisticRange.DAY)); - stats.getDonate().put("month", getDonateStatistic(StatisticRange.MONTH)); - stats.getDonate().put("year", getDonateStatistic(StatisticRange.YEAR)); + //stats.getDonate().put("day", getDonateStatistic(StatisticRange.DAY)); + //stats.getDonate().put("month", getDonateStatistic(StatisticRange.MONTH)); + //stats.getDonate().put("year", getDonateStatistic(StatisticRange.YEAR)); } - //надо добавить шейхов GROUP BY `steam2` ORDER BY `r` DESC + //надо добавить шейхов GROUP BY steam2 ORDER BY r DESC } diff --git a/src/main/java/app/services/db/FreeVIPService.java b/src/main/java/app/services/db/FreeVIPService.java index 73495cf..b2c517b 100644 --- a/src/main/java/app/services/db/FreeVIPService.java +++ b/src/main/java/app/services/db/FreeVIPService.java @@ -34,19 +34,19 @@ public class FreeVIPService{ } public Long getDBServerUTime() { - return jdbcTemplate.query("SELECT UNIX_TIMESTAMP(NOW()) as c", + return jdbcTemplate.query("select CAST(extract(epoch from now()) as BIGINT) as c", (rs, n) -> rs.getLong("c")) .stream().findFirst().orElse(0L); } public List getSocial(SteamID steamID) { - return jdbcTemplate.query("SELECT id, steam3, vk_id, discord_id, date FROM free_vip WHERE steam3 = ? ORDER BY `free_vip`.`date` DESC", + return jdbcTemplate.query("SELECT id, steam3, vk_id, discord_id, date FROM free_vip WHERE steam3 = ? ORDER BY free_vip.date DESC", new Object[]{steamID.steam2}, (rs, n) -> new FreeVIP(rs)); } public Long getLastGivedFreeVIP(SteamID steamID) { - return jdbcTemplate.query("SELECT UNIX_TIMESTAMP(`date`) as u_time FROM free_vip WHERE steam3 LIKE ? ORDER BY `date` DESC", + return jdbcTemplate.query("SELECT extract(epoch from date) as u_time FROM free_vip WHERE steam3 LIKE ? ORDER BY date DESC", new Object[]{ steamID.steam2 }, (rs, n) -> rs.getLong("u_time")) .stream().findFirst().orElse(0L); @@ -57,12 +57,12 @@ public class FreeVIPService{ return 0L; } else if (socialAuth.getVk_id() != 0) { - return jdbcTemplate.query("SELECT UNIX_TIMESTAMP(`date`) as u_time FROM free_vip WHERE vk_id = ? ORDER BY `date` DESC", + return jdbcTemplate.query("SELECT extract(epoch from date) as u_time FROM free_vip WHERE vk_id = ? ORDER BY date DESC", new Object[]{ socialAuth.getVk_id() }, (rs, n) -> rs.getLong("u_time")) .stream().findFirst().orElse(0L); } else if (socialAuth.getDiscord_id() != 0) { - return jdbcTemplate.query("SELECT UNIX_TIMESTAMP(`date`) as u_time FROM free_vip WHERE discord_id = ? ORDER BY `date` DESC", + return jdbcTemplate.query("SELECT extract(epoch from date) as u_time FROM free_vip WHERE discord_id = ? ORDER BY date DESC", new Object[]{ socialAuth.getDiscord_id() }, (rs, n) -> rs.getLong("u_time")) .stream().findFirst().orElse(0L); @@ -73,7 +73,7 @@ public class FreeVIPService{ public boolean setGivedFreeVip(SteamID steamID, SocialAuth socialAuth) { if (socialAuth == null) socialAuth = new SocialAuth(); - return jdbcTemplate.update("INSERT INTO `free_vip` (`id`, `steam3`, `vk_id`, `discord_id`, `date`) VALUES (NULL, ?, ?, ?, current_timestamp())", + return jdbcTemplate.update("INSERT INTO free_vip (steam3, vk_id, discord_id, date) VALUES (?, ?, ?, current_timestamp)", steamID.steam2, socialAuth.getVk_id(), socialAuth.getDiscord_id()) > 0; } diff --git a/src/main/java/app/services/db/KillfeedService.java b/src/main/java/app/services/db/KillfeedService.java index 730d925..fb97463 100644 --- a/src/main/java/app/services/db/KillfeedService.java +++ b/src/main/java/app/services/db/KillfeedService.java @@ -35,51 +35,51 @@ public class KillfeedService { } 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 `server_id` like ?", + return jdbcTemplate.query("SELECT COUNT(*) as c FROM user_killfeed WHERE utime > ? AND attacker_id = ? AND attacker_id != victim_id AND server_id like ?", new Object[]{ utime==null?0:utime, steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id }, (rs, n) -> rs.getLong("c")) .stream().findFirst().orElse(0L); } public List getTopKills(String server_id) { - return jdbcTemplate.query("SELECT `attacker_id`, count(*) as c, `server_id` FROM `user_killfeed` WHERE `victim_id` != `attacker_id` AND `attacker_id` != 0 AND `server_id` LIKE ? GROUP BY `attacker_id` ORDER BY `c` DESC LIMIT 10", + return jdbcTemplate.query("SELECT attacker_id, count(*) as c, server_id FROM user_killfeed WHERE victim_id != attacker_id AND attacker_id != 0 AND server_id LIKE ? GROUP BY attacker_id ORDER BY c DESC LIMIT 10", new Object[]{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 `server_id` like ?", + return jdbcTemplate.query("SELECT COUNT(*) as c FROM user_killfeed WHERE utime > ? AND victim_id = ? AND attacker_id != victim_id AND server_id like ?", new Object[]{ utime==null?0:utime, steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id }, (rs, n) -> rs.getLong("c")) .stream().findFirst().orElse(0L); } public List getTopDeads(String server_id) { - return jdbcTemplate.query("SELECT `victim_id`, count(`id`) as c, `server_id` FROM `user_killfeed` WHERE `victim_id` != `attacker_id` AND `victim_id` != 0 AND `server_id` LIKE ? GROUP BY `victim_id` ORDER BY `c` DESC LIMIT 10", + return jdbcTemplate.query("SELECT victim_id, count(id) as c, server_id FROM user_killfeed WHERE victim_id != attacker_id AND victim_id != 0 AND server_id LIKE ? GROUP BY victim_id ORDER BY c DESC LIMIT 10", new Object[]{ 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 `server_id` like ?", + return jdbcTemplate.query("SELECT COUNT(*) as c FROM user_killfeed WHERE utime > ? AND victim_id = ? AND attacker_id = victim_id AND server_id like ?", new Object[]{ utime==null?0:utime, steamID.account_id, 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 `server_id` like ?", + return jdbcTemplate.query("SELECT COUNT(*) as c FROM user_killfeed WHERE utime > ? AND assister_id = ? AND server_id like ?", new Object[]{ utime==null?0:utime, steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id}, (rs, n) -> rs.getLong("c")) .stream().findFirst().orElse(0L); } public Map getPopulateWeapons(SteamID steamID, String server_id, int offset, int limit) { - List result = jdbcTemplate.query("SELECT COUNT(u.`weapon_index`) as c, 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.`attacker_id` = ? AND `attacker_id` != `victim_id` AND u.server_id like ? GROUP BY u.`weapon_index` DESC ORDER BY `c` DESC LIMIT ? OFFSET ?", + List result = jdbcTemplate.query("SELECT COUNT(u.weapon_index) as c, 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.attacker_id = ? AND attacker_id != victim_id AND u.server_id like ? GROUP BY u.weapon_index ORDER BY c DESC LIMIT ? OFFSET ?", new Object[]{ steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id, limit, offset }, (rs, n) -> new HypeWeapons(rs)); - /*Long count = (Long) createNativeQuery("SELECT COUNT(`weapon_index`) FROM `user_killfeed` WHERE `attacker_id` = ? AND server_id like ? GROUP BY `weapon_index`") + /*Long count = (Long) createNativeQuery("SELECT COUNT(weapon_index) FROM user_killfeed WHERE attacker_id = ? AND server_id like ? GROUP BY weapon_index") .setParameter(1, steamID.account_id) .setParameter(2, server_id==null||server_id.isEmpty()?"%":server_id) .getSingleResult();*/ @@ -94,11 +94,11 @@ public class KillfeedService { else current_utime = Instant.now().getEpochSecond(); } - List 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 u.`server_id` like ? ORDER BY u.`id` DESC LIMIT ? OFFSET ?", + List 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 u.server_id like ? ORDER BY u.id DESC LIMIT ? OFFSET ?", new Object[]{ current_utime, steamID.account_id, 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 `server_id` like ?", + Long count = jdbcTemplate.query("SELECT COUNT(*) as c FROM user_killfeed WHERE utime > ? AND attacker_id = ? AND attacker_id != victim_id AND server_id like ?", new Object[]{ current_utime, steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id }, (rs, n) -> rs.getLong("c")) .stream().findFirst().orElse(0L); @@ -114,11 +114,11 @@ public class KillfeedService { else current_utime = Instant.now().getEpochSecond(); } - List 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 u.`server_id` like ? ORDER BY u.`id` DESC LIMIT ? OFFSET ?", + List 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 u.server_id like ? ORDER BY u.id DESC LIMIT ? OFFSET ?", new Object[]{ current_utime, steamID.account_id, 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 `server_id` like ?", + Long count = jdbcTemplate.query("SELECT COUNT(*) as c FROM user_killfeed WHERE utime > ? AND victim_id = ? AND attacker_id != victim_id AND server_id like ?", new Object[]{ current_utime, steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id }, (rs, n) -> rs.getLong("c")).stream().findFirst().orElse(0L); @@ -133,11 +133,11 @@ public class KillfeedService { else current_utime = Instant.now().getEpochSecond(); } - List 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 u.`server_id` like ? ORDER BY u.`id` DESC LIMIT ? OFFSET ?", + List 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 u.server_id like ? ORDER BY u.id DESC LIMIT ? OFFSET ?", new Object[]{ current_utime, steamID.account_id, 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 `assister_id` = ? AND `server_id` like ?", + Long count = jdbcTemplate.query("SELECT COUNT(*) as c FROM user_killfeed WHERE utime > ? AND assister_id = ? AND server_id like ?", new Object[]{ current_utime, steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id }, (rs, n) -> rs.getLong("c")) .stream().findFirst().orElse(0L); @@ -179,7 +179,7 @@ public class KillfeedService { this.server_id = (String) ((Object[]) obj) [2]; } - //`attacker_id`, count(*) as c, `server_id` + //attacker_id, count(*) as c, server_id TopInFeed(ResultSet resultSet) throws SQLException { uid = resultSet.getInt(1); count = resultSet.getLong("c"); @@ -193,19 +193,19 @@ public class KillfeedService { } /* - 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 + 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 diff --git a/src/main/java/app/services/db/MessageService.java b/src/main/java/app/services/db/MessageService.java index afab67d..0163282 100644 --- a/src/main/java/app/services/db/MessageService.java +++ b/src/main/java/app/services/db/MessageService.java @@ -31,11 +31,11 @@ public class MessageService { } public Map getAllMessages(String server_id, String filter, int offset, int limit) { - List result = jdbcTemplate.query("SELECT `account_id`,`utime`,`message`,`server_id` FROM `user_messages` WHERE `server_id` like ? AND `message` LIKE ? ORDER BY `user_messages`.`id` DESC LIMIT ? OFFSET ?", + List result = jdbcTemplate.query("SELECT account_id,utime,message,server_id FROM user_messages WHERE server_id like ? AND message LIKE ? ORDER BY user_messages.id DESC LIMIT ? OFFSET ?", new Object[]{ server_id == null || server_id.isEmpty() ? "%" : server_id, filter == null || filter.isEmpty() ? "%" : "%" + filter + "%", limit, offset}, (rs, n) -> new Message(rs)); - Long count = jdbcTemplate.query("SELECT COUNT(*) as c FROM `user_messages` WHERE `server_id` like ? AND `message` LIKE ?", + Long count = jdbcTemplate.query("SELECT COUNT(*) as c FROM user_messages WHERE server_id like ? AND message LIKE ?", new Object[]{ server_id==null||server_id.isEmpty()?"%":server_id, filter==null||filter.isEmpty()?"%":"%" + filter + "%" }, (rs, n) -> rs.getLong("c")) .stream().findFirst().orElse(0L); @@ -44,11 +44,11 @@ public class MessageService { } public Map getAccountMessages(SteamID steamID, String server_id, String filter, int offset, int limit) { - List result = jdbcTemplate.query("SELECT `account_id`,`utime`,`message`,`server_id` FROM `user_messages` WHERE `account_id` = ? AND `server_id` like ? AND `message` LIKE ? ORDER BY `user_messages`.`id` DESC LIMIT ? OFFSET ?", + List result = jdbcTemplate.query("SELECT account_id,utime,message,server_id FROM user_messages WHERE account_id = ? AND server_id like ? AND message LIKE ? ORDER BY user_messages.id DESC LIMIT ? OFFSET ?", new Object[]{steamID.account_id, server_id == null || server_id.isEmpty() ? "%" : server_id, filter == null || filter.isEmpty() ? "%" : "%" + filter + "%", limit, offset}, (rs, n) -> new Message(rs)); - Long count = jdbcTemplate.query("SELECT COUNT(*) as c FROM `user_messages` WHERE `account_id` = ? AND `server_id` like ? AND `message` LIKE ?", + Long count = jdbcTemplate.query("SELECT COUNT(*) as c FROM user_messages WHERE account_id = ? AND server_id like ? AND message LIKE ?", new Object[]{ steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id, filter==null||filter.isEmpty()?"%":"%" + filter + "%"}, (rs, n) -> rs.getLong("c")) .stream().findFirst().orElse(0L); @@ -57,19 +57,19 @@ public class MessageService { } public Long getMessageCount(SteamID steamID, String server_id) { - return jdbcTemplate.query("SELECT COUNT(*) as c FROM `user_messages` WHERE `account_id` = ? AND `server_id` like ?", + return jdbcTemplate.query("SELECT COUNT(*) as c FROM user_messages WHERE account_id = ? AND server_id like ?", new Object[]{ steamID.account_id, server_id==null||server_id.isEmpty()?"%":server_id }, (rs, n) -> rs.getLong("c")) .stream().findFirst().orElse(0L); } /* - CREATE TABLE `user_messages` ( - `id` int(11) NOT NULL, - `account_id` int(11) NOT NULL, - `utime` bigint(20) NOT NULL, - `message` varchar(512) NOT NULL, - `server_id` varchar(32) NOT NULL + CREATE TABLE user_messages ( + id int(11) NOT NULL, + account_id int(11) NOT NULL, + utime bigint(20) NOT NULL, + message varchar(512) NOT NULL, + server_id varchar(32) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; */ @Data diff --git a/src/main/java/app/services/db/NicknameService.java b/src/main/java/app/services/db/NicknameService.java index 20e093f..cb25af5 100644 --- a/src/main/java/app/services/db/NicknameService.java +++ b/src/main/java/app/services/db/NicknameService.java @@ -44,9 +44,14 @@ public class NicknameService { } private String grabNicknameFromDb(int account_id, String db) { - return jdbcTemplate.query("SELECT `player_name` FROM `"+db+"`.`user_connections` WHERE `account_id` = ? ORDER BY `user_connections`.`id` DESC LIMIT 1;", - new Object[]{ account_id }, - (rs, n) -> rs.getString("player_name")) - .stream().findFirst().orElse("Unknown"); + try { + return jdbcTemplate.query("SELECT player_name FROM " + db + ".user_connections WHERE account_id = ? ORDER BY user_connections.id DESC LIMIT 1;", + new Object[]{account_id}, + (rs, n) -> rs.getString("player_name")) + .stream().findFirst().orElse("Unknown"); + } catch (Exception e) { + e.printStackTrace(); + return "Unknown"; + } } } diff --git a/src/main/java/app/services/db/PermitionService.java b/src/main/java/app/services/db/PermitionService.java index a5cbb4b..f41e7f1 100644 --- a/src/main/java/app/services/db/PermitionService.java +++ b/src/main/java/app/services/db/PermitionService.java @@ -21,7 +21,7 @@ public class PermitionService { JdbcTemplate jdbcTemplate; public Permition getPermition(SteamID steamID){ - return jdbcTemplate.query("SELECT id, flags, immunity, status, amount, UNIX_TIMESTAMP(`reg_date`) as utime FROM sm_admins WHERE `identity` LIKE ?", + return jdbcTemplate.query("SELECT id, flags, immunity, status, amount, extract(epoch from reg_date) as utime FROM sm_admins WHERE identity LIKE ?", new Object[]{steamID.steam2}, (rs, n) -> new Permition(rs)) .stream().findFirst().orElse(null); @@ -38,7 +38,7 @@ public class PermitionService { public int addPermition(SteamID steamID, Integer amount, String flags, Integer immunity_level, String status, String comment) { if (getPermition(steamID) != null) return 0; - return jdbcTemplate.update("INSERT INTO `sm_admins` (`authtype`, `identity`, `password`, `flags`, `name`, `immunity`, `comment`, `status`, `reg_date`, `amount`) VALUES ('steam', ?, NULL, ?, '', ?, ?, ?, CURRENT_TIMESTAMP, ?)", + return jdbcTemplate.update("INSERT INTO sm_admins (authtype, identity, password, flags, name, immunity, comment, status, reg_date, amount) VALUES ('steam', ?, NULL, ?, '', ?, ?, ?, CURRENT_TIMESTAMP, ?)", steamID.steam2, flags, immunity_level, comment, status, amount); } @@ -49,7 +49,7 @@ public class PermitionService { } public int extendPermition(SteamID steamID, Integer amount, String status) { - return jdbcTemplate.update("UPDATE `sm_admins` SET `amount`=`amount`+?, `reg_date`=`reg_date` WHERE `identity` LIKE ? AND `status` LIKE ?", + return jdbcTemplate.update("UPDATE sm_admins SET amount=amount+?, reg_date=reg_date WHERE identity LIKE ? AND status LIKE ?", amount, steamID.steam2, status); } diff --git a/src/main/java/app/services/db/PromoCodeService.java b/src/main/java/app/services/db/PromoCodeService.java index 42256fd..fdafd4d 100644 --- a/src/main/java/app/services/db/PromoCodeService.java +++ b/src/main/java/app/services/db/PromoCodeService.java @@ -27,7 +27,7 @@ public class PromoCodeService { while (null != getPromoCode(code)) { code = generatePromoCode(append); } - return jdbcTemplate.update("INSERT INTO `gived_promocode` (`code`, `creator_steam64`, `action`) VALUES (?, ?, ?)", + return jdbcTemplate.update("INSERT INTO gived_promocode (code, creator_steam64, action) VALUES (?, ?, ?)", code, String.valueOf(steamID.steam64), action) > 0 ? code : ""; } @@ -39,18 +39,18 @@ public class PromoCodeService { if (lastAccept != null) { if (Instant.now().getEpochSecond() - lastAccept.getTime()/1000 < cd) return (int) (lastAccept.getTime() / 1000 - Instant.now().getEpochSecond()); } - return jdbcTemplate.update("UPDATE `gived_promocode` SET accepted_steam64 = ?, accepted_timestamp = ?, status = ? WHERE id = ?", + return jdbcTemplate.update("UPDATE gived_promocode SET accepted_steam64 = ?, accepted_timestamp = ?, status = ? WHERE id = ?", String.valueOf(steamID.steam64), Timestamp.from(Instant.now()), PromoCodeStatus.ACCEPTED.ordinal(), promoCode.getId()); } public List getActualPromocodes() { - return jdbcTemplate.query("SELECT * FROM `gived_promocode` WHERE `status` = ?", + return jdbcTemplate.query("SELECT * FROM gived_promocode WHERE status = ?", new Object[]{PromoCodeStatus.CREATED.ordinal()}, (rs, n) -> new PromoCode(rs)); } public PromoCode getPromoCode(String code) { - return jdbcTemplate.query("SELECT * FROM `gived_promocode` WHERE `code` LIKE ?", + return jdbcTemplate.query("SELECT * FROM gived_promocode WHERE code LIKE ?", new Object[]{code}, (rs, n) -> new PromoCode(rs)).stream().findFirst().orElse(null); } @@ -62,7 +62,7 @@ public class PromoCodeService { } private Timestamp getLastedAcceptFromUser(SteamID steamID) { - return jdbcTemplate.query("SELECT `accepted_timestamp` FROM `gived_promocode` WHERE accepted_steam64 LIKE ? ORDER BY `gived_promocode`.`id` DESC LIMIT 1", + return jdbcTemplate.query("SELECT accepted_timestamp FROM gived_promocode WHERE accepted_steam64 LIKE ? ORDER BY gived_promocode.id DESC LIMIT 1", new Object[]{ String.valueOf(steamID.steam64) }, (rs, n) -> rs.getTimestamp("accepted_timestamp")).stream().findFirst().orElse(null); } diff --git a/src/main/java/app/services/db/UsertimeService.java b/src/main/java/app/services/db/UsertimeService.java index c7a1921..95438d2 100644 --- a/src/main/java/app/services/db/UsertimeService.java +++ b/src/main/java/app/services/db/UsertimeService.java @@ -86,14 +86,14 @@ public class UsertimeService { } public Timestamp getLastplay(String db, SteamID steamID, String map) { - return jdbcTemplate.query("select timestamp from `"+db+"`.`user_connections` WHERE map LIKE ? and connection_type LIKE ? AND account_id = ? ORDER BY `user_connections`.`id` DESC LIMIT 1", + return jdbcTemplate.query("select timestamp from "+db+".user_connections WHERE map LIKE ? and connection_type LIKE ? AND account_id = ? ORDER BY user_connections.id DESC LIMIT 1", new Object[]{ map, "disconnect", steamID.account_id}, (rs, n) -> rs.getTimestamp("timestamp")) .stream().findFirst().orElse(null); } public BigDecimal getTotalPlaytime(String db, SteamID steamID, String map) { - List l = jdbcTemplate.query("select sum(connect_duration) as total from `"+db+"`.`user_connections` WHERE map LIKE ? and connection_type LIKE ? AND account_id = ?", + List l = jdbcTemplate.query("select sum(connect_duration) as total from "+db+".user_connections WHERE map LIKE ? and connection_type LIKE ? AND account_id = ?", new Object[]{ map, "disconnect", steamID.account_id }, (rs, n) -> rs.getBigDecimal("total")); //optional if has error @@ -101,20 +101,20 @@ public class UsertimeService { } public List getMap(String db, int limit) { - return jdbcTemplate.query("select distinct `map` from `"+db+"`.`user_connections` WHERE 1 ORDER BY `user_connections`.`id` DESC LIMIT ?", + return jdbcTemplate.query("select distinct map from "+db+".user_connections WHERE true ORDER BY user_connections.id DESC LIMIT ?", new Object[]{ limit }, (rs, n) -> rs.getString("map")); } public List getMaps(String db) { - return jdbcTemplate.query("select distinct `map` from `"+db+"`.`user_connections`", + return jdbcTemplate.query("select distinct map from "+db+".user_connections", (rs, n) -> rs.getString("map")); } public SteamID getSteamOnUsername(String username) { SteamID steamID; for (Map.Entry stringServerEntry : stats.getServers().entrySet()) { - steamID = jdbcTemplate.query("SELECT account_id FROM `"+stringServerEntry.getValue().getDb()+"`.`user_connections` WHERE player_name LIKE ? ORDER BY `id` DESC LIMIT 1", + steamID = jdbcTemplate.query("SELECT account_id FROM "+stringServerEntry.getValue().getDb()+".user_connections WHERE player_name LIKE ? ORDER BY id DESC LIMIT 1", new Object[]{ username }, (rs, n) -> SteamIDConverter.getSteamID("[U:1:%d]".formatted(rs.getInt("account_id")))) .stream().findFirst().orElse(null); @@ -127,7 +127,7 @@ public class UsertimeService { String db = stats.getServers().entrySet().stream().filter(s -> s.getKey().equals(server)).map(s -> s.getValue().getDb()).findFirst().orElse(null); if (db == null) return new ArrayList<>(); - return jdbcTemplate.query("SELECT * FROM `" + db + "`.`user_connections` WHERE account_id = ? AND connection_type LIKE ? ORDER BY `" + db + "`.`user_connections`.`id` DESC LIMIT ? OFFSET ?", + return jdbcTemplate.query("SELECT * FROM " + db + ".user_connections WHERE account_id = ? AND connection_type LIKE ? ORDER BY " + db + ".user_connections.id DESC LIMIT ? OFFSET ?", new Object[]{steamID.account_id, "disconnect", limit, offset}, (rs, n) -> new Gametime(rs)); } @@ -135,7 +135,7 @@ public class UsertimeService { public Long getTotalGametimeOnServer(SteamID steamID, String server) { String db = stats.getServers().entrySet().stream().filter(s -> s.getKey().equals(server)).map(s -> s.getValue().getDb()).findFirst().orElse(null); if (db == null) return 0L; - return jdbcTemplate.query("SELECT count(id) FROM `"+db+"`.`user_connections` WHERE account_id = ? AND connection_type LIKE ?", + return jdbcTemplate.query("SELECT count(id) FROM "+db+".user_connections WHERE account_id = ? AND connection_type LIKE ?", new Object[]{ steamID.account_id, "disconnect" }, (rs, n) -> rs.getLong(1)) .stream().findFirst().orElse(0L); diff --git a/src/main/java/app/services/db/VIPService.java b/src/main/java/app/services/db/VIPService.java index 63a0cc9..24973f1 100644 --- a/src/main/java/app/services/db/VIPService.java +++ b/src/main/java/app/services/db/VIPService.java @@ -61,25 +61,25 @@ public class VIPService { // Список ид из дискорда кто имеет платную випку public List getUsersDiscordWithActiveVIP() { - return jdbcTemplate.query("SELECT `discord_id` FROM `sm_admins` INNER JOIN `steam2discord` ON `sm_admins`.`status` LIKE ? AND (`sm_admins`.`comment` LIKE ? OR `sm_admins`.`comment` LIKE ?) AND `sm_admins`.`identity` = `steam2discord`.`steam_id`", + return jdbcTemplate.query("SELECT discord_id FROM sm_admins INNER JOIN steam2discord ON sm_admins.status LIKE ? AND (sm_admins.comment LIKE ? OR sm_admins.comment LIKE ?) AND sm_admins.identity = steam2discord.steam_id", new Object[]{"VIP", "Donate.User", "f13bot.User"}, (rs, n) -> rs.getLong("discord_id")); } // Список ид из дискорда кто имеет бесплатную випку public List getUsersDiscordWithActiveFreeVIP() { - return jdbcTemplate.query("SELECT `discord_id` FROM `sm_admins` INNER JOIN `steam2discord` ON `sm_admins`.`status` LIKE ? AND `sm_admins`.`comment` LIKE ? AND `sm_admins`.`identity` = `steam2discord`.`steam_id`", + return jdbcTemplate.query("SELECT discord_id FROM sm_admins INNER JOIN steam2discord ON sm_admins.status LIKE ? AND sm_admins.comment LIKE ? AND sm_admins.identity = steam2discord.steam_id", new Object[]{"VIP", "f13bot.FreeVIP"}, (rs, n) -> rs.getLong("discord_id")); } // Транзакция, проверка и удаление випок подшедших к концу public int removeEndedVIPs() { - List steamid2remove = jdbcTemplate.query("SELECT `identity` FROM `sm_admins` WHERE NOT UNIX_TIMESTAMP(`reg_date`) LIKE 0 AND `amount` NOT LIKE 0 AND `status` LIKE ? AND ((unix_timestamp(now()) - UNIX_TIMESTAMP(`reg_date`)) > `amount`)", + List steamid2remove = jdbcTemplate.query("SELECT identity FROM sm_admins WHERE NOT extract(epoch from reg_date) = 0 AND amount != 0 AND status LIKE ? AND ((extract(epoch from now()) - extract(epoch from reg_date)) > amount)", new Object[]{"VIP"}, (rs, n) -> SteamIDConverter.getSteamID(rs.getString("identity"))); - int removed_vips = jdbcTemplate.update("DELETE FROM `sm_admins` WHERE NOT UNIX_TIMESTAMP(`reg_date`) LIKE 0 AND `amount` NOT LIKE 0 AND `status` LIKE ? AND ((unix_timestamp(now()) - UNIX_TIMESTAMP(`reg_date`)) > `amount`)", + int removed_vips = jdbcTemplate.update("DELETE FROM sm_admins WHERE NOT extract(epoch from reg_date) = 0 AND amount != 0 AND status LIKE ? AND ((extract(epoch from now()) - extract(epoch from reg_date)) > amount)", "VIP"); steamid2remove.forEach(steamid -> { @@ -91,7 +91,7 @@ public class VIPService { // Транзакция, добавление статистики public int regesterGiveVIP(SteamID steamID, int amount, VipGiveMethod vipGiveMethod, String extra, boolean extend) { - return jdbcTemplate.update("INSERT INTO `gived_vip` (`id`, `steam2`, `amount`, `timestamp`, `givemethod`, `reserved`, `extend`) VALUES (NULL, ?, ?, current_timestamp(), ?, ?, ?)", + return jdbcTemplate.update("INSERT INTO gived_vip (steam2, amount, timestamp, givemethod, reserved, extend) VALUES (?, ?, current_timestamp, ?, ?, ?)", steamID.steam2, amount, vipGiveMethod.ordinal(), extra, extend); } diff --git a/src/main/java/app/services/db/web/CollectStatisticService.java b/src/main/java/app/services/db/web/CollectStatisticService.java index 5540378..23dbd7e 100644 --- a/src/main/java/app/services/db/web/CollectStatisticService.java +++ b/src/main/java/app/services/db/web/CollectStatisticService.java @@ -30,7 +30,7 @@ public class CollectStatisticService extends BaseUpdater { if (!collectableStatistic.isStatsRequests()) logger.info(collectableStatistic.toString()); - jdbcTemplate.update("INSERT INTO `web_statistic` (`steam64`, `client_ip`, `method`, `path`, `query`, `useragent`) VALUES (?, ?, ?, ?, ?, ?)", + jdbcTemplate.update("INSERT INTO web_statistic (steam64, client_ip, method, path, query, useragent) VALUES (?, ?, ?, ?, ?, ?)", collectableStatistic.getSteam64(), collectableStatistic.getClient_ip(), collectableStatistic.getMethod(), collectableStatistic.getPath(), collectableStatistic.getQuery(), collectableStatistic.getUseragent()); diff --git a/src/main/java/app/updates/BanCountUpdater.java b/src/main/java/app/updates/BanCountUpdater.java index d6bcace..8d48b5d 100644 --- a/src/main/java/app/updates/BanCountUpdater.java +++ b/src/main/java/app/updates/BanCountUpdater.java @@ -41,7 +41,7 @@ public class BanCountUpdater extends BaseUpdater{ public boolean UpdateBanCount(){ logger.info("Update current ban count"); - long ban_count = jdbcTemplate.query("SELECT COUNT(*) as count FROM `light_bans` WHERE active = 1", (rs, n) -> rs.getLong("count")).stream().findFirst().orElse(0L); + long ban_count = jdbcTemplate.query("SELECT COUNT(*) as count FROM light_bans WHERE active = true", (rs, n) -> rs.getLong("count")).stream().findFirst().orElse(0L); stats.setBan_count(ban_count); stats.getUpdates().merge("ban_count", Instant.now().getEpochSecond(), (x, y) -> y); return true; diff --git a/src/main/java/app/updates/CountriesUpdater.java b/src/main/java/app/updates/CountriesUpdater.java index 042e0b9..f7b005c 100644 --- a/src/main/java/app/updates/CountriesUpdater.java +++ b/src/main/java/app/updates/CountriesUpdater.java @@ -31,7 +31,7 @@ public class CountriesUpdater extends BaseUpdater{ @Value("${backend.updates.countries}") private boolean update; - private final String countries_in_current_year = "SELECT DISTINCT `connect_ip` FROM `%s`.`user_connections` WHERE connection_type LIKE 'connect' AND `timestamp` > CAST(DATE_FORMAT(NOW() ,'%%Y-01-01') as DATE) UNION\n"; + private final String countries_in_current_year = "SELECT DISTINCT connect_ip FROM %s.user_connections WHERE connection_type LIKE 'connect' AND timestamp > CAST(DATE_FORMAT(NOW() ,'%%Y-01-01') as DATE) UNION\n"; @Autowired public CountriesUpdater(Stats stats, diff --git a/src/main/java/app/updates/OnlineUpdater.java b/src/main/java/app/updates/OnlineUpdater.java index fe591d9..8e44451 100644 --- a/src/main/java/app/updates/OnlineUpdater.java +++ b/src/main/java/app/updates/OnlineUpdater.java @@ -54,26 +54,26 @@ public class OnlineUpdater extends BaseUpdater { long utime = Instant.now().getEpochSecond(); int player_count = stats.getServers().get(server_id).getPlayer_count(); int player_max = stats.getServers().get(server_id).getMax_players(); - jdbcTemplate.update("INSERT INTO `servers_online` (`utime`, `player_count`, `max_players`, `server_id`) VALUES (?, ?, ?, ?)", + jdbcTemplate.update("INSERT INTO servers_online (utime, player_count, max_players, server_id) VALUES (?, ?, ?, ?)", utime, player_count, player_max, server_id); return true; } public long getDifferentReplica() { - Long rw = jdbcTemplate.query("SELECT COUNT(*) FROM `servers_online`", (rs, n) -> rs.getLong(1)).get(0); - Long ro = jdbcTemplate_ro.query("SELECT COUNT(*) FROM `servers_online`", (rs, n) -> rs.getLong(1)).get(0); + Long rw = jdbcTemplate.query("SELECT COUNT(*) FROM servers_online", (rs, n) -> rs.getLong(1)).get(0); + Long ro = jdbcTemplate_ro.query("SELECT COUNT(*) FROM servers_online", (rs, n) -> rs.getLong(1)).get(0); return Math.abs(rw - ro); } //todo abstract on all public List getStatsOnPerDay(String server_id) { - return jdbcTemplate_ro.query("SELECT avg(`player_count`) as player_count, FROM_UNIXTIME(`utime`) as fulldate, `utime` as ts FROM `servers_online` WHERE `player_count` != 0 and `server_id` like ? GROUP BY FROM_UNIXTIME(`utime`) div ? ORDER BY `ts` ASC", + return jdbcTemplate_ro.query("SELECT avg(player_count) as player_count, cast(to_timestamp(utime) as date) as fulldate, utime as ts FROM servers_online WHERE player_count != 0 and server_id like ? GROUP BY cast(to_timestamp(utime) as date) / ? ORDER BY ts ASC", new Object[]{server_id == null || server_id.isEmpty() ? "%": server_id, PerDayStats.period}, (rs, rowNum) -> new PerDayStats(rs)); } public List getStatsOnPerFiveMinutes(String server_id) { - return jdbcTemplate_ro.query("SELECT avg(`player_count`) as player_count, FROM_UNIXTIME(`utime`) as fulldate, `utime` as ts FROM `servers_online` WHERE `player_count` != 0 and `server_id` like ? GROUP BY FROM_UNIXTIME(`utime`) div ? ORDER BY `ts` ASC", + return jdbcTemplate_ro.query("SELECT avg(player_count) as player_count, cast(to_timestamp(utime) as date) as fulldate, utime as ts FROM servers_online WHERE player_count != 0 and server_id like ? GROUP BY cast(to_timestamp(utime) as date) / ? ORDER BY ts ASC", new Object[]{server_id == null || server_id.isEmpty() ? "%": server_id, PerFiveMinutesStats.period}, (rs, rowNum) -> new PerFiveMinutesStats(rs)); } @@ -90,20 +90,20 @@ public class OnlineUpdater extends BaseUpdater { public List getMaxPeakOfDays(String server_id, int limit) { - return jdbcTemplate_ro.query("SELECT SUM(s.player_count) as player_count, s.fulldate as fl FROM (SELECT max(`player_count`) as player_count, cast(FROM_UNIXTIME(`utime`) as date) as fulldate,`server_id` FROM `servers_online` WHERE `player_count` != 0 and `server_id` like ? GROUP BY cast(FROM_UNIXTIME(`utime`) as date), server_id ORDER BY `fulldate` DESC) s GROUP BY s.fulldate ORDER BY `s`.`fulldate` DESC LIMIT ?", + return jdbcTemplate_ro.query("SELECT SUM(s.player_count) as player_count, s.fulldate as fl FROM (SELECT max(player_count) as player_count, cast(to_timestamp(utime) as date) as fulldate,server_id FROM servers_online WHERE player_count != 0 and server_id like ? GROUP BY cast(to_timestamp(utime) as date), server_id ORDER BY fulldate DESC) s GROUP BY s.fulldate ORDER BY s.fulldate DESC LIMIT ?", new Object[]{server_id==null||server_id.isEmpty()?"%":server_id, limit}, (rs, rowNum) -> new MaxPeakOfDay(rs)); } public List getAvgPeakOfDays(String server_id, int limit) { - return jdbcTemplate_ro.query("SELECT SUM(s.player_count) as player_count, s.fulldate as fl FROM (SELECT avg(`player_count`) as player_count, cast(FROM_UNIXTIME(`utime`) as date) as fulldate,`server_id` FROM `servers_online` WHERE `player_count` != 0 and `server_id` like ? GROUP BY cast(FROM_UNIXTIME(`utime`) as date), server_id ORDER BY `fulldate` DESC) s GROUP BY s.fulldate ORDER BY `s`.`fulldate` DESC LIMIT ?", + return jdbcTemplate_ro.query("SELECT SUM(s.player_count) as player_count, s.fulldate as fl FROM (SELECT avg(player_count) as player_count, cast(to_timestamp(utime) as date) as fulldate,server_id FROM servers_online WHERE player_count != 0 and server_id like ? GROUP BY cast(to_timestamp(utime) as date), server_id ORDER BY fulldate DESC) s GROUP BY s.fulldate ORDER BY s.fulldate DESC LIMIT ?", new Object[]{server_id==null||server_id.isEmpty()?"%":server_id, limit}, (rs, rowNum) -> new AvgPeakOfDay(rs)); } public List getMaxPeakOfFiveMinutes(String server_id, int limit, int minutes) { long utime = LocalDate.now().minusDays(limit -1).toEpochSecond(LocalTime.MIN, ZoneOffset.ofHours(3)); - return jdbcTemplate_ro.query("select sum(sub.player_count) as player_count, sub.fulldate from (SELECT max(`player_count`) as player_count, FROM_UNIXTIME(`utime`) as fulldate, `utime` as ts, server_id FROM `servers_online` WHERE utime >= ? and `server_id` like ? GROUP BY FROM_UNIXTIME(`utime`) div ?, server_id ORDER BY `ts` DESC) sub GROUP BY sub.fulldate", + return jdbcTemplate_ro.query("select sum(sub.player_count) as player_count, sub.fulldate from (SELECT max(player_count) as player_count, cast(to_timestamp(utime) as date) as fulldate, utime as ts, server_id FROM servers_online WHERE utime >= ? and server_id like ? GROUP BY cast(to_timestamp(utime) as date) / ?, server_id ORDER BY ts DESC) sub GROUP BY sub.fulldate", new Object[]{utime, server_id==null||server_id.isEmpty()?"%":server_id, minutes * 100}, (rs, rowNum) -> new StatsOfPeakOfPerFiveMinutes(rs)); } @@ -196,7 +196,7 @@ public class OnlineUpdater extends BaseUpdater { } } - //CREATE TABLE `tf2.facti13`.`servers_online` (`id` INT NOT NULL AUTO_INCREMENT , `utime` INT NOT NULL , `player_count` INT NOT NULL , `max_players` INT NOT NULL , `server_id` VARCHAR(32) NOT NULL , PRIMARY KEY (`id`)) ENGINE = InnoDB; + //CREATE TABLE tf2.facti13.servers_online (id INT NOT NULL AUTO_INCREMENT , utime INT NOT NULL , player_count INT NOT NULL , max_players INT NOT NULL , server_id VARCHAR(32) NOT NULL , PRIMARY KEY (id)) ENGINE = InnoDB; public class ServerOnline { private int id; private long utime; @@ -206,7 +206,7 @@ public class OnlineUpdater extends BaseUpdater { } //stats per 5 = '500' minutes - //SELECT avg(`player_count`) as player_count, FROM_UNIXTIME(`utime`) as fulldate, `utime` as ts FROM `servers_online` WHERE `server_id` like 'srv5' and FROM_UNIXTIME(`utime`) > CAST(DATE_FORMAT(NOW() ,'%Y-%m-26') as DATE) GROUP BY FROM_UNIXTIME(`utime`) div 500 ORDER BY `ts` ASC; + //SELECT avg(player_count) as player_count, FROM_UNIXTIME(utime) as fulldate, utime as ts FROM servers_online WHERE server_id like 'srv5' and FROM_UNIXTIME(utime) > CAST(DATE_FORMAT(NOW() ,'%Y-%m-26') as DATE) GROUP BY FROM_UNIXTIME(utime) div 500 ORDER BY ts ASC; public class PerFiveMinutesStats extends PerPeriodStats { private static final int period = 500; PerFiveMinutesStats(ResultSet rs) throws SQLException { @@ -215,7 +215,7 @@ public class OnlineUpdater extends BaseUpdater { } - //SELECT avg(`player_count`) as player_count, FROM_UNIXTIME(`utime`) as fulldate, `utime` as ts FROM `servers_online` WHERE `player_count` != 0 and `server_id` like 'srv5' GROUP BY FROM_UNIXTIME(`utime`) div 800000 ORDER BY `ts` ASC; + //SELECT avg(player_count) as player_count, FROM_UNIXTIME(utime) as fulldate, utime as ts FROM servers_online WHERE player_count != 0 and server_id like 'srv5' GROUP BY FROM_UNIXTIME(utime) div 800000 ORDER BY ts ASC; public class PerDayStats extends PerPeriodStats { private static final int period = 800000; PerDayStats(ResultSet rs) throws SQLException { diff --git a/src/main/java/app/updates/UniqueUpdater.java b/src/main/java/app/updates/UniqueUpdater.java index 03a4dac..b2edbbf 100644 --- a/src/main/java/app/updates/UniqueUpdater.java +++ b/src/main/java/app/updates/UniqueUpdater.java @@ -24,10 +24,10 @@ public class UniqueUpdater extends BaseUpdater{ @Qualifier("jt_ro") private JdbcTemplate jdbcTemplate; - private final String query_total = "SELECT DISTINCT `account_id` FROM `%s`.`user_connections` WHERE `connection_type` LIKE \"disconnect\" AND `connect_duration` > 300 UNION\n"; - private final String query_day = "SELECT DISTINCT `account_id` FROM `%s`.`user_connections` WHERE `connection_type` LIKE \"disconnect\" AND `connect_duration` > 300 AND `timestamp` > CAST(DATE_FORMAT(NOW() ,'%%Y-%%m-%%d') as DATE) UNION\n"; - private final String query_month = "SELECT DISTINCT `account_id` FROM `%s`.`user_connections` WHERE `connection_type` LIKE \"disconnect\" AND `connect_duration` > 300 AND `timestamp` > CAST(DATE_FORMAT(NOW() ,'%%Y-%%m-01') as DATE) UNION\n"; - private final String query_year = "SELECT DISTINCT `account_id` FROM `%s`.`user_connections` WHERE `connection_type` LIKE \"disconnect\" AND `connect_duration` > 300 AND `timestamp` > CAST(DATE_FORMAT(NOW() ,'%%Y-01-01') as DATE) UNION\n"; + private final String query_total = "SELECT DISTINCT account_id FROM %s.user_connections WHERE connection_type LIKE \"disconnect\" AND connect_duration > 300 UNION\n"; + private final String query_day = "SELECT DISTINCT account_id FROM %s.user_connections WHERE connection_type LIKE \"disconnect\" AND connect_duration > 300 AND timestamp > CAST(DATE_FORMAT(NOW() ,'%%Y-%%m-%%d') as DATE) UNION\n"; + private final String query_month = "SELECT DISTINCT account_id FROM %s.user_connections WHERE connection_type LIKE \"disconnect\" AND connect_duration > 300 AND timestamp > CAST(DATE_FORMAT(NOW() ,'%%Y-%%m-01') as DATE) UNION\n"; + private final String query_year = "SELECT DISTINCT account_id FROM %s.user_connections WHERE connection_type LIKE \"disconnect\" AND connect_duration > 300 AND timestamp > CAST(DATE_FORMAT(NOW() ,'%%Y-01-01') as DATE) UNION\n"; private final Logger logger = LoggerFactory.getLogger(UniqueUpdater.class); diff --git a/src/main/java/app/updates/VipCountUpdater.java b/src/main/java/app/updates/VipCountUpdater.java index 4ffde07..9c0fb46 100644 --- a/src/main/java/app/updates/VipCountUpdater.java +++ b/src/main/java/app/updates/VipCountUpdater.java @@ -55,7 +55,7 @@ public class VipCountUpdater extends BaseUpdater{ public boolean UpdateVIPCount() { logger.info("Update current active VIP profiles"); - Long count = jdbcTemplate.query("SELECT COUNT(*) as count FROM `sm_admins` WHERE `status` LIKE 'VIP' AND (`comment` LIKE 'Donate.User' OR `comment` LIKE 'f13bot.User')", + Long count = jdbcTemplate.query("SELECT COUNT(*) as count FROM sm_admins WHERE status LIKE 'VIP' AND (comment LIKE 'Donate.User' OR comment LIKE 'f13bot.User')", (rs, n) -> rs.getLong("count")).stream().findFirst().orElse(0L); stats.setVip_players(count); stats.getUpdates().merge("vip_count", Instant.now().getEpochSecond(), (x, y) -> y); @@ -64,7 +64,7 @@ public class VipCountUpdater extends BaseUpdater{ public boolean UpdateFreeVIPCount() { logger.info("Update current active FreeVIP profiles"); - Long count = jdbcTemplate.query("SELECT COUNT(*) as count FROM `sm_admins` WHERE `status` LIKE 'VIP' AND `comment` LIKE 'f13bot.FreeVIP'", + Long count = jdbcTemplate.query("SELECT COUNT(*) as count FROM sm_admins WHERE status LIKE 'VIP' AND comment LIKE 'f13bot.FreeVIP'", (rs, n) -> rs.getLong("count")).stream().findFirst().orElse(0L); stats.setFreevip_players(count); stats.getUpdates().merge("freevip_count", Instant.now().getEpochSecond(), (x, y) -> y); diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 85da286..c3bc2a8 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -7,7 +7,7 @@ spring: application: name: facti13_web_backend datasource: - driver-class-name: com.mysql.jdbc.Driver + driver-class-name: org.postgresql.Driver url: ${DB_URL} username: ${DB_USERNAME} password: ${DB_PASSWORD}