-
Notifications
You must be signed in to change notification settings - Fork 2
Character Plugin #1
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
base: master
Are you sure you want to change the base?
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| import { sumOfElementPoints } from './utils'; | ||
|
|
||
| /** | ||
| * The stats data of the character | ||
| */ | ||
| class Character { | ||
|
|
||
| constructor(username){ | ||
| this.username = username; | ||
| this.level = {}; | ||
| this.elements = {}; | ||
| this.actionPoints = 0; | ||
| this.movementPoints = 0; | ||
| 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.actionPoints = actionPointsCurrent; | ||
| this.movementPoints = movementPointsCurrent; | ||
|
|
||
|
|
||
| //Updating life points | ||
| const {lifePoints, maxLifePoints} = stats; | ||
| this.lifePoints = { | ||
| current: lifePoints, | ||
| max: maxLifePoints | ||
| } | ||
|
|
||
| //Update level & xp | ||
| const {experience, experienceLevelFloor, experienceNextLevelFloor} = stats; | ||
| this.level = { | ||
| currentXp: experience, | ||
| nextLevelXp: experienceNextLevelFloor, | ||
| currentLevelXpFloor: experienceLevelFloor | ||
| } | ||
|
|
||
| } | ||
| } | ||
|
|
||
| export default CharacterStats; | ||
| 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 connections){ | ||
| this.character[username] = new Character(username); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo ?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes typo it should be connections
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and |
||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get character's current kamas | ||
| * | ||
| * @param {String} username | ||
| * @return {Number} Kamas | ||
| */ | ||
| getKamas(username){ | ||
| if(!this.isUsernameExists(username)) return; | ||
| return this.characters[username].kamas; | ||
| } | ||
|
|
||
| /** | ||
| * Get character's current spells | ||
| * | ||
| * @param {String} username | ||
| * @return {Object} Spells | ||
| */ | ||
| getSpells(username){ | ||
| if(!this.isUsernameExists(username)) return; | ||
| return this.characters[username].spells; | ||
| } | ||
|
|
||
| /** | ||
| * Get character's current level | ||
| * | ||
| * @param {String} username | ||
| * @return {Number} Level | ||
| */ | ||
| getLevel(username){ | ||
| if(!this.isUsernameExists(username)) return; | ||
| return this.characters[username].level; | ||
| } | ||
|
|
||
| /** | ||
| * get character's stats elements | ||
| * | ||
| * @param {String} username | ||
| * @return {Object} Elements | ||
| */ | ||
| getElementsStats(username){ | ||
| if(!this.isUsernameExists(username)) return; | ||
| return this.characters[username].elements; | ||
| } | ||
|
|
||
| /** | ||
| * Get character's current lifepoints | ||
| * | ||
| * @param {String} username | ||
| * @return {Number} Lifepoints | ||
| */ | ||
| getLifePoints(username){ | ||
| if(!this.isUsernameExists(username)) return; | ||
| return this.characters[username].lifePoints.current; | ||
| } | ||
|
|
||
| /** | ||
| * Get character's max lifepoints | ||
| * | ||
| * @param {String} username | ||
| * @return {Number} Max Lifepoints | ||
| */ | ||
| getMaxLifePoints(username){ | ||
| if(!this.isUsernameExists(username)) return; | ||
| return this.characters[username].lifePoints.max; | ||
| } | ||
|
|
||
| /** | ||
| * Get character's Action Points (AP) | ||
| * | ||
| * @param {String} username | ||
| * @return {Number} AP | ||
| */ | ||
| getAp(username){ | ||
| if(!this.isUsernameExists(username)) return; | ||
| return this.characters[username].AP; | ||
| } | ||
|
|
||
| /** | ||
| * Get character's Movement Points (MP) | ||
| * | ||
| * @param {String} username | ||
| * @return {Number} MP | ||
| */ | ||
| getMP(username){ | ||
| if(!this.isUsernameExists(username)) return; | ||
| return this.characters[username].MP; | ||
| } | ||
|
|
||
| /** | ||
| * Get character's state points | ||
| * | ||
| * @param {String} username | ||
| * @return {Number} State points | ||
| */ | ||
| getStatsPoints(username){ | ||
| if(!this.isUsernameExists(username)) return; | ||
| return this.characters[username].MP; | ||
| } | ||
|
|
||
| /** | ||
| * Get character's spell points | ||
| * | ||
| * @param {String} username | ||
| * @return {Number} Spell points | ||
| */ | ||
| getSpellPoints(username){ | ||
| if(!this.isUsernameExists(username)) 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(username)) 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(username)) return; | ||
|
|
||
| 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; | ||
| 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; | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.