SQL formatting

This commit is contained in:
Michael Wain 2025-01-31 19:39:18 +03:00
parent dfaffa20f6
commit 265bc1d608
6 changed files with 148 additions and 34 deletions

View File

@ -153,8 +153,8 @@
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<configuration> <configuration>
<source>14</source> <source>15</source>
<target>14</target> <target>15</target>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>

View File

@ -104,6 +104,7 @@ public class GameServer {
switch (UserCommandType.fromString(message.getCommandType())) { switch (UserCommandType.fromString(message.getCommandType())) {
case GetUserInitData: case GetUserInitData:
String r = xmlMapper.writeValueAsString(users.getUserInitInfoByUserId(playerId)); String r = xmlMapper.writeValueAsString(users.getUserInitInfoByUserId(playerId));
log.info("GetUserInitData: {}", r);
this.sendResult(playerId, message.getTransactionId(), r); this.sendResult(playerId, message.getTransactionId(), r);
break; break;
case UserFriendsGet: // todo: implement case UserFriendsGet: // todo: implement

View File

@ -6,14 +6,14 @@ import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
@Getter
@AllArgsConstructor public interface InitMagicAbility {
public class InitMagicAbility {
@JacksonXmlProperty(localName = "Id", isAttribute = true) @JacksonXmlProperty(localName = "Id", isAttribute = true)
private Integer id; Integer getId();
@JacksonXmlProperty(localName = "ExpirationDate", isAttribute = true) @JacksonXmlProperty(localName = "ExpirationDate", isAttribute = true)
private String expirationDate; String getExpirationDate();
@JacksonXmlProperty(localName = "IsLimited", isAttribute = true) @JacksonXmlProperty(localName = "IsLimited", isAttribute = true)
@JsonSerialize(using= NumericBooleanSerializer.class) Integer getIsLimited();
private Boolean isLimited;
} }

View File

@ -3,8 +3,10 @@ package com.alterdekim.game.repository;
import com.alterdekim.game.component.game.AvatarInventoryType; import com.alterdekim.game.component.game.AvatarInventoryType;
import com.alterdekim.game.component.game.avatar.IBodyAvatarItem; import com.alterdekim.game.component.game.avatar.IBodyAvatarItem;
import com.alterdekim.game.component.game.avatar.GoodClothType; import com.alterdekim.game.component.game.avatar.GoodClothType;
import com.alterdekim.game.component.game.avatar.InitMagicAbility;
import com.alterdekim.game.component.game.inventory.InventoryItem; import com.alterdekim.game.component.game.inventory.InventoryItem;
import com.alterdekim.game.entity.AvatarInventory; import com.alterdekim.game.entity.AvatarInventory;
import com.alterdekim.game.entity.Smile;
import jakarta.transaction.Transactional; import jakarta.transaction.Transactional;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Modifying;
@ -33,18 +35,107 @@ public interface AvatarInventoryRepository extends JpaRepository<AvatarInventory
@Query(value = "UPDATE AvatarInventory a SET a.isUsed = true WHERE a.user.id = :userId AND a.type = :type AND a.goodId = :goodId") @Query(value = "UPDATE AvatarInventory a SET a.isUsed = true WHERE a.user.id = :userId AND a.type = :type AND a.goodId = :goodId")
void setUsed(@Param("userId") Long userId, @Param("goodId") Long goodId, @Param("type") AvatarInventoryType type); void setUsed(@Param("userId") Long userId, @Param("goodId") Long goodId, @Param("type") AvatarInventoryType type);
@Query(value = "SELECT a.good_id AS id, g.trid AS textResourceId, 0 AS bodyPartId, g.mrid AS mediaResourceId, (case when (a.is_used > 0) then true else false end) AS isUsed, false AS isColorable, 0 AS color, false AS isLimited, g.publish_date AS date, 0 AS pollution, false AS isBodyPart, 0 AS phoneId FROM avatar_inventory AS a JOIN goods AS g ON a.good_id = g.id WHERE a.user_id = :uid AND a.type = 'Clothes' AND g.good_type_id = :#{#goodTypeId.getVal()}", nativeQuery = true) @Query(value = """
SELECT
a.good_id AS id,
g.trid AS textResourceId,
0 AS bodyPartId,
g.mrid AS mediaResourceId,
(case when (a.is_used > 0) then true else false end) AS isUsed,
false AS isColorable,
0 AS color,
false AS isLimited,
g.publish_date AS date,
0 AS pollution,
false AS isBodyPart,
0 AS phoneId
FROM avatar_inventory AS a
JOIN goods AS g
ON a.good_id = g.id
WHERE a.user_id = :uid AND a.type = 'Clothes' AND g.good_type_id = :#{#goodTypeId.getVal()}""", nativeQuery = true)
List<InventoryItem> findInventoryItems(@Param("uid") Long userId, @Param("goodTypeId") GoodClothType goodClothType); List<InventoryItem> findInventoryItems(@Param("uid") Long userId, @Param("goodTypeId") GoodClothType goodClothType);
@Query(value = "SELECT b.id AS id, 0 AS textResourceId, 0 AS bodyPartId, 0 AS mediaResourceId, (case when (a.is_used > 0) then true else false end) AS isUsed, false AS isColorable, 0 AS color, false AS isLimited, '' AS date, 0 AS pollution, false AS isBodyPart, 0 AS phoneId FROM backgrounds b JOIN (SELECT a.good_id, a.is_used FROM avatar_inventory a WHERE a.`type` = 'Backgrounds' AND a.user_id = :uid) AS a ON a.good_id = b.id", nativeQuery = true) @Query(value = """
SELECT
b.id AS id,
0 AS textResourceId,
0 AS bodyPartId,
0 AS mediaResourceId,
(case when (a.is_used > 0) then true else false end) AS isUsed,
false AS isColorable,
0 AS color,
false AS isLimited,
'' AS date,
0 AS pollution,
false AS isBodyPart,
0 AS phoneId
FROM backgrounds b
JOIN (SELECT a.good_id, a.is_used FROM avatar_inventory a WHERE a.`type` = 'Backgrounds' AND a.user_id = :uid) AS a
ON a.good_id = b.id""", nativeQuery = true)
List<InventoryItem> findBackgrounds(@Param("uid") Long userId); List<InventoryItem> findBackgrounds(@Param("uid") Long userId);
@Query(value = "SELECT p.id AS id, 0 AS textResourceId, 0 AS bodyPartId, 0 AS mediaResourceId, (case when (a.is_used > 0) then true else false end) AS isUsed, false AS isColorable, 0 AS color, false AS isLimited, '' AS date, 0 AS pollution, false AS isBodyPart, p.id AS phoneId FROM phone_icons p JOIN (SELECT a.good_id, a.is_used FROM avatar_inventory a WHERE a.`type` = 'Phones' AND a.user_id = :uid) AS a ON a.good_id = p.id", nativeQuery = true) @Query(value = """
SELECT
p.id AS id,
0 AS textResourceId,
0 AS bodyPartId,
0 AS mediaResourceId,
(case when (a.is_used > 0) then true else false end) AS isUsed,
false AS isColorable,
0 AS color,
false AS isLimited,
'' AS date,
0 AS pollution,
false AS isBodyPart,
p.id AS phoneId
FROM phone_icons p
JOIN (SELECT a.good_id, a.is_used FROM avatar_inventory a WHERE a.`type` = 'Phones' AND a.user_id = :uid) AS a
ON a.good_id = p.id""", nativeQuery = true)
List<InventoryItem> findPhones(@Param("uid") Long userId); List<InventoryItem> findPhones(@Param("uid") Long userId);
@Query(value = "SELECT b.`type` AS bodyPartTypeId, b.id AS id, a.color AS color, 1 AS isBodyPart, b.id AS bodyPartId, 0 AS goodId, b.media_resource_id AS mediaResourceId, b.`type` AS layerId, 0 AS goodTypeId FROM bodyparts b JOIN (SELECT a.color, a.good_id FROM avatar_inventory a WHERE a.user_id = :uid AND a.`type` = 'BodyParts' AND a.is_used = true) AS a ON a.good_id = b.id", nativeQuery = true) @Query(value = """
SELECT
b.`type` AS bodyPartTypeId,
b.id AS id,
a.color AS color,
1 AS isBodyPart,
b.id AS bodyPartId,
0 AS goodId,
b.media_resource_id AS mediaResourceId,
b.`type` AS layerId,
0 AS goodTypeId
FROM bodyparts b
JOIN (SELECT a.color, a.good_id FROM avatar_inventory a WHERE a.user_id = :uid AND a.`type` = 'BodyParts' AND a.is_used = true) AS a
ON a.good_id = b.id""", nativeQuery = true)
List<IBodyAvatarItem> findUsedBodyPartsByUserId(@Param("uid") Long userId); List<IBodyAvatarItem> findUsedBodyPartsByUserId(@Param("uid") Long userId);
@Query(value = "SELECT 0 AS bodyPartTypeId, g.id AS id, 0 AS color, 0 AS isBodyPart, 0 AS bodyPartId, g.id AS goodId, g.mrid AS mediaResourceId, g.layer_id AS layerId, g.good_type_id AS goodTypeId FROM goods g JOIN (SELECT a.good_id FROM avatar_inventory a WHERE a.user_id = :uid AND a.`type` = 'Clothes' AND a.is_used = true) AS a ON a.good_id = g.id", nativeQuery = true) @Query(value = """
SELECT
0 AS bodyPartTypeId,
g.id AS id,
0 AS color,
0 AS isBodyPart,
0 AS bodyPartId,
g.id AS goodId,
g.mrid AS mediaResourceId,
g.layer_id AS layerId,
g.good_type_id AS goodTypeId
FROM goods g
JOIN (SELECT a.good_id FROM avatar_inventory a WHERE a.user_id = :uid AND a.`type` = 'Clothes' AND a.is_used = true) AS a
ON a.good_id = g.id""", nativeQuery = true)
List<IBodyAvatarItem> findUsedClothesByUserId(@Param("uid") Long userId); List<IBodyAvatarItem> findUsedClothesByUserId(@Param("uid") Long userId);
@Query(value = """
SELECT
g.magic_ability_id AS id,
'' AS expirationDate,
0 AS isLimited
FROM gms g
JOIN (SELECT a.good_id FROM avatar_inventory a WHERE a.`type` = 'Magic' AND a.user_id = :uid) a
ON g.id = a.good_id""", nativeQuery = true)
List<InitMagicAbility> findMagicAbilitiesByUserId(@Param("uid") Long userId);
@Query(value = "SELECT s FROM Smile s WHERE s.id IN (SELECT a.goodId FROM AvatarInventory a WHERE a.user.id = :uid AND a.type = 'Smile')")
List<Smile> findSmilesByUserId(@Param("uid") Long userId);
} }

View File

@ -18,6 +18,44 @@ public interface BodyPartRepository extends JpaRepository<BodyPart, Long> {
@Query(value = "SELECT b FROM BodyPart b WHERE b.type = :type") @Query(value = "SELECT b FROM BodyPart b WHERE b.type = :type")
List<BodyPart> findByBodyPartType(@Param("type") BodyPartType type); List<BodyPart> findByBodyPartType(@Param("type") BodyPartType type);
@Query(value = "SELECT a.id AS id, 0 AS textResourceId, a.id AS bodyPartId, a.media_resource_id AS mediaResourceId, (case when (MAX(a.is_used) > 0) then 1 else 0 end) AS isUsed, (case when (a.is_colorable > 0) then 1 else 0 end) AS isColorable, MAX(a.color) AS color, 0 AS isLimited, '' AS date, 0 AS pollution, 1 AS isBodyPart, 0 AS phoneId FROM (SELECT b.id, b.is_colorable, b.media_resource_id, b.`type`, 0 AS is_used, 0 AS color FROM bodyparts AS b UNION (SELECT b.id, b.is_colorable, b.media_resource_id, b.`type`, a.is_used, a.color FROM bodyparts AS b JOIN avatar_inventory AS a ON b.id = a.good_id WHERE a.`type` = 'BodyParts' AND a.user_id = :uid)) AS a WHERE a.`type` = :#{#type.name()} GROUP BY a.id, a.`type`, a.media_resource_id, a.is_colorable", nativeQuery = true) @Query(value = """
SELECT
a.id AS id,
0 AS textResourceId,
a.id AS bodyPartId,
a.media_resource_id AS mediaResourceId,
(case when (MAX(a.is_used) > 0) then 1 else 0 end) AS isUsed,
(case when (a.is_colorable > 0) then 1 else 0 end) AS isColorable,
MAX(a.color) AS color,
0 AS isLimited,
'' AS date,
0 AS pollution,
1 AS isBodyPart,
0 AS phoneId
FROM (
SELECT
b.id,
b.is_colorable,
b.media_resource_id,
b.`type`,
0 AS is_used,
0 AS color
FROM bodyparts AS b
UNION (
SELECT
b.id,
b.is_colorable,
b.media_resource_id,
b.`type`,
a.is_used,
a.color
FROM bodyparts AS b
JOIN avatar_inventory AS a
ON b.id = a.good_id
WHERE a.`type` = 'BodyParts' AND a.user_id = :uid
)
) AS a
WHERE a.`type` = :#{#type.name()}
GROUP BY a.id, a.`type`, a.media_resource_id, a.is_colorable""", nativeQuery = true)
List<InventoryItem> findInventoryItems(@Param("uid") Long userId, @Param("type") BodyPartType type); List<InventoryItem> findInventoryItems(@Param("uid") Long userId, @Param("type") BodyPartType type);
} }

View File

@ -87,24 +87,11 @@ public class AvatarInventoryService {
} }
public List<InitMagicAbility> getMagicAbilitiesItemsByUserId(Long userId) { public List<InitMagicAbility> getMagicAbilitiesItemsByUserId(Long userId) {
return this.inventoryRepository.findItemsByUserIdAndType(userId, AvatarInventoryType.Magic) return this.inventoryRepository.findMagicAbilitiesByUserId(userId); // todo: implement expiration ability
.stream()
.map(AvatarInventory::getGoodId)
.map(id -> goodMagicRepository.findById(id))
.filter(Optional::isPresent)
.map(Optional::get)
.map(g -> new InitMagicAbility(g.getMagicAbilityId(), "", false)) // todo: implement expiration ability
.collect(Collectors.toList());
} }
public List<Smile> getSmilesByUserId(Long userId) { public List<Smile> getSmilesByUserId(Long userId) {
return this.inventoryRepository.findItemsByUserIdAndType(userId, AvatarInventoryType.Smile) return this.inventoryRepository.findSmilesByUserId(userId);
.stream()
.map(AvatarInventory::getGoodId)
.map(id -> smileService.findById(id))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(Collectors.toList());
} }
public List<InventoryItem> getBackgroundsByUserId(Long userId) { public List<InventoryItem> getBackgroundsByUserId(Long userId) {
@ -158,10 +145,7 @@ public class AvatarInventoryService {
public String getAchievementsByUserId(Long userId) { public String getAchievementsByUserId(Long userId) {
return this.inventoryRepository.findItemsByUserIdAndType(userId, AvatarInventoryType.Achievments) return this.inventoryRepository.findItemsByUserIdAndType(userId, AvatarInventoryType.Achievments)
.stream() .stream()
.map(a -> achievementService.findById(a.getGoodId())) .map(a -> a.getGoodId().toString())
.filter(Optional::isPresent)
.map(Optional::get)
.map(a -> a.getId().toString())
.collect(Collectors.joining(",")); .collect(Collectors.joining(","));
} }