Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Y
yugioh-ccb
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Locked Files
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Security & Compliance
Security & Compliance
Dependency List
License Compliance
Packages
Packages
List
Container Registry
Analytics
Analytics
CI / CD
Code Review
Insights
Issues
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
MyCard
yugioh-ccb
Commits
e05ada0d
Commit
e05ada0d
authored
May 02, 2025
by
EN1AK
Committed by
GitHub
May 02, 2025
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add files via upload
parent
c8746b10
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
126 additions
and
11 deletions
+126
-11
build.py
build.py
+82
-0
guess_card_game.py
guess_card_game.py
+44
-11
No files found.
build.py
0 → 100644
View file @
e05ada0d
#!/usr/bin/env python3
# build.py — 自动化调用 PyInstaller 以生成精简版可执行文件
import
subprocess
import
sys
import
shutil
import
os
from
pathlib
import
Path
# ========== 配置区 ==========
# 入口脚本
ENTRY_SCRIPT
=
"guess_card_game.py"
# 要打包的数据库文件
DB_FILE
=
"cards.cdb"
# 要打包的模板目录
TEMPLATE_DIR
=
"templates"
# 输出目录
DIST_DIR
=
"dist"
BUILD_DIR
=
"build"
SPEC_FILE
=
ENTRY_SCRIPT
.
replace
(
".py"
,
".spec"
)
# UPX 可执行文件所在路径(Windows 下一般安装在 C:\Program Files\upx-5.0.0-win64)
UPX_DIR
=
r"C:\Program Files\upx-5.0.0-win64"
# 要排除的模块列表
EXCLUDE_MODULES
=
[
"tkinter"
,
"pytest"
,
"unittest"
,
"pdb"
,
"numpy.tests"
,
"pandas.tests"
,
]
# =============================
def
run
(
cmd
):
print
(
f
">>> {' '.join(cmd)}"
)
res
=
subprocess
.
run
(
cmd
,
shell
=
False
)
if
res
.
returncode
!=
0
:
sys
.
exit
(
res
.
returncode
)
def
main
():
# 1. 清理上次 build
for
path
in
(
BUILD_DIR
,
DIST_DIR
,
SPEC_FILE
):
p
=
Path
(
path
)
if
p
.
exists
():
if
p
.
is_dir
():
shutil
.
rmtree
(
p
)
else
:
p
.
unlink
()
# 2. 检查 UPX
if
not
Path
(
UPX_DIR
)
.
exists
():
print
(
f
"[!] 没找到 UPX:{UPX_DIR},将跳过 UPX 压缩"
)
upx_arg
=
[]
else
:
upx_arg
=
[
"--upx-dir"
,
UPX_DIR
]
# 3. 构造 PyInstaller 命令
cmd
=
[
sys
.
executable
,
"-m"
,
"PyInstaller"
,
"--onefile"
,
"--clean"
,
*
upx_arg
,
]
# 3.1 把 cards.cdb 和 templates 目录都加入到可执行文件数据区
# os.pathsep 在 Windows 下是 ';',在 macOS/Linux 下是 ':'
data_entries
=
[
f
"{DB_FILE}{os.pathsep}."
,
f
"{TEMPLATE_DIR}{os.pathsep}{TEMPLATE_DIR}"
,
]
for
entry
in
data_entries
:
cmd
+=
[
"--add-data"
,
entry
]
# 3.2 排除不需要的模块
for
mod
in
EXCLUDE_MODULES
:
cmd
+=
[
"--exclude-module"
,
mod
]
# 3.3 最后加上入口脚本
cmd
+=
[
ENTRY_SCRIPT
]
# 4. 运行打包
run
(
cmd
)
print
(
"
\n
✅ 打包完成!可执行文件在"
,
Path
(
DIST_DIR
)
/
Path
(
ENTRY_SCRIPT
)
.
stem
)
if
__name__
==
"__main__"
:
main
()
guess_card_game.py
View file @
e05ada0d
...
...
@@ -3,8 +3,13 @@ import sqlite3
import
pandas
as
pd
import
random
import
numbers
from
pathlib
import
Path
import
sys
,
os
app
=
Flask
(
__name__
)
base_path
=
getattr
(
sys
,
"_MEIPASS"
,
os
.
path
.
dirname
(
__file__
))
template_folder
=
os
.
path
.
join
(
base_path
,
"templates"
)
app
=
Flask
(
__name__
,
template_folder
=
template_folder
)
db
=
None
target_row
=
None
app
.
secret_key
=
"你自己的随机 Secret Key"
...
...
@@ -44,11 +49,36 @@ def extract_arrows(def_value):
return
[
sym
for
bit
,
sym
in
LINK_MARKERS
.
items
()
if
def_value
&
bit
]
def
load_card_database
(
path
):
conn
=
sqlite3
.
connect
(
path
)
# 先把两个表读进 DataFrame
def
load_card_database
(
path
:
str
=
None
)
->
pd
.
DataFrame
:
"""
加载 cards.cdb 里的 datas 和 texts 两张表,
合并、去重、按 id 排序后返回一个 DataFrame。
如果不传入 path,则自动:
· 在 PyInstaller 打包后的环境中,从 sys._MEIPASS 找到临时目录里的 cards.cdb
· 否则从当前脚本同级目录下加载 cards.cdb
"""
# 1. 自动定位数据库文件
if
path
is
None
:
# PyInstaller 打包后会把数据放到 _MEIPASS 里
base
=
getattr
(
sys
,
"_MEIPASS"
,
None
)
if
base
is
None
:
# 普通脚本运行,数据库和脚本在同一个目录
base
=
Path
(
__file__
)
.
parent
else
:
# 打包执行时,_MEIPASS 已经是一个 str 临时目录
base
=
Path
(
base
)
db_file
=
base
/
"cards.cdb"
else
:
db_file
=
Path
(
path
)
if
not
db_file
.
exists
():
raise
FileNotFoundError
(
f
"找不到数据库文件:{db_file}"
)
# 2. 连接并读取表
conn
=
sqlite3
.
connect
(
str
(
db_file
))
datas
=
pd
.
read_sql_query
(
"SELECT id, type, atk, def, level, race, attribute, category
,hot,
setcode FROM datas"
,
"SELECT id, type, atk, def, level, race, attribute, category
, hot,
setcode FROM datas"
,
conn
,
index_col
=
"id"
)
texts
=
pd
.
read_sql_query
(
...
...
@@ -56,12 +86,15 @@ def load_card_database(path):
conn
,
index_col
=
"id"
)
conn
.
close
()
# 合并
# 3. 合并去重并返回
df
=
datas
.
join
(
texts
,
how
=
"inner"
)
.
reset_index
()
# 按 id 升序排序,drop_duplicates 保留每个 name 的第一个(即最小 id)
df
=
df
.
sort_values
(
"id"
)
.
drop_duplicates
(
subset
=
"name"
,
keep
=
"first"
)
# 以 id 重新设为索引
df
=
df
.
set_index
(
"id"
)
df
=
(
df
.
sort_values
(
"id"
)
.
drop_duplicates
(
subset
=
"name"
,
keep
=
"first"
)
.
set_index
(
"id"
)
)
return
df
def
card_to_tags
(
row
):
...
...
@@ -322,5 +355,5 @@ def suggest():
return
jsonify
(
matches
)
if
__name__
==
"__main__"
:
db
=
load_card_database
(
"cards.cdb"
)
db
=
load_card_database
()
app
.
run
(
debug
=
True
)
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