From 5f6563d35651e21f41b83ff83035c4c59eff0b7a Mon Sep 17 00:00:00 2001 From: alterwain Date: Thu, 6 Mar 2025 20:16:06 +0300 Subject: [PATCH] Changed home id logic --- .../alterdekim/game/component/GameServer.java | 20 ++++++++----------- .../game/component/game/Player.java | 3 +-- .../game/service/LocationService.java | 3 ++- .../com/alterdekim/game/utils/GameUtils.java | 14 +++++++++++++ 4 files changed, 25 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/alterdekim/game/component/GameServer.java b/src/main/java/com/alterdekim/game/component/GameServer.java index 54776a5..0ee58ef 100644 --- a/src/main/java/com/alterdekim/game/component/GameServer.java +++ b/src/main/java/com/alterdekim/game/component/GameServer.java @@ -153,7 +153,6 @@ public class GameServer { long requestedLocation = message.getArguments().get(1).getLong(); if( requestedLocation == -1L ) { int homePlayerId = message.getArguments().get(0).getInt(); - this.players.get(playerId).setHomeId(homePlayerId); this.sendResult(playerId, message.getTransactionId(), xmlMapper.writeValueAsString(locationService.getHomeByUserId(homePlayerId))); return; } @@ -388,7 +387,7 @@ public class GameServer { private void setPlayerLocation(int playerId, SetLocationMessage message) { Player p = this.players.get(playerId); this.deleteSelf(playerId, p.getLocationId()); - int prevLocation = p.getLocationId(); + long prevLocation = p.getLocationId(); p.setLocationId(GameUtils.extractLocationId(message.getLocation())); p.setX(message.getCoordinates().get("x").getDouble()); p.setY(message.getCoordinates().get("y").getDouble()); @@ -397,15 +396,14 @@ public class GameServer { this.sendResult(playerId, message.getTransactionId(), ""); } - private void deleteSelf(int playerId, int locationId) { + private void deleteSelf(int playerId, long locationId) { Player p = this.players.get(playerId); - this.sendInLocation(locationId, p.getHomeId(), CommandType.RemoveUserFromLocation, playerId); + this.sendInLocation(locationId, CommandType.RemoveUserFromLocation, playerId); } - private void updateLocationPlayers(int playerId, int prevLocation) { - int locationId = this.players.get(playerId).getLocationId(); - long homeId = this.players.get(playerId).getHomeId(); - this.sendInLocation(prevLocation, homeId, CommandType.RemoveUserFromLocation, playerId); + private void updateLocationPlayers(int playerId, long prevLocation) { + long locationId = this.players.get(playerId).getLocationId(); + this.sendInLocation(prevLocation, CommandType.RemoveUserFromLocation, playerId); for( int i = 0; i < 3; i++ ) { try { Thread.sleep(300); @@ -420,7 +418,6 @@ public class GameServer { this.players.keySet().forEach(pid -> { Player p = this.players.get(pid); if( p.getLocationId() != locationId ) return; - if( p.getLocationId() == -1 && p.getHomeId() != homeId ) return; this.call(playerId, CommandType.AddUserToLocation, new AddUserToLocation( pid, users.getAvatarById(pid, this.players.get(pid)) @@ -434,13 +431,12 @@ public class GameServer { private void sendInPlayersLocation(int playerId, CommandType type, Object obj) { Player player = this.players.get(playerId); - this.sendInLocation(player.getLocationId(), player.getHomeId(), type, obj); + this.sendInLocation(player.getLocationId(), type, obj); } - private void sendInLocation(int locationId, long playersHome, CommandType type, Object obj) { + private void sendInLocation(long locationId, CommandType type, Object obj) { players.keySet().forEach(k -> { if( locationId != players.get(k).getLocationId() ) return; - if( locationId == -1 && players.get(k).getHomeId() != playersHome) return; this.call(k, type, obj); }); } diff --git a/src/main/java/com/alterdekim/game/component/game/Player.java b/src/main/java/com/alterdekim/game/component/game/Player.java index 505c885..3bc9663 100644 --- a/src/main/java/com/alterdekim/game/component/game/Player.java +++ b/src/main/java/com/alterdekim/game/component/game/Player.java @@ -13,7 +13,6 @@ public class Player { private final String username; private double x; private double y; - private int locationId; + private long locationId; private double state; - private long homeId; } diff --git a/src/main/java/com/alterdekim/game/service/LocationService.java b/src/main/java/com/alterdekim/game/service/LocationService.java index 90b3779..48bde6d 100644 --- a/src/main/java/com/alterdekim/game/service/LocationService.java +++ b/src/main/java/com/alterdekim/game/service/LocationService.java @@ -10,6 +10,7 @@ import com.alterdekim.game.repository.GoodRepository; import com.alterdekim.game.repository.HouseLocationRepository; import com.alterdekim.game.repository.LocationObjectInstanceRepository; import com.alterdekim.game.repository.LocationRepository; +import com.alterdekim.game.utils.GameUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -80,7 +81,7 @@ public class LocationService { public LocationObject getHomeByUserId(Integer userId) { Long goodId = goodRepository.findById(avatarInventoryService.getUsedHouse(userId)).map(Good::getId).orElse(102L); var house = this.houseLocationRepository.findDefaultHouseLocationByGoodId(goodId).get(); - return new LocationObject(-1L, true, false, userService.getBooleanUserProperty(userId, PlayerProperties.IsHomeLocked, true), true, house.getMediaResourceId().longValue(), 300.0, 300.0, "", userId, userService.getUsernameById(userId).get(), Collections.emptyList()); + return new LocationObject(GameUtils.convertToSpecialLocation(userId, true), true, false, userService.getBooleanUserProperty(userId, PlayerProperties.IsHomeLocked, true), true, house.getMediaResourceId().longValue(), 300.0, 300.0, "", userId, userService.getUsernameById(userId).get(), Collections.emptyList()); } public void addHouseLocation(HouseLocation house) { diff --git a/src/main/java/com/alterdekim/game/utils/GameUtils.java b/src/main/java/com/alterdekim/game/utils/GameUtils.java index 5f0aa79..1374d33 100644 --- a/src/main/java/com/alterdekim/game/utils/GameUtils.java +++ b/src/main/java/com/alterdekim/game/utils/GameUtils.java @@ -1,5 +1,7 @@ package com.alterdekim.game.utils; +import org.javatuples.Pair; + public class GameUtils { public static final Long EmptyGoodId = 360L; @@ -9,4 +11,16 @@ public class GameUtils { if( r.length < 3) return 0; return Integer.parseInt(r[2]); } + + // returns true if it's user's club + public static Pair getPlayerIdFromNegative(long l) { + if (l < -4294967295L) { + return Pair.with((int) (Math.abs(l) >> 32), true); + } + return Pair.with((int) Math.abs(l), false); + } + + public static long convertToSpecialLocation(int playerId, boolean isHome) { + return -(isHome ? (long) playerId : ((long) playerId) << 32); + } }