213 lines
4.3 KiB
JavaScript
213 lines
4.3 KiB
JavaScript
const express = require('express');
|
|
const CraftingSystem = require('../systems/CraftingSystem');
|
|
|
|
const router = express.Router();
|
|
|
|
// Initialize crafting system
|
|
const craftingSystem = new CraftingSystem();
|
|
|
|
// Get all recipes
|
|
router.get('/recipes', async (req, res) => {
|
|
try {
|
|
const recipes = craftingSystem.getAllRecipes();
|
|
res.json({
|
|
success: true,
|
|
recipes
|
|
});
|
|
} catch (error) {
|
|
res.status(500).json({
|
|
success: false,
|
|
error: error.message
|
|
});
|
|
}
|
|
});
|
|
|
|
// Get recipe by ID
|
|
router.get('/recipes/:id', async (req, res) => {
|
|
try {
|
|
const { id } = req.params;
|
|
const recipe = craftingSystem.getRecipe(id);
|
|
|
|
if (!recipe) {
|
|
return res.status(404).json({
|
|
success: false,
|
|
error: 'Recipe not found'
|
|
});
|
|
}
|
|
|
|
res.json({
|
|
success: true,
|
|
recipe
|
|
});
|
|
} catch (error) {
|
|
res.status(500).json({
|
|
success: false,
|
|
error: error.message
|
|
});
|
|
}
|
|
});
|
|
|
|
// Get recipes by type
|
|
router.get('/recipes/type/:type', async (req, res) => {
|
|
try {
|
|
const { type } = req.params;
|
|
const recipes = craftingSystem.getRecipesByType(type);
|
|
|
|
res.json({
|
|
success: true,
|
|
recipes
|
|
});
|
|
} catch (error) {
|
|
res.status(500).json({
|
|
success: false,
|
|
error: error.message
|
|
});
|
|
}
|
|
});
|
|
|
|
// Get player crafting data
|
|
router.get('/player/:userId', async (req, res) => {
|
|
try {
|
|
const { userId } = req.params;
|
|
const playerData = craftingSystem.getPlayerData(userId);
|
|
|
|
res.json({
|
|
success: true,
|
|
playerData
|
|
});
|
|
} catch (error) {
|
|
res.status(500).json({
|
|
success: false,
|
|
error: error.message
|
|
});
|
|
}
|
|
});
|
|
|
|
// Check if can craft
|
|
router.post('/can-craft/:userId/:recipeId', async (req, res) => {
|
|
try {
|
|
const { userId, recipeId } = req.params;
|
|
const { inventory } = req.body;
|
|
|
|
if (!inventory) {
|
|
return res.status(400).json({
|
|
success: false,
|
|
error: 'Inventory data required'
|
|
});
|
|
}
|
|
|
|
const canCraft = craftingSystem.canCraft(userId, recipeId, inventory);
|
|
|
|
res.json({
|
|
success: true,
|
|
canCraft
|
|
});
|
|
} catch (error) {
|
|
res.status(500).json({
|
|
success: false,
|
|
error: error.message
|
|
});
|
|
}
|
|
});
|
|
|
|
// Craft item
|
|
router.post('/craft/:userId/:recipeId', async (req, res) => {
|
|
try {
|
|
const { userId, recipeId } = req.params;
|
|
const { inventory } = req.body;
|
|
|
|
if (!inventory) {
|
|
return res.status(400).json({
|
|
success: false,
|
|
error: 'Inventory data required'
|
|
});
|
|
}
|
|
|
|
const result = await craftingSystem.craftItem(userId, recipeId, inventory);
|
|
|
|
res.json({
|
|
success: true,
|
|
result
|
|
});
|
|
} catch (error) {
|
|
res.status(500).json({
|
|
success: false,
|
|
error: error.message
|
|
});
|
|
}
|
|
});
|
|
|
|
// Learn recipe
|
|
router.post('/learn/:userId/:recipeId', async (req, res) => {
|
|
try {
|
|
const { userId, recipeId } = req.params;
|
|
const result = craftingSystem.learnRecipe(userId, recipeId);
|
|
|
|
res.json({
|
|
success: true,
|
|
result
|
|
});
|
|
} catch (error) {
|
|
res.status(500).json({
|
|
success: false,
|
|
error: error.message
|
|
});
|
|
}
|
|
});
|
|
|
|
// Get player known recipes
|
|
router.get('/known/:userId', async (req, res) => {
|
|
try {
|
|
const { userId } = req.params;
|
|
const recipes = craftingSystem.getPlayerKnownRecipes(userId);
|
|
|
|
res.json({
|
|
success: true,
|
|
recipes
|
|
});
|
|
} catch (error) {
|
|
res.status(500).json({
|
|
success: false,
|
|
error: error.message
|
|
});
|
|
}
|
|
});
|
|
|
|
// Get crafting stats
|
|
router.get('/stats/:userId', async (req, res) => {
|
|
try {
|
|
const { userId } = req.params;
|
|
const stats = craftingSystem.getCraftingStats(userId);
|
|
|
|
res.json({
|
|
success: true,
|
|
stats
|
|
});
|
|
} catch (error) {
|
|
res.status(500).json({
|
|
success: false,
|
|
error: error.message
|
|
});
|
|
}
|
|
});
|
|
|
|
// Discover recipe
|
|
router.post('/discover/:userId/:recipeId', async (req, res) => {
|
|
try {
|
|
const { userId, recipeId } = req.params;
|
|
const result = craftingSystem.discoverRecipe(userId, recipeId);
|
|
|
|
res.json({
|
|
success: true,
|
|
result
|
|
});
|
|
} catch (error) {
|
|
res.status(500).json({
|
|
success: false,
|
|
error: error.message
|
|
});
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|