Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
W
web_nangaoclub
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
孙谢炜
web_nangaoclub
Commits
683f5f9b
Commit
683f5f9b
authored
May 12, 2024
by
孙谢炜
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
新增轮播图与登录检测
parent
d4c37363
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
1151 additions
and
0 deletions
+1151
-0
app.py
clubwebapp/app.py
+1085
-0
models.py
clubwebapp/models.py
+66
-0
No files found.
clubwebapp/app.py
0 → 100644
View file @
683f5f9b
import
os
from
flask
import
Flask
,
render_template
,
redirect
,
url_for
,
request
,
session
from
flask_login
import
LoginManager
,
UserMixin
,
login_user
,
logout_user
,
login_required
,
current_user
from
models
import
db
,
User
,
SQLAlchemy
,
Games
,
SeasonPlayers
,
Seasons
,
Carousel
from
sqlalchemy
import
desc
,
or_
,
and_
from
werkzeug.utils
import
secure_filename
import
datetime
import
uuid
import
copy
# import json
# current_season = 1
data
=
None
app
=
Flask
(
__name__
)
app
.
config
[
'SECRET_KEY'
]
=
'your_secret_key'
app
.
config
[
'SQLALCHEMY_DATABASE_URI'
]
=
'mysql+pymysql://root@localhost:3306/beta'
app
.
config
[
'SQLALCHEMY_TRACK_MODIFICATIONS'
]
=
False
# 可选,若不需要ORM模型修改追踪,则设为False以减少警告信息
# app.config['STATIC_URL_PATH'] = '/static'
# app.config['STATIC_FOLDER'] = './static_files/'
db
.
init_app
(
app
)
login_manager
=
LoginManager
(
app
)
login_manager
.
login_view
=
'login'
@
login_manager
.
user_loader
def
load_user
(
user_id
):
user
=
User
.
query
.
filter
(
User
.
id
==
user_id
)
.
first
()
return
user
if
user
is
not
None
else
None
@
app
.
route
(
'/'
)
# @login_required
def
home
():
return
render_template
(
'ajax.html'
)
@
app
.
route
(
'/api/main'
,
methods
=
[
"GET"
,
"POST"
])
@
login_required
def
mainpage
():
return
f
'Hello, World!{current_user.username}'
@
app
.
route
(
'/api/login'
,
methods
=
[
'POST'
])
def
login
():
# username = request.args.get("usrname")
# password = request.args.get("psword")
data
=
request
.
get_json
()
username
=
data
.
get
(
'usrname'
)
password
=
data
.
get
(
'psword'
)
if
username
==
None
or
password
==
None
:
return
{
"status"
:
"READY"
}
user
=
User
.
query
.
filter
(
User
.
username
==
username
)
.
first
()
if
user
==
None
:
return
{
"status"
:
"USER_NOT_EXIST"
}
if
user
.
password
!=
password
:
return
{
"status"
:
"AUTHENTICATION_FAILED"
}
# user = User(user.id)
login_user
(
user
)
return
{
"status"
:
"SUCCESS"
,
"is_su"
:
user
.
is_su
,
"is_member"
:
user
.
is_member
,
"real_name"
:
user
.
real_name
}
@
app
.
route
(
'/api/setRealName'
,
methods
=
[
'POST'
])
def
setRealName
():
# real_name = request.args.get("name")
real_name
=
request
.
get_json
()
.
get
(
'name'
)
if
real_name
==
None
:
return
{
"status"
:
"READY"
}
user
=
User
.
query
.
filter_by
(
id
=
current_user
.
get_id
())
.
first
()
user
.
real_name
=
real_name
try
:
db
.
session
.
commit
()
return
{
"status"
:
"SUCCESS"
}
except
:
db
.
session
.
rollback
()
return
{
"status"
:
"FAILED"
}
@
app
.
route
(
'/api/logout'
)
@
login_required
def
logout
():
logout_user
()
return
{
"status"
:
"SUCCESS"
}
# return redirect(url_for('home'))
@
app
.
route
(
'/api/register'
,
methods
=
[
'POST'
])
def
register
():
# psword = request.args.get("psword")
data
=
request
.
get_json
()
username
=
data
.
get
(
'usrname'
)
psword
=
data
.
get
(
"psword"
)
if
username
==
None
or
psword
==
None
:
return
{
"status"
:
"FAILED"
,
"msg"
:
"缺少用户名密码"
}
user
=
User
()
user
.
username
=
username
user
.
password
=
psword
try
:
db
.
session
.
add
(
user
)
db
.
session
.
commit
()
return
{
"status"
:
"SUCCESS"
}
except
:
db
.
session
.
rollback
()
return
{
"status"
:
"FAILED"
}
@
app
.
route
(
'/api/currentUser'
,
methods
=
[
'GET'
])
def
get_current_user
():
# 获取当前登录用户信息
if
not
current_user
.
is_authenticated
:
return
{
"status"
:
"LOGOUT"
}
return
{
"status"
:
"SUCCESS"
,
"user_id"
:
current_user
.
id
,
"username"
:
current_user
.
username
,
"is_su"
:
current_user
.
is_su
,
"is_member"
:
current_user
.
is_member
,
"real_name"
:
current_user
.
real_name
,
"profile_file"
:
current_user
.
profile_file
,
"background_file"
:
current_user
.
background_file
}
@
app
.
route
(
'/api/setAvatar'
,
methods
=
[
'POST'
])
def
set_avatar
():
user_avatar
=
request
.
files
.
get
(
'avatar'
)
extension
=
user_avatar
.
filename
.
split
(
'.'
)[
-
1
]
.
lower
()
if
extension
not
in
[
'jpg'
,
'gif'
,
'png'
,
'jpeg'
]:
return
{
"status"
:
"FAILED"
}
unique_filename
=
str
(
uuid
.
uuid4
())
+
'.'
+
extension
filename
=
secure_filename
(
unique_filename
)
user_avatar
.
save
(
os
.
path
.
join
(
"/home/sxw/clubdata/avatar/"
,
filename
))
user
=
User
.
query
.
filter_by
(
id
=
current_user
.
get_id
())
.
first
()
user
.
profile_file
=
"/avatar/"
+
filename
try
:
db
.
session
.
commit
()
return
{
"status"
:
"SUCCESS"
,
"new_avatar_path"
:
"/avatar/"
+
filename
}
except
:
db
.
session
.
rollback
()
return
{
"status"
:
"FAILED"
}
@
app
.
route
(
'/api/setBackground'
,
methods
=
[
'POST'
])
def
set_background
():
user_background
=
request
.
files
.
get
(
'background'
)
extension
=
user_background
.
filename
.
split
(
'.'
)[
-
1
]
.
lower
()
if
extension
not
in
[
'jpg'
,
'gif'
,
'png'
,
'jpeg'
]:
return
{
"status"
:
"FAILED"
}
unique_filename
=
str
(
uuid
.
uuid4
())
+
'.'
+
extension
filename
=
secure_filename
(
unique_filename
)
user_background
.
save
(
os
.
path
.
join
(
"/home/sxw/clubdata/userBackground/"
,
filename
))
user
=
User
.
query
.
filter_by
(
id
=
current_user
.
get_id
())
.
first
()
user
.
background_file
=
"/userBackground/"
+
filename
try
:
db
.
session
.
commit
()
return
{
"status"
:
"SUCCESS"
,
"new_background_path"
:
"/userBackground/"
+
filename
}
except
:
db
.
session
.
rollback
()
return
{
"status"
:
"FAILED"
}
@
app
.
route
(
'/api/upCarousel'
,
methods
=
[
'POST'
])
def
upload_carousel
():
carousel_link
=
request
.
form
.
get
(
'carousel_link'
)
carousel_id
=
request
.
form
.
get
(
'carousel_id'
)
carousel_actions
=
request
.
form
.
get
(
'carousel_actions'
)
carousel_actions
=
int
(
carousel_actions
)
# carousel_data = request.get_json()
# carousel_actions = carousel_data.get('carousel_actions')
# carousel_id = carousel_data.get('carousel_id')
carousel_files
=
request
.
files
.
get
(
'carousel_files'
)
# print(carousel_link)
# carousel_link = carousel_data.get('carousel_link')
if
carousel_actions
==
1
:
#删除操作
carousel_id
=
int
(
carousel_id
)
del_carousel
=
Carousel
.
query
.
filter
(
Carousel
.
id
==
carousel_id
)
.
first
()
try
:
db
.
session
.
delete
(
del_carousel
)
db
.
session
.
commit
()
return
{
"status"
:
"SUCCESS"
}
except
:
db
.
session
.
rollback
()
return
{
"status"
:
"FAILED"
}
if
carousel_actions
==
2
:
#修改操作
if
carousel_files
is
not
None
:
carousel_id
=
int
(
carousel_id
)
extension
=
carousel_files
.
filename
.
split
(
'.'
)[
-
1
]
.
lower
()
unique_filename
=
str
(
uuid
.
uuid4
())
+
'.'
+
extension
filename
=
secure_filename
(
unique_filename
)
carousel_files
.
save
(
os
.
path
.
join
(
"/home/sxw/clubdata/carousel/"
,
filename
))
alter_carousel
=
Carousel
.
query
.
filter
(
Carousel
.
id
==
carousel_id
)
.
first
()
alter_carousel
.
carousel_file
=
"/carousel/"
+
filename
if
carousel_link
!=
""
:
alter_carousel
.
link
=
carousel_link
try
:
db
.
session
.
commit
()
return
{
"status"
:
"SUCCESS"
}
except
:
db
.
session
.
rollback
()
return
{
"status"
:
"FAILED"
}
else
:
carousel_id
=
int
(
carousel_id
)
if
carousel_link
!=
""
:
alter_carousel
=
Carousel
.
query
.
filter
(
Carousel
.
id
==
carousel_id
)
.
first
()
alter_carousel
.
link
=
carousel_link
try
:
db
.
session
.
commit
()
return
{
"status"
:
"SUCCESS"
}
except
:
db
.
session
.
rollback
()
return
{
"status"
:
"FAILED"
}
if
carousel_actions
==
3
:
#新增操作
if
carousel_files
is
not
None
:
extension
=
carousel_files
.
filename
.
split
(
'.'
)[
-
1
]
.
lower
()
unique_filename
=
str
(
uuid
.
uuid4
())
+
'.'
+
extension
filename
=
secure_filename
(
unique_filename
)
carousel_files
.
save
(
os
.
path
.
join
(
"/home/sxw/clubdata/carousel/"
,
filename
))
new_carousel
=
Carousel
()
new_carousel
.
carousel_file
=
"/carousel/"
+
filename
if
carousel_link
!=
""
:
new_carousel
.
link
=
carousel_link
try
:
db
.
session
.
add
(
new_carousel
)
db
.
session
.
commit
()
return
{
"status"
:
"SUCCESS"
}
except
:
db
.
session
.
rollback
()
return
{
"status"
:
"FAILED"
}
@
app
.
route
(
'/api/getCarousel'
,
methods
=
[
'GET'
])
def
get_carousel
():
carousels
=
Carousel
.
query
.
all
()
carousel_list
=
[]
for
carousel
in
carousels
:
carousel_info
=
{}
carousel_info
[
"carouselid"
]
=
carousel
.
id
carousel_info
[
"src"
]
=
carousel
.
carousel_file
carousel_info
[
"link"
]
=
carousel
.
link
carousel_list
.
append
(
carousel_info
)
return
{
"status"
:
"SUCCESS"
,
"Carousels_info"
:
carousel_list
}
# @app.route('/api/winrate', methods=['GET'])
# def winrate():
# user_id = current_user.id
# total_games = Games.query.filter(or_(Games.player1_id == user_id, Games.player2_id == user_id)).count()
# if total_games == 0:
# return {'status': 'SUCCESS', 'winrate': 0.0}
# wins = Games.query.filter(((Games.player1_id == user_id) & (Games.score1 > Games.score2)) |
# ((Games.player2_id == user_id) & (Games.score2 > Games.score1))).count()
# winrate = (wins / total_games) * 100
# return {'status': 'SUCCESS', 'winrate': int(winrate)}
@
app
.
route
(
'/api/setUserinform'
,
methods
=
[
'POST'
])
def
set_userinform
():
data
=
request
.
get_json
()
username
=
data
.
get
(
'user_name'
)
psword
=
data
.
get
(
"user_password"
)
user
=
User
.
query
.
filter_by
(
id
=
current_user
.
get_id
())
.
first
()
if
username
!=
None
:
user
.
username
=
username
if
psword
!=
None
:
user
.
password
=
psword
try
:
db
.
session
.
commit
()
return
{
"status"
:
"SUCCESS"
}
except
:
db
.
session
.
rollback
()
return
{
"status"
:
"FAILED"
}
@
app
.
route
(
'/api/setMembers'
,
methods
=
[
'POST'
])
def
su_set_members
():
if
not
current_user
.
is_su
:
return
{
"status"
:
"PERMISSION_DENIED"
}
data
=
request
.
get_json
()
user_list
=
[]
if
len
(
data
)
!=
0
:
for
item
in
data
:
id
=
item
.
get
(
'usr_id'
)
is_member
=
item
.
get
(
'is_member'
)
if
id
==
None
or
is_member
==
None
:
return
{
"status"
:
"FAILED"
}
usr
=
User
.
query
.
filter_by
(
id
=
id
)
.
first
()
if
usr
==
None
:
return
{
"status"
:
"FAILED"
}
usr
.
is_member
=
is_member
user_list
.
append
(
usr
)
try
:
db
.
session
.
commit
()
return
{
"status"
:
"SUCCESS"
}
except
:
db
.
session
.
rollback
()
return
{
"status"
:
"FAILED"
}
if
len
(
data
)
==
0
:
return
{
"status"
:
"FAILED"
}
@
app
.
route
(
'/api/getUser'
,
methods
=
[
'GET'
])
def
get_user
():
# if not current_user.is_su:
# return {"status":"PERMISSION_DENIED"}
users
=
User
.
query
.
all
()
unauidted_users
=
User
.
query
.
filter
(
User
.
is_member
!=
True
)
.
all
()
users_list
=
[]
unauidted_users_list
=
[]
for
user
in
users
:
user_info
=
{}
user_info
[
'user_id'
]
=
user
.
id
user_info
[
'username'
]
=
user
.
username
user_info
[
'password'
]
=
user
.
password
user_info
[
'is_su'
]
=
user
.
is_su
user_info
[
'is_member'
]
=
user
.
is_member
user_info
[
'real_name'
]
=
user
.
real_name
users_list
.
append
(
user_info
)
for
unauidted_user
in
unauidted_users
:
unauidted_user_info
=
{}
unauidted_user_info
[
'user_id'
]
=
unauidted_user
.
id
unauidted_user_info
[
'username'
]
=
unauidted_user
.
username
unauidted_user_info
[
'real_name'
]
=
unauidted_user
.
real_name
unauidted_users_list
.
append
(
unauidted_user_info
)
return
{
"status"
:
"SUCCESS"
,
"Users"
:
users_list
,
"Unauidted_users"
:
unauidted_users_list
}
# @app.route('/api/myRecentGames',methods=['GET'])
# def my_recent_games():
# # data = request.get_json().get("data")
# session['mygameflag'] = request.args.get("myid")
# return {"status":"SUCCESS"}
# # return redirect(url_for('/my_recentgames.html'))
@
app
.
route
(
'/api/userManage'
,
methods
=
[
'POST'
])
def
user_manage
():
# if not current_user.is_su:
# return {"status":"PERMISSION_DENIED"}
change_user
=
request
.
get_json
()
user_id
=
change_user
.
get
(
'user_id'
)
user_name
=
change_user
.
get
(
'user_name'
)
user_realname
=
change_user
.
get
(
'user_realname'
)
user_password
=
change_user
.
get
(
'user_password'
)
user_is_su
=
change_user
.
get
(
'user_is_su'
)
user_is_member
=
change_user
.
get
(
'user_is_member'
)
user
=
User
.
query
.
filter
(
User
.
id
==
user_id
)
.
first
()
if
user_name
!=
None
:
user
.
username
=
user_name
if
user_realname
!=
None
:
user
.
real_name
=
user_realname
if
user_password
!=
None
:
user
.
password
=
user_password
if
user_is_su
!=
None
:
user
.
is_su
=
user_is_su
if
user_is_member
!=
None
:
user
.
is_member
=
user_is_member
try
:
db
.
session
.
commit
()
return
{
"status"
:
"SUCCESS"
}
except
:
db
.
session
.
rollback
()
return
{
"status"
:
"FAILED"
}
@
app
.
route
(
'/api/games'
,
methods
=
[
'GET'
])
def
get_games
():
# 获取最近比赛列表
# global data
season_id
=
request
.
args
.
get
(
"season_id"
)
pages
=
request
.
args
.
get
(
"pages"
)
player
=
request
.
args
.
get
(
"myid"
)
lim
=
request
.
args
.
get
(
"lim"
)
# player = data
current_Season
=
Seasons
.
query
.
filter_by
(
enable
=
1
)
.
first
()
if
season_id
is
None
and
current_Season
is
not
None
:
season_id
=
current_Season
.
id
elif
season_id
is
None
and
current_Season
is
None
:
season_id
=
0
if
pages
is
None
:
pages
=
1
else
:
pages
=
int
(
pages
)
# games = Games.query.all()
if
lim
is
None
:
lim
=
10
else
:
lim
=
int
(
lim
)
# lim = 10
if
season_id
is
None
and
player
is
None
:
totalGames
=
Games
.
query
.
count
()
games
=
[]
# games = Games.query.order_by(desc(Games.game_date)).slice((pages-1)*lim,(pages)*lim).all()
elif
season_id
is
not
None
and
player
is
None
:
season_id
=
int
(
season_id
)
totalGames
=
Games
.
query
.
filter
(
and_
(
Games
.
season_id
==
season_id
,
Games
.
enable
==
1
))
.
count
()
games
=
Games
.
query
.
order_by
(
desc
(
Games
.
game_date
))
.
filter
(
and_
(
Games
.
season_id
==
season_id
,
Games
.
enable
==
1
))
.
slice
((
pages
-
1
)
*
lim
,(
pages
)
*
lim
)
.
all
()
elif
season_id
is
None
and
player
is
not
None
:
player
=
int
(
player
)
totalGames
=
Games
.
query
.
filter
(
and_
(
Games
.
enable
==
1
,
or_
(
Games
.
player1_id
==
player
,
Games
.
player2_id
==
player
)))
.
count
()
games
=
Games
.
query
.
order_by
(
desc
(
Games
.
game_date
))
.
filter
(
and_
(
Games
.
enable
==
1
,
or_
(
Games
.
player1_id
==
player
,
Games
.
player2_id
==
player
)))
.
slice
((
pages
-
1
)
*
lim
,(
pages
)
*
lim
)
.
all
()
elif
season_id
is
not
None
and
player
is
not
None
:
season_id
=
int
(
season_id
)
player
=
int
(
player
)
totalGames
=
Games
.
query
.
filter
(
and_
(
Games
.
season_id
==
season_id
,
Games
.
enable
==
1
,
or_
(
Games
.
player1_id
==
player
,
Games
.
player2_id
==
player
)))
.
count
()
games
=
Games
.
query
.
order_by
(
desc
(
Games
.
game_date
))
.
filter
(
and_
(
Games
.
season_id
==
season_id
,
Games
.
enable
==
1
,
or_
(
Games
.
player1_id
==
player
,
Games
.
player2_id
==
player
)))
.
slice
((
pages
-
1
)
*
lim
,(
pages
)
*
lim
)
.
all
()
else
:
return
{
"status"
:
"FAILED"
}
game_list
=
[]
for
game
in
games
:
if
game
.
enable
==
1
:
game_info
=
{}
game_info
[
'id'
]
=
game
.
id
game_info
[
'game_date'
]
=
game
.
game_date
.
strftime
(
'
%
Y/
%
m/
%
d'
)
game_info
[
'player1_id'
]
=
game
.
player1_id
game_info
[
'player1_real_name'
]
=
game
.
player1
.
real_name
game_info
[
'player1_profile_file'
]
=
game
.
player1
.
profile_file
game_info
[
'score1'
]
=
game
.
score1
game_info
[
'player2_id'
]
=
game
.
player2_id
game_info
[
'player2_real_name'
]
=
game
.
player2
.
real_name
game_info
[
'player2_profile_file'
]
=
game
.
player2
.
profile_file
game_info
[
'score2'
]
=
game
.
score2
game_info
[
'season_name'
]
=
game
.
season
.
season_name
game_list
.
append
(
game_info
)
return
{
"status"
:
"SUCCESS"
,
"Games"
:
game_list
,
"totalGames"
:
totalGames
}
@
app
.
route
(
'/api/gameDetail'
,
methods
=
[
'GET'
])
def
get_game_detail
():
# 获取比赛详情
game_id
=
request
.
args
.
get
(
"game_id"
)
if
game_id
==
None
:
return
{
"status"
:
"READY"
}
game_id
=
int
(
game_id
)
game
=
Games
.
query
.
filter_by
(
id
=
game_id
)
.
first
()
if
game
is
None
:
return
{
"status"
:
"GAME_NOT_FOUND"
}
game_info
=
{}
game_info
[
"status"
]
=
"SUCCESS"
game_info
[
'id'
]
=
game
.
id
# game_info['game_date'] = str(game.game_date)
game_info
[
'game_date'
]
=
game
.
game_date
.
strftime
(
'
%
Y/
%
m/
%
d'
)
game_info
[
'player1_id'
]
=
game
.
player1_id
game_info
[
'player1_real_name'
]
=
game
.
player1
.
real_name
game_info
[
'player1_profile_file'
]
=
game
.
player1
.
profile_file
game_info
[
'score1'
]
=
game
.
score1
game_info
[
'player2_id'
]
=
game
.
player2_id
game_info
[
'player2_real_name'
]
=
game
.
player2
.
real_name
game_info
[
'player2_profile_file'
]
=
game
.
player2
.
profile_file
game_info
[
'score2'
]
=
game
.
score2
game_info
[
'season_id'
]
=
game
.
season_id
game_info
[
'season_name'
]
=
game
.
season
.
season_name
game_info
[
'laucher_id'
]
=
game
.
laucher_id
game_info
[
'laucher_real_name'
]
=
game
.
laucher
.
real_name
game_info
[
'laucher_profile_file'
]
=
game
.
laucher
.
profile_file
game_info
[
'lauch_date'
]
=
game
.
lauch_date
.
strftime
(
'
%
Y/
%
m/
%
d'
)
game_info
[
'confirmer_id'
]
=
game
.
confirmer_id
game_info
[
'confirmer_real_name'
]
=
game
.
confirmer
.
real_name
game_info
[
'confirmer_profile_file'
]
=
game
.
confirmer
.
profile_file
game_info
[
'confirm_date'
]
=
game
.
confirm_date
.
strftime
(
'
%
Y/
%
m/
%
d'
)
game_info
[
'game_type'
]
=
game
.
game_type
return
game_info
@
app
.
route
(
'/api/players'
,
methods
=
[
'GET'
])
def
get_players
():
# 获取队员(赛季积分排序)
season_id
=
request
.
args
.
get
(
"season_id"
)
myplayer_id
=
request
.
args
.
get
(
"player_id"
)
pages
=
request
.
args
.
get
(
"pages"
)
lim
=
request
.
args
.
get
(
"lim"
)
current_Season
=
Seasons
.
query
.
filter_by
(
enable
=
1
)
.
first
()
if
lim
is
None
:
lim
=
10
elif
lim
is
not
None
:
lim
=
int
(
lim
)
if
pages
is
None
:
pages
=
1
if
pages
is
not
None
:
pages
=
int
(
pages
)
if
current_Season
is
None
:
exist_season
=
False
if
current_Season
is
not
None
:
exist_season
=
True
if
season_id
is
None
and
current_Season
is
not
None
:
season_id
=
current_Season
.
id
elif
season_id
is
None
and
current_Season
is
None
:
season_id
=
0
else
:
season_id
=
int
(
season_id
)
if
myplayer_id
==
None
:
players
=
SeasonPlayers
.
query
.
filter_by
(
season_id
=
season_id
)
.
order_by
(
desc
(
SeasonPlayers
.
score
))
.
slice
((
pages
-
1
)
*
lim
,(
pages
)
*
lim
)
.
all
()
else
:
players
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
season_id
==
season_id
,
SeasonPlayers
.
player_id
==
myplayer_id
))
.
all
()
player_list
=
[]
for
player
in
players
:
player_info
=
{}
player_info
[
'season_id'
]
=
player
.
season_id
player_info
[
'season_name'
]
=
player
.
season
.
season_name
player_info
[
'score'
]
=
player
.
score
player_info
[
'group'
]
=
player
.
group
player_info
[
'player_id'
]
=
player
.
player_id
player_info
[
'player_real_name'
]
=
player
.
player
.
real_name
player_info
[
'player_profile_file'
]
=
player
.
player
.
profile_file
player_info
[
'player_background_file'
]
=
player
.
player
.
background_file
player_info
[
'winrate'
]
=
player
.
winrate
player_info
[
'total_games'
]
=
player
.
total_games
player_info
[
'season_enable'
]
=
player
.
season_enable
player_list
.
append
(
player_info
)
return
{
"status"
:
"SUCCESS"
,
"Players"
:
player_list
,
"ExistSeason"
:
exist_season
}
@
app
.
route
(
'/api/launchGame'
,
methods
=
[
'POST'
])
def
lauch_game
():
# 发起比赛
game_info
=
request
.
get_json
()
current_Season
=
Seasons
.
query
.
filter_by
(
enable
=
1
)
.
first
()
new_game
=
Games
()
new_game
.
game_type
=
1
new_game
.
game_date
=
datetime
.
datetime
.
strptime
(
game_info
[
'game_date'
],
"
%
Y/
%
m/
%
d"
)
# 时间转换
new_game
.
player1_id
=
game_info
.
get
(
'player1_id'
)
new_game
.
score1
=
game_info
.
get
(
'score1'
)
new_game
.
player2_id
=
game_info
.
get
(
'player2_id'
)
new_game
.
score2
=
game_info
.
get
(
'score2'
)
new_game
.
confirmer_id
=
game_info
.
get
(
'confirmer_id'
)
new_game
.
laucher_id
=
current_user
.
get_id
()
new_game
.
lauch_date
=
datetime
.
datetime
.
now
()
new_game
.
season_id
=
current_Season
.
id
try
:
db
.
session
.
add
(
new_game
)
db
.
session
.
commit
()
return
{
"status"
:
"SUCCESS"
}
except
:
db
.
session
.
rollback
()
return
{
"status"
:
"FAILED"
}
@
app
.
route
(
'/api/confirmGame'
,
methods
=
[
'POST'
])
def
confirm_game
():
# 审核比赛
game_info
=
request
.
get_json
()
game_id
=
game_info
.
get
(
"game_id"
)
game_confirm
=
game_info
.
get
(
"confirm"
)
if
game_id
==
None
or
game_confirm
==
None
:
return
{
"status"
:
"READY"
}
game
=
Games
.
query
.
filter_by
(
id
=
game_id
)
.
first
()
if
game
==
None
:
return
{
"status"
:
"GAME_NOT_FOUND"
}
if
game
.
confirmer_id
!=
current_user
.
id
:
return
{
"status"
:
"PERMISSION_DENIED"
}
if
game_confirm
:
if
game
.
enable
==
0
:
game
.
confirm_date
=
datetime
.
datetime
.
now
()
game
.
enable
=
1
update_scores
(
game
)
update_winrate
(
game
)
else
:
game
.
enable
=
-
1
game
.
confirm_date
=
datetime
.
datetime
.
now
()
try
:
db
.
session
.
commit
()
return
{
"status"
:
"SUCCESS"
}
except
:
db
.
session
.
rollback
()
return
{
"status"
:
"FAILED"
}
def
update_scores
(
game
):
if
game
.
score1
>
game
.
score2
:
winner_id
=
game
.
player1_id
elif
game
.
score1
<
game
.
score2
:
winner_id
=
game
.
player2_id
else
:
return
season_player
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
season_id
==
game
.
season_id
,
SeasonPlayers
.
player_id
==
winner_id
))
.
first
()
season_player
.
score
+=
1
try
:
db
.
session
.
commit
()
except
:
db
.
session
.
rollback
()
def
update_winrate
(
game
):
# total_game = Games.query.filter(or_(Games.player1_id == game.player1_id, Games.player2_id == game.player2_id)).count()
season_player1
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
season_id
==
game
.
season_id
,
SeasonPlayers
.
player_id
==
game
.
player1_id
))
.
first
()
season_player2
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
season_id
==
game
.
season_id
,
SeasonPlayers
.
player_id
==
game
.
player2_id
))
.
first
()
season_player1
.
total_games
+=
1
season_player2
.
total_games
+=
1
season_player1
.
winrate
=
int
((
season_player1
.
score
/
season_player1
.
total_games
)
*
100
)
season_player2
.
winrate
=
int
((
season_player2
.
score
/
season_player2
.
total_games
)
*
100
)
try
:
db
.
session
.
commit
()
except
:
db
.
session
.
rollback
()
@
app
.
route
(
'/api/lauchedGames'
,
methods
=
[
'GET'
])
def
get_lauched_game
():
# 获取审核中比赛列表
current_Season
=
Seasons
.
query
.
filter_by
(
enable
=
1
)
.
first
()
if
current_Season
is
None
:
current_SeasonId
=
0
if
current_Season
is
not
None
:
current_SeasonId
=
current_Season
.
id
games
=
Games
.
query
.
filter
(
and_
(
Games
.
season_id
==
current_SeasonId
,
Games
.
confirmer_id
==
current_user
.
id
,
Games
.
enable
==
0
))
.
order_by
(
desc
(
Games
.
lauch_date
))
game_list
=
[]
for
game
in
games
:
game_info
=
{}
game_info
[
'id'
]
=
game
.
id
game_info
[
'game_date'
]
=
game
.
game_date
.
strftime
(
'
%
Y/
%
m/
%
d'
)
game_info
[
'player1_id'
]
=
game
.
player1_id
game_info
[
'player1_real_name'
]
=
game
.
player1
.
real_name
game_info
[
'player1_profile_file'
]
=
game
.
player1
.
profile_file
game_info
[
'score1'
]
=
game
.
score1
game_info
[
'player2_id'
]
=
game
.
player2_id
game_info
[
'player2_real_name'
]
=
game
.
player2
.
real_name
game_info
[
'player2_profile_file'
]
=
game
.
player2
.
profile_file
game_info
[
'score2'
]
=
game
.
score2
game_info
[
'season_name'
]
=
game
.
season
.
season_name
game_list
.
append
(
game_info
)
return
{
"status"
:
"SUCCESS"
,
"Games"
:
game_list
}
@
app
.
route
(
'/api/myLauchedGames'
,
methods
=
[
'GET'
])
def
get_my_lauched_game
():
# 获取发起审核比赛列表
pages
=
request
.
args
.
get
(
"pages"
)
lim
=
request
.
args
.
get
(
"lim"
)
current_Season
=
Seasons
.
query
.
filter_by
(
enable
=
1
)
.
first
()
if
current_Season
is
None
:
current_SeasonId
=
0
if
current_Season
is
not
None
:
current_SeasonId
=
current_Season
.
id
if
pages
is
None
:
pages
=
1
else
:
pages
=
int
(
pages
)
# games = Games.query.all()
if
lim
is
None
:
lim
=
10
else
:
lim
=
int
(
lim
)
totalGames
=
Games
.
query
.
filter
(
and_
(
Games
.
season_id
==
current_SeasonId
,
Games
.
laucher_id
==
current_user
.
id
))
.
count
()
games
=
Games
.
query
.
filter
(
and_
(
Games
.
season_id
==
current_SeasonId
,
Games
.
laucher_id
==
current_user
.
id
))
.
order_by
(
desc
(
Games
.
game_date
))
.
slice
((
pages
-
1
)
*
lim
,(
pages
)
*
lim
)
.
all
()
game_list
=
[]
for
game
in
games
:
game_info
=
{}
game_info
[
'id'
]
=
game
.
id
game_info
[
'game_date'
]
=
game
.
game_date
.
strftime
(
'
%
Y/
%
m/
%
d'
)
game_info
[
'player1_id'
]
=
game
.
player1_id
game_info
[
'player1_real_name'
]
=
game
.
player1
.
real_name
game_info
[
'player1_profile_file'
]
=
game
.
player1
.
profile_file
game_info
[
'score1'
]
=
game
.
score1
game_info
[
'player2_id'
]
=
game
.
player2_id
game_info
[
'player2_real_name'
]
=
game
.
player2
.
real_name
game_info
[
'player2_profile_file'
]
=
game
.
player2
.
profile_file
game_info
[
'score2'
]
=
game
.
score2
game_info
[
'season_name'
]
=
game
.
season
.
season_name
if
game
.
enable
==
1
:
game_info
[
'status'
]
=
'PASSED'
elif
game
.
enable
==
0
:
game_info
[
'status'
]
=
'WAITING'
elif
game
.
enable
==
-
1
:
game_info
[
'status'
]
=
"REJECT"
game_list
.
append
(
game_info
)
return
{
"status"
:
"SUCCESS"
,
"Games"
:
game_list
,
"totalGames"
:
totalGames
}
# @app.after_request
# def after_request(response):
# response.headers.add('Access-Control-Allow-Origin', '*')
# response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
# response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
# response.headers.add('Access-Control-Allow-Credentials', 'true')
# return response
@
app
.
route
(
'/api/player_ranking'
,
methods
=
[
'GET'
])
def
getplayer_ranking
():
# 获取队员(赛季胜率分组排序)
season_id
=
request
.
args
.
get
(
"season_id"
)
current_Season
=
Seasons
.
query
.
filter_by
(
enable
=
1
)
.
first
()
if
season_id
is
None
and
current_Season
is
not
None
:
season_id
=
current_Season
.
id
elif
season_id
is
None
and
current_Season
is
None
:
season_id
=
0
else
:
season_id
=
int
(
season_id
)
players_1
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
season_id
==
season_id
,
SeasonPlayers
.
group
==
1
))
.
order_by
(
desc
(
SeasonPlayers
.
winrate
))
.
all
()
player_list_1
=
[]
for
player_1
in
players_1
:
player_info
=
{}
player_info
[
'season_id'
]
=
player_1
.
season_id
player_info
[
'season_name'
]
=
player_1
.
season
.
season_name
player_info
[
'score'
]
=
player_1
.
score
player_info
[
'group'
]
=
player_1
.
group
player_info
[
'player_id'
]
=
player_1
.
player_id
player_info
[
'player_real_name'
]
=
player_1
.
player
.
real_name
player_info
[
'player_profile_file'
]
=
player_1
.
player
.
profile_file
player_info
[
'winrate'
]
=
player_1
.
winrate
player_info
[
'total_games'
]
=
player_1
.
total_games
player_list_1
.
append
(
player_info
)
players_2
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
season_id
==
season_id
,
SeasonPlayers
.
group
==
2
))
.
order_by
(
desc
(
SeasonPlayers
.
winrate
))
.
all
()
player_list_2
=
[]
for
player_2
in
players_2
:
player_info
=
{}
player_info
[
'season_id'
]
=
player_2
.
season_id
player_info
[
'season_name'
]
=
player_2
.
season
.
season_name
player_info
[
'score'
]
=
player_2
.
score
player_info
[
'group'
]
=
player_2
.
group
player_info
[
'player_id'
]
=
player_2
.
player_id
player_info
[
'player_real_name'
]
=
player_2
.
player
.
real_name
player_info
[
'player_profile_file'
]
=
player_2
.
player
.
profile_file
player_info
[
'winrate'
]
=
player_2
.
winrate
player_info
[
'total_games'
]
=
player_2
.
total_games
player_list_2
.
append
(
player_info
)
players_3
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
season_id
==
season_id
,
SeasonPlayers
.
group
==
3
))
.
order_by
(
desc
(
SeasonPlayers
.
winrate
))
.
all
()
player_list_3
=
[]
for
player_3
in
players_3
:
player_info
=
{}
player_info
[
'season_id'
]
=
player_3
.
season_id
player_info
[
'season_name'
]
=
player_3
.
season
.
season_name
player_info
[
'score'
]
=
player_3
.
score
player_info
[
'group'
]
=
player_3
.
group
player_info
[
'player_id'
]
=
player_3
.
player_id
player_info
[
'player_real_name'
]
=
player_3
.
player
.
real_name
player_info
[
'player_profile_file'
]
=
player_3
.
player
.
profile_file
player_info
[
'winrate'
]
=
player_3
.
winrate
player_info
[
'total_games'
]
=
player_3
.
total_games
player_list_3
.
append
(
player_info
)
return
{
"status"
:
"SUCCESS"
,
"Group_1"
:
player_list_1
,
"Group_2"
:
player_list_2
,
"Group_3"
:
player_list_3
}
@
app
.
route
(
'/api/seasonInfo'
,
methods
=
[
'GET'
])
def
su_get_season_info
():
seasons
=
Seasons
.
query
.
order_by
(
desc
(
Seasons
.
start_time
))
.
all
()
current_Season
=
Seasons
.
query
.
filter
(
Seasons
.
enable
==
1
)
.
first
()
selectSeasonInfo
=
{}
if
current_Season
is
None
:
# selectSeason = Seasons.query.filter(Seasons.id == 1).first()
# if selectSeason is not None:
selectSeasonInfo
[
'season_id'
]
=
-
1
selectSeasonInfo
[
'season_name'
]
=
'xxx'
if
current_Season
is
not
None
:
selectSeasonInfo
[
'season_id'
]
=
current_Season
.
id
selectSeasonInfo
[
'season_name'
]
=
current_Season
.
season_name
season_list
=
[]
for
season
in
seasons
:
seasonInfo
=
{}
seasonInfo
[
'season_id'
]
=
season
.
id
seasonInfo
[
'season_name'
]
=
season
.
season_name
seasonInfo
[
'start_date'
]
=
season
.
start_time
seasonInfo
[
'end_date'
]
=
season
.
end_time
seasonInfo
[
'season_enable'
]
=
season
.
enable
season_list
.
append
(
seasonInfo
)
return
{
"status"
:
"SUCCESS"
,
"Seasons"
:
season_list
,
"selectSeason"
:
selectSeasonInfo
}
@
app
.
route
(
'/api/seasonManage'
,
methods
=
[
'POST'
])
def
su_season_manage
():
season_info
=
request
.
get_json
()
season_id
=
season_info
.
get
(
"season_id"
)
new_seasonName
=
season_info
.
get
(
"season_name"
)
new_seasonStart
=
season_info
.
get
(
"start_date"
)
season_condition
=
season_info
.
get
(
"condition_singal"
)
if
season_condition
==
-
1
:
#重启赛季
old_season
=
Seasons
.
query
.
filter_by
(
id
=
season_id
)
.
first
()
old_season
.
enable
=
True
old_season
.
end_time
=
None
other_seasons
=
Seasons
.
query
.
filter
(
Seasons
.
id
!=
season_id
)
.
all
()
for
other_season
in
other_seasons
:
other_season
.
enable
=
False
if
other_season
.
end_time
==
None
:
other_season
.
end_time
=
datetime
.
datetime
.
now
()
try
:
db
.
session
.
commit
()
return
{
"status"
:
"SUCCESS"
}
except
:
db
.
session
.
rollback
()
return
{
"status"
:
"FAILED"
}
if
season_condition
==
0
:
#开启赛季
new_season
=
Seasons
()
new_season
.
season_name
=
new_seasonName
new_season
.
start_time
=
new_seasonStart
new_season
.
enable
=
True
other_oldseasons
=
Seasons
.
query
.
filter
(
Seasons
.
id
!=
new_season
.
id
)
.
all
()
for
other_season
in
other_oldseasons
:
other_season
.
enable
=
False
if
other_season
.
end_time
==
None
:
other_season
.
end_time
=
datetime
.
datetime
.
now
()
try
:
db
.
session
.
add
(
new_season
)
db
.
session
.
commit
()
return
{
"status"
:
"SUCCESS"
}
except
:
db
.
session
.
rollback
()
return
{
"status"
:
"FAILED"
}
if
season_condition
==
1
:
#结束赛季
end_season
=
Seasons
.
query
.
filter_by
(
id
=
season_id
)
.
first
()
end_season
.
enable
=
False
end_season
.
end_time
=
datetime
.
datetime
.
now
()
try
:
db
.
session
.
commit
()
return
{
"status"
:
"SUCCESS"
}
except
:
db
.
session
.
rollback
()
return
{
"status"
:
"FAILED"
}
@
app
.
route
(
'/api/mySeasonInfo'
,
methods
=
[
'GET'
])
def
season_info
():
# my_id = request.args.get("my_id")
current_Season
=
Seasons
.
query
.
filter_by
(
enable
=
1
)
.
first
()
my_seasonInfo
=
{}
current_SeasonInfo
=
{}
my_in_seasonPlay
=
-
1
#未加入
if
current_Season
!=
None
:
players
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
season_id
==
current_Season
.
id
,
SeasonPlayers
.
season_enable
==
1
))
.
all
()
my_player
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
player_id
==
current_user
.
id
,
SeasonPlayers
.
season_id
==
current_Season
.
id
,
SeasonPlayers
.
season_enable
==
1
))
.
first
()
my_confirm
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
player_id
==
current_user
.
id
,
SeasonPlayers
.
season_id
==
current_Season
.
id
,
SeasonPlayers
.
season_enable
==
0
))
.
first
()
current_SeasonInfo
[
'season_name'
]
=
current_Season
.
season_name
current_SeasonInfo
[
'players_number'
]
=
len
(
players
)
if
my_player
!=
None
:
my_in_seasonPlay
=
1
#已加入
my_seasonInfo
[
'season_id'
]
=
my_player
.
season_id
my_seasonInfo
[
'score'
]
=
my_player
.
score
my_seasonInfo
[
'group'
]
=
my_player
.
group
my_seasonInfo
[
'player_id'
]
=
my_player
.
player_id
my_seasonInfo
[
'player_real_name'
]
=
my_player
.
player
.
real_name
my_seasonInfo
[
'player_profile_file'
]
=
my_player
.
player
.
profile_file
my_seasonInfo
[
'winrate'
]
=
my_player
.
winrate
my_seasonInfo
[
'total_games'
]
=
my_player
.
total_games
if
my_player
==
None
and
my_confirm
!=
None
:
my_in_seasonPlay
=
0
#待审核
if
current_Season
==
None
:
my_in_seasonPlay
=
-
2
#当前无赛季
return
{
"status"
:
"SUCCESS"
,
"SeasonInfo"
:
my_seasonInfo
,
"Current_season"
:
current_SeasonInfo
,
"Season_Play"
:
my_in_seasonPlay
}
@
app
.
route
(
'/api/mySeasonJoin'
,
methods
=
[
'POST'
])
def
my_season_join
():
current_Season
=
Seasons
.
query
.
filter_by
(
enable
=
1
)
.
first
()
my_player
=
SeasonPlayers
()
my_player
.
player_id
=
current_user
.
id
my_player
.
season_id
=
current_Season
.
id
my_player
.
season_enable
=
0
try
:
db
.
session
.
add
(
my_player
)
db
.
session
.
commit
()
return
{
"status"
:
"SUCCESS"
}
except
:
db
.
session
.
rollback
()
return
{
"status"
:
"FAILED"
}
@
app
.
route
(
'/api/getPlayers'
,
methods
=
[
'GET'
])
def
su_get_players
():
current_Season
=
Seasons
.
query
.
filter_by
(
enable
=
1
)
.
first
()
season_players
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
season_id
==
current_Season
.
id
,
SeasonPlayers
.
season_enable
==
1
))
.
all
()
unaudited_season_players
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
season_id
==
current_Season
.
id
,
SeasonPlayers
.
season_enable
==
0
))
.
all
()
season_players_list
=
[]
unaudited_season_players_list
=
[]
for
season_player
in
season_players
:
season_playerInfo
=
{}
season_playerInfo
[
'player_id'
]
=
season_player
.
player_id
season_playerInfo
[
'score'
]
=
season_player
.
score
season_playerInfo
[
'group'
]
=
season_player
.
group
season_playerInfo
[
'total_games'
]
=
season_player
.
total_games
season_playerInfo
[
'winrate'
]
=
season_player
.
winrate
season_playerInfo
[
'player_real_name'
]
=
season_player
.
player
.
real_name
season_players_list
.
append
(
season_playerInfo
)
for
unaudited_season_player
in
unaudited_season_players
:
unaudited_season_playerInfo
=
{}
unaudited_season_playerInfo
[
'player_id'
]
=
unaudited_season_player
.
player_id
unaudited_season_playerInfo
[
'player_real_name'
]
=
unaudited_season_player
.
player
.
real_name
unaudited_season_players_list
.
append
(
unaudited_season_playerInfo
)
return
{
"status"
:
"SUCCESS"
,
"season_playersInfo"
:
season_players_list
,
"unaudited_season_playersInfo"
:
unaudited_season_players_list
}
@
app
.
route
(
'/api/playerAudit'
,
methods
=
[
'POST'
])
def
su_player_audit
():
player_audit
=
request
.
get_json
()
player_id
=
player_audit
.
get
(
"player_id"
)
group
=
player_audit
.
get
(
"group_number"
)
current_Season
=
Seasons
.
query
.
filter_by
(
enable
=
1
)
.
first
()
unaudited_player
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
season_id
==
current_Season
.
id
,
SeasonPlayers
.
season_enable
==
0
,
SeasonPlayers
.
player_id
==
player_id
))
.
first
()
unaudited_player
.
group
=
group
unaudited_player
.
season_enable
=
1
try
:
db
.
session
.
commit
()
return
{
"status"
:
"SUCCESS"
}
except
:
db
.
session
.
rollback
()
return
{
"status"
:
"FAILED"
}
@
app
.
route
(
'/api/playerManage'
,
methods
=
[
'POST'
])
def
su_player_manage
():
player_info
=
request
.
get_json
()
player_id
=
player_info
.
get
(
"player_id"
)
score
=
player_info
.
get
(
"score"
)
group
=
player_info
.
get
(
"group_number"
)
total_games
=
player_info
.
get
(
"total_games"
)
current_Season
=
Seasons
.
query
.
filter_by
(
enable
=
1
)
.
first
()
player
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
season_id
==
current_Season
.
id
,
SeasonPlayers
.
season_enable
==
1
,
SeasonPlayers
.
player_id
==
player_id
))
.
first
()
if
score
!=
None
:
player
.
score
=
score
if
group
!=
None
:
player
.
group
=
group
if
total_games
!=
None
:
player
.
total_games
=
total_games
if
total_games
==
0
:
player
.
winrate
=
0
else
:
player
.
winrate
=
(
player
.
score
/
player
.
total_games
)
*
100
try
:
db
.
session
.
commit
()
return
{
"status"
:
"SUCCESS"
}
except
:
db
.
session
.
rollback
()
return
{
"status"
:
"FAILED"
}
@
app
.
route
(
'/api/getGames'
,
methods
=
[
'GET'
])
def
su_get_games
():
current_Season
=
Seasons
.
query
.
filter_by
(
enable
=
1
)
.
first
()
season_id
=
current_Season
.
id
pages
=
request
.
args
.
get
(
"pages"
)
lim
=
request
.
args
.
get
(
"lim"
)
# player = data
if
pages
is
None
:
pages
=
1
else
:
pages
=
int
(
pages
)
if
lim
is
None
:
lim
=
10
else
:
lim
=
int
(
lim
)
totalGames
=
Games
.
query
.
filter
(
and_
(
Games
.
enable
==
1
,
Games
.
season_id
==
season_id
))
.
count
()
games
=
Games
.
query
.
order_by
(
desc
(
Games
.
game_date
))
.
filter
(
and_
(
Games
.
enable
==
1
,
Games
.
season_id
==
season_id
))
.
slice
((
pages
-
1
)
*
lim
,(
pages
)
*
lim
)
.
all
()
game_list
=
[]
for
game
in
games
:
if
game
.
enable
==
1
:
game_info
=
{}
game_info
[
'game_id'
]
=
game
.
id
game_info
[
'game_date'
]
=
game
.
game_date
.
strftime
(
'
%
Y/
%
m/
%
d'
)
game_info
[
'player1_id'
]
=
game
.
player1_id
game_info
[
'player1_real_name'
]
=
game
.
player1
.
real_name
game_info
[
'player1_profile_file'
]
=
game
.
player1
.
profile_file
game_info
[
'score1'
]
=
game
.
score1
game_info
[
'player2_id'
]
=
game
.
player2_id
game_info
[
'player2_real_name'
]
=
game
.
player2
.
real_name
game_info
[
'player2_profile_file'
]
=
game
.
player2
.
profile_file
game_info
[
'score2'
]
=
game
.
score2
game_info
[
'season_name'
]
=
game
.
season
.
season_name
game_list
.
append
(
game_info
)
return
{
"status"
:
"SUCCESS"
,
"Games"
:
game_list
,
"totalGames"
:
totalGames
}
@
app
.
route
(
'/api/gameManage'
,
methods
=
[
'POST'
])
def
su_game_manage
():
game_info
=
request
.
get_json
()
game_id
=
game_info
.
get
(
'game_id'
)
game_date
=
game_info
.
get
(
'game_date'
)
player1_id
=
game_info
.
get
(
'player1_id'
)
player2_id
=
game_info
.
get
(
'player2_id'
)
score1
=
game_info
.
get
(
'score1'
)
score2
=
game_info
.
get
(
'score2'
)
game
=
Games
.
query
.
filter
(
Games
.
id
==
game_id
)
.
first
()
old_game
=
copy
.
deepcopy
(
game
)
game
.
game_date
=
game_date
game
.
player1_id
=
player1_id
game
.
player2_id
=
player2_id
game
.
score1
=
score1
game
.
score2
=
score2
su_update_scores
(
game
,
old_game
)
try
:
db
.
session
.
commit
()
return
{
"status"
:
"SUCCESS"
}
except
:
db
.
session
.
rollback
()
return
{
"status"
:
"FAILED"
}
def
calculate_winrate
(
player
):
if
player
.
total_games
==
0
:
return
0
else
:
return
player
.
score
/
player
.
total_games
*
100
def
su_update_scores
(
game
,
old_game
):
current_Season
=
Seasons
.
query
.
filter_by
(
enable
=
1
)
.
first
()
if
game
.
player1_id
==
old_game
.
player1_id
and
game
.
player2_id
==
old_game
.
player2_id
:
#人不变
if
old_game
.
score1
>
old_game
.
score2
:
#人不变//一开始A赢
if
game
.
score1
>
game
.
score2
:
#人不变//一开始A赢//改成A赢
return
if
game
.
score1
<
game
.
score2
:
#人不变//一开始A赢//改成B赢x
player1
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
player_id
==
game
.
player1_id
,
SeasonPlayers
.
season_id
==
current_Season
.
id
))
.
first
()
player2
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
player_id
==
game
.
player2_id
,
SeasonPlayers
.
season_id
==
current_Season
.
id
))
.
first
()
if
player1
.
score
>
0
:
#A一开始不是零分,A减一分B加一分
player1
.
score
-=
1
player2
.
score
+=
1
player1
.
win_rate
=
calculate_winrate
(
player1
)
player2
.
win_rate
=
calculate_winrate
(
player2
)
if
old_game
.
score1
<
old_game
.
score2
:
#人不变//一开始B赢
if
game
.
score1
<
game
.
score2
:
#人不变//一开始B赢//改成B赢
return
if
game
.
score1
>
game
.
score2
:
#人不变//一开始B赢//改成A赢
player1
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
player_id
==
game
.
player1_id
,
SeasonPlayers
.
season_id
==
current_Season
.
id
))
.
first
()
player2
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
player_id
==
game
.
player2_id
,
SeasonPlayers
.
season_id
==
current_Season
.
id
))
.
first
()
player1
.
score
+=
1
if
player2
.
score
>
0
:
player2
.
score
-=
1
player1
.
win_rate
=
calculate_winrate
(
player1
)
player2
.
win_rate
=
calculate_winrate
(
player2
)
if
game
.
player1_id
!=
old_game
.
player1_id
and
game
.
player2_id
==
old_game
.
player2_id
:
#A变B不变
player_A
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
player_id
==
old_game
.
player1_id
,
SeasonPlayers
.
season_id
==
current_Season
.
id
))
.
first
()
player_B
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
player_id
==
old_game
.
player2_id
,
SeasonPlayers
.
season_id
==
current_Season
.
id
))
.
first
()
player_C
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
player_id
==
game
.
player1_id
,
SeasonPlayers
.
season_id
==
current_Season
.
id
))
.
first
()
if
player_A
.
total_games
>
0
:
player_A
.
total_games
-=
1
#A变,A场次减一
player_C
.
total_games
+=
1
#C加入,场次加一
if
old_game
.
score1
>
old_game
.
score2
:
#A变B不变 一开始A赢
if
player_A
.
score
>
0
:
player_A
.
score
-=
1
player_A
.
winrate
=
calculate_winrate
(
player_A
)
if
game
.
score1
>
game
.
score2
:
#A变B不变 一开始A赢 C进来C赢
if
player_A
.
score
>
0
:
player_A
.
score
-=
1
player_C
.
score
+=
1
player_C
.
winrate
=
calculate_winrate
(
player_C
)
player_A
.
winrate
=
calculate_winrate
(
player_A
)
if
game
.
score1
<
game
.
score2
:
#A变B不变 一开始A赢 C进来C输
player_B
.
score
+=
1
player_B
.
winrate
=
calculate_winrate
(
player_B
)
player_C
.
winrate
=
calculate_winrate
(
player_C
)
if
old_game
.
score1
<
old_game
.
score2
:
#A变B不变 一开始B赢
player_A
.
winrate
=
calculate_winrate
(
player_A
)
if
game
.
score1
>
game
.
score2
:
#A变B不变 一开始B赢 C进来C赢
player_C
.
score
+=
1
if
player_B
.
score
>
0
:
player_B
.
score
-=
1
player_B
.
winrate
=
calculate_winrate
(
player_B
)
player_C
.
winrate
=
calculate_winrate
(
player_C
)
if
game
.
score1
<
game
.
score2
:
#A变B不变 一开始B赢 C进来C输
player_C
.
winrate
=
calculate_winrate
(
player_C
)
if
game
.
player1_id
==
old_game
.
player1_id
and
game
.
player2_id
!=
old_game
.
player2_id
:
#A不变B变
player_A
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
player_id
==
old_game
.
player1_id
,
SeasonPlayers
.
season_id
==
current_Season
.
id
))
.
first
()
player_B
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
player_id
==
old_game
.
player2_id
,
SeasonPlayers
.
season_id
==
current_Season
.
id
))
.
first
()
player_C
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
player_id
==
game
.
player2_id
,
SeasonPlayers
.
season_id
==
current_Season
.
id
))
.
first
()
if
player_B
.
total_games
>
0
:
player_B
.
total_games
-=
1
#B变,B场次减一
player_C
.
total_games
+=
1
#C加入,场次加一
if
old_game
.
score1
<
old_game
.
score2
:
#A不变B变 一开始B赢
if
player_B
.
score
>
0
:
player_B
.
score
-=
1
player_B
.
winrate
=
calculate_winrate
(
player_B
)
if
game
.
score1
<
game
.
score2
:
#A不变B变 一开始B赢 C进来C赢
player_C
.
score
+=
1
player_C
.
winrate
=
calculate_winrate
(
player_C
)
if
player_B
.
score
>
0
:
player_B
.
score
-=
1
player_B
.
winrate
=
calculate_winrate
(
player_B
)
if
game
.
score1
<
game
.
score2
:
#A不变B变 一开始B赢 C进来C输
player_A
.
score
+=
1
player_A
.
winrate
=
calculate_winrate
(
player_A
)
player_C
.
winrate
=
calculate_winrate
(
player_C
)
if
old_game
.
score1
<
old_game
.
score2
:
#A不变B变 一开始A赢
player_B
.
winrate
=
calculate_winrate
(
player_B
)
if
game
.
score1
>
game
.
score2
:
#A不变B变 一开始A赢 C进来C赢
player_C
.
score
+=
1
player_C
.
winrate
=
calculate_winrate
(
player_C
)
if
player_A
.
score
>
0
:
player_A
.
score
-=
1
player_A
.
winrate
=
calculate_winrate
(
player_A
)
if
game
.
score1
<
game
.
score2
:
#A不变B变 一开始A赢 C进来C输
player_C
.
winrate
=
calculate_winrate
(
player_C
)
if
game
.
player1_id
!=
old_game
.
player1_id
and
game
.
player2_id
!=
old_game
.
player2_id
:
#A变B变
player_A
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
player_id
==
old_game
.
player1_id
,
SeasonPlayers
.
season_id
==
current_Season
.
id
))
.
first
()
player_B
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
player_id
==
old_game
.
player2_id
,
SeasonPlayers
.
season_id
==
current_Season
.
id
))
.
first
()
player_C
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
player_id
==
game
.
player1_id
,
SeasonPlayers
.
season_id
==
current_Season
.
id
))
.
first
()
player_D
=
SeasonPlayers
.
query
.
filter
(
and_
(
SeasonPlayers
.
player_id
==
game
.
player2_id
,
SeasonPlayers
.
season_id
==
current_Season
.
id
))
.
first
()
if
player_A
.
total_games
>
0
:
player_A
.
total_games
-=
1
if
player_B
.
total_games
>
0
:
player_B
.
total_games
-=
1
player_C
.
total_games
+=
1
player_C
.
total_games
+=
1
if
old_game
.
score1
>
old_game
.
score2
:
#变之前 A赢
if
player_A
.
score
>
0
:
player_A
.
score
-=
1
player_A
.
winrate
=
calculate_winrate
(
player_A
)
player_B
.
winrate
=
calculate_winrate
(
player_B
)
if
old_game
.
score1
<
old_game
.
score2
:
#变之前 B赢
if
player_B
.
score
>
0
:
player_B
.
score
-=
1
player_B
.
winrate
=
calculate_winrate
(
player_B
)
player_A
.
winrate
=
calculate_winrate
(
player_A
)
if
game
.
score1
>
game
.
score2
:
#C赢了
player_C
.
score
+=
1
player_C
.
winrate
=
calculate_winrate
(
player_C
)
player_D
.
winrate
=
calculate_winrate
(
player_D
)
if
game
.
score1
<
game
.
score2
:
#D赢了
player_D
.
score
+=
1
player_C
.
winrate
=
calculate_winrate
(
player_C
)
player_D
.
winrate
=
calculate_winrate
(
player_D
)
try
:
db
.
session
.
commit
()
return
{
"status"
:
"SUCCESS"
}
except
:
db
.
session
.
rollback
()
return
{
"status"
:
"FAILED"
}
if
__name__
==
'__main__'
:
with
app
.
app_context
():
db
.
create_all
()
app
.
run
(
port
=
8000
,
host
=
'0.0.0.0'
,
debug
=
True
)
\ No newline at end of file
clubwebapp/models.py
0 → 100644
View file @
683f5f9b
# models.py
from
flask_sqlalchemy
import
SQLAlchemy
from
flask_login
import
UserMixin
db
=
SQLAlchemy
()
class
User
(
db
.
Model
,
UserMixin
):
id
=
db
.
Column
(
db
.
Integer
,
primary_key
=
True
)
username
=
db
.
Column
(
db
.
String
(
80
),
unique
=
True
,
nullable
=
False
)
password
=
db
.
Column
(
db
.
String
(
120
),
unique
=
False
,
nullable
=
False
)
is_su
=
db
.
Column
(
db
.
Boolean
,
unique
=
False
,
nullable
=
False
,
default
=
False
)
is_member
=
db
.
Column
(
db
.
Boolean
,
unique
=
False
,
nullable
=
False
,
default
=
False
)
real_name
=
db
.
Column
(
db
.
String
(
80
),
unique
=
False
,
nullable
=
True
)
profile_file
=
db
.
Column
(
db
.
String
(
120
),
unique
=
False
,
nullable
=
True
,
default
=
"/avatar/default_avatar.jpg"
)
background_file
=
db
.
Column
(
db
.
String
(
120
),
unique
=
False
,
nullable
=
True
,
default
=
"/userBackground/default_background.jpg"
)
class
Seasons
(
db
.
Model
):
id
=
db
.
Column
(
db
.
Integer
,
primary_key
=
True
)
season_name
=
db
.
Column
(
db
.
String
(
80
),
unique
=
False
,
nullable
=
True
)
start_time
=
db
.
Column
(
db
.
Date
,
unique
=
False
,
nullable
=
False
)
end_time
=
db
.
Column
(
db
.
Date
,
unique
=
False
,
nullable
=
True
)
enable
=
db
.
Column
(
db
.
Boolean
,
unique
=
False
,
nullable
=
False
,
default
=
False
)
class
Games
(
db
.
Model
):
id
=
db
.
Column
(
db
.
Integer
,
primary_key
=
True
)
game_date
=
db
.
Column
(
db
.
DateTime
,
unique
=
False
,
nullable
=
False
)
# player1_id = db.Column(db.Integer, unique=False, nullable=False)
player1_id
=
db
.
Column
(
db
.
Integer
,
db
.
ForeignKey
(
'user.id'
),
unique
=
False
,
nullable
=
False
)
player1
=
db
.
relationship
(
"User"
,
foreign_keys
=
[
player1_id
],
uselist
=
False
)
score1
=
db
.
Column
(
db
.
SmallInteger
,
unique
=
False
,
nullable
=
False
)
# player2_id = db.Column(db.Integer, unique=False, nullable=False)
player2_id
=
db
.
Column
(
db
.
Integer
,
db
.
ForeignKey
(
'user.id'
),
unique
=
False
,
nullable
=
False
)
player2
=
db
.
relationship
(
"User"
,
foreign_keys
=
[
player2_id
],
uselist
=
False
)
score2
=
db
.
Column
(
db
.
SmallInteger
,
unique
=
False
,
nullable
=
False
)
enable
=
db
.
Column
(
db
.
SmallInteger
,
unique
=
False
,
nullable
=
False
,
default
=
False
)
# laucher_id = db.Column(db.Integer, unique=False, nullable=False)
laucher_id
=
db
.
Column
(
db
.
Integer
,
db
.
ForeignKey
(
'user.id'
),
unique
=
False
,
nullable
=
False
)
laucher
=
db
.
relationship
(
"User"
,
foreign_keys
=
[
laucher_id
],
uselist
=
False
)
lauch_date
=
db
.
Column
(
db
.
DateTime
,
unique
=
False
,
nullable
=
True
)
# confirmer_id = db.Column(db.Integer, unique=False, nullable=True)
confirmer_id
=
db
.
Column
(
db
.
Integer
,
db
.
ForeignKey
(
'user.id'
),
unique
=
False
,
nullable
=
False
)
confirmer
=
db
.
relationship
(
"User"
,
foreign_keys
=
[
confirmer_id
],
uselist
=
False
)
confirm_date
=
db
.
Column
(
db
.
DateTime
,
unique
=
False
,
nullable
=
True
)
season_id
=
db
.
Column
(
db
.
Integer
,
db
.
ForeignKey
(
'seasons.id'
),
unique
=
False
,
nullable
=
False
)
season
=
db
.
relationship
(
"Seasons"
,
foreign_keys
=
[
season_id
],
uselist
=
False
)
game_type
=
db
.
Column
(
db
.
Integer
,
unique
=
False
,
nullable
=
False
)
class
SeasonPlayers
(
db
.
Model
):
id
=
db
.
Column
(
db
.
Integer
,
primary_key
=
True
)
player_id
=
db
.
Column
(
db
.
Integer
,
db
.
ForeignKey
(
'user.id'
),
unique
=
False
,
nullable
=
False
)
player
=
db
.
relationship
(
"User"
,
foreign_keys
=
[
player_id
])
season_id
=
db
.
Column
(
db
.
Integer
,
db
.
ForeignKey
(
'seasons.id'
),
unique
=
False
,
nullable
=
False
)
season
=
db
.
relationship
(
"Seasons"
,
foreign_keys
=
[
season_id
])
score
=
db
.
Column
(
db
.
Integer
,
unique
=
False
,
nullable
=
False
,
default
=
0
)
group
=
db
.
Column
(
db
.
Integer
,
unique
=
False
,
nullable
=
True
)
total_games
=
db
.
Column
(
db
.
Integer
,
unique
=
False
,
nullable
=
False
,
default
=
0
)
winrate
=
db
.
Column
(
db
.
Integer
,
unique
=
False
,
nullable
=
False
,
default
=
0
)
season_enable
=
db
.
Column
(
db
.
Integer
,
unique
=
False
,
nullable
=
False
,
default
=-
1
)
class
Carousel
(
db
.
Model
):
id
=
db
.
Column
(
db
.
Integer
,
primary_key
=
True
)
carousel_file
=
db
.
Column
(
db
.
String
(
120
),
unique
=
False
,
nullable
=
True
)
link
=
db
.
Column
(
db
.
String
(
255
),
unique
=
False
,
nullable
=
False
)
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment