Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions src/shop/migrations/0005_shoppurchaselog_purchased_price.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Generated by Django 3.2.12 on 2024-03-03 17:33
# Edited manually by f2koi@plus on the same date

from django.apps.registry import Apps
from django.db import migrations, models
from django.db.backends.base.schema import BaseDatabaseSchemaEditor

import shop.models as shop_models


def forwards_func(apps: Apps, schema_editor: BaseDatabaseSchemaEditor):
ShopPurchaseLog: shop_models.ShopPurchaseLog = apps.get_model(
"shop", "ShopPurchaseLog"
)
for log in ShopPurchaseLog.objects.all():
log.purchased_price = log.item.price
log.save()


class Migration(migrations.Migration):

dependencies = [
("shop", "0004_shopitem_author"),
]

operations = [
migrations.AddField(
model_name="shoppurchaselog",
name="purchased_price",
field=models.PositiveIntegerField(default=0),
),
# https://docs.djangoproject.com/en/3.2/ref/migration-operations/#runpython
migrations.RunPython(
code=forwards_func, reverse_code=migrations.RunPython.noop
),
]
6 changes: 3 additions & 3 deletions src/shop/models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import os

from django.db import models
from django.conf import settings
from django.core.files.storage import FileSystemStorage
from django.contrib.auth import get_user_model
from django.core.files.storage import FileSystemStorage
from django.db import models

from problem.models import ProblemList


User = get_user_model()


Expand Down Expand Up @@ -53,6 +52,7 @@ class ShopPurchaseLog(models.Model):
succeed = models.BooleanField()
retrieved = models.BooleanField()
purchase_time = models.DateTimeField(auto_now_add=True)
purchased_price = models.PositiveIntegerField()

class Meta:
verbose_name = "상점 구매 로그"
Expand Down
24 changes: 16 additions & 8 deletions src/shop/views.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
from decimal import Decimal
from random import SystemRandom
from functools import reduce
from random import SystemRandom

from django import forms
from django.db import IntegrityError
from django.core.exceptions import ObjectDoesNotExist
from django.db import IntegrityError
from django.http import Http404, HttpResponseBadRequest, JsonResponse
from django.shortcuts import render
from django.views import View

from website.views import PlusMemberCheck
from problem.helpers.problem_info import get_problem_list_user_info
from website.views import PlusMemberCheck

from .models import Shop, ShopItem, ShopPurchaseLog


Expand All @@ -36,7 +37,10 @@ def get(self, request, pk=None):
shop_items = shop.shop_items.filter(hidden=False)
_, user_money = get_problem_list_user_info(shop.problem_list, request.user)
purchase_log = list(
map(lambda x: x.item.price, ShopPurchaseLog.objects.filter(user=request.user, shop=shop))
map(
lambda x: x.purchased_price,
ShopPurchaseLog.objects.filter(user=request.user, shop=shop),
)
)
if purchase_log:
user_money -= reduce(lambda x, y: x + y, purchase_log)
Expand Down Expand Up @@ -75,11 +79,14 @@ def post(self, request, pk):
required_luck = Decimal(100) - item_to_buy.chance

_, user_money = get_problem_list_user_info(shop_point_source, request.user)
purchase_log = list(
map(lambda x: x.item.price, ShopPurchaseLog.objects.filter(user=request.user, shop=shop_to_visit))
purchased_prices = list(
map(
lambda x: x.purchased_price,
ShopPurchaseLog.objects.filter(user=request.user, shop=shop_to_visit),
)
)
if purchase_log:
user_money -= reduce(lambda x, y: x + y, purchase_log)
if purchased_prices:
user_money -= reduce(lambda x, y: x + y, purchased_prices)

enough_point = user_money >= required_point
enough_luck = SystemRandom().uniform(0, 100) > required_luck
Expand All @@ -95,6 +102,7 @@ def post(self, request, pk):
item=item_to_buy,
succeed=succeed_purchase,
retrieved=False,
purchased_price=required_point,
)

response["result"] = succeed_purchase
Expand Down