Fix inconformity of Player Object at TowerManager (#1226)

* Fix inconformity of Player Object at TowerManager

Fix inconformity of Player Object at TowerManager class from GameSession

* fix with a reasonable resolution

* Make sure towerManager'player is online player

* Make sure towerManager'player is online player

* Obsolete storage

* Fix build

* fix build

* fix build

* add @Entity

* set record map

* replace tab with space

* add Entity to TowerData

* make infomater more accurate

* move player register to onLogin
This commit is contained in:
zhaodice 2022-06-10 04:38:53 +08:00 committed by GitHub
parent 992742d746
commit f67649e56f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 58 deletions

View File

@ -47,8 +47,7 @@ public class DungeonManager {
if (data == null) { if (data == null) {
return false; return false;
} }
Grasscutter.getLogger().info("{}({}) is trying to enter dungeon {}" ,player.getNickname(),player.getUid(),dungeonId);
Grasscutter.getLogger().info(player.getNickname() + " is trying to enter dungeon " + dungeonId);
int sceneId = data.getSceneId(); int sceneId = data.getSceneId();
player.getScene().setPrevScene(sceneId); player.getScene().setPrevScene(sceneId);
@ -72,7 +71,7 @@ public class DungeonManager {
if (data == null) { if (data == null) {
return false; return false;
} }
Grasscutter.getLogger().info(player.getNickname() + " is trying to enter tower dungeon " + dungeonId); Grasscutter.getLogger().info("{}({}) is trying to enter tower dungeon {}" ,player.getNickname(),player.getUid(),dungeonId);
if(player.getWorld().transferPlayerToScene(player, data.getSceneId(), data)){ if(player.getWorld().transferPlayerToScene(player, data.getSceneId(), data)){
dungeonSettleListeners.forEach(player.getScene()::addDungeonSettleObserver); dungeonSettleListeners.forEach(player.getScene()::addDungeonSettleObserver);

View File

@ -39,6 +39,7 @@ import emu.grasscutter.game.props.SceneType;
import emu.grasscutter.game.quest.QuestManager; import emu.grasscutter.game.quest.QuestManager;
import emu.grasscutter.game.shop.ShopLimit; import emu.grasscutter.game.shop.ShopLimit;
import emu.grasscutter.game.managers.MapMarkManager.*; import emu.grasscutter.game.managers.MapMarkManager.*;
import emu.grasscutter.game.tower.TowerData;
import emu.grasscutter.game.tower.TowerManager; import emu.grasscutter.game.tower.TowerManager;
import emu.grasscutter.game.world.Scene; import emu.grasscutter.game.world.Scene;
import emu.grasscutter.game.world.World; import emu.grasscutter.game.world.World;
@ -118,7 +119,8 @@ public class Player {
private TeamManager teamManager; private TeamManager teamManager;
private TowerManager towerManager; @Transient private TowerManager towerManager;
private TowerData towerData;
private PlayerGachaInfo gachaInfo; private PlayerGachaInfo gachaInfo;
private PlayerProfile playerProfile; private PlayerProfile playerProfile;
private boolean showAvatar; private boolean showAvatar;
@ -475,11 +477,15 @@ public class Player {
public TeamManager getTeamManager() { public TeamManager getTeamManager() {
return this.teamManager; return this.teamManager;
} }
public TowerManager getTowerManager() { public TowerManager getTowerManager() {
return towerManager; return towerManager;
} }
public TowerData getTowerData() {
return towerData;
}
public QuestManager getQuestManager() { public QuestManager getQuestManager() {
return questManager; return questManager;
} }
@ -1220,6 +1226,9 @@ public class Player {
// Called from tokenrsp // Called from tokenrsp
public void loadFromDatabase() { public void loadFromDatabase() {
// Make sure these exist // Make sure these exist
if (this.getTowerManager() == null) {
this.towerManager = new TowerManager(this);
}
if (this.getTeamManager() == null) { if (this.getTeamManager() == null) {
this.teamManager = new TeamManager(this); this.teamManager = new TeamManager(this);
} }
@ -1229,7 +1238,8 @@ public class Player {
if (this.getProfile().getUid() == 0) { if (this.getProfile().getUid() == 0) {
this.getProfile().syncWithCharacter(this); this.getProfile().syncWithCharacter(this);
} }
//Make sure towerManager's player is online player
this.getTowerManager().setPlayer(this);
// Load from db // Load from db
this.getAvatars().loadFromDatabase(); this.getAvatars().loadFromDatabase();
this.getInventory().loadFromDatabase(); this.getInventory().loadFromDatabase();
@ -1239,12 +1249,6 @@ public class Player {
this.getMailHandler().loadFromDatabase(); this.getMailHandler().loadFromDatabase();
this.getQuestManager().loadFromDatabase(); this.getQuestManager().loadFromDatabase();
// Add to gameserver (Always handle last)
if (getSession().isActive()) {
getServer().registerPlayer(this);
getProfile().setPlayer(this); // Set online
}
} }
public void onLogin() { public void onLogin() {
@ -1255,7 +1259,6 @@ public class Player {
if (quest != null) { if (quest != null) {
quest.finish(); quest.finish();
} }
getQuestManager().addQuest(35101); getQuestManager().addQuest(35101);
this.setSceneId(3); this.setSceneId(3);
@ -1299,8 +1302,14 @@ public class Player {
// Call join event. // Call join event.
PlayerJoinEvent event = new PlayerJoinEvent(this); event.call(); PlayerJoinEvent event = new PlayerJoinEvent(this); event.call();
if(event.isCanceled()) // If event is not cancelled, continue. if(event.isCanceled()){ // If event is not cancelled, continue.
session.close(); session.close();
return;
}
// register
getServer().registerPlayer(this);
getProfile().setPlayer(this); // Set online
} }
public void onLogout() { public void onLogout() {

View File

@ -0,0 +1,25 @@
package emu.grasscutter.game.tower;
import java.util.Map;
import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Transient;
@Entity
public class TowerData{
/**
* the floor players chose
*/
int currentFloorId;
int currentLevel;
@Transient
int currentLevelId;
/**
* floorId - Record
*/
Map<Integer, TowerLevelRecord> recordMap;
@Transient
int entryScene;
}

View File

@ -1,7 +1,5 @@
package emu.grasscutter.game.tower; package emu.grasscutter.game.tower;
import dev.morphia.annotations.Entity;
import dev.morphia.annotations.Transient;
import emu.grasscutter.data.GameData; import emu.grasscutter.data.GameData;
import emu.grasscutter.data.excels.TowerLevelData; import emu.grasscutter.data.excels.TowerLevelData;
import emu.grasscutter.game.dungeons.DungeonSettleListener; import emu.grasscutter.game.dungeons.DungeonSettleListener;
@ -13,71 +11,55 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@Entity
public class TowerManager { public class TowerManager {
@Transient private Player player; private Player player;
private TowerData towerData;
public TowerManager(Player player) { public TowerManager(Player player) {
this.player = player; setPlayer(player);
} }
public void setPlayer(Player player) { public void setPlayer(Player player) {
this.player = player; this.player = player;
this.towerData = player.getTowerData();
} }
/**
* the floor players chose
*/
private int currentFloorId;
private int currentLevel;
@Transient
private int currentLevelId;
/**
* floorId - Record
*/
private Map<Integer, TowerLevelRecord> recordMap;
@Transient
private int entryScene;
public int getCurrentFloorId() { public int getCurrentFloorId() {
return currentFloorId; return towerData.currentFloorId;
} }
public int getCurrentLevelId(){ public int getCurrentLevelId(){
return this.currentLevelId + currentLevel; return towerData.currentLevelId + towerData.currentLevel;
} }
/** /**
* form 1-3 * form 1-3
*/ */
public int getCurrentLevel(){ public int getCurrentLevel(){
return currentLevel + 1; return towerData.currentLevel + 1;
} }
private static final List<DungeonSettleListener> towerDungeonSettleListener = List.of(new TowerDungeonSettleListener()); private static final List<DungeonSettleListener> towerDungeonSettleListener = List.of(new TowerDungeonSettleListener());
public Map<Integer, TowerLevelRecord> getRecordMap() { public Map<Integer, TowerLevelRecord> getRecordMap() {
if(recordMap == null){ Map<Integer, TowerLevelRecord> recordMap = towerData.recordMap;
if(recordMap == null || recordMap.size()==0){
recordMap = new HashMap<>(); recordMap = new HashMap<>();
recordMap.put(1001, new TowerLevelRecord(1001)); recordMap.put(1001, new TowerLevelRecord(1001));
towerData.recordMap = recordMap;
} }
return recordMap; return recordMap;
} }
public void teamSelect(int floor, List<List<Long>> towerTeams) { public void teamSelect(int floor, List<List<Long>> towerTeams) {
var floorData = GameData.getTowerFloorDataMap().get(floor); var floorData = GameData.getTowerFloorDataMap().get(floor);
towerData.currentFloorId = floorData.getFloorId();
this.currentFloorId = floorData.getFloorId(); towerData.currentLevel = 0;
this.currentLevel = 0; towerData.currentLevelId = GameData.getTowerLevelDataMap().values().stream()
this.currentLevelId = GameData.getTowerLevelDataMap().values().stream()
.filter(x -> x.getLevelGroupId() == floorData.getLevelGroupId() && x.getLevelIndex() == 1) .filter(x -> x.getLevelGroupId() == floorData.getLevelGroupId() && x.getLevelIndex() == 1)
.findFirst() .findFirst()
.map(TowerLevelData::getId) .map(TowerLevelData::getId)
.orElse(0); .orElse(0);
if (entryScene == 0){ if (towerData.entryScene == 0){
entryScene = player.getSceneId(); towerData.entryScene = player.getSceneId();
} }
player.getTeamManager().setupTemporaryTeam(towerTeams); player.getTeamManager().setupTemporaryTeam(towerTeams);
@ -96,20 +78,22 @@ public class TowerManager {
towerDungeonSettleListener); towerDungeonSettleListener);
// make sure user can exit dungeon correctly // make sure user can exit dungeon correctly
player.getScene().setPrevScene(entryScene); player.getScene().setPrevScene(towerData.entryScene);
player.getScene().setPrevScenePoint(enterPointId); player.getScene().setPrevScenePoint(enterPointId);
player.getSession().send(new PacketTowerEnterLevelRsp(currentFloorId, getCurrentLevel())); player.getSession().send(new PacketTowerEnterLevelRsp(towerData.currentFloorId, getCurrentLevel()));
// stop using skill // stop using skill
player.getSession().send(new PacketCanUseSkillNotify(false)); player.getSession().send(new PacketCanUseSkillNotify(false));
// notify the cond of stars // notify the cond of stars
player.getSession().send(new PacketTowerLevelStarCondNotify(currentFloorId, getCurrentLevel())); player.getSession().send(new PacketTowerLevelStarCondNotify(towerData.currentFloorId, getCurrentLevel()));
} }
public void notifyCurLevelRecordChange(){ public void notifyCurLevelRecordChange(){
player.getSession().send(new PacketTowerCurLevelRecordChangeNotify(currentFloorId, getCurrentLevel())); player.getSession().send(new PacketTowerCurLevelRecordChangeNotify(towerData.currentFloorId, getCurrentLevel()));
} }
public void notifyCurLevelRecordChangeWhenDone(int stars){ public void notifyCurLevelRecordChangeWhenDone(int stars){
Map<Integer, TowerLevelRecord> recordMap = getRecordMap();
int currentFloorId = towerData.currentFloorId;
if(!recordMap.containsKey(currentFloorId)){ if(!recordMap.containsKey(currentFloorId)){
recordMap.put(currentFloorId, recordMap.put(currentFloorId,
new TowerLevelRecord(currentFloorId).setLevelStars(getCurrentLevelId(),stars)); new TowerLevelRecord(currentFloorId).setLevelStars(getCurrentLevelId(),stars));
@ -118,7 +102,7 @@ public class TowerManager {
recordMap.get(currentFloorId).setLevelStars(getCurrentLevelId(),stars)); recordMap.get(currentFloorId).setLevelStars(getCurrentLevelId(),stars));
} }
this.currentLevel++; towerData.currentLevel++;
if(!hasNextLevel()){ if(!hasNextLevel()){
// set up the next floor // set up the next floor
@ -129,20 +113,21 @@ public class TowerManager {
} }
} }
public boolean hasNextLevel(){ public boolean hasNextLevel(){
return this.currentLevel < 3; return towerData.currentLevel < 3;
} }
public int getNextFloorId() { public int getNextFloorId() {
return player.getServer().getTowerScheduleManager().getNextFloorId(this.currentFloorId); return player.getServer().getTowerScheduleManager().getNextFloorId(towerData.currentFloorId);
} }
public boolean hasNextFloor(){ public boolean hasNextFloor(){
return player.getServer().getTowerScheduleManager().getNextFloorId(this.currentFloorId) > 0; return player.getServer().getTowerScheduleManager().getNextFloorId(towerData.currentFloorId) > 0;
} }
public void clearEntry() { public void clearEntry() {
this.entryScene = 0; towerData.entryScene = 0;
} }
public boolean canEnterScheduleFloor(){ public boolean canEnterScheduleFloor(){
Map<Integer, TowerLevelRecord> recordMap = getRecordMap();
if(!recordMap.containsKey(player.getServer().getTowerScheduleManager().getLastEntranceFloor())){ if(!recordMap.containsKey(player.getServer().getTowerScheduleManager().getLastEntranceFloor())){
return false; return false;
} }