Skip to content

Commit 8eee3ce

Browse files
committed
重写ini设置相关
1 parent c86fe90 commit 8eee3ce

File tree

10 files changed

+498
-498
lines changed

10 files changed

+498
-498
lines changed

.github/workflows/python-app.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ jobs:
8383
8484
- name: Package with PyInstaller
8585
run: |
86-
pyinstaller -i "media/cat.ico" -w -y --clean --add-data "./media/*;./media" --add-data "./de_DE.qm;." --add-data "./pl_PL.qm;." --add-data "./en_US.qm;." -p src src/main.py -n metaminesweeper
86+
pyinstaller -i "./media/cat.ico" -w -y --clean --add-data "./media/*;./media" --add-data "./de_DE.qm;." --add-data "./pl_PL.qm;." --add-data "./en_US.qm;." -p src src/main.py -n metaminesweeper
8787
8888
- name: Move media and qm file
8989
run: |

src/gameScoreBoard.py

Lines changed: 59 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class gameScoreBoardManager():
7777

7878
# is_visible = False
7979
# 5、错误的表达式,一旦算出报错,永远不再算,显示error
80-
def __init__(self, r_path, score_board_path, game_setting_path, pix_size, parent):
80+
def __init__(self, r_path, score_board_setting, game_setting, pix_size, parent):
8181
# 从文件中读取指标并设置
8282
# self.ms_board = None
8383
self.pix_size = pix_size
@@ -88,42 +88,42 @@ def __init__(self, r_path, score_board_path, game_setting_path, pix_size, parent
8888
self.delta_time = 0.0
8989

9090
self.initialized = False
91-
self.game_setting_path = game_setting_path
92-
self.score_board_path = score_board_path
93-
config_score_board = configparser.ConfigParser()
94-
if config_score_board.read(self.score_board_path):
95-
# 计时器配置list[tuple(str, str)]
96-
_score_board_items = config_score_board.items('DEFAULT')
97-
else:
98-
config_score_board["DEFAULT"] = {
99-
"游戏模式": "mode",
100-
"RTime": "f'{time:.3f}'",
101-
"Est RTime": "f'{etime:.3f}'",
102-
"3BV": "f'{bbbv_solved}/{bbbv}'",
103-
"3BV/s": "f'{bbbv_s:.3f}'",
104-
"Ops": "op",
105-
"Isls": "isl",
106-
"Left": "f'{left}@{left_s:.3f}'",
107-
"Right": "f'{right}@{right_s:.3f}'",
108-
"Double": "f'{double}@{double_s:.3f}'",
109-
"STNB": "f'{stnb:.3f}'",
110-
"IOE": "f'{ioe:.3f}'",
111-
"Thrp": "f'{thrp:.3f}'",
112-
"Corr": "f'{corr:.3f}'",
113-
"Path": "f'{path:.1f}'",
114-
}
115-
_score_board_items = list(config_score_board.items('DEFAULT'))
116-
with open(self.score_board_path, 'w') as configfile:
117-
config_score_board.write(configfile) # 将对象写入文件
118-
self.score_board_items = [[i[0], mm.trans_expression(i[1])] for\
119-
i in _score_board_items]
91+
self.game_setting = game_setting
92+
self.score_board_setting = score_board_setting
93+
# self.score_board_setting.beginGroup('DEFAULT')
94+
# _score_board_items = self.score_board_setting.allKeys()
95+
# config_score_board = configparser.ConfigParser()
96+
# if not _score_board_items:
97+
default_config = [
98+
("游戏模式", "mode"),
99+
("RTime", "f'{time:.3f}'"),
100+
("Est RTime", "f'{etime:.3f}'"),
101+
("3BV", "f'{bbbv_solved}/{bbbv}'"),
102+
("3BV/s", "f'{bbbv_s:.3f}'"),
103+
("Ops", "op"),
104+
("Isls", "isl"),
105+
("Left", "f'{left}@{left_s:.3f}'"),
106+
("Right", "f'{right}@{right_s:.3f}'"),
107+
("Double", "f'{double}@{double_s:.3f}'"),
108+
("STNB", "f'{stnb:.3f}'"),
109+
("IOE", "f'{ioe:.3f}'"),
110+
("Thrp", "f'{thrp:.3f}'"),
111+
("Corr", "f'{corr:.3f}'"),
112+
("Path", "f'{path:.1f}'"),
113+
("Pluck", "f'{pluck:.3f}'"),
114+
]
115+
self.score_board_items = self.score_board_setting.get_or_set_section("DEFAULT", default_config)
116+
117+
self.score_board_setting.sync()
118+
120119
self.update_score_board_items_type()
121120
self.index_num = len(self.score_board_items_type)
122121
self.ui = ui_Form(r_path, pix_size, parent)
123122
self.ui.tableWidget.doubleClicked.connect(self.__table_change)
124123
self.ui.tableWidget.clicked.connect(self.__table_ok)
125-
self.ui.tableWidget.cellChanged.connect(self.__cell_changed)
124+
# self.ui.tableWidget.cellChanged.connect(self.__cell_changed)
126125
self.ui.pushButton_add.clicked.connect(self.__add_blank_line)
126+
self.ui.QWidget.closeEvent_.connect(self.close)
127127
QShortcut(QtGui.QKeySequence(QtCore.Qt.Key_Return), self.ui.QWidget).\
128128
activated.connect(self.__table_ok)
129129
self.editing_row = -1 # -1不在编辑状态,-2不能编辑(正在游戏)
@@ -213,7 +213,7 @@ def update_namespace(self, ms_board, index_type):
213213
"flag": ms_board.flag,
214214
"flag_s": ms_board.flag_s,
215215
})
216-
if index_type == 2:
216+
if index_type >= 2:
217217
self.namespace.update({
218218
"rtime": ms_board.rtime,
219219
"etime": ms_board.etime,
@@ -228,6 +228,9 @@ def update_namespace(self, ms_board, index_type):
228228
"corr": ms_board.corr,
229229
"ce": ms_board.ce,
230230
"ce_s": ms_board.ce_s,
231+
})
232+
if index_type >= 3:
233+
self.namespace.update({
231234
"pluck": ms_board.pluck,
232235
})
233236

@@ -300,28 +303,31 @@ def __table_ok(self, e = None):
300303
if e == None or (self.editing_row >= 0 and self.editing_column >= 0 and (self.editing_row != e.row() or\
301304
self.editing_column != e.column())):
302305
# 编辑完成后修改指标值
303-
self.ui.tableWidget.setDisabled(True)
304-
self.ui.tableWidget.setDisabled(False)
306+
# self.ui.tableWidget.setDisabled(True)
307+
# self.ui.tableWidget.setDisabled(False)
305308
new_formula = self.ui.tableWidget.item(self.editing_row, self.editing_column).text()
306309
if self.editing_column == 0:
307310
if not new_formula:
311+
# 删除键名后并完成编辑后,删除此指标
308312
self.score_board_items.pop(self.editing_row)
309313
self.score_board_items_type.pop(self.editing_row)
310314
else:
311-
self.score_board_items[self.editing_row][0] = new_formula
315+
# 正常修改键名
316+
self.score_board_items[self.editing_row] = (new_formula, self.score_board_items[self.editing_row][1])
312317
else:
313-
self.score_board_items[self.editing_row][1] = new_formula
318+
# 正常修改公式
319+
self.score_board_items[self.editing_row] = (self.score_board_items[self.editing_row][0], new_formula)
314320
self.update_score_board_items_type()
315321
self.reshow(self.ms_board)
316322
self.editing_row = -1
317323
self.editing_column = -1
318324

319-
def __cell_changed(self, x, y):
320-
# 把计数器里的公式改成新设置的公式
321-
if y == 0:
322-
t = self.ui.tableWidget.item(x, y).text()
323-
if self.score_board_items[x][0] != t:
324-
self.score_board_items[x][0] = self.ui.tableWidget.item(x, 0).text()
325+
# def __cell_changed(self, x, y):
326+
# # 把计数器里的公式改成新设置的公式
327+
# if y == 0:
328+
# t = self.ui.tableWidget.item(x, y).text()
329+
# if self.score_board_items[x][0] != t:
330+
# self.score_board_items[x][0] = self.ui.tableWidget.item(x, 0).text()
325331

326332
def __add_blank_line(self):
327333
# 添加一个空开的行,并刷新显示
@@ -330,15 +336,17 @@ def __add_blank_line(self):
330336
self.reshow(self.ms_board)
331337

332338
def close(self):
333-
config = configparser.ConfigParser()
334-
config["DEFAULT"] = dict(filter(lambda x: x[0], self.score_board_items))
335-
config.write(open(self.score_board_path, "w"))
336-
conf = configparser.ConfigParser()
337-
conf.read(self.game_setting_path, encoding='utf-8')
338-
conf.set("DEFAULT", "scoreBoardTop", str(self.ui.QWidget.x()))
339-
conf.set("DEFAULT", "scoreBoardLeft", str(self.ui.QWidget.y()))
340-
conf.write(open(self.game_setting_path, "w", encoding='utf-8'))
341-
self.ui.QWidget.close()
339+
self.score_board_setting.set_section("DEFAULT", self.score_board_items)
340+
self.score_board_setting.sync()
341+
# config = configparser.ConfigParser()
342+
# config["DEFAULT"] = dict(filter(lambda x: x[0], self.score_board_items))
343+
# config.write(open(self.score_board_path, "w"))
344+
# conf = configparser.ConfigParser()
345+
# conf.read(self.game_setting_path, encoding='utf-8')
346+
# conf.set("DEFAULT", "scoreBoardTop", str(self.ui.QWidget.x()))
347+
# conf.set("DEFAULT", "scoreBoardLeft", str(self.ui.QWidget.y()))
348+
# conf.write(open(self.game_setting_path, "w", encoding='utf-8'))
349+
# self.ui.QWidget.close()
342350

343351

344352

src/gameSettings.py

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,47 @@
1-
# -*- coding: utf-8 -*-
2-
3-
# Form implementation generated from reading ui file 'ui_gs.ui'
4-
#
5-
# Created by: PyQt5 UI code generator 5.9.2
6-
#
7-
# WARNING! All changes made in this file will be lost!
8-
9-
from PyQt5 import QtCore, QtGui, QtWidgets
1+
from PyQt5 import QtGui
102
import configparser
113
from ui.ui_gameSettings import Ui_Form
124
from ui.uiComponents import RoundQDialog
13-
from PyQt5.QtCore import QPoint
14-
# from PyQt5.QtWidgets import QWidget, QDialog
15-
import json
165
from country_name import country_name
176
from PyQt5.QtGui import QPixmap
187

198
class ui_Form(Ui_Form):
209
def __init__(self, mainWindow):
2110
# 设置界面的参数,不能用快捷键修改的从配置文件里来;能用快捷键修改的从mainWindow来
22-
self.game_setting_path = mainWindow.game_setting_path
11+
self.game_setting = mainWindow.game_setting
2312
self.r_path = mainWindow.r_path
24-
config = configparser.ConfigParser()
25-
config.read(self.game_setting_path, encoding='utf-8')
13+
# config = configparser.ConfigParser()
14+
# config.read(self.game_setting_path, encoding='utf-8')
2615
self.gameMode = mainWindow.gameMode
27-
self.transparency = config.getint('DEFAULT','transparency')
16+
self.transparency = self.game_setting.value('DEFAULT/transparency', None, int)
17+
# self.transparency = config.getint('DEFAULT','transparency')
2818
self.pixSize = mainWindow.pixSize
2919
self.row = mainWindow.row
3020
self.column = mainWindow.column
3121
self.mineNum = mainWindow.mineNum
3222

33-
self.auto_replay = config.getint("DEFAULT", "auto_replay")
34-
self.allow_auto_replay = config.getboolean("DEFAULT", "allow_auto_replay")
35-
self.auto_notification = config.getboolean("DEFAULT", "auto_notification")
36-
# self.board_constraint = config.getboolean("DEFAULT", "board_constraint")
37-
# self.attempt_times_limit = config.getboolean("DEFAULT", "attempt_times_limit")
23+
self.auto_replay = self.game_setting.value('DEFAULT/auto_replay', None, int)
24+
self.auto_notification = self.game_setting.value('DEFAULT/auto_notification', None, bool)
25+
# self.auto_replay = config.getint("DEFAULT", "auto_replay")
26+
# self.allow_auto_replay = config.getboolean("DEFAULT", "allow_auto_replay")
27+
# self.auto_notification = config.getboolean("DEFAULT", "auto_notification")
3828

39-
self.player_identifier = config["DEFAULT"]["player_identifier"]
40-
self.race_identifier = config["DEFAULT"]["race_identifier"]
41-
self.unique_identifier = config["DEFAULT"]["unique_identifier"]
42-
self.country = config["DEFAULT"]["country"]
43-
self.autosave_video = config.getboolean("DEFAULT", "autosave_video")
44-
self.filter_forever = config.getboolean("DEFAULT", "filter_forever")
45-
# self.auto_show_score = config.getint("DEFAULT", "auto_show_score") # 自动弹成绩
46-
self.end_then_flag = config.getboolean("DEFAULT", "end_then_flag") # 游戏结束后自动标雷
47-
self.cursor_limit = config.getboolean("DEFAULT", "cursor_limit")
29+
self.player_identifier = self.game_setting.value('DEFAULT/player_identifier', None, str)
30+
self.race_identifier = self.game_setting.value('DEFAULT/race_identifier', None, str)
31+
self.unique_identifier = self.game_setting.value('DEFAULT/unique_identifier', None, str)
32+
self.country = self.game_setting.value('DEFAULT/country', None, str)
33+
# self.player_identifier = config["DEFAULT"]["player_identifier"]
34+
# self.race_identifier = config["DEFAULT"]["race_identifier"]
35+
# self.unique_identifier = config["DEFAULT"]["unique_identifier"]
36+
# self.country = config["DEFAULT"]["country"]
37+
self.autosave_video = self.game_setting.value('DEFAULT/autosave_video', None, bool)
38+
self.filter_forever = self.game_setting.value('DEFAULT/filter_forever', None, bool)
39+
self.end_then_flag = self.game_setting.value('DEFAULT/end_then_flag', None, bool)
40+
self.cursor_limit = self.game_setting.value('DEFAULT/cursor_limit', None, bool)
41+
# self.autosave_video = config.getboolean("DEFAULT", "autosave_video")
42+
# self.filter_forever = config.getboolean("DEFAULT", "filter_forever")
43+
# self.end_then_flag = config.getboolean("DEFAULT", "end_then_flag") # 游戏结束后自动标雷
44+
# self.cursor_limit = config.getboolean("DEFAULT", "cursor_limit")
4845
self.board_constraint = mainWindow.board_constraint
4946
self.attempt_times_limit = mainWindow.attempt_times_limit
5047

@@ -96,8 +93,8 @@ def set_combobox_country(self, qtext):
9693

9794
def setParameter(self):
9895
self.spinBox_pixsize.setValue (self.pixSize)
99-
self.spinBox_auto_replay.setValue (self.auto_replay)
100-
self.checkBox_auto_replay.setChecked(self.allow_auto_replay)
96+
self.spinBox_auto_replay.setValue (abs(self.auto_replay))
97+
self.checkBox_auto_replay.setChecked(self.auto_replay >= 0)
10198
self.checkBox_auto_notification.setChecked(self.auto_notification)
10299
self.checkBox_autosave_video.setChecked(self.autosave_video)
103100
self.checkBox_filter_forever.setChecked(self.filter_forever)
@@ -132,8 +129,9 @@ def processParameter(self):
132129
self.alter = True
133130
self.transparency = self.horizontalSlider_transparency.value()
134131
self.pixSize = self.spinBox_pixsize.value()
135-
self.auto_replay = self.spinBox_auto_replay.value()
136-
self.allow_auto_replay = self.checkBox_auto_replay.isChecked()
132+
v = self.spinBox_auto_replay.value()
133+
self.auto_replay = v if self.checkBox_auto_replay.isChecked() else -v
134+
# self.allow_auto_replay = self.checkBox_auto_replay.isChecked()
137135
self.auto_notification = self.checkBox_auto_notification.isChecked()
138136
self.player_identifier = self.lineEdit_label.text()
139137
self.race_identifier = self.lineEdit_race_label.text()

src/main.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import time
33
from PyQt5 import QtWidgets
44
from PyQt5 import QtCore
5-
from PyQt5.QtWidgets import QApplication,QMessageBox
5+
from PyQt5.QtWidgets import QApplication
66
from PyQt5.QtNetwork import QLocalSocket,QLocalServer
77
import sys, os
88
import mainWindowGUI as mainWindowGUI
@@ -76,7 +76,7 @@ def find_window(class_name, window_name):
7676
mainWindow = mainWindowGUI.MainWindow()
7777
ui = mineSweeperGUI.MineSweeperGUI(mainWindow, sys.argv)
7878
ui.mainWindow.show()
79-
ui.mainWindow.game_setting_path = ui.game_setting_path
79+
# ui.mainWindow.game_setting = ui.game_setting
8080

8181
_translate = QtCore.QCoreApplication.translate
8282
hwnd = find_window(None, _translate("MainWindow", "元扫雷"))
@@ -86,8 +86,8 @@ def find_window(class_name, window_name):
8686
ui.enable_screenshot = lambda: ... if SetWindowDisplayAffinity(hwnd, 0x00000000) else 1/0
8787

8888
sys.exit(app.exec_())
89-
...
90-
except Exception as e:
89+
...
90+
except:
9191
pass
9292

9393
# 最高优先级

0 commit comments

Comments
 (0)