Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Character Plugin #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions CharactersManager/Character.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { sumOfElementPoints } from './utils';
tanukijs marked this conversation as resolved.
Show resolved Hide resolved

/**
* The stats data of the character
*/
class Character {

constructor(username){
this.username = username;
this.level = {};
this.elements = {};
this.AP = 0;
this.MP = 0;
tanukijs marked this conversation as resolved.
Show resolved Hide resolved
this.lifePoints = {};
this.kamas = 0;
this.statsPoints = 0;
this.spellsPoints = 0;
this.spells = {};
}

updateStat(stats){

//Updating Kamas
const {kamas} = stats;
this.kamas = kamas;

//Update State & Spell points
const {spellsPoints, statsPoints} = stats;
this.spellsPoints = spellsPoints;
this.statsPoints = statsPoints;

//Updating Elements Stats
const {vitality, strength, agility, chance, intelligence, wisdom} = stats;
this.elements = {
vitality: sumOfElementPoints(vitality),
strength: sumOfElementPoints(strength),
agility: sumOfElementPoints(agility),
chance: sumOfElementPoints(chance),
intelligence: sumOfElementPoints(intelligence),
wisdom: sumOfElementPoints(wisdom)
}

//Updating AP & MP
const {actionPointsCurrent, movementPointsCurrent} = stats;
this.AP = actionPointsCurrent;
this.MP = movementPointsCurrent;


//Updating life points
const {lifePoints, maxLifePoints} = stats;
this.lifePoints = {
current: lifePoints,
max: maxLifePoints
}

//Update level & xp
//TODO: Get level from xp table
const {experience, experienceLevelFloor, experienceNextLevelFloor} = stats;
this.level = {
currentXp: experience,
nextLevelXp: experienceNextLevelFloor,
currentLevelXpFloor: experienceLevelFloor
}

}
}

export default CharacterStats;
238 changes: 238 additions & 0 deletions CharactersManager/CharactersManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,238 @@
import Character from './Character';

const info = {
name: "Character Manager Plugin",
description: "Get/Upgrade characters stats and stuff",
author: "AbdSab",
version: "0.0.1"
}

class CharactersManager {

constructor(){
this.config = config;
this.connections = {};
this.listeners = [this.SpellListMessage, this.CharacterStatsListMessage];
this.characters = {};
}

mount(connections){
this.connections = connections;
for(let username in connection){
this.character[username] = new Character(username);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes typo it should be connections

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this.character ?

}
}

/**
* Get character's current kamas
*
* @param {String} username
* @return {Number} Kamas
*/
getKamas(username){
if(!this.isUsernameExists) return;
return this.characters[username].kamas;
}

/**
* Get character's current spells
*
* @param {String} username
* @return {Object} Spells
*/
getSpells(username){
if(!this.isUsernameExists) return;
return this.characters[username].spells;
}

/**
* Get character's current level
*
* @param {String} username
* @return {Number} Level
*/
getLevel(username){
if(!this.isUsernameExists) return;
return this.characters[username].level;
}

/**
* get character's stats elements
*
* @param {String} username
* @return {Object} Elements
*/
getElementsStats(username){
if(!this.isUsernameExists) return;
return this.characters[username].elements;
}

/**
* Get character's current lifepoints
*
* @param {String} username
* @return {Number} Lifepoints
*/
getLifePoints(username){
if(!this.isUsernameExists) return;
return this.characters[username].lifePoints.current;
}

/**
* Get character's max lifepoints
*
* @param {String} username
* @return {Number} Max Lifepoints
*/
getMaxLifePoints(username){
if(!this.isUsernameExists) return;
return this.characters[username].lifePoints.max;
}

/**
* Get character's Action Points (AP)
*
* @param {String} username
* @return {Number} AP
*/
getAp(username){
if(!this.isUsernameExists) return;
return this.characters[username].AP;
}

/**
* Get character's Movement Points (MP)
*
* @param {String} username
* @return {Number} MP
*/
getMP(username){
if(!this.isUsernameExists) return;
return this.characters[username].MP;
}

/**
* Get character's state points
*
* @param {String} username
* @return {Number} State points
*/
getStatsPoints(username){
if(!this.isUsernameExists) return;
return this.characters[username].MP;
}

/**
* Get character's spell points
*
* @param {String} username
* @return {Number} Spell points
*/
getSpellPoints(username){
if(!this.isUsernameExists) return;
return this.characters[username].MP;
}

/**
* Add points to a character.
*
* @param {String} username
* @param {Number} statId
* @param {Number} points
*/
addElementPoints(username, statId, points){
if(!this.isUsernameExists) return;

const character = this.characters[username];
if(points > character.statsPoints) {
console.log(`Make sure to enter ${character.statsPoints} or less !`);
};

const data = {
statId,
boostPoint:points,
useAdditionnal:false
}
this.connections[username].sendMessage("StatsUpgradeRequestMessage", data);
}

/**
* Add level to a selected spell.
*
* @param {String} username
* @param {Number} spellId
* @param {Number} spellLevel
*/
spellUpgrade(username, spellId, spellLevel){
if(!this.isUsernameExists) return;
tanukijs marked this conversation as resolved.
Show resolved Hide resolved

const character = this.characters[username];

if(spellLevel > character.spells[spellId].spellLevel){
console.log(`Make sure to enter ${character.spells[spellId].spellLevel} or less !`);
return;
}

const data = {
spellId,
spellLevel
}

this.connections[username].sendMessage("SpellUpgradeRequestMessage", data);
}

/**
* Update character's stats
*
* @param {Object} payload
*/
CharacterStatsListMessage(payload){
const {stats, socket} = payload;
const destination = socket.account.username;
this.characters[destination].updateStat(stats);
}

/**
* Get character's spells
*
* @param {Object} payload
*/
SpellListMessage(payload){
const {spells, socket} = payload;
const destination = socket.account.username;
this.characters[destination].spells.forEach(({spellId, spellLevel}) => {
this.spells[spellId] = spellLevel;
});
}

/**
* Get character's level
*
* @param {Object} payload
*/
CharacterLevelUpMessage(payload){
const {newLevel} = payload;
this.state.level = newLevel;
}

/**
* Get character's new learned spell
*
* @param {Object} payload
*/
SpellUpgradeSuccessMessage(payload){
const {spellId, spellLevel, socket} = payload;
const destination = socket.account.username;
this.characters[destination].spells[spellId] = spellLevel;
}


isUsernameExists(username){
if(username in this.connections) return true;
console.log(`Error in username !`);
return false;
}

}

export default Character;
12 changes: 12 additions & 0 deletions CharactersManager/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

/**
* Get total of element points
*
* @param {Object} element
*
* @return {Number} Total
*/
export const sumOfElementPoints = (element) =>{
const sum = Object.keys(element).reduce((acc, curr)=> acc+curr);
return sum;
}