forked from mathewthe2/Game2Text
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamescript.py
92 lines (81 loc) · 2.94 KB
/
gamescript.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import glob
from pathlib import Path
from tkinter import *
from tkinter.filedialog import askopenfile
from shutil import copyfile
from fuzzywuzzy import process
from config import r_config, w_config, LOG_CONFIG, SCRIPT_MATCH_CONFIG
from tools import bundle_dir
GAME_SCRIPT_PATH = Path(bundle_dir, 'gamescripts')
MATCH_LIMIT= int(r_config(SCRIPT_MATCH_CONFIG, 'match_limit'))
CONFIDENCE_THRESHOLD= int(r_config(SCRIPT_MATCH_CONFIG, 'confidence_threshold'))
REGIONAL_SCAN_AREA = 100
current_gamescript = ''
gamescript_dict = {}
last_match_line = None
def open_game_script():
root = Tk()
root.withdraw()
file = askopenfile(initialdir=str(GAME_SCRIPT_PATH), filetypes = (("TXT files","*.txt"),("all files","*.*")), defaultextension=".txt")
if not file:
return
try:
new_file = str(Path(GAME_SCRIPT_PATH, Path(file.name).stem + '.txt'))
copyfile(file.name, new_file)
w_config(LOG_CONFIG, {'gamescriptfile': file.name})
except:
print('File not selected')
file.close()
root.destroy()
return Path(file.name).stem
def load_game_scripts():
files = glob.glob(str(Path(GAME_SCRIPT_PATH, '*.txt')))
return [{
'name': Path(file).stem,
'path': str(Path(file))
} for file in files]
def get_regional_scan_lines():
if last_match_line is None:
return gamescript_dict
else:
lower = last_match_line - REGIONAL_SCAN_AREA
upper = 0
if lower < 0:
upper = lower * -1
lower = 0
upper += last_match_line + REGIONAL_SCAN_AREA
if upper > len(gamescript_dict):
upper = len(gamescript_dict)
lines = {}
print('lower',lower)
print('upper', upper)
for index in range(lower, upper):
lines[index] = gamescript_dict[index]
return lines
def init_gamescript(gamescript):
if (Path(gamescript).is_file()):
global current_gamescript
current_gamescript = gamescript
with open(gamescript, 'r', encoding='utf-8') as f:
global gamescript_dict
gamescript_dict = {index: line.strip() for index, line in enumerate(f)}
f.close()
def add_matching_script_to_logs(gamescript, logs):
if (gamescript != current_gamescript):
init_gamescript(gamescript)
if (gamescript_dict is None):
init_gamescript(gamescript)
lines = get_regional_scan_lines()
for log in logs:
matches = process.extract(log['text'], lines, limit=MATCH_LIMIT)
log['matches'] = matches
if (matches):
if (matches[0][1] > CONFIDENCE_THRESHOLD):
global last_match_line
last_match_line = matches[0][2]
else:
if (last_match_line): # Regional scan failed
last_match_line = None
add_matching_script_to_logs(gamescript, logs)
last_match_line = None
return logs