更新
This commit is contained in:
@@ -1,134 +1,122 @@
|
||||
const CardModel = require('../cards/cards.model');
|
||||
const CardModel = require("../cards/cards.model");
|
||||
const Activity = require("../activity/activity.model");
|
||||
const config = require('../config');
|
||||
const config = require("../config");
|
||||
|
||||
exports.AddCard = async(req, res) =>
|
||||
{
|
||||
var tid = req.body.tid;
|
||||
var type = req.body.type;
|
||||
var team = req.body.team;
|
||||
var rarity = req.body.rarity || "";
|
||||
var mana = req.body.mana || 0;
|
||||
var attack = req.body.attack || 0;
|
||||
var hp = req.body.hp || 0;
|
||||
var cost = req.body.cost || 1;
|
||||
var packs = req.body.packs || [];
|
||||
exports.AddCard = async (req, res) => {
|
||||
var tid = req.body.tid;
|
||||
var type = req.body.type;
|
||||
var team = req.body.team;
|
||||
var rarity = req.body.rarity || "";
|
||||
var mana = req.body.mana || 0;
|
||||
var attack = req.body.attack || 0;
|
||||
var hp = req.body.hp || 0;
|
||||
var cost = req.body.cost || 1;
|
||||
var packs = req.body.packs || [];
|
||||
|
||||
if(!tid || typeof tid !== "string")
|
||||
return res.status(400).send({error: "Invalid parameters"});
|
||||
if (!tid || typeof tid !== "string")
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
if(!type || typeof type !== "string")
|
||||
return res.status(400).send({error: "Invalid parameters"});
|
||||
if (!type || typeof type !== "string")
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
if(!team || typeof team !== "string")
|
||||
return res.status(400).send({error: "Invalid parameters"});
|
||||
if (!team || typeof team !== "string")
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
if(!rarity || typeof rarity !== "string")
|
||||
return res.status(400).send({error: "Invalid parameters"});
|
||||
if (!rarity || typeof rarity !== "string")
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
if(!Number.isInteger(mana) || !Number.isInteger(attack) || !Number.isInteger(hp) || !Number.isInteger(cost))
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
if (
|
||||
!Number.isInteger(mana) ||
|
||||
!Number.isInteger(attack) ||
|
||||
!Number.isInteger(hp) ||
|
||||
!Number.isInteger(cost)
|
||||
)
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
if(packs && !Array.isArray(packs))
|
||||
return res.status(400).send({error: "Invalid parameters"});
|
||||
if (packs && !Array.isArray(packs))
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
var data = {
|
||||
tid: tid,
|
||||
type: type,
|
||||
team: team,
|
||||
rarity: rarity,
|
||||
mana: mana,
|
||||
attack: attack,
|
||||
hp: hp,
|
||||
cost: cost,
|
||||
packs: packs,
|
||||
var data = {
|
||||
tid: tid,
|
||||
type: type,
|
||||
team: team,
|
||||
rarity: rarity,
|
||||
mana: mana,
|
||||
attack: attack,
|
||||
hp: hp,
|
||||
cost: cost,
|
||||
packs: packs,
|
||||
};
|
||||
|
||||
//Update or create
|
||||
var card = await CardModel.get(tid);
|
||||
if (card) card = await CardModel.update(card, data);
|
||||
else card = await CardModel.create(data);
|
||||
|
||||
if (!card) return res.status(500).send({ error: "Error updating card" });
|
||||
|
||||
return res.status(200).send(data);
|
||||
};
|
||||
|
||||
exports.AddCardList = async (req, res) => {
|
||||
var cards = req.body.cards;
|
||||
if (!Array.isArray(cards))
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
var ocards = [];
|
||||
for (var i = 0; i < cards.length; i++) {
|
||||
var card = cards[i];
|
||||
if (card && card.tid && card.type && card.team) {
|
||||
var data = {
|
||||
tid: card.tid,
|
||||
type: card.type,
|
||||
team: card.team,
|
||||
rarity: card.rarity || "",
|
||||
mana: card.mana || 0,
|
||||
attack: card.attack || 0,
|
||||
hp: card.hp || 0,
|
||||
cost: card.cost || 0,
|
||||
packs: card.packs || [],
|
||||
};
|
||||
|
||||
var ccard = await CardModel.get(card.tid);
|
||||
if (ccard) ccard = await CardModel.update(ccard, data);
|
||||
else ccard = await CardModel.create(data);
|
||||
|
||||
ocards.push(ccard);
|
||||
}
|
||||
}
|
||||
|
||||
//Update or create
|
||||
var card = await CardModel.get(tid);
|
||||
if(card)
|
||||
card = await CardModel.update(card, data);
|
||||
else
|
||||
card = await CardModel.create(data);
|
||||
|
||||
if(!card)
|
||||
return res.status(500).send({error: "Error updating card"});
|
||||
|
||||
return res.status(200).send(data);
|
||||
return res.status(200).send(ocards);
|
||||
};
|
||||
|
||||
exports.AddCardList = async(req, res) =>
|
||||
{
|
||||
var cards = req.body.cards;
|
||||
if(!Array.isArray(cards))
|
||||
return res.status(400).send({error: "Invalid parameters"});
|
||||
|
||||
var ocards = [];
|
||||
for(var i=0; i<cards.length; i++)
|
||||
{
|
||||
var card = cards[i];
|
||||
if(card && card.tid && card.type && card.team)
|
||||
{
|
||||
var data = {
|
||||
tid: card.tid,
|
||||
type: card.type,
|
||||
team: card.team,
|
||||
rarity: card.rarity || "",
|
||||
mana: card.mana || 0,
|
||||
attack: card.attack || 0,
|
||||
hp: card.hp || 0,
|
||||
cost: card.cost || 0,
|
||||
packs: card.packs || [],
|
||||
}
|
||||
|
||||
var ccard = await CardModel.get(card.tid);
|
||||
if(ccard)
|
||||
ccard = await CardModel.update(ccard, data);
|
||||
else
|
||||
ccard = await CardModel.create(data);
|
||||
|
||||
ocards.push(ccard);
|
||||
}
|
||||
}
|
||||
|
||||
return res.status(200).send(ocards);
|
||||
exports.DeleteCard = async (req, res) => {
|
||||
CardModel.remove(req.params.tid);
|
||||
return res.status(204).send({});
|
||||
};
|
||||
|
||||
exports.DeleteCard = async(req, res) =>
|
||||
{
|
||||
CardModel.remove(req.params.tid);
|
||||
return res.status(204).send({});
|
||||
exports.DeleteAll = async (req, res) => {
|
||||
CardModel.removeAll();
|
||||
return res.status(204).send({});
|
||||
};
|
||||
|
||||
exports.DeleteAll = async(req, res) =>
|
||||
{
|
||||
CardModel.removeAll();
|
||||
return res.status(204).send({});
|
||||
exports.GetCard = async (req, res) => {
|
||||
var tid = req.params.tid;
|
||||
|
||||
if (!tid) return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
var card = await CardModel.get(tid);
|
||||
if (!card) return res.status(404).send({ error: "Card not found: " + tid });
|
||||
|
||||
return res.status(200).send(card.toObj());
|
||||
};
|
||||
|
||||
exports.GetCard = async(req, res) =>
|
||||
{
|
||||
var tid = req.params.tid;
|
||||
exports.GetAll = async (req, res) => {
|
||||
var cards = await CardModel.getAll();
|
||||
|
||||
if(!tid)
|
||||
return res.status(400).send({error: "Invalid parameters"});
|
||||
for (var i = 0; i < cards.length; i++) {
|
||||
cards[i] = cards[i].toObj();
|
||||
}
|
||||
|
||||
var card = await CardModel.get(tid);
|
||||
if(!card)
|
||||
return res.status(404).send({error: "Card not found: " + tid});
|
||||
|
||||
return res.status(200).send(card.toObj());
|
||||
return res.status(200).send(cards);
|
||||
};
|
||||
|
||||
exports.GetAll = async(req, res) =>
|
||||
{
|
||||
var cards = await CardModel.getAll();
|
||||
|
||||
for(var i=0; i<cards.length; i++){
|
||||
cards[i] = cards[i].toObj();
|
||||
}
|
||||
|
||||
return res.status(200).send(cards);
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -1,111 +1,92 @@
|
||||
const config = require('../config.js');
|
||||
const crypto = require('crypto');
|
||||
const CardModel = require('../cards/cards.model');
|
||||
const config = require("../config.js");
|
||||
const crypto = require("crypto");
|
||||
const CardModel = require("../cards/cards.model");
|
||||
|
||||
const CardTool = {};
|
||||
|
||||
CardTool.getPackCards = async(pack) =>
|
||||
{
|
||||
var pack_cards = await CardModel.getByPack(pack.tid);
|
||||
|
||||
var cards = [];
|
||||
for(var i=0; i<pack.cards; i++)
|
||||
{
|
||||
if(pack.random)
|
||||
{
|
||||
//Randomized set
|
||||
var rarity_tid = CardTool.getRandomRarity(pack, i==0);
|
||||
var variant_tid = CardTool.getRandomVariant(pack);
|
||||
var rarity_cards = CardTool.getCardArray(pack_cards, rarity_tid);
|
||||
var card = CardTool.getRandomCard(rarity_cards);
|
||||
if(card)
|
||||
{
|
||||
var cardQ = {tid: card.tid, variant: variant_tid, quantity: 1};
|
||||
cards.push(cardQ);
|
||||
}
|
||||
}
|
||||
else if(i < pack_cards.length)
|
||||
{
|
||||
//Fixed set
|
||||
var card = pack_cards[i];
|
||||
var variant_tid = CardTool.getRandomVariant(pack);
|
||||
var cardQ = {tid: card.tid, variant: variant_tid, quantity: 1};
|
||||
cards.push(cardQ);
|
||||
}
|
||||
CardTool.getPackCards = async (pack) => {
|
||||
var pack_cards = await CardModel.getByPack(pack.tid);
|
||||
console.log("pack_cards", pack_cards);
|
||||
var cards = [];
|
||||
for (var i = 0; i < pack.cards; i++) {
|
||||
if (pack.random) {
|
||||
//Randomized set
|
||||
var rarity_tid = CardTool.getRandomRarity(pack, i == 0);
|
||||
var variant_tid = CardTool.getRandomVariant(pack);
|
||||
var rarity_cards = CardTool.getCardArray(pack_cards, rarity_tid);
|
||||
var card = CardTool.getRandomCard(rarity_cards);
|
||||
if (card) {
|
||||
var cardQ = { tid: card.tid, variant: variant_tid, quantity: 1 };
|
||||
cards.push(cardQ);
|
||||
}
|
||||
} else if (i < pack_cards.length) {
|
||||
//Fixed set
|
||||
var card = pack_cards[i];
|
||||
var variant_tid = CardTool.getRandomVariant(pack);
|
||||
var cardQ = { tid: card.tid, variant: variant_tid, quantity: 1 };
|
||||
cards.push(cardQ);
|
||||
}
|
||||
return cards;
|
||||
}
|
||||
return cards;
|
||||
};
|
||||
|
||||
CardTool.getRandomRarity = (pack, is_first) =>
|
||||
{
|
||||
var rarities = is_first ? pack.rarities_1st : pack.rarities;
|
||||
if(!rarities || rarities.length == 0)
|
||||
return ""; //Any rarity
|
||||
CardTool.getRandomRarity = (pack, is_first) => {
|
||||
var rarities = is_first ? pack.rarities_1st : pack.rarities;
|
||||
if (!rarities || rarities.length == 0) return ""; //Any rarity
|
||||
|
||||
var total = 0;
|
||||
for(var rarity of rarities) {
|
||||
total += rarity.value;
|
||||
var total = 0;
|
||||
for (var rarity of rarities) {
|
||||
total += rarity.value;
|
||||
}
|
||||
|
||||
var rvalue = Math.floor(Math.random() * total);
|
||||
|
||||
for (var i = 0; i < rarities.length; i++) {
|
||||
var rarity = rarities[i];
|
||||
if (rvalue < rarity.value) {
|
||||
return rarity.tid;
|
||||
}
|
||||
|
||||
var rvalue = Math.floor(Math.random()*total);
|
||||
|
||||
for(var i=0; i<rarities.length; i++)
|
||||
{
|
||||
var rarity = rarities[i];
|
||||
if(rvalue < rarity.value)
|
||||
{
|
||||
return rarity.tid;
|
||||
}
|
||||
rvalue -= rarity.value;
|
||||
}
|
||||
return "";
|
||||
rvalue -= rarity.value;
|
||||
}
|
||||
return "";
|
||||
};
|
||||
|
||||
CardTool.getRandomVariant = (pack) =>
|
||||
{
|
||||
var variants = pack.variants;
|
||||
if(!variants || variants.length == 0)
|
||||
return "";
|
||||
CardTool.getRandomVariant = (pack) => {
|
||||
var variants = pack.variants;
|
||||
if (!variants || variants.length == 0) return "";
|
||||
|
||||
var total = 0;
|
||||
for(var variant of variants) {
|
||||
total += variant.value;
|
||||
var total = 0;
|
||||
for (var variant of variants) {
|
||||
total += variant.value;
|
||||
}
|
||||
|
||||
var rvalue = Math.floor(Math.random() * total);
|
||||
|
||||
for (var i = 0; i < variants.length; i++) {
|
||||
var variant = variants[i];
|
||||
if (rvalue < variant.value) {
|
||||
return variant.tid;
|
||||
}
|
||||
|
||||
var rvalue = Math.floor(Math.random()*total);
|
||||
|
||||
for(var i=0; i<variants.length; i++)
|
||||
{
|
||||
var variant = variants[i];
|
||||
if(rvalue < variant.value)
|
||||
{
|
||||
return variant.tid;
|
||||
}
|
||||
rvalue -= variant.value;
|
||||
}
|
||||
return "";
|
||||
rvalue -= variant.value;
|
||||
}
|
||||
return "";
|
||||
};
|
||||
|
||||
CardTool.getCardArray = (all_cards, rarity) =>
|
||||
{
|
||||
var valid_cards = [];
|
||||
for(var i=0; i<all_cards.length; i++)
|
||||
{
|
||||
var card = all_cards[i];
|
||||
if(!rarity || card.rarity == rarity)
|
||||
valid_cards.push(card);
|
||||
}
|
||||
return valid_cards;
|
||||
}
|
||||
CardTool.getCardArray = (all_cards, rarity) => {
|
||||
var valid_cards = [];
|
||||
for (var i = 0; i < all_cards.length; i++) {
|
||||
var card = all_cards[i];
|
||||
if (!rarity || card.rarity == rarity) valid_cards.push(card);
|
||||
}
|
||||
return valid_cards;
|
||||
};
|
||||
|
||||
CardTool.getRandomCard = (all_cards, suffix) =>
|
||||
{
|
||||
if(all_cards.length > 0)
|
||||
{
|
||||
var card = all_cards[Math.floor(Math.random()*all_cards.length)];
|
||||
return card;
|
||||
}
|
||||
return null;
|
||||
CardTool.getRandomCard = (all_cards, suffix) => {
|
||||
if (all_cards.length > 0) {
|
||||
var card = all_cards[Math.floor(Math.random() * all_cards.length)];
|
||||
return card;
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
module.exports = CardTool;
|
||||
54
config.js
54
config.js
@@ -1,12 +1,12 @@
|
||||
module.exports = {
|
||||
version: "0.0.5",
|
||||
description: "0.0.1",
|
||||
md5: "36b441e48050bb919b5f0afcae3f076d",
|
||||
version: "0.0.9",
|
||||
description: "更新日志:0.0.9",
|
||||
md5: "4be73c5ccf5bf5e956d172f7fd29d336",
|
||||
|
||||
port: 8080,
|
||||
port_https: 443,
|
||||
api_title: "TCG Engine API", //Display name
|
||||
api_url: "", //If you set the URL, will block all direct IP access, or wrong url access, leave blank to allow all url access
|
||||
api_title: "TCG Engine API", //Display name
|
||||
api_url: "", //If you set the URL, will block all direct IP access, or wrong url access, leave blank to allow all url access
|
||||
|
||||
//HTTPS config, certificate is required if you want to enable HTTPS
|
||||
https_key: "/etc/letsencrypt/live/yoursite.com/privkey.pem",
|
||||
@@ -16,8 +16,8 @@ module.exports = {
|
||||
allow_https: false,
|
||||
|
||||
//JS Web Token Config
|
||||
jwt_secret: "JWT_123456789", //Change this to a unique secret value
|
||||
jwt_expiration: 3600 * 10, //In seconds (10 hours)
|
||||
jwt_secret: "JWT_123456789", //Change this to a unique secret value
|
||||
jwt_expiration: 3600 * 10, //In seconds (10 hours)
|
||||
jwt_refresh_expiration: 3600 * 100, //In seconds (100 hours)
|
||||
|
||||
//User Permissions Config
|
||||
@@ -35,28 +35,28 @@ module.exports = {
|
||||
mongo_db: "tcgengine",
|
||||
|
||||
//Limiter to protect from DDOS, will block IP that do too many requests
|
||||
limiter_window: 1000 * 120, //in ms, will reset the counts after this time
|
||||
limiter_max: 500, //max nb of GET requests within the time window
|
||||
limiter_post_max: 100, //max nb of POST requests within the time window
|
||||
limiter_auth_max: 10, //max nb of Login/Register request within the time window
|
||||
limiter_proxy: false, //Must be set to true if your server is behind a proxy, otherwise the proxy itself will be blocked
|
||||
limiter_window: 1000 * 120, //in ms, will reset the counts after this time
|
||||
limiter_max: 500, //max nb of GET requests within the time window
|
||||
limiter_post_max: 100, //max nb of POST requests within the time window
|
||||
limiter_auth_max: 10, //max nb of Login/Register request within the time window
|
||||
limiter_proxy: false, //Must be set to true if your server is behind a proxy, otherwise the proxy itself will be blocked
|
||||
|
||||
ip_whitelist: ["127.0.0.1"], //These IP are not affected by the limiter, for example you could add your game server's IP
|
||||
ip_blacklist: [], //These IP are blocked forever
|
||||
ip_whitelist: ["127.0.0.1"], //These IP are not affected by the limiter, for example you could add your game server's IP
|
||||
ip_blacklist: [], //These IP are blocked forever
|
||||
|
||||
//Email config, required for the API to send emails
|
||||
smtp_enabled: false,
|
||||
smtp_name: "TCG Engine", //Name of sender in emails
|
||||
smtp_email: "", //Email used to send
|
||||
smtp_server: "", //SMTP server URL
|
||||
smtp_name: "TCG Engine", //Name of sender in emails
|
||||
smtp_email: "", //Email used to send
|
||||
smtp_server: "", //SMTP server URL
|
||||
smtp_port: "465",
|
||||
smtp_user: "", //SMTP auth user
|
||||
smtp_password: "", //SMTP auth password
|
||||
smtp_user: "", //SMTP auth user
|
||||
smtp_password: "", //SMTP auth password
|
||||
|
||||
//ELO settings
|
||||
elo_k: 32, //Higher K number will affect elo more each match
|
||||
elo_ini_k: 128, //K value for the first X matches can be higher
|
||||
elo_ini_match: 5, //X number of match for the previous value
|
||||
elo_k: 32, //Higher K number will affect elo more each match
|
||||
elo_ini_k: 128, //K value for the first X matches can be higher
|
||||
elo_ini_match: 5, //X number of match for the previous value
|
||||
|
||||
//New Users
|
||||
start_coins: 5000,
|
||||
@@ -65,13 +65,13 @@ module.exports = {
|
||||
start_elo: 1000,
|
||||
|
||||
//Match Rewards
|
||||
coins_victory: 200, //Victory coins reward
|
||||
coins_defeat: 100, //Defeat coins reward
|
||||
xp_victory: 100, //Victory xp reward
|
||||
xp_defeat: 50, //Defeat xp reward
|
||||
coins_victory: 200, //Victory coins reward
|
||||
coins_defeat: 100, //Defeat coins reward
|
||||
xp_victory: 100, //Victory xp reward
|
||||
xp_defeat: 50, //Defeat xp reward
|
||||
|
||||
//Market
|
||||
sell_ratio: 0.8, //Sell ratio compared to buy price
|
||||
sell_ratio: 0.8, //Sell ratio compared to buy price
|
||||
avatar_cost: 500,
|
||||
cardback_cost: 1000,
|
||||
};
|
||||
|
||||
@@ -22,8 +22,8 @@ GET /api/tasks
|
||||
"value1": 1,
|
||||
"value2": "",
|
||||
"value3": "",
|
||||
"rewardTypes": [0],
|
||||
"rewardNums": [100],
|
||||
"rewardTypes": [0 , 1],
|
||||
"rewardNums": [100 , 6],
|
||||
"isDailyTask": true,
|
||||
"durationHours": 24
|
||||
},
|
||||
@@ -109,6 +109,7 @@ POST /api/tasks/{userId}
|
||||
| 值 | 名称 | 描述 |
|
||||
|---|------|------|
|
||||
| 0 | Coins | 金币 |
|
||||
| 1 | Crystal | 钻石 |
|
||||
|
||||
### 任务状态 (TaskStatus)
|
||||
| 值 | 名称 | 描述 |
|
||||
|
||||
@@ -7,8 +7,8 @@
|
||||
"value1": 1,
|
||||
"value2": "",
|
||||
"value3": "",
|
||||
"rewardTypes": [0],
|
||||
"rewardNums": [100],
|
||||
"rewardTypes": [0 , 1],
|
||||
"rewardNums": [100 , 6],
|
||||
"isDailyTask": true,
|
||||
"durationHours": 24
|
||||
},
|
||||
|
||||
@@ -10,13 +10,13 @@ const playerTaskSchema = new Schema({
|
||||
assignedTime: { type: Date, required: true },
|
||||
expireTime: { type: Date, required: true },
|
||||
status: { type: Number, required: true, default: 0 }, // 0: Active, 1: Completed, 2: Expired, 3: Claimed
|
||||
progress: { type: Number, required: true, default: 0 }
|
||||
progress: { type: Number, required: true, default: 0 },
|
||||
});
|
||||
|
||||
const playerTaskDataSchema = new Schema({
|
||||
userId: { type: String, required: true, index: true },
|
||||
tasks: [playerTaskSchema],
|
||||
lastDailyTaskAssigned: { type: Date, default: null }
|
||||
lastDailyTaskAssigned: { type: Date, default: null },
|
||||
});
|
||||
|
||||
playerTaskDataSchema.methods.toObj = function () {
|
||||
@@ -39,7 +39,7 @@ exports.getAllTaskConfigs = async () => {
|
||||
|
||||
// Get task configuration by ID
|
||||
exports.getTaskConfigById = async (taskId) => {
|
||||
return taskConfigurations.find(task => task.id === taskId);
|
||||
return taskConfigurations.find((task) => task.id === taskId);
|
||||
};
|
||||
|
||||
// Get player tasks
|
||||
@@ -50,7 +50,7 @@ exports.getPlayerTasks = async (userId) => {
|
||||
playerTaskData = new PlayerTaskData({
|
||||
userId: userId,
|
||||
tasks: [],
|
||||
lastDailyTaskAssigned: null
|
||||
lastDailyTaskAssigned: null,
|
||||
});
|
||||
await playerTaskData.save();
|
||||
}
|
||||
@@ -80,11 +80,13 @@ exports.savePlayerTasks = async (playerTaskData) => {
|
||||
exports.initializeTaskConfig = (configPath) => {
|
||||
try {
|
||||
if (fs.existsSync(configPath)) {
|
||||
const configFile = fs.readFileSync(configPath, 'utf8');
|
||||
const configFile = fs.readFileSync(configPath, "utf8");
|
||||
taskConfigurations = JSON.parse(configFile);
|
||||
console.log(`Loaded ${taskConfigurations.length} task configurations`);
|
||||
} else {
|
||||
console.log("Task configuration file not found, using empty configuration");
|
||||
console.log(
|
||||
"Task configuration file not found, using empty configuration"
|
||||
);
|
||||
taskConfigurations = [];
|
||||
}
|
||||
} catch (e) {
|
||||
|
||||
@@ -1,194 +1,195 @@
|
||||
const UserModel = require("./users.model");
|
||||
const PackModel = require("../packs/packs.model");
|
||||
const CardModel = require("../cards/cards.model");
|
||||
const VariantModel = require('../variants/variants.model');
|
||||
const VariantModel = require("../variants/variants.model");
|
||||
const UserTool = require("./users.tool");
|
||||
const CardTool = require("../cards/cards.tool");
|
||||
const Activity = require("../activity/activity.model");
|
||||
const config = require('../config');
|
||||
const config = require("../config");
|
||||
|
||||
exports.UpdateDeck = async(req, res) => {
|
||||
exports.UpdateDeck = async (req, res) => {
|
||||
if (!req.params.deckId)
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
if(!req.params.deckId)
|
||||
return res.status(400).send({error: "Invalid parameters"});
|
||||
var userId = req.jwt.userId;
|
||||
var deckId = req.params.deckId;
|
||||
|
||||
var userId = req.jwt.userId;
|
||||
var deckId = req.params.deckId;
|
||||
var ndeck = {
|
||||
tid: req.params.deckId,
|
||||
title: req.body.title || "Deck",
|
||||
cover: req.body.cover || "",
|
||||
hero: req.body.hero || {},
|
||||
cards: req.body.cards || [],
|
||||
};
|
||||
|
||||
var ndeck = {
|
||||
tid: req.params.deckId,
|
||||
title: req.body.title || "Deck",
|
||||
hero: req.body.hero || {},
|
||||
cards: req.body.cards || [],
|
||||
};
|
||||
var user = await UserModel.getById(userId);
|
||||
if (!user)
|
||||
return res.status(404).send({ error: "User not found: " + userId });
|
||||
|
||||
var user = await UserModel.getById(userId);
|
||||
if(!user)
|
||||
return res.status(404).send({error: "User not found: " + userId});
|
||||
|
||||
var decks = user.decks || [];
|
||||
var found = false;
|
||||
var index = 0;
|
||||
for(var i=0; i<decks.length; i++){
|
||||
var deck = decks[i];
|
||||
if(deck.tid == deckId)
|
||||
{
|
||||
decks[i]= ndeck;
|
||||
found = true;
|
||||
index = i;
|
||||
}
|
||||
var decks = user.decks || [];
|
||||
var found = false;
|
||||
var index = 0;
|
||||
for (var i = 0; i < decks.length; i++) {
|
||||
var deck = decks[i];
|
||||
if (deck.tid == deckId) {
|
||||
decks[i] = ndeck;
|
||||
found = true;
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
|
||||
//Add new
|
||||
if(!found && ndeck.cards.length > 0)
|
||||
decks.push(ndeck);
|
||||
//Add new
|
||||
if (!found && ndeck.cards.length > 0) decks.push(ndeck);
|
||||
|
||||
//Delete deck
|
||||
if(found && ndeck.cards.length == 0)
|
||||
decks.splice(index, 1);
|
||||
//Delete deck
|
||||
if (found && ndeck.cards.length == 0) decks.splice(index, 1);
|
||||
|
||||
var userData = { decks: decks};
|
||||
var upUser = await UserModel.update(user, userData);
|
||||
if (!upUser) return res.status(500).send({ error: "Error updating user: " + userId });
|
||||
var userData = { decks: decks };
|
||||
var upUser = await UserModel.update(user, userData);
|
||||
if (!upUser)
|
||||
return res.status(500).send({ error: "Error updating user: " + userId });
|
||||
|
||||
return res.status(200).send(upUser.decks);
|
||||
return res.status(200).send(upUser.decks);
|
||||
};
|
||||
|
||||
exports.DeleteDeck = async(req, res) => {
|
||||
exports.DeleteDeck = async (req, res) => {
|
||||
if (!req.params.deckId)
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
if(!req.params.deckId)
|
||||
return res.status(400).send({error: "Invalid parameters"});
|
||||
var userId = req.jwt.userId;
|
||||
var deckId = req.params.deckId;
|
||||
|
||||
var userId = req.jwt.userId;
|
||||
var deckId = req.params.deckId;
|
||||
var user = await UserModel.getById(userId);
|
||||
if (!user)
|
||||
return res.status(404).send({ error: "User not found: " + userId });
|
||||
|
||||
var user = await UserModel.getById(userId);
|
||||
if(!user)
|
||||
return res.status(404).send({error: "User not found: " + userId});
|
||||
|
||||
var decks = user.decks || {};
|
||||
var index = -1;
|
||||
for(var i=0; i<decks.length; i++){
|
||||
var deck = decks[i];
|
||||
if(deck.tid == deckId)
|
||||
{
|
||||
index = i;
|
||||
}
|
||||
var decks = user.decks || {};
|
||||
var index = -1;
|
||||
for (var i = 0; i < decks.length; i++) {
|
||||
var deck = decks[i];
|
||||
if (deck.tid == deckId) {
|
||||
index = i;
|
||||
}
|
||||
}
|
||||
|
||||
if(index >= 0)
|
||||
decks.splice(index, 1);
|
||||
if (index >= 0) decks.splice(index, 1);
|
||||
|
||||
var userData = { decks: decks};
|
||||
var upUser = await UserModel.update(user, userData);
|
||||
if (!upUser) return res.status(500).send({ error: "Error updating user: " + userId });
|
||||
var userData = { decks: decks };
|
||||
var upUser = await UserModel.update(user, userData);
|
||||
if (!upUser)
|
||||
return res.status(500).send({ error: "Error updating user: " + userId });
|
||||
|
||||
return res.status(200).send(upUser.decks);
|
||||
return res.status(200).send(upUser.decks);
|
||||
};
|
||||
|
||||
// 购买卡片
|
||||
exports.BuyCard = async (req, res) => {
|
||||
|
||||
const userId = req.jwt.userId;
|
||||
const cardId = req.body.card;
|
||||
const variantId = req.body.variant;
|
||||
const quantity = req.body.quantity || 1;
|
||||
|
||||
if (!cardId || typeof cardId !== "string")
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
if(!variantId || typeof variantId !== "string")
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
if (!variantId || typeof variantId !== "string")
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
if(!Number.isInteger(quantity) || quantity <= 0)
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
if (!Number.isInteger(quantity) || quantity <= 0)
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
//Get the user add update the array
|
||||
var user = await UserModel.getById(userId);
|
||||
if (!user)
|
||||
return res.status(404).send({ error: "Cant find user " + userId });
|
||||
if (!user) return res.status(404).send({ error: "Cant find user " + userId });
|
||||
|
||||
var card = await CardModel.get(cardId);
|
||||
if (!card)
|
||||
return res.status(404).send({ error: "Cant find card " + cardId });
|
||||
if (!card) return res.status(404).send({ error: "Cant find card " + cardId });
|
||||
|
||||
if(card.cost <= 0)
|
||||
if (card.cost <= 0)
|
||||
return res.status(400).send({ error: "Can't be purchased" });
|
||||
|
||||
var variant = await VariantModel.get(variantId);
|
||||
var factor = variant != null ? variant.cost_factor : 1;
|
||||
var cost = quantity * factor * card.cost;
|
||||
if(user.cardfragments < cost)
|
||||
if (user.cardfragments < cost)
|
||||
return res.status(400).send({ error: "Not enough cardfragments" });
|
||||
|
||||
user.cardfragments -= cost;
|
||||
|
||||
var valid = await UserTool.addCards(user, [{tid: cardId, variant: variantId, quantity: quantity}]);
|
||||
if (!valid)
|
||||
return res.status(500).send({ error: "Error when adding cards" });
|
||||
var valid = await UserTool.addCards(user, [
|
||||
{ tid: cardId, variant: variantId, quantity: quantity },
|
||||
]);
|
||||
if (!valid) return res.status(500).send({ error: "Error when adding cards" });
|
||||
|
||||
//Update the user array
|
||||
var updatedUser = await UserModel.save(user, ["cardfragments", "cards"]);
|
||||
if (!updatedUser) return res.status(500).send({ error: "Error updating user: " + userId });
|
||||
if (!updatedUser)
|
||||
return res.status(500).send({ error: "Error updating user: " + userId });
|
||||
|
||||
// Activity Log -------------
|
||||
const activityData = {card: cardId, variant: variantId, quantity: quantity};
|
||||
const act = await Activity.LogActivity("user_buy_card", req.jwt.username, activityData);
|
||||
const activityData = { card: cardId, variant: variantId, quantity: quantity };
|
||||
const act = await Activity.LogActivity(
|
||||
"user_buy_card",
|
||||
req.jwt.username,
|
||||
activityData
|
||||
);
|
||||
if (!act) return res.status(500).send({ error: "Failed to log activity!!" });
|
||||
|
||||
// -------------
|
||||
return res.status(200).send();
|
||||
|
||||
};
|
||||
|
||||
exports.SellCard = async (req, res) => {
|
||||
|
||||
const userId = req.jwt.userId;
|
||||
const cardId = req.body.card;
|
||||
const variantId = req.body.variant;
|
||||
const quantity = req.body.quantity || 1;
|
||||
|
||||
if (!cardId || typeof cardId !== "string")
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
if(!variantId || typeof variantId !== "string")
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
if(!Number.isInteger(quantity) || quantity <= 0)
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
if (!variantId || typeof variantId !== "string")
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
if (!Number.isInteger(quantity) || quantity <= 0)
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
//Get the user add update the array
|
||||
var user = await UserModel.getById(userId);
|
||||
if (!user)
|
||||
return res.status(404).send({ error: "Cant find user " + userId });
|
||||
if (!user) return res.status(404).send({ error: "Cant find user " + userId });
|
||||
|
||||
var card = await CardModel.get(cardId);
|
||||
if (!card)
|
||||
return res.status(404).send({ error: "Cant find card " + cardId });
|
||||
if (!card) return res.status(404).send({ error: "Cant find card " + cardId });
|
||||
|
||||
if(card.cost <= 0)
|
||||
return res.status(400).send({ error: "Can't be sold" });
|
||||
if (card.cost <= 0) return res.status(400).send({ error: "Can't be sold" });
|
||||
|
||||
var variant = await VariantModel.get(variantId);
|
||||
|
||||
if(!UserTool.hasCard(user, cardId, variantId, quantity))
|
||||
if (!UserTool.hasCard(user, cardId, variantId, quantity))
|
||||
return res.status(400).send({ error: "Not enough cards" });
|
||||
|
||||
var factor = variant != null ? variant.cost_factor : 1;
|
||||
var cost = quantity * Math.round(card.cost * factor * config.sell_ratio);
|
||||
user.cardfragments += cost;
|
||||
|
||||
var valid = await UserTool.addCards(user, [{tid: cardId, variant: variantId, quantity: -quantity}]);
|
||||
var valid = await UserTool.addCards(user, [
|
||||
{ tid: cardId, variant: variantId, quantity: -quantity },
|
||||
]);
|
||||
if (!valid)
|
||||
return res.status(500).send({ error: "Error when removing cards" });
|
||||
|
||||
//Update the user array
|
||||
var updatedUser = await UserModel.save(user, ["cardfragments", "cards"]);
|
||||
if (!updatedUser) return res.status(500).send({ error: "Error updating user: " + userId });
|
||||
if (!updatedUser)
|
||||
return res.status(500).send({ error: "Error updating user: " + userId });
|
||||
|
||||
// Activity Log -------------
|
||||
const activityData = {card: cardId, variant: variantId, quantity: quantity};
|
||||
const act = await Activity.LogActivity("user_sell_card", req.jwt.username, activityData);
|
||||
const activityData = { card: cardId, variant: variantId, quantity: quantity };
|
||||
const act = await Activity.LogActivity(
|
||||
"user_sell_card",
|
||||
req.jwt.username,
|
||||
activityData
|
||||
);
|
||||
if (!act) return res.status(500).send({ error: "Failed to log activity!!" });
|
||||
|
||||
// -------------
|
||||
@@ -196,60 +197,57 @@ exports.SellCard = async (req, res) => {
|
||||
};
|
||||
|
||||
exports.SellDuplicateCards = async (req, res) => {
|
||||
|
||||
const userId = req.jwt.userId;
|
||||
const rarityId = req.body.rarity || ""; //If not set, will sell cards of all rarities
|
||||
const rarityId = req.body.rarity || ""; //If not set, will sell cards of all rarities
|
||||
const variantId = req.body.variant || ""; //If not set, will sell cards of all variants
|
||||
const keep = req.body.keep; //Number of copies to keep
|
||||
|
||||
if(typeof rarityId !== "string")
|
||||
if (typeof rarityId !== "string")
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
if(typeof variantId !== "string")
|
||||
if (typeof variantId !== "string")
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
if(!Number.isInteger(keep) || keep < 0)
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
if (!Number.isInteger(keep) || keep < 0)
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
//Get the user add update the array
|
||||
var user = await UserModel.getById(userId);
|
||||
if (!user)
|
||||
return res.status(404).send({ error: "Cant find user " + userId });
|
||||
if (!user) return res.status(404).send({ error: "Cant find user " + userId });
|
||||
|
||||
var all_variants = await VariantModel.getAll();
|
||||
if (!all_variants)
|
||||
return res.status(404).send({ error: "Cant find variants" });
|
||||
|
||||
var all_cards = await CardModel.getAll();
|
||||
if (!all_cards)
|
||||
return res.status(404).send({ error: "Cant find cards" });
|
||||
if (!all_cards) return res.status(404).send({ error: "Cant find cards" });
|
||||
|
||||
var cards_to_sell = [];
|
||||
var cardfragments = 0;
|
||||
for(var i=0; i<user.cards.length; i++)
|
||||
{
|
||||
for (var i = 0; i < user.cards.length; i++) {
|
||||
var card = user.cards[i];
|
||||
var card_data = UserTool.getData(all_cards, card.tid);
|
||||
if(card_data && card_data.cost > 0 && card.quantity > keep)
|
||||
{
|
||||
if(!variantId || card.variant == variantId)
|
||||
{
|
||||
if(!rarityId || card_data.rarity == rarityId)
|
||||
{
|
||||
var variant = UserTool.getData(all_variants, card.variant);
|
||||
var quantity = card.quantity - keep;
|
||||
var sell = {tid: card.tid, variant: card.variant, quantity: -quantity};
|
||||
var factor = variant != null ? variant.cost_factor : 1;
|
||||
var cost = quantity * Math.round(card_data.cost * factor * config.sell_ratio);
|
||||
cards_to_sell.push(sell);
|
||||
cardfragments += cost;
|
||||
if (card_data && card_data.cost > 0 && card.quantity > keep) {
|
||||
if (!variantId || card.variant == variantId) {
|
||||
if (!rarityId || card_data.rarity == rarityId) {
|
||||
var variant = UserTool.getData(all_variants, card.variant);
|
||||
var quantity = card.quantity - keep;
|
||||
var sell = {
|
||||
tid: card.tid,
|
||||
variant: card.variant,
|
||||
quantity: -quantity,
|
||||
};
|
||||
var factor = variant != null ? variant.cost_factor : 1;
|
||||
var cost =
|
||||
quantity * Math.round(card_data.cost * factor * config.sell_ratio);
|
||||
cards_to_sell.push(sell);
|
||||
cardfragments += cost;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(cards_to_sell.length == 0)
|
||||
return res.status(200).send();
|
||||
if (cards_to_sell.length == 0) return res.status(200).send();
|
||||
|
||||
user.cardfragments += cardfragments;
|
||||
|
||||
@@ -259,11 +257,16 @@ exports.SellDuplicateCards = async (req, res) => {
|
||||
|
||||
//Update the user array
|
||||
var updatedUser = await UserModel.save(user, ["cardfragments", "cards"]);
|
||||
if (!updatedUser) return res.status(500).send({ error: "Error updating user: " + userId });
|
||||
if (!updatedUser)
|
||||
return res.status(500).send({ error: "Error updating user: " + userId });
|
||||
|
||||
// Activity Log -------------
|
||||
const activityData = {rarity: rarityId, variant: variantId, keep: keep};
|
||||
const act = await Activity.LogActivity("user_sell_cards_duplicate", req.jwt.username, activityData);
|
||||
const activityData = { rarity: rarityId, variant: variantId, keep: keep };
|
||||
const act = await Activity.LogActivity(
|
||||
"user_sell_cards_duplicate",
|
||||
req.jwt.username,
|
||||
activityData
|
||||
);
|
||||
if (!act) return res.status(500).send({ error: "Failed to log activity!!" });
|
||||
|
||||
// -------------
|
||||
@@ -272,160 +275,169 @@ exports.SellDuplicateCards = async (req, res) => {
|
||||
|
||||
// 购买卡包 TODO 金币&钻石
|
||||
exports.BuyPack = async (req, res) => {
|
||||
|
||||
const userId = req.jwt.userId;
|
||||
const packId = req.body.pack;
|
||||
const quantity = req.body.quantity || 1;
|
||||
|
||||
if (!packId || typeof packId !== "string")
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
if(!Number.isInteger(quantity) || quantity <= 0)
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
if (!Number.isInteger(quantity) || quantity <= 0)
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
//Get the user add update the array
|
||||
var user = await UserModel.getById(userId);
|
||||
if (!user)
|
||||
return res.status(404).send({ error: "Cant find user " + userId });
|
||||
if (!user) return res.status(404).send({ error: "Cant find user " + userId });
|
||||
|
||||
var pack = await PackModel.get(packId);
|
||||
if (!pack)
|
||||
return res.status(404).send({ error: "Cant find pack " + packId });
|
||||
if (!pack) return res.status(404).send({ error: "Cant find pack " + packId });
|
||||
|
||||
if(pack.cost <= 0)
|
||||
if (pack.cost <= 0)
|
||||
return res.status(400).send({ error: "Can't be purchased" });
|
||||
|
||||
var cost = quantity * pack.cost;
|
||||
if(user.coins < cost)
|
||||
if (user.coins < cost)
|
||||
return res.status(400).send({ error: "Not enough coins" });
|
||||
|
||||
user.coins -= cost;
|
||||
|
||||
var valid = await UserTool.addPacks(user, [{tid: packId, quantity: quantity}]);
|
||||
if (!valid)
|
||||
return res.status(500).send({ error: "Error when adding packs" });
|
||||
var valid = await UserTool.addPacks(user, [
|
||||
{ tid: packId, quantity: quantity },
|
||||
]);
|
||||
if (!valid) return res.status(500).send({ error: "Error when adding packs" });
|
||||
|
||||
//Update the user array
|
||||
var updatedUser = await UserModel.save(user, ["coins", "packs"]);
|
||||
if (!updatedUser) return res.status(500).send({ error: "Error updating user: " + userId });
|
||||
if (!updatedUser)
|
||||
return res.status(500).send({ error: "Error updating user: " + userId });
|
||||
|
||||
// Activity Log -------------
|
||||
const activityData = {pack: packId, quantity: quantity};
|
||||
const act = await Activity.LogActivity("user_buy_pack", req.jwt.username, activityData);
|
||||
const activityData = { pack: packId, quantity: quantity };
|
||||
const act = await Activity.LogActivity(
|
||||
"user_buy_pack",
|
||||
req.jwt.username,
|
||||
activityData
|
||||
);
|
||||
if (!act) return res.status(500).send({ error: "Failed to log activity!!" });
|
||||
|
||||
// -------------
|
||||
return res.status(200).send();
|
||||
|
||||
};
|
||||
|
||||
exports.SellPack = async (req, res) => {
|
||||
|
||||
const userId = req.jwt.userId;
|
||||
const packId = req.body.pack;
|
||||
const quantity = req.body.quantity || 1;
|
||||
|
||||
if (!packId || typeof packId !== "string")
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
if(!Number.isInteger(quantity) || quantity <= 0)
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
if (!Number.isInteger(quantity) || quantity <= 0)
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
//Get the user add update the array
|
||||
var user = await UserModel.getById(userId);
|
||||
if (!user)
|
||||
return res.status(404).send({ error: "Cant find user " + userId });
|
||||
if (!user) return res.status(404).send({ error: "Cant find user " + userId });
|
||||
|
||||
var pack = await PackModel.get(packId);
|
||||
if (!pack)
|
||||
return res.status(404).send({ error: "Cant find pack " + packId });
|
||||
if (!pack) return res.status(404).send({ error: "Cant find pack " + packId });
|
||||
|
||||
if(pack.cost <= 0)
|
||||
return res.status(400).send({ error: "Can't be sold" });
|
||||
if (pack.cost <= 0) return res.status(400).send({ error: "Can't be sold" });
|
||||
|
||||
if(!UserTool.hasPack(user, packId, quantity))
|
||||
if (!UserTool.hasPack(user, packId, quantity))
|
||||
return res.status(400).send({ error: "Not enough coins" });
|
||||
|
||||
var cost = quantity * Math.round(pack.cost * config.sell_ratio);
|
||||
user.coins += cost;
|
||||
|
||||
var valid = await UserTool.addPacks(user, [{tid: packId, quantity: -quantity}]);
|
||||
if (!valid)
|
||||
return res.status(500).send({ error: "Error when adding packs" });
|
||||
var valid = await UserTool.addPacks(user, [
|
||||
{ tid: packId, quantity: -quantity },
|
||||
]);
|
||||
if (!valid) return res.status(500).send({ error: "Error when adding packs" });
|
||||
|
||||
//Update the user array
|
||||
var updatedUser = await UserModel.save(user, ["coins", "packs"]);
|
||||
if (!updatedUser) return res.status(500).send({ error: "Error updating user: " + userId });
|
||||
if (!updatedUser)
|
||||
return res.status(500).send({ error: "Error updating user: " + userId });
|
||||
|
||||
// Activity Log -------------
|
||||
const activityData = {pack: packId, quantity: quantity};
|
||||
const act = await Activity.LogActivity("user_sell_pack", req.jwt.username, activityData);
|
||||
const activityData = { pack: packId, quantity: quantity };
|
||||
const act = await Activity.LogActivity(
|
||||
"user_sell_pack",
|
||||
req.jwt.username,
|
||||
activityData
|
||||
);
|
||||
if (!act) return res.status(500).send({ error: "Failed to log activity!!" });
|
||||
|
||||
// -------------
|
||||
return res.status(200).send();
|
||||
|
||||
};
|
||||
|
||||
exports.OpenPack = async (req, res) => {
|
||||
|
||||
const userId = req.jwt.userId;
|
||||
const packId = req.body.pack;
|
||||
|
||||
if (!packId || typeof packId !== "string")
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
//Get the user add update the array
|
||||
var user = await UserModel.getById(userId);
|
||||
if (!user)
|
||||
return res.status(404).send({ error: "Cant find user " + userId });
|
||||
if (!user) return res.status(404).send({ error: "Cant find user " + userId });
|
||||
|
||||
var pack = await PackModel.get(packId);
|
||||
if (!pack)
|
||||
return res.status(404).send({ error: "Cant find pack " + packId });
|
||||
if (!pack) return res.status(404).send({ error: "Cant find pack " + packId });
|
||||
|
||||
if(!UserTool.hasPack(user, packId, 1))
|
||||
if (!UserTool.hasPack(user, packId, 1))
|
||||
return res.status(400).send({ error: "You don't have this pack" });
|
||||
|
||||
console.log("pack", pack);
|
||||
|
||||
var cardsToAdd = await CardTool.getPackCards(pack);
|
||||
var validCards = await UserTool.addCards(user, cardsToAdd);
|
||||
var validPacks = await UserTool.addPacks(user, [{tid: packId, quantity: -1}]);
|
||||
var validPacks = await UserTool.addPacks(user, [
|
||||
{ tid: packId, quantity: -1 },
|
||||
]);
|
||||
|
||||
console.log("getPackCards", cardsToAdd);
|
||||
console.log("validPacks", validPacks);
|
||||
|
||||
if (!validCards || !validPacks)
|
||||
return res.status(500).send({ error: "Error when adding cards" });
|
||||
|
||||
//Update the user array
|
||||
var updatedUser = await UserModel.save(user, ["cards", "packs"]);
|
||||
if (!updatedUser) return res.status(500).send({ error: "Error updating user: " + userId });
|
||||
if (!updatedUser)
|
||||
return res.status(500).send({ error: "Error updating user: " + userId });
|
||||
|
||||
// Activity Log -------------
|
||||
const activityData = {pack: packId, cards: cardsToAdd};
|
||||
const act = await Activity.LogActivity("user_open_pack", req.jwt.username, activityData);
|
||||
const activityData = { pack: packId, cards: cardsToAdd };
|
||||
const act = await Activity.LogActivity(
|
||||
"user_open_pack",
|
||||
req.jwt.username,
|
||||
activityData
|
||||
);
|
||||
if (!act) return res.status(500).send({ error: "Failed to log activity!!" });
|
||||
|
||||
// -------------
|
||||
return res.status(200).send(cardsToAdd);
|
||||
|
||||
};
|
||||
|
||||
exports.BuyAvatar = async (req, res) => {
|
||||
|
||||
const userId = req.jwt.userId;
|
||||
const avatarId = req.body.avatar;
|
||||
|
||||
if (!avatarId || typeof avatarId !== "string")
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
//Get the user add update the array
|
||||
var user = await UserModel.getById(userId);
|
||||
if (!user)
|
||||
return res.status(404).send({ error: "Cant find user " + userId });
|
||||
if (!user) return res.status(404).send({ error: "Cant find user " + userId });
|
||||
|
||||
var cost = config.avatar_cost;
|
||||
if(user.coins < cost)
|
||||
if (user.coins < cost)
|
||||
return res.status(400).send({ error: "Not enough coins" });
|
||||
|
||||
if(UserTool.hasAvatar(user, avatarId))
|
||||
if (UserTool.hasAvatar(user, avatarId))
|
||||
return res.status(400).send({ error: "Already have this avatar" });
|
||||
|
||||
user.coins -= cost;
|
||||
@@ -433,34 +445,37 @@ exports.BuyAvatar = async (req, res) => {
|
||||
|
||||
//Update the user array
|
||||
var updatedUser = await UserModel.save(user, ["coins", "avatars"]);
|
||||
if (!updatedUser) return res.status(500).send({ error: "Error updating user: " + userId });
|
||||
if (!updatedUser)
|
||||
return res.status(500).send({ error: "Error updating user: " + userId });
|
||||
|
||||
// Activity Log -------------
|
||||
const activityData = {avatar: avatarId};
|
||||
const act = await Activity.LogActivity("user_buy_avatar", req.jwt.username, activityData);
|
||||
const activityData = { avatar: avatarId };
|
||||
const act = await Activity.LogActivity(
|
||||
"user_buy_avatar",
|
||||
req.jwt.username,
|
||||
activityData
|
||||
);
|
||||
if (!act) return res.status(500).send({ error: "Failed to log activity!!" });
|
||||
|
||||
return res.status(200).send();
|
||||
};
|
||||
|
||||
exports.BuyCardback = async (req, res) => {
|
||||
|
||||
const userId = req.jwt.userId;
|
||||
const cardbackId = req.body.cardback;
|
||||
|
||||
if (!cardbackId || typeof cardbackId !== "string")
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
return res.status(400).send({ error: "Invalid parameters" });
|
||||
|
||||
//Get the user add update the array
|
||||
var user = await UserModel.getById(userId);
|
||||
if (!user)
|
||||
return res.status(404).send({ error: "Cant find user " + userId });
|
||||
if (!user) return res.status(404).send({ error: "Cant find user " + userId });
|
||||
|
||||
var cost = config.cardback_cost;
|
||||
if(user.coins < cost)
|
||||
if (user.coins < cost)
|
||||
return res.status(400).send({ error: "Not enough coins" });
|
||||
|
||||
if(UserTool.hasCardback(user, cardbackId))
|
||||
if (UserTool.hasCardback(user, cardbackId))
|
||||
return res.status(400).send({ error: "Already have this cardback" });
|
||||
|
||||
user.coins -= cost;
|
||||
@@ -468,19 +483,23 @@ exports.BuyCardback = async (req, res) => {
|
||||
|
||||
//Update the user array
|
||||
var updatedUser = await UserModel.save(user, ["coins", "cardbacks"]);
|
||||
if (!updatedUser) return res.status(500).send({ error: "Error updating user: " + userId });
|
||||
if (!updatedUser)
|
||||
return res.status(500).send({ error: "Error updating user: " + userId });
|
||||
|
||||
// Activity Log -------------
|
||||
const activityData = {cardback: cardbackId};
|
||||
const act = await Activity.LogActivity("user_buy_cardback", req.jwt.username, activityData);
|
||||
const activityData = { cardback: cardbackId };
|
||||
const act = await Activity.LogActivity(
|
||||
"user_buy_cardback",
|
||||
req.jwt.username,
|
||||
activityData
|
||||
);
|
||||
if (!act) return res.status(500).send({ error: "Failed to log activity!!" });
|
||||
|
||||
return res.status(200).send();
|
||||
};
|
||||
|
||||
//Fix variant from previous version
|
||||
exports.FixVariants = async (req, res) =>
|
||||
{
|
||||
exports.FixVariants = async (req, res) => {
|
||||
var from = req.body.from || "";
|
||||
var to = req.body.to || "";
|
||||
|
||||
@@ -494,30 +513,25 @@ exports.FixVariants = async (req, res) =>
|
||||
var default_tid = default_variant ? default_variant.tid : "";
|
||||
var count = 0;
|
||||
|
||||
for(var u=0; u<users.length; u++)
|
||||
{
|
||||
for (var u = 0; u < users.length; u++) {
|
||||
var user = users[u];
|
||||
var changed = false;
|
||||
for(var i=0; i<user.cards.length; i++)
|
||||
{
|
||||
for (var i = 0; i < user.cards.length; i++) {
|
||||
var card = user.cards[i];
|
||||
if(!card.variant)
|
||||
{
|
||||
if (!card.variant) {
|
||||
card.variant = default_tid;
|
||||
changed = true;
|
||||
}
|
||||
if(from && to && card.variant == from)
|
||||
{
|
||||
if (from && to && card.variant == from) {
|
||||
card.variant = to;
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
|
||||
if(changed)
|
||||
{
|
||||
if (changed) {
|
||||
var new_cards = user.cards;
|
||||
user.cards = [];
|
||||
await UserTool.addCards(user, new_cards); //Re-add in correct format
|
||||
await UserTool.addCards(user, new_cards); //Re-add in correct format
|
||||
UserModel.save(user, ["cards"]);
|
||||
count++;
|
||||
}
|
||||
@@ -527,5 +541,5 @@ exports.FixVariants = async (req, res) =>
|
||||
const act = await Activity.LogActivity("fix_variants", req.jwt.username, {});
|
||||
if (!act) return res.status(500).send({ error: "Failed to log activity!!" });
|
||||
|
||||
return res.status(200).send({updated: count});
|
||||
}
|
||||
return res.status(200).send({ updated: count });
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user