Skip to content

Commit

Permalink
- Reldens - v4.0.0-beta.38.2
Browse files Browse the repository at this point in the history
- Merge pull request #266 from damian-pastorini/v4.0.0-beta.38.2
  • Loading branch information
damian-pastorini authored Nov 22, 2024
2 parents 00de50c + ef874aa commit 8d968e0
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 71 deletions.
6 changes: 5 additions & 1 deletion lib/game/client/game-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ class GameClient
this.gameRoomsByServer = {};
this.featuresByServerFlag = {};
this.featuresRoomsByServer = {};
this.lastErrorMessage = '';
}

async joinOrCreate(roomName, options)
{
this.lastErrorMessage = '';
try {
let client = this.roomClient(roomName);
if(!client){
Expand All @@ -48,9 +50,11 @@ class GameClient
await new Promise(resolve => setTimeout(resolve, 500));
return await this.joinOrCreate(roomName, options);
}
// any connection errors will be handled in the higher level class, see snippet "game.errors.joiningRoom"
this.lastErrorMessage = error.message;
// any connection errors will be handled in the higher level class
Logger.error('Joining room error: '+error.message);
}
return false;
}

async connectToGlobalGameRoom(roomUrl, client, options)
Expand Down
120 changes: 70 additions & 50 deletions lib/game/client/game-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,70 +66,88 @@ class GameManager
this.startHandler.clientStart();
}

startGame(formData, isNewUser)
async startGame(formData, isNewUser)
{
this.events.emitSync('reldens.startGameBefore', this);
let gameRoom = this.joinGame(formData, isNewUser);
let errorElement = this.gameDom.getElement('#'+formData.formId+' '+GameConst.SELECTORS.RESPONSE_ERROR);
errorElement.innerHTML = '';
let gameRoom = await this.joinGame(formData, isNewUser);
if(gameRoom){
this.handleLoginSuccess();
return true;
}
this.handleLoginError(formData);
return false;
}

handleLoginSuccess()
{
let body = this.gameDom.getElement(GameConst.SELECTORS.BODY);
gameRoom.then(() => {
body.classList.add(GameConst.CLASSES.GAME_STARTED);
body.classList.remove(GameConst.CLASSES.GAME_ERROR);
this.gameDom.getElement(GameConst.SELECTORS.FORMS_CONTAINER).remove();
this.gameDom.getElement(GameConst.SELECTORS.GAME_CONTAINER).classList.remove(GameConst.CLASSES.HIDDEN);
}).catch((error) => {
body.classList.remove(GameConst.CLASSES.GAME_STARTED);
body.classList.add(GameConst.CLASSES.GAME_ERROR);
// @NOTE: game room errors should be always because some wrong login or registration data. For these cases
// we will check the isNewUser variable to know where display the error.
this.submitedForm = false;
if(errorElement){
Logger.error(error);
errorElement.innerHTML = error.message || error;
}
this.events.emitSync('reldens.gameRoomError', this, error);
// @TODO - BETA - Move to firebase plugin with an event subscriber.
if(this.firebase && 'firebase-login' === formData.formId){
this.firebase.app.auth().signOut();
}
});
body.classList.add(GameConst.CLASSES.GAME_STARTED);
body.classList.remove(GameConst.CLASSES.GAME_ERROR);
this.gameDom.getElement(GameConst.SELECTORS.FORMS_CONTAINER).remove();
this.gameDom.getElement(GameConst.SELECTORS.GAME_CONTAINER).classList.remove(GameConst.CLASSES.HIDDEN);
this.events.emitSync('reldens.startGameAfter', this);
}

handleLoginError(formData)
{
let body = this.gameDom.getElement(GameConst.SELECTORS.BODY);
body.classList.remove(GameConst.CLASSES.GAME_STARTED);
body.classList.add(GameConst.CLASSES.GAME_ERROR);
// @NOTE: game room errors should be always because some wrong login or registration data. For these cases
// we will check the isNewUser variable to know where display the error.
this.submitedForm = false;
this.events.emitSync('reldens.gameRoomError', this);
// @TODO - BETA - Move to firebase plugin with an event subscriber.
if(this.firebase && 'firebase-login' === formData.formId){
this.firebase.app.auth().signOut();
}
}

async joinGame(formData, isNewUser = false)
{
// reset the user data in the object in case another form was used before:
this.userData = {};
await this.events.emit('reldens.beforeJoinGame', {gameManager: this, formData, isNewUser});
if(sc.hasOwn(formData, 'forgot')){
this.mapFormDataToUserData(formData, isNewUser);
// join initial game room, since we return the promise we don't need to catch the error here:
this.gameRoom = await this.gameClient.joinOrCreate(GameConst.ROOM_GAME, this.userData);
if(!this.gameRoom){
this.displayFormError('#'+formData.formId, this.gameClient.lastErrorMessage);
return false;
}
await this.events.emit('reldens.beforeJoinGameRoom', this.gameRoom);
this.handleGameRoomMessages();
this.activateResponsiveBehavior();
return this.gameRoom;
}

mapFormDataToUserData(formData, isNewUser)
{
if (sc.hasOwn(formData, 'forgot')) {
this.userData.forgot = 1;
this.userData.email = formData['email'];
}
this.initializeClient();
if(formData.isGuest){
if (formData.isGuest) {
this.userData.isGuest = true;
this.userData.isNewUser = true;
}
if(isNewUser){
if (isNewUser) {
this.userData.isNewUser = true;
this.userData.email = formData['email'];
}
this.userData.username = formData['username'];
this.userData.password = formData['password'];
// join initial game room, since we return the promise we don't need to catch the error here:
this.gameRoom = await this.gameClient.joinOrCreate(GameConst.ROOM_GAME, this.userData);
if(!this.gameRoom){
this.displayPlayerCreateError('GameRoom not available, try again later please.');
return false;
}
await this.events.emit('reldens.beforeJoinGameRoom', this.gameRoom);
}

handleGameRoomMessages()
{
this.gameRoom.onMessage('*', async (message) => {
if(message.error){
this.displayPlayerCreateError(message);
return false;
if(message.error){
Logger.error('Game Room message error.', message.error);
this.displayFormError(GameConst.SELECTORS.PLAYER_CREATE_FORM, message.error);
return false;
}
// only the current client will get this message:
if(GameConst.START_GAME === message.act){
this.initialGameData = message;
return await this.beforeStartGame();
Expand All @@ -144,26 +162,28 @@ class GameManager
}
await this.initEngine();
});
}

activateResponsiveBehavior()
{
this.events.on('reldens.afterSceneDynamicCreate', async () => {
if(this.config.get('client/ui/screen/responsive')){
this.gameEngine.updateGameSize(this);
this.gameDom.getWindow().addEventListener('resize', () => {
this.gameEngine.updateGameSize(this);
});
if(!this.config.getWithoutLogs('client/ui/screen/responsive', true)){
return;
}
this.gameEngine.updateGameSize(this);
this.gameDom.getWindow().addEventListener('resize', () => {
this.gameEngine.updateGameSize(this);
});
});
return this.gameRoom;
}

displayPlayerCreateError(message)
displayFormError(formId, message)
{
let errorElement = this.gameDom.getElement(
GameConst.SELECTORS.PLAYER_CREATE_FORM+' '+GameConst.SELECTORS.RESPONSE_ERROR
);
let errorElement = this.gameDom.getElement(formId+' '+GameConst.SELECTORS.RESPONSE_ERROR);
if(!errorElement){
return false;
}
errorElement.innerHTML = message.message;
errorElement.innerHTML = message;
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/game/client/handlers/login-form-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class LoginFormHandler
username: this.form.querySelector(GameConst.SELECTORS.LOGIN.USERNAME).value,
password: this.form.querySelector(GameConst.SELECTORS.LOGIN.PASSWORD).value
};
this.gameManager.startGame(formData, false);
return this.gameManager.startGame(formData, false);
});
}

Expand Down
1 change: 0 additions & 1 deletion lib/inventory/server/message-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ class InventoryMessageActions
{
// @TODO - BETA - Refactor when include false conditions in the shortcuts and include a new property "tradable".
let fromInventoryItems = [
// @TODO - BETA - Refactor to make findItemsByPropertyValue always return an array.
...(playerOwner.inventory.manager.findItemsByPropertyValue('equipped', false) || []),
...(playerOwner.inventory.manager.findItemsByPropertyValue('equipped', undefined) || [])
];
Expand Down
Loading

0 comments on commit 8d968e0

Please sign in to comment.