Skip to content

Commit

Permalink
document functions & adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
Yrahcaz7 committed Nov 21, 2023
1 parent 867fd3b commit 855866f
Show file tree
Hide file tree
Showing 12 changed files with 947 additions and 720 deletions.
43 changes: 34 additions & 9 deletions js/artifacts.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/* Dungeon of Souls
* Copyright (C) 2022 Yrahcaz7
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

const ENDOFBATTLE = 900, ONPICKUP = 901;

const artifacts = {
Expand Down Expand Up @@ -56,15 +73,10 @@ for (const key in artifacts) {
};
};

function randomArtifactSet(length = 0) {
if (length <= 0) return [];
let result = [];
for (let index = 0; index < length; index++) {
result.push(randomArtifact(result));
};
return result;
};

/**
* Returns a random artifact's id.
* @param {number[]} notInclude - the ids to not include.
*/
function randomArtifact(notInclude = []) {
let bool = true;
if (notInclude.length) {
Expand All @@ -85,3 +97,16 @@ function randomArtifact(notInclude = []) {
};
return result;
};

/**
* Returns a random artifact set.
* @param {number} length - the length of the set. Default is `0`.
*/
function randomArtifactSet(length = 0) {
if (length <= 0) return [];
let result = [];
for (let index = 0; index < length; index++) {
result.push(randomArtifact(result));
};
return result;
};
51 changes: 33 additions & 18 deletions js/cards.js
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,9 @@ class Card {
};
};

/**
* Sorts an array of cards. This method mutates the array and returns a reference to the same array.
*/
Array.prototype.cardSort = function() {
return this.sort((a, b) => {
if (cards[a.id].rarity > cards[b.id].rarity) {
Expand All @@ -214,24 +217,22 @@ Array.prototype.cardSort = function() {

const cardNames = {};

/**
* Constructs the `cardNames` array.
*/
function constructNames() {
const entries = Object.entries(cards);
for (let index = 0; index < entries.length; index++) {
cardNames[entries[index][1].name] = +entries[index][0];
};
};

function randomCardSet(length = 0) {
if (length <= 0) return [];
let result = [];
for (let index = 0; index < length; index++) {
result.push(randomCard(result));
};
return result;
};

const common = Object.keys(card.common), rare = Object.keys(card.rare);

/**
* Returns a random card's id.
* @param {string[]} notInclude - the ids to not include.
*/
function randomCard(notInclude = []) {
let bool = true;
if (Object.keys(cardNames).length === 0) constructNames();
Expand All @@ -245,21 +246,35 @@ function randomCard(notInclude = []) {
};
};
if (bool) {
let result;
let result = 0;
if (chance(7/10)) {
result = cardNames[common[randomInt(0, Object.keys(card.common).length - 1)]];
} else {
result = cardNames[rare[randomInt(0, Object.keys(card.rare).length - 1)]];
result = cardNames[rare[randomInt(0, Object.keys(card.rare).length - 1)]];
};
return result;
};
let result;
while (!result || notInclude.includes(result)) {
if (chance(7/10)) {
result = cardNames[common[randomInt(0, Object.keys(card.common).length - 1)]];
} else {
result = cardNames[rare[randomInt(0, Object.keys(card.rare).length - 1)]];
} else {
let result = 0;
while (!result || notInclude.includes(result)) {
if (chance(7/10)) {
result = cardNames[common[randomInt(0, Object.keys(card.common).length - 1)]];
} else {
result = cardNames[rare[randomInt(0, Object.keys(card.rare).length - 1)]];
};
};
return result;
};
};

/**
* Returns a random card set.
* @param {number} length - the length of the set. Default is `0`.
*/
function randomCardSet(length = 0) {
if (length <= 0) return [];
let result = [];
for (let index = 0; index < length; index++) {
result.push(randomCard(result));
};
return result;
};
Loading

0 comments on commit 855866f

Please sign in to comment.