From b97d8fc18408611001792a6dc78ea52b131f89dd Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 31 May 2019 02:12:35 +1000 Subject: [PATCH] Started equipment; need to rework state system --- server/equipment.js | 45 +++++++++++ server/equipment_prices.js | 19 ----- server/equipment_statistics.json | 15 ++++ server/index.js | 4 + sql/create_database_structure.sql | 15 ++++ src/components/pages/profile.jsx | 19 ++++- src/components/panels/equipment.jsx | 112 ++++++++++++++++++++++++++++ webpack.config.js | 2 +- 8 files changed, 210 insertions(+), 21 deletions(-) create mode 100644 server/equipment.js delete mode 100644 server/equipment_prices.js create mode 100644 server/equipment_statistics.json create mode 100644 src/components/panels/equipment.jsx diff --git a/server/equipment.js b/server/equipment.js new file mode 100644 index 0000000..1fcce23 --- /dev/null +++ b/server/equipment.js @@ -0,0 +1,45 @@ +//environment variables +require('dotenv').config(); + +//utilities +let { log } = require('../common/utilities.js'); + +//the statistics +let equipmentStatistics = require('./equipment_statistics.json'); + +const statisticsRequest = () => (req, res) => { + res.status(200).json(equipmentStatistics); + res.end(); +} + +const listRequest = (connection) => (req, res) => { + //verify identity + let query = 'SELECT accountId FROM sessions WHERE accountId IN (SELECT id FROM accounts WHERE username = ?) AND token = ?;'; + connection.query(query, [req.body.username, req.body.token], (err, results) => { + if (err) throw err; + + let query = 'SELECT name, quantity, type FROM equipment WHERE accountId = ?;'; + connection.query(query, [results[0].accountId], (err, results) => { + if (err) throw err; + + //transform the results into a sendable array + let list = {}; + + results.map((record) => { + //initialize this type + list[record.type] = list[record.type] || {}; + + //send the quantity of every type + list[record.type][record.name] = record.quantity; + }); + + res.status(200).json(list); + res.end(); + }); + }); +} + +module.exports = { + statisticsRequest: statisticsRequest, + listRequest: listRequest +} \ No newline at end of file diff --git a/server/equipment_prices.js b/server/equipment_prices.js deleted file mode 100644 index af5d8e4..0000000 --- a/server/equipment_prices.js +++ /dev/null @@ -1,19 +0,0 @@ -let weapons = [ - { name: 'Stick', cost: 50, boost: 0.02, level: 1 }, - { name: 'Dagger', cost: 75, boost: 0.03, level: 2 }, - { name: 'Sword', cost: 100, boost: 0.04, level: 3 }, - { name: 'Longsword', cost: 150, boost: 0.05, level: 4 }, - { name: 'Frying Pan', cost: 200, boost: 0.06, level: 5 }, -]; - -let armour = [ - { name: 'leather', cost: 75, boost: 0.02, level: 2 }, - { name: 'gambeson', cost: 100, boost: 0.03, level: 3 }, - { name: 'chainmail', cost: 150, boost: 0.04, level: 4 }, - { name: 'platemail', cost: 200, boost: 0.05, level: 5 }, -]; - -module.exports = { - weapons: weapons, - armour: armour -}; \ No newline at end of file diff --git a/server/equipment_statistics.json b/server/equipment_statistics.json new file mode 100644 index 0000000..8523b4a --- /dev/null +++ b/server/equipment_statistics.json @@ -0,0 +1,15 @@ +{ + "Weapons": { + "Stick": { "cost": 50, "combatBoost": 0.02, "scientists": 1 }, + "Dagger": { "cost": 75, "combatBoost": 0.03, "scientists": 2 }, + "Sword": { "cost": 100, "combatBoost": 0.04, "scientists": 3 }, + "Longsword": { "cost": 150, "combatBoost": 0.05, "scientists": 4 }, + "Frying Pan": { "cost": 200, "combatBoost": 0.06, "scientists": 5 } + }, + "Armour": { + "Leather": { "cost": 75, "combatBoost": 0.02, "scientists": 2 }, + "Gambeson": { "cost": 100, "combatBoost": 0.03, "scientists": 3 }, + "Chainmail": { "cost": 150, "combatBoost": 0.04, "scientists": 4 }, + "Platemail": { "cost": 200, "combatBoost": 0.05, "scientists": 5 } + } +} \ No newline at end of file diff --git a/server/index.js b/server/index.js index c4e2b86..896bb97 100644 --- a/server/index.js +++ b/server/index.js @@ -46,6 +46,10 @@ app.post('/attackstatusrequest', combat.attackStatusRequest(connection)); app.post('/combatlogrequest', combat.combatLogRequest(connection)); combat.runCombatTick(connection); +let equipment = require('./equipment.js'); +app.post('/equipmentstatisticsrequest', equipment.statisticsRequest()); +app.post('/equipmentlistrequest', equipment.listRequest(connection)); + //static directories app.use('/styles', express.static(path.resolve(__dirname + '/../public/styles')) ); app.use('/img', express.static(path.resolve(__dirname + '/../public/img')) ); diff --git a/sql/create_database_structure.sql b/sql/create_database_structure.sql index ae0b929..76cbc12 100644 --- a/sql/create_database_structure.sql +++ b/sql/create_database_structure.sql @@ -92,4 +92,19 @@ CREATE TABLE IF NOT EXISTS pastCombat ( CONSTRAINT FOREIGN KEY fk_attackerId(attackerId) REFERENCES accounts(id) ON UPDATE CASCADE ON DELETE CASCADE, CONSTRAINT FOREIGN KEY fk_defenderId(defenderId) REFERENCES accounts(id) ON UPDATE CASCADE ON DELETE CASCADE +); + +#equipment system +CREATE TABLE IF NOT EXISTS equipment ( + id INTEGER UNSIGNED AUTO_INCREMENT PRIMARY KEY UNIQUE, + td TIMESTAMP DEFAULT CURRENT_TIMESTAMP(), + + accountId INTEGER UNSIGNED, + + name VARCHAR(50), + quantity INTEGER, + + type VARCHAR(50), + + CONSTRAINT FOREIGN KEY fk_accountId(accountId) REFERENCES accounts(id) ON UPDATE CASCADE ON DELETE CASCADE ); \ No newline at end of file diff --git a/src/components/pages/profile.jsx b/src/components/pages/profile.jsx index 7f32273..a810f29 100644 --- a/src/components/pages/profile.jsx +++ b/src/components/pages/profile.jsx @@ -7,6 +7,7 @@ import queryString from 'query-string'; //panels import CommonLinks from '../panels/common_links.jsx'; import AttackButton from '../panels/attack_button.jsx'; +import Equipment from '../panels/equipment.jsx'; import CombatLog from '../panels/combat_log.jsx'; class Profile extends React.Component { @@ -27,12 +28,24 @@ class Profile extends React.Component { warning: '', - start: params.log + //combat log + start: params.log, + + //equipment + fetchStatistics: null, + fetchEquipment: null }; this.sendRequest('/profilerequest', this.state.params.username ? this.state.params.username : this.props.username); } + componentDidUpdate(prevProps, prevState, snapshot) { + if (JSON.stringify(this.state) !== JSON.stringify(prevState)) { +// if (this.state.fetchStatistics) this.state.fetchStatistics(); +// if (this.state.fetchEquipment) this.state.fetchEquipment(); + } + } + render() { let warningStyle = { display: this.state.warning.length > 0 ? 'flex' : 'none' @@ -197,6 +210,10 @@ class Profile extends React.Component { +
+

Equipment

+ this.setState({ fetchStatistics: fn }) } getFetchEquipment={ (fn) => this.setState({ fetchEquipment: fn}) } /> +

Combat Log

diff --git a/src/components/panels/equipment.jsx b/src/components/panels/equipment.jsx new file mode 100644 index 0000000..5eeaecf --- /dev/null +++ b/src/components/panels/equipment.jsx @@ -0,0 +1,112 @@ +import React from 'react'; +import PropTypes from 'prop-types'; + +class Equipment extends React.Component { + constructor(props) { + super(props); + this.state = { + statistics: {}, + equipment: {} + }; + + if (this.props.getFetchStatistics) { +// this.props.getFetchStatistics(this.fetchStatistics.bind(this)); + } + + this.fetchStatistics(); + + if (this.props.getFetchEquipment) { +// this.props.getFetchEquipment(this.fetchEquipmentList.bind(this)); + } + + this.fetchEquipmentList(); + } + + render() { + //print the purchasable weapons, then purchasable armour, then stuff you can't buy + let statistics = JSON.parse(JSON.stringify(this.state.statistics)); + + //filter out what you can't get at your current scientist count + Object.keys(statistics).forEach((typeKey) => { + Object.keys(statistics[typeKey]).forEach((nameKey) => { + if (statistics[typeKey][nameKey].scientists > this.props.scientists) { + delete statistics[typeKey][nameKey]; + } + if (Object.keys(statistics[typeKey]).length === 0) { + delete statistics[typeKey]; + } + }); + }); + + console.log(this.state.statistics); + console.log(statistics); + + return ( +
+
+ +
+

Equipment Name

+

Equipment Type

+

Quantity

+

Cost

+

Buy

+

Sell

+
+ + + +
+
+ ); + } + + fetchStatistics() { + //build the XHR + let xhr = new XMLHttpRequest(); + + xhr.onreadystatechange = () => { + if (xhr.readyState === 4) { + if (xhr.status === 200) { + let data = JSON.parse(xhr.responseText); + this.setState({ statistics: data }); + } + } + } + + xhr.open('POST', '/equipmentstatisticsrequest', true); + xhr.send(); + } + + fetchEquipmentList(username = this.props.username, token = this.props.token) { + //build the XHR + let xhr = new XMLHttpRequest(); + + xhr.onreadystatechange = () => { + if (xhr.readyState === 4) { + if (xhr.status === 200) { + let data = JSON.parse(xhr.responseText); + this.setState({ equipment: data }); + } + } + } + + xhr.open('POST', '/equipmentlistrequest', true); + xhr.setRequestHeader('Content-Type', 'application/json; charset=UTF-8'); + xhr.send(JSON.stringify({ + username: username, + token: token + })); + } +}; + +Equipment.propTypes = { + username: PropTypes.string.isRequired, + token: PropTypes.number.isRequired, + scientists: PropTypes.number.isRequired, + + getFetchStatistics: PropTypes.func, + getFetchEquipmentList: PropTypes.func +}; + +export default Equipment; \ No newline at end of file diff --git a/webpack.config.js b/webpack.config.js index 212c444..908c145 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -24,7 +24,7 @@ module.exports = { ] }, optimization: { - minimize: true, + minimize: false, minimizer: [ new TerserPlugin({ terserOptions: {