Skip to content

Commit 1fa6bac

Browse files
authored
Merge pull request #5 from mkwng-org/block-mythical-oneofone-updates
Block user from adding features only available through random mints
2 parents aab6b4f + 0f84c4f commit 1fa6bac

File tree

4 files changed

+140
-5
lines changed

4 files changed

+140
-5
lines changed

data/helpers.test.ts

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,103 @@ describe("getDifferences", () => {
129129
}
130130
])
131131
});
132+
it("calculates mythic and oneofone costs properly", async () => {
133+
let newCompanion = JSON.parse(JSON.stringify(oldCompanion));
134+
newCompanion.attributes.top.name = "tattooshirt";
135+
newCompanion.attributes.headwear = {
136+
name: "hijab",
137+
color: [
138+
{
139+
"r": 163,
140+
"g": 192,
141+
"b": 191
142+
}
143+
]
144+
}
145+
let differences = getDifferences(oldCompanion, newCompanion)
146+
expect(differences).toEqual([
147+
{
148+
cost: 500000,
149+
prev: 'earphones',
150+
curr: 'hijab',
151+
key: 'headwear',
152+
type: 'attribute'
153+
},
154+
{
155+
"cost": 250,
156+
"curr": "lightblue",
157+
"key": "headwearColor1",
158+
"prev": "",
159+
"type": "color",
160+
},
161+
{
162+
cost: 500000,
163+
prev: 'buttonup',
164+
curr: 'tattooshirt',
165+
key: 'top',
166+
type: 'attribute'
167+
},
168+
{
169+
cost: 0,
170+
prev: 'lightblue',
171+
curr: '',
172+
key: 'topColor1',
173+
type: 'color'
174+
}
175+
])
176+
});
177+
178+
it("calculates pose costs correctly", async () => {
179+
let newCompanion = JSON.parse(JSON.stringify(oldCompanion));
180+
newCompanion.properties.pose = 3;
181+
182+
let differences = getDifferences(oldCompanion, newCompanion)
183+
expect(differences).toEqual(
184+
[{
185+
"cost": 500,
186+
"curr": 3,
187+
"key": "pose",
188+
"prev": 2,
189+
"type": "property"
190+
}])
191+
192+
newCompanion.properties.pose = 4;
193+
194+
differences = getDifferences(oldCompanion, newCompanion)
195+
expect(differences).toEqual(
196+
[{
197+
"cost": 500000,
198+
"curr": 4,
199+
"key": "pose",
200+
"prev": 2,
201+
"type": "property"
202+
}])
203+
});
204+
205+
it("calculates gender costs correctly", async () => {
206+
let newCompanion = JSON.parse(JSON.stringify(oldCompanion));
207+
newCompanion.properties.gender = "f";
208+
209+
let differences = getDifferences(oldCompanion, newCompanion)
210+
expect(differences).toEqual(
211+
[{
212+
"cost": 500,
213+
"curr": "f",
214+
"key": "gender",
215+
"prev": "m",
216+
"type": "property"
217+
}])
218+
219+
newCompanion.properties.gender = "w";
220+
221+
differences = getDifferences(oldCompanion, newCompanion)
222+
expect(differences).toEqual(
223+
[{
224+
"cost": 500000,
225+
"curr": "w",
226+
"key": "gender",
227+
"prev": "m",
228+
"type": "property"
229+
}])
230+
});
132231
})

data/helpers.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ import {
66
AttributeSelection,
77
AttributeType,
88
Companion,
9+
GenderValue,
910
Layer,
1011
LayerStaticWithData,
1112
LayerWithData,
1213
Pose,
14+
Property,
1315
Rarity,
1416
Restrictions,
1517
RGBColor,
1618
Variant,
1719
} from "./types";
20+
// @ts-ignore -- .prisma/client is only generated after running migrations
1821
import { Companion as PrismaCompanion } from ".prisma/client";
1922
import _ from "lodash";
2023

@@ -629,15 +632,24 @@ export const rarityToCost = {
629632
common: 1000,
630633
uncommon: 5000,
631634
rare: 10000,
632-
mythic: 20000,
635+
// It should not be possible to buy mythics or oneofone items
636+
mythic: 500000,
637+
oneofone: 500000,
633638
};
634639

635640
const getKeyCost = (key: string, value: string) => {
636641
switch (getKeyType(key)) {
637642
case "name":
638643
return 1;
639644
case "property":
640-
return 500;
645+
switch (key) {
646+
case Property.Pose:
647+
return parseInt(value) === Pose.Headdown ? rarityToCost.mythic : 500;
648+
case Property.Gender:
649+
return value === GenderValue.W ? rarityToCost.mythic : 500;
650+
default:
651+
return 500;
652+
}
641653
case "color":
642654
return 250;
643655
case "attribute":

data/types.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ export enum Pose {
1919
Headdown,
2020
}
2121

22+
export enum Property {
23+
Gender = "gender",
24+
Pose = "pose",
25+
Skin = "skin",
26+
Hair = "hair",
27+
Background = "background"
28+
}
29+
30+
export type PropertyType = `${Property}`
31+
2232
export type AttributeType =
2333
| "background"
2434
| "bodyBack"
@@ -41,7 +51,13 @@ export type AttributeType =
4151
| "shoes"
4252
| "accessory";
4353

44-
type Gender = "m" | "f" | "w";
54+
// TODO:: GenderValue is named inconsistently for now because we have to rename Gender to GenderType
55+
export enum GenderValue {
56+
M = "m",
57+
F = "f",
58+
W = "w"
59+
}
60+
type Gender = `${GenderValue}`;
4561
type HeadShape = "big" | "flat";
4662
type ProfileShape = "flat" | "encroached";
4763
export type Rarity = "common" | "uncommon" | "rare" | "mythic" | "oneofone";

pages/api/companion/[id].ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Companion as PrismaCompanion } from ".prisma/client";
22
import { NextApiRequest, NextApiResponse } from "next";
33
import { ownerAddress } from "../../../components/contract";
44
import { colors, rgbToHex } from "../../../data/colors";
5-
import { apiToKeys, getDifferences, keysToCompanion, messageToSign, zeroPad } from "../../../data/helpers";
5+
import { apiToKeys, getDifferences, keysToCompanion, messageToSign, rarityToCost, zeroPad } from "../../../data/helpers";
66
import { updateCompanion } from "../../../data/operations";
77
import { randomCompanion } from "../../../data/random";
88
import { Companion } from "../../../data/types";
@@ -122,10 +122,18 @@ export default async function apiCompanions(req: NextApiRequest, res: NextApiRes
122122
where: { tokenId: parseInt(tokenId) },
123123
})
124124
);
125+
125126
const uneditedCompanion = keysToCompanion(apiToKeys(prismaResponse));
126127
const differences = getDifferences(uneditedCompanion, companion);
127-
const balance = coupon ? 0 : differences.reduce((acc, cur) => acc + cur.cost, 0);
128+
const costOfChanges = differences.reduce((acc, cur) => acc + cur.cost, 0);
129+
// It should be impossible to have changes that cost more than a mythic unless someone added a mythic
130+
if (costOfChanges >= rarityToCost.mythic) {
131+
throw new Error("Invalid items were added to companion");
132+
} else if (costOfChanges === 0) {
133+
throw new Error("Companion has no changes!");
134+
}
128135

136+
const balance = coupon ? 0 : costOfChanges;
129137
if (balance > 0) {
130138
let hashUsed;
131139
try {

0 commit comments

Comments
 (0)