refactor encounter check to take advantage of our new creature database

This commit is contained in:
Greg Gauthier 2024-07-01 22:35:47 +01:00
parent b915484190
commit 5a502fb308
13 changed files with 123 additions and 28 deletions

View File

@ -0,0 +1,6 @@
from app.tables.creature import Creatures
def get_creature(creature_id):
creature = Creatures()
return creature.get_creature(creature_id)

View File

@ -1,5 +1,7 @@
from flask import request
from flask_restx import Resource, Namespace, reqparse
from app.functions.get_creature import get_creature
from app.functions.roll_encounter import roll_encounter
namespace = Namespace('gameplay', description='Gamma World Game Play')
@ -18,7 +20,13 @@ class RollEncounter(Resource):
valid_terrains = ["clear", "mountains", "forest", "desert", "watery", "ruins", "deathlands"]
if terrain:
if terrain.lower() in valid_terrains:
return roll_encounter(terrain.lower()), 200
details = dict()
encounter = roll_encounter(terrain.lower())
details["name"] = encounter["encounter"]
creature_stats = get_creature(encounter["encounter"])
if creature_stats is not None:
details.update(creature_stats)
return details, 200
else:
return {
'error': 'Invalid terrain type provided.',

View File

@ -367,5 +367,26 @@
"mutations": ["Death Field Generation", "Life Leach", "Manipulative Vines", "Mobility",
"Molecular Disruption", "Symbiotic Attachment"],
"description": "[Red Crep | Pink Crep] Creps come in 2 varieties: the Water Crep (also called the Pink Crep) and the Land Crep (also called the Red Crep). Water Creps live totally submerged and Land Creps grow under a mat of other foliage. Both are carnivorous, using their broad flat leaves to feed by Life Leaching those with whom they come into contact. Leaves that have been used to feed drop off once the victim escapes or dies, eventually sprouting new plants. [HOUSE NOTE: Players snared by a Crep Plant must make a Dex Check to escape. Life Leach attacks continue for each round the Dex Check fails. - GMG]"
},
"fen": {
"number": [1, 10, 0],
"morale": [1, 6, 4],
"hit dice": [10, 6, 0],
"armour": 7,
"environ": ["land","water", "air"],
"land speed": [0, 300, 6],
"water speed": [6, 1800, 36],
"air speed": [0, 1800, 36],
"ms": [1, 10, 2],
"in": [1, 10, 8],
"dx": [1, 10, 2],
"ch": [1, 8, 1],
"cn": [1, 12, 4],
"ps": [1, 10, 8],
"attacks": {"Tail Slap": [6,6,0]},
"mutations": ["Shape change"],
"description": "[Man Fish] These intelligent Humanoids are adapted for living both on land and in water, having fish-like tails, stubby legs, and both lungs and gills. They can remain out of water for only 24 hours at a time. They are not affected by attacks involving heat or lasers during the first 5 action turns these attacks are used against them in a combat. Fens can shape-change into the form of a bird of their own size. They carry Tech Level I weapons (usually clubs) and wear Fishkin Armour (similar to fiber armour)."
}
}

View File

@ -9,18 +9,18 @@ class EncounterTable:
'centisteed', 'rakox', 'rakox', 'brutorz', 'hoop', 'hawkoid', 'hopper', 'hopper', None, None],
'mountains': ['hisser', 'blight', 'parn', 'zarn', 'manta', 'orlen', 'zeethh', 'sep', 'arn', 'yexil', 'herp',
'wardent', 'kep plant', 'crep plant', 'cal then', 'ark', 'hawkoid', 'podog', 'carrin', None],
'forest': ['hisser', 'sep', 'blaash', 'blackun', 'terl', 'winseen', 'pineto', 'perth', 'obb', 'kailin',
'grens', 'badder', 'arn', 'lil', 'blood bird', 'horl choo', 'soul besh', 'dabber', 'centisteed',
'forest': ['hisser', 'sep', 'blaash', 'blackun', 'terl', 'win seen', 'pineto', 'perth', 'obb', 'kailin',
'gren', 'badder', 'arn', 'lil', 'blood bird', 'horl choo', 'soul besh', 'dabber', 'centisteed',
None],
'desert': ['serf', 'kamodo', 'blight', 'perth', 'parn', 'zarn', 'yexil', 'hisser', 'sep', 'calthen',
'manta', 'kep plant', 'carrin', 'podog', None, None, None, None, None, None],
'watery': ['winseen', 'crep plant', 'seroon loo', 'terl', 'ert telden', 'barl nep', 'ert', 'fleshin',
'keeshin', 'narlep', 'menarl', 'herkel', 'berlep', 'crentosh', 'fen', 'gator', None, None, None,
'watery': ['win seen', 'crep plant', 'seroon lou', 'terl', 'ert telden', 'barl nep', 'ert', 'fleshin',
'keeshin', 'narl ep', 'menarl', 'herkel', 'ber lep', 'cren tosh', 'fen', 'gator', None, None, None,
None],
'ruins': ['arn', 'obb', 'hoop', 'android', 'badder', 'serf', 'blaash', 'yexil', 'manta', 'ark', 'orlen',
'dabber', 'sleeth', 'carrin', 'squeeker', 'squeeker', 'squeeker', None, None, None],
'deathlands': ['android', 'hisser', 'blight', 'zarn', 'perth', 'blaash', 'serf', 'parn', 'squeeker',
'squeekr', None, None, None, None, None, None, None, None, None, None]
'squeeker', None, None, None, None, None, None, None, None, None, None]
}
self.table_dataframe = pd.DataFrame(self.table)
self.table_dataframe.index = np.arange(1, len(self.table_dataframe) + 1)

View File

@ -2,6 +2,7 @@
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="stylesheet" type="text/css" href="styles.css">
<title>Gamma World Encounter Check</title>
<script src="config.js"></script>
<script src="encounter.js" defer></script>

View File

@ -1,6 +1,6 @@
document.getElementById('terrainForm').addEventListener('submit', function (event) {
event.preventDefault();
var terrain = document.getElementById('terrainType').value;
const terrain = document.getElementById('terrainType').value;
const queryParams = new URLSearchParams({
terrain: terrain,
});
@ -17,30 +17,89 @@ document.getElementById('terrainForm').addEventListener('submit', function (even
imgElement.style.marginRight = 'auto';
resultSection.appendChild(imgElement);
fetch(`${window.BASE_URL}/gameplay/encounter?${queryParams}`)
.then((response) => {
if (!response.ok) {
throw response;
}
return response.json(); // we only get here if there is no error
})
.then((json) => {
// Handle null encounter
let encounterText = json.encounter ? json.encounter : 'No Encounter';
// Change the src attribute of the image after fetch resolves
setTimeout(() => {
imgElement.src = './img/' + encounterText + ".jpg";
}, 1000);
setTimeout(() => {
fetch(`${window.BASE_URL}/gameplay/encounter?${queryParams}`)
.then((response) => {
if (!response.ok) {
throw response;
}
return response.json(); // we only get here if there is no error
})
.then((json) => {
resultSection.innerHTML = ''; // Clear previous content
if (Object.keys(json).length === 1) { // Short version of response
let name = json.name !== null ? json.name : 'No Encounter';
console.log(name);
setResultImage(name);
} else { // Long version of the response
setResultTable(json);
}
})
.catch((err) => {
err.text().then((errorMessage) => {
imgElement.src = './img/404.jpg';
});
});
function setResultImage(name) {
let imgElement = document.createElement('img');
imgElement.src = './img/' + name + ".jpg";
console.log(imgElement.src);
imgElement.onerror = function () {
this.onerror = null;
this.src = './img/404.jpg';
};
})
.catch((err) => {
err.text().then((errorMessage) => {
imgElement.src = './img/404.jpg';
});
});
resultSection.appendChild(imgElement);
}
function setResultTable(data) {
let table = document.createElement('table');
for (let key in data) {
let tr = document.createElement('tr');
let tdKey = document.createElement('td');
let tdValue = document.createElement('td');
tdKey.innerText = key.toUpperCase();
// Make the key names bold:
tdKey.style.fontWeight = 'bold';
if (Array.isArray(data[key])) {
if (key === 'environ' || key === 'mutations') {
tdValue.innerText = data[key].join(', ');
} else if (key === 'land speed' || key === 'water speed' || key === 'air speed') {
tdValue.innerText = `${data[key][0]}/${data[key][1]}/${data[key][2]}`;
} else {
tdValue.innerText = `${data[key][0]}d${data[key][1]}${data[key][2] === 0 ? "" : "+" + data[key][2]}`;
}
} else if (key === 'attacks' && typeof data[key] === 'object') {
let attacksTable = document.createElement('table');
for (let attack in data[key]) {
let tr = document.createElement('tr');
let tdKey = document.createElement('td');
let tdValue = document.createElement('td');
tdKey.innerText = attack;
if (data[key][attack].join() === "0,0,0") {
tdValue.innerText = "See notes below";
} else {
tdValue.innerText = `${data[key][attack][0]}d${data[key][attack][1]}${data[key][attack][2] === 0 ? "" : "+" + data[key][attack][2]}`;
}
tr.appendChild(tdKey);
tr.appendChild(tdValue);
attacksTable.appendChild(tr);
}
tdValue.appendChild(attacksTable);
} else {
tdValue.innerText = data[key];
}
tr.appendChild(tdKey);
tr.appendChild(tdValue);
table.appendChild(tr);
}
resultSection.appendChild(table);
}
}, 1000);
});

View File

Before

Width:  |  Height:  |  Size: 78 KiB

After

Width:  |  Height:  |  Size: 78 KiB

View File

Before

Width:  |  Height:  |  Size: 72 KiB

After

Width:  |  Height:  |  Size: 72 KiB

View File

Before

Width:  |  Height:  |  Size: 100 KiB

After

Width:  |  Height:  |  Size: 100 KiB

View File

Before

Width:  |  Height:  |  Size: 95 KiB

After

Width:  |  Height:  |  Size: 95 KiB

View File

Before

Width:  |  Height:  |  Size: 86 KiB

After

Width:  |  Height:  |  Size: 86 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 61 KiB

View File

Before

Width:  |  Height:  |  Size: 96 KiB

After

Width:  |  Height:  |  Size: 96 KiB