Skip to content

Commit

Permalink
cake
Browse files Browse the repository at this point in the history
  • Loading branch information
Hri7566 committed Aug 30, 2022
1 parent 865bfe7 commit 8fb5222
Show file tree
Hide file tree
Showing 6 changed files with 303 additions and 9 deletions.
31 changes: 31 additions & 0 deletions .vscode/cosmic.code-snippets
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
// Place your cosmic workspace snippets here. Each snippet is defined under a snippet name and has a scope, prefix, body and
// description. Add comma separated ids of the languages where the snippet is applicable in the scope field. If scope
// is left empty or omitted, the snippet gets applied to all languages. The prefix is what is
// used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders.
// Placeholders with the same ids are connected.
// Example:
// "Print to console": {
// "scope": "javascript,typescript",
// "prefix": "log",
// "body": [
// "console.log('$1');",
// "$2"
// ],
// "description": "Log output to console"
// }

"cheader": {
"scope": "javascript,typescript",
"prefix": "cheader",
"body": [
"/**",
" * COSMIC PROJECT",
" * ",
" * $0",
" */"
],
"description": "Cosmic header"
}
}
20 changes: 20 additions & 0 deletions src/CosmicCakeFactory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* COSMIC PROJECT
*
* Cake factory module
*/

const { Cake, FoodItem, Item } = require('./CosmicTypes');

const { cakes } = require('./CosmicCakes');

class CosmicCakeFactory {
static generateRandomCake() {
let c = cakes[Math.floor(Math.random() * cakes.length)];
return c;
}
}

export {
CosmicCakeFactory
}
106 changes: 106 additions & 0 deletions src/CosmicCakes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* COSMIC PROJECT
*
* Cakes
*/

const { Cake } = require('./CosmicTypes');

export const cakes: Array<typeof Cake> = [
{
id: 'cake_chocolate',
displayName: 'Chocolate Cake',
emoji: '🎂',
count: 1,
edible: true,
icing: 'chocolate',
filling: 'chocolate'
},
{
id: 'cake_vanilla',
displayName: 'Vanilla Cake',
emoji: '🍰',
count: 1,
edible: true,
icing: 'white',
filling: 'vanilla',
topping: 'cherry'
},
{
id: 'cupcake_strawberry',
displayName: 'Strawberry Cupcake',
emoji: '🧁',
count: 1,
edible: true,
icing: 'pink',
filling: 'chocolate',
topping: 'strawberry'
},
{
id: 'cake_angel',
displayName: 'Angel Food Cake',
emoji: '😇',
count: 1,
edible: true,
icing: 'white',
filling: 'vanilla',
topping: 'fruit'
},
{
id: 'cake_carrot',
displayName: 'Carrot Cake',
emoji: '🥕',
count: 1,
edible: true,
icing: 'white',
filling: 'carrot'
},
{
id: 'cake_velvet',
displayName: 'Red Velvet Cake',
emoji: '❤',
count: 1,
edible: true,
icing: 'white',
filling: 'red'
},
{
id: 'cake_sponge',
displayName: 'Sponge Cake',
emoji: '🧽',
count: 1,
edible: true,
icing: 'none',
filling: 'lemon'
},
{
id: 'cake_pineapple',
displayName: 'Pineapple Upside-Down Cake',
emoji: '🍍',
count: 1,
edible: true,
icing: 'none',
filling: 'pineapple',
topping: 'pineapple'
},
{
id: 'cake_fruit',
displayName: 'Fruit Cake',
emoji: '🎄',
count: 1,
edible: true,
icing: 'none',
filling: 'fruit',
topping: 'nuts'
},
{
id: 'cake_birthday',
displayName: 'Birthday Cake',
emoji: '🎂',
count: 1,
edible: true,
icing: 'chocolate',
filling: 'chocolate',
topping: 'candle'
}
];
68 changes: 65 additions & 3 deletions src/CosmicCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
* Cosmic commands
*/

import { CosmicCakeFactory } from "./CosmicCakeFactory";

const { Command, CosmicCommandHandler } = require('./CosmicCommandHandler');
const { CosmicColor } = require('./CosmicColor');
const { CosmicData } = require('./CosmicData');
Expand Down Expand Up @@ -211,12 +213,13 @@ CosmicCommandHandler.registerCommand(new Command(

for (let it of inventory.items) {
if (it.count > 0) {
items.push(`${it.displayName}${it.count > 1 ? ` (x${it.count})` : ``}`);
let dn = (it.emoji ? it.emoji : '') + it.displayName
items.push(`${dn}${it.count > 1 ? ` (x${it.count})` : ``}`);
}
}

if (items.length > 0) {
return `Inventory: ${items.join('')}`;
return `Inventory: ${items.join(', ')}`;
} else {
return `Inventory: (empty)`;
}
Expand All @@ -241,7 +244,7 @@ CosmicCommandHandler.registerCommand(new Command(
'wipeinv',
[ 'wipeinv' ],
'%PREFIX%wipeinv',
`Wipe inventory data.`,
`Wipe all inventory data.`,
[ 'admin' ],
false,
'info',
Expand All @@ -250,3 +253,62 @@ CosmicCommandHandler.registerCommand(new Command(
return `Inventories purged successfully.`;
}
));

CosmicCommandHandler.registerCommand(new Command(
'rcake',
[ 'rcake' ],
'%PREFIX%rcake',
`Generate a random cake.`,
[ 'admin' ],
false,
'cake',
async (msg, cl) => {
let c = CosmicCakeFactory.generateRandomCake();

let res = await CosmicData.addItem(msg.sender._id, c);
console.debug(res);

let displayName = (c.emoji ? `${c.emoji}` : '') + c.displayName;
return `Cake: ${displayName}`;
}
));

CosmicCommandHandler.registerCommand(new Command(
'addbal',
[ 'addbal' ],
'%PREFIX%addbal <userId> <amount>',
`Add an amount to a balance.`,
[ 'admin' ],
false,
'cake',
async (msg, cl) => {
if (msg.argv[2]) {
try {
await CosmicData.addBalance(msg.argv[1], parseInt(msg.argv[2]));
return `Successfully added to balance of account '${msg.argv[1]}'.`;
} catch (err) {
return `Addbal failed. (maybe NaN?)`;
}
}
}
));

CosmicCommandHandler.registerCommand(new Command(
'subbal',
[ 'subbal' ],
'%PREFIX%subbal <userId> <amount>',
`Add an amount to a balance.`,
[ 'admin' ],
false,
'cake',
async (msg, cl) => {
if (msg.argv[2]) {
try {
await CosmicData.removeBalance(msg.argv[1], parseInt(msg.argv[2]));
return `Successfully removed from balance of account '${msg.argv[1]}'.`;
} catch (err) {
return `Addbal failed. (maybe NaN?)`;
}
}
}
));
84 changes: 79 additions & 5 deletions src/CosmicData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,48 @@ class CosmicData {
}
}

public static async hasItem(_id: string, item_id) {
try {
const response = await this.inventories.find({
_id,
items: {
$in: [{
$id: item_id
}]
}
});

console.debug('--- has item debug');
console.debug(response);

return response.matchedCount > 0;
} catch (err) {
return err;
}
}

public static async addItem(_id: string, item: typeof Item) {
try {
const result = await this.inventories.updateOne({
$push: { "groups": item }
if (await this.hasItem(_id, item.id)) {
console.debug('already has item');
let res = await this.inventories.updateOne({
_id,
items: {
id: item.id
}
}, {
$inc: {
'items.$': {
count: item.count
}
}
});
return res;
}
const result = await this.inventories.updateOne({ _id }, {
$push: {
items: item
}
});

return result;
Expand All @@ -184,11 +222,23 @@ class CosmicData {
}
}

public static async removeItem(_id: string, item_id) {
// TODO
public static async removeItem(_id: string, item_id: string) {
try {
let inventory = await this.getInventory(_id);

for (let it of inventory.items) {
if (it.id == item_id) {
inventory.items.splice(inventory.items.indexOf(it), 1);
}
}

// return result;
} catch (err) {
return err;
}
}

public static async getInventory(_id: string) {
public static async getInventory(_id: string): Promise<typeof Inventory | any> {
try {
const result = await this.inventories.findOne({ _id: _id });

Expand All @@ -205,6 +255,30 @@ class CosmicData {
return `${bal.toFixed(fixate)}${currency}`;
}
}

public static addBalance(_id: string, amount: number) {
try {
this.inventories.updateOne({ _id }, {
$inc: {
balance: amount
}
})
} catch (err) {
return err;
}
}

public static subtractBalance(_id: string, amount: number) {
try {
this.inventories.updateOne({ _id }, {
$dec: {
balance: amount
}
})
} catch (err) {
return err;
}
}
}

export {
Expand Down
3 changes: 2 additions & 1 deletion src/CosmicTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface Item {
displayName: string;
count: number;
emoji?: string;
value?: number;
}

export interface FoodItem {
Expand Down Expand Up @@ -186,6 +187,6 @@ namespace Cosmic {
export interface Cake extends FoodItem {
icing: string;
filling: string;
candy?: string;
topping?: string;
}
}

0 comments on commit 8fb5222

Please sign in to comment.