Skip to content

Commit 5d14f1c

Browse files
committed
added shop
1 parent 9e3b033 commit 5d14f1c

File tree

5 files changed

+160
-9
lines changed

5 files changed

+160
-9
lines changed

core/README.md

+78-6
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ GET /
77
```
88
| Parameter | Type | Description |
99
| :--- | :--- | :--- |
10-
| `token` | `string` | Your token |
1110

11+
need token
1212
**Response:**
1313
```javascript
1414
{
@@ -29,7 +29,8 @@ POST /register
2929
| `email` | `string` | |
3030

3131
**Response:**
32-
201 / 400
32+
- Invalid Form : 400
33+
- Success : 201
3334

3435
### Login
3536
```http
@@ -41,7 +42,9 @@ POST /Login
4142
| `password` | `string` | |
4243

4344
**Response:**
44-
200 / 400, 401
45+
- Invalid Form : 400
46+
- Login Failed : 401
47+
- Success : 200
4548

4649
See `Set-Cookie` header!
4750

@@ -52,9 +55,78 @@ GET /dashboard
5255
```
5356
| Parameter | Type | Description |
5457
| :--- | :--- | :--- |
55-
| `username` | `string` | |
56-
| `password` | `string` | |
5758

5859
**Response:**
59-
200 / 400, 401
60+
```javascript
61+
{
62+
"<teamname1>" : {
63+
"score" : int
64+
"attacks" : {
65+
"SQLi" : {
66+
"to_team" : string,
67+
"is_success" : bool
68+
}
69+
"XSS" : {
70+
"to_team" : string,
71+
"is_success" : bool
72+
}
73+
}
74+
},
75+
...
76+
}
77+
```
78+
79+
80+
## Shop
81+
### Shop
82+
```http
83+
GET /shop
84+
```
85+
| Parameter | Type | Description |
86+
| :--- | :--- | :--- |
87+
88+
needs token
89+
**Response:**
90+
```javascript
91+
{
92+
"money" : int,
93+
"item_list" : {
94+
"SQLi" : array
95+
"XSS" : array
96+
}
97+
}
98+
```
6099

100+
### Item_Info
101+
```http
102+
GET /shop/<item_id>
103+
```
104+
| Parameter | Type | Description |
105+
| :--- | :--- | :--- |
106+
107+
needs token
108+
**Response:**
109+
```javascript
110+
{
111+
"id" : int,
112+
"name" : string,
113+
"description" : string,
114+
"type" : string,
115+
"price" : int,
116+
"already_bought" : bool
117+
}
118+
```
119+
120+
### Item_Buy
121+
```http
122+
POST /shop/<item_id>
123+
```
124+
| Parameter | Type | Description |
125+
| :--- | :--- | :--- |
126+
127+
needs token
128+
**Response:**
129+
- No Such Item : 404
130+
- Already Bought : 409
131+
- Not Enough balance : 402
132+
- Success : 200

core/base/views.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,6 @@ def get(self, request):
7777
for team in teams:
7878
ret[team.username] = {"score": team.score}
7979
for cate in CATEGORY:
80-
ret[team.username]["attacks"] = dict([(cate, get_latest_attack(team, cate[0])) for cate in CATEGORY])
80+
ret[team.username]["attacks"] = dict([(cate[1], get_latest_attack(team, cate[0])) for cate in CATEGORY])
8181

8282
return JsonResponse(ret, status=200)

core/plt/urls.py

+1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,5 @@
1919
urlpatterns = [
2020
path('admin/', admin.site.urls),
2121
path('', include('base.urls')),
22+
path('shop/', include('shop.urls')),
2223
]

core/shop/urls.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
"""plt URL Configuration
2+
The `urlpatterns` list routes URLs to views. For more information please see:
3+
https://docs.djangoproject.com/en/3.0/topics/http/urls/
4+
Examples:
5+
Function views
6+
1. Add an import: from my_app import views
7+
2. Add a URL to urlpatterns: path('', views.home, name='home')
8+
Class-based views
9+
1. Add an import: from other_app.views import Home
10+
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
11+
Including another URLconf
12+
1. Import the include() function: from django.urls import include, path
13+
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
14+
"""
15+
from django.urls import path
16+
from . import views
17+
18+
urlpatterns = [
19+
path('', views.ShopView.as_view(), name='shop'),
20+
path('item/<int:item_id>', views.ItemView.as_view(), name='item'),
21+
]

core/shop/views.py

+59-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,60 @@
1-
from django.shortcuts import render
1+
from django.http import JsonResponse, HttpResponse
2+
from django.contrib.auth import get_user_model
3+
from django.views import View
4+
from utils.validator import unique_team_id
5+
from django.contrib.auth.mixins import LoginRequiredMixin
6+
from django.contrib.auth import authenticate, login
7+
from django.views.decorators.csrf import csrf_protect
8+
import json
29

3-
# Create your views here.
10+
from env.environ import CATEGORY
11+
12+
Team = get_user_model()
13+
14+
15+
class ShopView(LoginRequiredMixin, View):
16+
def get(self, request):
17+
item_list = [(cate[1], Item.objects.filter(category=cate[0])) for cate in CATEGORY]
18+
return JsonResponse({
19+
'money': request.user.balance,
20+
'item_list': item_list
21+
})
22+
23+
24+
class ItemView(LoginRequiredMixin, View):
25+
def get(self, request, item_id):
26+
try:
27+
item = Item.objects.get(id=item_id)
28+
except model.DoesNotExist:
29+
return HttpResponse(status=404)
30+
31+
try:
32+
item.get(teams__username=request.user.username)
33+
already_bought = True
34+
except model.DoesNotExist:
35+
already_bought = False
36+
37+
return JsonResponse({
38+
'id': item_id,
39+
'name': item,
40+
'description': getattr(item, 'description'),
41+
'type': getattr(item, 'category'),
42+
'price': getattr(item, 'price'),
43+
'already_bought': already_bought
44+
})
45+
46+
def post(self, request, item_id):
47+
try:
48+
item = Item.objects.get(id=item_id)
49+
except model.DoesNotExist:
50+
return HttpResponse(status=404)
51+
52+
try:
53+
item.get(teams__username=request.user.username)
54+
return HttpResponse(status=409)
55+
except model.DoesNotExist:
56+
pass
57+
58+
if not item.buy(request.user):
59+
return HttpResponse(status=402)
60+
return HttpResponse(status=200)

0 commit comments

Comments
 (0)