mirror of
https://github.com/Yourdaylight/CloudGame.git
synced 2026-07-28 01:25:17 +00:00
Initial commit
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# @Time :2023/11/19 14:21
|
||||
# @Author :lzh
|
||||
# @File : __init__.py
|
||||
# @Software: PyCharm
|
||||
@@ -0,0 +1,132 @@
|
||||
import time
|
||||
import datetime
|
||||
import traceback
|
||||
from bson import ObjectId
|
||||
from config import client, DATABASE_NAME, COLLECTION, USER_COLLECTION
|
||||
from utils import JSONEncoder
|
||||
from flask import Blueprint, request, jsonify, Response
|
||||
|
||||
game = Blueprint('game', __name__);
|
||||
user_collection = client[DATABASE_NAME][USER_COLLECTION]
|
||||
game_collection = client[DATABASE_NAME][COLLECTION]
|
||||
|
||||
|
||||
@game.route('/api/v1/game/getGames', methods=['POST', 'GET'])
|
||||
def game_list():
|
||||
try:
|
||||
page = request.json.get("page", 1)
|
||||
size = request.json.get("size", 10)
|
||||
|
||||
# 获取查询参数
|
||||
original_price = request.json.get("Original Price")
|
||||
title = request.json.get("Title")
|
||||
developer = request.json.get("Developer")
|
||||
game_features = request.json.get("Game Features")
|
||||
|
||||
# 构建查询条件
|
||||
query_conditions = {}
|
||||
# 不区分大小写的模糊查询
|
||||
if original_price:
|
||||
query_conditions["Original Price"] = {"$regex": original_price, "$options": "i"}
|
||||
if title:
|
||||
query_conditions["Title"] = {"$regex": title, "$options": "i"}
|
||||
if developer:
|
||||
query_conditions["Developer"] = {"$regex": developer, "$options": "i"}
|
||||
# 匹配列表中所有元素
|
||||
if game_features:
|
||||
query_conditions["Game Features"] = {"$all": game_features}
|
||||
|
||||
# 执行查询
|
||||
data = game_collection.find(query_conditions).skip((page - 1) * size).limit(size).sort("update_time", -1)
|
||||
res = []
|
||||
for i in data:
|
||||
res.append(i)
|
||||
total = game_collection.count_documents(query_conditions)
|
||||
content = {"code": 200, "total": total, "data": res, "msg": "SUCCESS"}
|
||||
content = JSONEncoder().encode(content)
|
||||
except Exception as e:
|
||||
content = {"code": 500, "msg": str(e)}
|
||||
|
||||
return Response(content, mimetype='application/json')
|
||||
|
||||
|
||||
@game.route('/api/v1/game/addGame', methods=['POST', 'GET'])
|
||||
def add_game():
|
||||
try:
|
||||
game_data = request.json
|
||||
# Title为必填项
|
||||
if not game_data.get("Title"):
|
||||
return jsonify({"code": 500, "msg": "Field: [Title] is required"}), 400
|
||||
existing_game = game_collection.find_one({"Title": game_data.get("Title")})
|
||||
if existing_game:
|
||||
return jsonify({
|
||||
"code": 500,
|
||||
"msg": f"A game with the same title [{existing_game}] already exists"}
|
||||
), 400
|
||||
|
||||
# 验证字符串类型的字段
|
||||
string_fields = ["Title", "Original Price", "Discounted Price", "Release Date", "Game Description", "Developer", "Publisher" ]
|
||||
for field in string_fields:
|
||||
if not isinstance(game_data.get(field), str):
|
||||
return jsonify({"code": 500, "msg": f"Field:[{field}] should be a string"}), 400
|
||||
|
||||
# 验证列表类型的字段
|
||||
# list_fields = ["Popular Tags", "Game Features", "Supported Languages"]
|
||||
# for field in list_fields:
|
||||
# if not isinstance(game_data.get(field), list):
|
||||
# return jsonify({"code": 500, "msg": f"Field: [{field}] should be a list"}), 400
|
||||
|
||||
# 添加时间戳
|
||||
game_data["date_added"] = datetime.datetime.now().strftime("%Y-%m-%d")
|
||||
game_data["update_time"] = time.time()
|
||||
|
||||
# 插入数据
|
||||
game_collection.insert_one(game_data)
|
||||
return jsonify({"code": 200, "msg": "SUCCESS"})
|
||||
except Exception as e:
|
||||
return jsonify({"code": 500, "msg": str(e)}), 500
|
||||
|
||||
|
||||
@game.route('/api/v1/game/deleteGame', methods=['POST', 'GET'])
|
||||
def delete_game():
|
||||
try:
|
||||
id = request.json.get("_id")
|
||||
game_collection.delete_one({"_id": ObjectId(id)})
|
||||
content = {"code": 200, "msg": "SUCCESS"}
|
||||
except Exception as e:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
content = {"code": 500, "msg": str(e)}
|
||||
return JSONEncoder().encode(content)
|
||||
|
||||
|
||||
@game.route('/api/v1/game/updateGame', methods=['POST', 'GET'])
|
||||
def update_game():
|
||||
try:
|
||||
id = request.json.pop("_id")
|
||||
game_collection.update_one({"_id": ObjectId(id)}, {"$set": request.json})
|
||||
content = {"code": 200, "msg": "SUCCESS"}
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
content = {"code": 500, "msg": str(e)}
|
||||
return JSONEncoder().encode(content)
|
||||
|
||||
|
||||
# 获取收藏列表
|
||||
@game.route('/api/v1/game/getCollectList', methods=['POST', 'GET'])
|
||||
def get_collect_list():
|
||||
try:
|
||||
username = request.json.get("username")
|
||||
user_document = user_collection.find_one({"username": username})
|
||||
game_ids = user_document.get("collect", [])
|
||||
collect_list = []
|
||||
# 批量查询game_ids
|
||||
for game_id in game_ids:
|
||||
game_document = game_collection.find_one({"_id": ObjectId(game_id)})
|
||||
collect_list.append(game_document)
|
||||
content = {"code": 200, "msg": "SUCCESS", "total": len(game_ids), "data": collect_list}
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
content = {"code": 500, "msg": str(e)}
|
||||
content = JSONEncoder().encode(content)
|
||||
return Response(content, mimetype='application/json')
|
||||
@@ -0,0 +1,62 @@
|
||||
import uuid
|
||||
import time
|
||||
import datetime
|
||||
from bson import ObjectId
|
||||
from flask import Blueprint, request, jsonify
|
||||
from config import client, DATABASE_NAME, COLLECTION
|
||||
|
||||
review = Blueprint('review', __name__)
|
||||
game_collection = client[DATABASE_NAME][COLLECTION]
|
||||
|
||||
|
||||
@review.route('/api/v1/addReview', methods=['POST'])
|
||||
def add_review():
|
||||
try:
|
||||
game_id = request.json.get('_id')
|
||||
username = request.json.get('username')
|
||||
content = request.json.get('review')
|
||||
format_now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
|
||||
review_document = {
|
||||
"review_id": str(uuid.uuid1()),
|
||||
"username": username,
|
||||
"review": content,
|
||||
"update_time": format_now,
|
||||
"timestamp": int(time.time())
|
||||
}
|
||||
game_collection.update_one(
|
||||
{"_id": ObjectId(game_id)},
|
||||
{"$push": {"reviews": review_document}}
|
||||
)
|
||||
content = {"code": 200, "msg": "SUCCESS", "data": {"update_time": format_now}}
|
||||
except Exception as e:
|
||||
content = {"code": 500, "msg": str(e)}
|
||||
return jsonify(content)
|
||||
|
||||
|
||||
@review.route('/api/v1/getReviews', methods=['POST'])
|
||||
def get_reviews():
|
||||
try:
|
||||
game_id = request.json.get('gameId')
|
||||
game_document = game_collection.find_one({"_id": ObjectId(game_id)}, {"reviews": 500})
|
||||
reviews = game_document.get("reviews", [])
|
||||
reviews.sort(key=lambda x: x["timestamp"], reverse=True)
|
||||
content = {"code": 200, "msg": "SUCCESS", "data": reviews}
|
||||
except Exception as e:
|
||||
content = {"code": 500, "msg": str(e)}
|
||||
return jsonify(content)
|
||||
|
||||
|
||||
@review.route('api/v1/deleteReview', methods=['POST'])
|
||||
def remove_review():
|
||||
try:
|
||||
game_id = request.json.get('gameId')
|
||||
review_id = request.json.get('review_id')
|
||||
game_collection.update_one(
|
||||
{"_id": ObjectId(game_id)},
|
||||
{"$pull": {"reviews": {"review_id": review_id}}}
|
||||
)
|
||||
content = {"code": 200, "msg": "SUCCESS"}
|
||||
except Exception as e:
|
||||
content = {"code": 500, "msg": str(e)}
|
||||
return jsonify(content)
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
import json
|
||||
import traceback
|
||||
from flask import Blueprint, request, jsonify
|
||||
from utils import get_token
|
||||
from config import client, DATABASE_NAME, USER_COLLECTION
|
||||
|
||||
db = client[DATABASE_NAME]
|
||||
dbUser = db["user"]
|
||||
user = Blueprint('user', __name__)
|
||||
user_collection = client[DATABASE_NAME][USER_COLLECTION]
|
||||
|
||||
|
||||
@user.route('/api/v1/games/login', methods=['POST'])
|
||||
def login():
|
||||
username = request.json.get('username')
|
||||
password = request.json.get('password')
|
||||
# 登录校验
|
||||
try:
|
||||
login_user = dbUser.find_one({"username": username, "password": password})
|
||||
if login_user:
|
||||
token = get_token(login_user.get("username"))
|
||||
content = {"code": 200, "msg": "SUCCESS",
|
||||
"data": {"token": token}}
|
||||
else:
|
||||
content = {"code": 500, "msg": "username or password is wrong!"}
|
||||
except Exception as e:
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
content = {"code": 500, "msg": str(e)}
|
||||
return jsonify(content)
|
||||
|
||||
|
||||
@user.route('/api/v1/games/register', methods=['POST'])
|
||||
def register():
|
||||
try:
|
||||
username = request.json.get('username')
|
||||
password = request.json.get('password')
|
||||
# 校验是否已经注册
|
||||
regist_user = dbUser.find_one({"username": username, "password": password})
|
||||
if not regist_user:
|
||||
regist_user = {"username": username, "password": password}
|
||||
dbUser.insert_one(regist_user)
|
||||
content = {"code": 200, "msg": "SUCCESS"}
|
||||
else:
|
||||
content = {"code": 500, "msg": "Register failed! The username already exists!!"}
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
content = {"code": 500, "msg": str(e)}
|
||||
return jsonify(content)
|
||||
|
||||
|
||||
# 收藏游戏
|
||||
@user.route('/api/v1/games/collectGame', methods=['POST', 'GET'])
|
||||
def collect_game():
|
||||
try:
|
||||
username = request.json.get("username")
|
||||
game_id = request.json.get("gameId")
|
||||
user_collection.update_one({"username": username}, {"$addToSet": {"collect": game_id}})
|
||||
content = {"code": 200, "msg": "SUCCESS"}
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
content = {"code": 500, "msg": str(e)}
|
||||
return jsonify(content)
|
||||
|
||||
|
||||
# 取消收藏
|
||||
@user.route('/api/v1/games/cancelCollectGame', methods=['POST', 'GET'])
|
||||
def cancel_collect_game():
|
||||
try:
|
||||
username = request.json.get("username")
|
||||
game_id = request.json.get("gameId")
|
||||
user_collection.update_one({"username": username}, {"$pull": {"collect": game_id}})
|
||||
content = {"code": 200, "msg": "SUCCESS"}
|
||||
except Exception as e:
|
||||
traceback.print_exc()
|
||||
content = {"code": 500, "msg": str(e)}
|
||||
return jsonify(content)
|
||||
Reference in New Issue
Block a user