-
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
Open
AbdSab
wants to merge
3
commits into
difysjs:master
Choose a base branch
from
AbdSab:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.characters[username] = new Character(username); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.