reldens
Version:
Reldens - MMORPG Platform
79 lines (72 loc) • 2.48 kB
JavaScript
/**
*
* Reldens - PvP
*
* Handles player versus player combat logic.
*
*/
const { Battle } = require('./battle');
const { Logger } = require('@reldens/utils');
const { GameConst } = require('../../game/constants');
/**
* @typedef {import('../../users/server/player').Player} Player
* @typedef {import('../../rooms/server/scene').RoomScene} RoomScene
*/
class Pvp extends Battle
{
/**
* @param {Player} playerSchema
* @param {Player} target
* @param {RoomScene} room
* @returns {Promise<boolean>}
*/
async runBattle(playerSchema, target, room)
{
// @TODO - BETA - Implement battle end for PvP.
if(GameConst.STATUS.ACTIVE !== playerSchema.state.inState){
Logger.info('PvP inactive player.', playerSchema.state.inState);
return false;
}
if(GameConst.STATUS.ACTIVE !== target.state.inState){
Logger.info('PvP inactive target.', target.state.inState);
return false;
}
// @TODO - BETA - Make PvP available by configuration.
// can't fight with yourself:
if(playerSchema.sessionId === target.sessionId){
await this.executeAction(playerSchema, target);
return false;
}
// @NOTE: run battle method is for when the player attacks a target.
let inBattle = await super.runBattle(playerSchema, target, room);
if(!inBattle){
return false;
}
let targetClient = room.getClientById(target.sessionId);
if(targetClient){
await this.updateTargetClient(targetClient, target, playerSchema.sessionId, room, playerSchema);
}
return true;
}
/**
* @param {Player} playerSchema
* @param {Player} target
* @returns {Promise<boolean>}
*/
async executeAction(playerSchema, target)
{
let currentAction = this.getCurrentAction(playerSchema);
if(!currentAction){
Logger.error('Actions not defined for this player. ID: '+playerSchema.player_id);
return false;
}
// @TODO - BETA - Move self target validation to skills npm package.
if(!currentAction.allowSelfTarget){
return false;
}
currentAction.currentBattle = this;
await currentAction.execute(target);
return false;
}
}
module.exports.Pvp = Pvp;