-
Notifications
You must be signed in to change notification settings - Fork 1
/
text_handling.py
366 lines (308 loc) · 13 KB
/
text_handling.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# text_handling.py
# Copyright (c) 2024 by @Cromha
#
# Bane Of Wargs is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later version.
#
# Bane Of Wargs is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
# PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program. If not, see <https://www.gnu.org/licenses/>.
# source imports
import logger_sys
import time_handling
import terminal_handling
from colors import *
from terminal_handling import cout, cinput
# external imports
import time
import random
import appdirs
import fade
import io
from sys import exit
# Handling functions
program_dir = str(appdirs.user_config_dir(appname='Bane-Of-Wargs'))
def print_speech_text_effect(text, preferences):
new_input = print_long_string(text, no_output=True) + "\n"
if not preferences["speed up"]:
wait = random.uniform(.05, .1)
else:
wait = .02
for character in new_input:
cout(character, end="")
time.sleep(wait)
def clear_prompt():
from os import system, name
# for windows
if name == 'nt':
_ = system('cls')
# for mac and linux(here, os.name is 'posix')
else:
_ = system('clear')
def exit_game():
time.sleep(1.5)
cout(COLOR_YELLOW + "Warning: closing game now" + COLOR_RESET_ALL)
logger_sys.log_message("WARNING: closing game now")
time.sleep(.5)
clear_prompt()
logger_sys.log_message(f"INFO: PROGRAM RUN END")
exit(1)
def print_title(preferences):
if preferences["theme"] == "OFF":
with open(program_dir + '/game/imgs/Title' + str(preferences["title style"]) + '.txt', 'r') as f:
cout(f.read())
else:
if preferences["theme"] == "blackwhite":
with open(program_dir + '/game/imgs/Title' + str(preferences["title style"]) + '.txt', 'r') as f:
faded_text = fade.blackwhite(f.read())
cout(faded_text)
elif preferences["theme"] == "purplepink":
with open(program_dir + '/game/imgs/Title' + str(preferences["title style"]) + '.txt', 'r') as f:
faded_text = fade.purplepink(f.read())
cout(faded_text)
elif preferences["theme"] == "greenblue":
with open(program_dir + '/game/imgs/Title' + str(preferences["title style"]) + '.txt', 'r') as f:
faded_text = fade.greenblue(f.read())
cout(faded_text)
elif preferences["theme"] == "water":
with open(program_dir + '/game/imgs/Title' + str(preferences["title style"]) + '.txt', 'r') as f:
faded_text = fade.water(f.read())
cout(faded_text)
elif preferences["theme"] == "fire":
with open(program_dir + '/game/imgs/Title' + str(preferences["title style"]) + '.txt', 'r') as f:
faded_text = fade.fire(f.read())
cout(faded_text)
elif preferences["theme"] == "pinkred":
with open(program_dir + '/game/imgs/Title' + str(preferences["title style"]) + '.txt', 'r') as f:
faded_text = fade.pinkred(f.read())
cout(faded_text)
elif preferences["theme"] == "purpleblue":
with open(program_dir + '/game/imgs/Title' + str(preferences["title style"]) + '.txt', 'r') as f:
faded_text = fade.purpleblue(f.read())
cout(faded_text)
elif preferences["theme"] == "brazil":
with open(program_dir + '/game/imgs/Title' + str(preferences["title style"]) + '.txt', 'r') as f:
faded_text = fade.brazil(f.read())
cout(faded_text)
elif preferences["theme"] == "random":
with open(program_dir + '/game/imgs/Title' + str(preferences["title style"]) + '.txt', 'r') as f:
faded_text = fade.random(f.read())
cout(faded_text)
def select_save(options, length=52):
options += ['EXIT']
choice = terminal_handling.show_menu(options, length)
if choice == 'EXIT':
clear_prompt()
logger_sys.log_message(f"INFO: PROGRAM RUN END")
exit(0)
return
return choice
def print_separator(character):
cout(COLOR_STYLE_BRIGHT + (character * 55) + COLOR_RESET_ALL)
def overstrike_text(text):
result = ""
for character in text:
result += character + '\u0336'
cout(str(result))
def print_long_string(text, no_output=False):
new_input = ""
dont = False
count = 0
letters = []
for i, letter in enumerate(text):
letters += [letter]
if letter == '@':
if not dont:
dont = True
elif dont:
dont = False
if count % 55 == 0:
new_input += '\n'
if letter != '@':
new_input += letter
if not dont:
count += 1
# this is just because at the beginning too, a `\n` character gets added
new_input = new_input[1:]
new_input_list = new_input.split(' ')
count = 0
for word in new_input_list:
if "\n" in word and not word.startswith('\n') and not word.endswith('\n'):
new_input_list[count] = "\n" + word.replace('\n', '')
count += 1
new_input = ""
for word in new_input_list:
new_input += word + " "
# other formatting stuff
new_input = new_input.replace('\n ', '\n')
new_input_list = list(new_input)
if new_input.endswith('\n'):
new_input_list = list(new_input)[:len(list(new_input)) - 1]
new_input = ""
for letter in new_input_list:
new_input += letter
if no_output:
return new_input
else:
cout(str(new_input))
def apply_yaml_data_color_code(to_print):
# Convert it to a proper string in case
to_print = str(to_print)
to_print = to_print.replace('$RED', '\033[0;31m')
to_print = to_print.replace('$DARK_RED', '\033[38;2;139;0;0m')
to_print = to_print.replace('$GREEN', '\033[0;32m')
to_print = to_print.replace('$DARK_GREEN', '\033[38;2;0;51;25m')
to_print = to_print.replace('$YELLOW', '\033[38;2;255;255;0m')
to_print = to_print.replace('$ORANGE', '\033[38;2;255;128;0m')
to_print = to_print.replace('$BLUE', '\033[0;34m')
to_print = to_print.replace('$DARK_BLUE', '\033[38;2;0;0;128m')
to_print = to_print.replace('$LIGHT_BLUE', '\033[38;2;30;144;255m')
to_print = to_print.replace('$PURPLE', '\033[0;34m')
to_print = to_print.replace('$PINK', '\033[38;2;255;0;127m')
to_print = to_print.replace('$CYAN', '\033[0;36m')
to_print = to_print.replace('$WHITE', COLOR_RESET_ALL)
to_print = to_print.replace('$BLACK', '\033[0;30m')
to_print = to_print.replace('$LIGHT_BLACK', '\033[38;2;46;46;46')
to_print = to_print.replace('$BROWN', '\033[38;2;244;164;96m')
to_print = to_print.replace('$TAN', '\033[38;2;210;180;140m')
to_print = to_print.replace('$DARK_BROWN', '\033[38;2;139;69;19m')
to_print = to_print.replace('$GRAY', '\033[1;30m')
to_print = to_print.replace('$LIGHT_GRAY', '\033[38;2;192;192;192m')
to_print = to_print.replace('$KHAKI', '\033[38;2;240;230;140m')
to_print = to_print.replace('$SILVER', '\033[38;2;245;245;245m')
return to_print
def print_zone_map(zone_name, zone, player, preferences):
logger_sys.log_message(f"INFO: Printing zone map '{zone_name}' ascii art")
to_print = zone[zone_name]["map"]["map full"]
to_print = apply_yaml_data_color_code(to_print)
player_equipment = []
logger_sys.log_message("INFO: Updating player equipped items")
if player["held item"] != " ":
player_equipment.append(player["held item"])
if player["held chestplate"] != " ":
player_equipment.append(player["held chestplate"])
if player["held leggings"] != " ":
player_equipment.append(player["held leggings"])
if player["held boots"] != " ":
player_equipment.append(player["held boots"])
player_equipment = str(player_equipment)
player_equipment = player_equipment.replace("'", "")
count = 0
logger_sys.log_message("INFO: Printing UI")
for line in to_print.splitlines():
if count == 0:
cout(line + " NAME: " + preferences["latest preset"]["save"])
if count == 1:
cout(
line + " HEALTH: " + COLOR_STYLE_BRIGHT + COLOR_BLUE +
str(player["health"]) + COLOR_RESET_ALL + "/" + COLOR_STYLE_BRIGHT +
COLOR_BLUE + str(player["max health"]) + COLOR_RESET_ALL
)
if count == 2:
cout(
line + " INVENTORY: " + COLOR_STYLE_BRIGHT + COLOR_CYAN +
str(len(player["inventory"]) + 1) + COLOR_RESET_ALL + "/" +
COLOR_STYLE_BRIGHT + COLOR_CYAN + str(player["inventory slots"]) + COLOR_RESET_ALL
)
if count == 3:
date = time_handling.date_prettifier(
time_handling.addition_to_date(player["starting date"], int(player["elapsed time game days"]))
)
cout(
line + " DATE: " + COLOR_STYLE_BRIGHT + COLOR_MAGENTA +
str(date) + COLOR_RESET_ALL
)
if count == 4:
cout(line + " EXP: " + COLOR_STYLE_BRIGHT + COLOR_GREEN + str(round(player["xp"], 2)) + COLOR_RESET_ALL)
if count == 5:
cout(line + " GOLD: " + COLOR_STYLE_BRIGHT + COLOR_YELLOW + str(round(player["gold"], 2)) + COLOR_RESET_ALL)
count += 1
def print_zone_map_alone(zone_name, zone):
logger_sys.log_message(f"INFO: Printing zone map '{zone_name}' ascii art")
to_print = zone[zone_name]["map"]["map full"]
cout(apply_yaml_data_color_code(to_print))
def print_npc_thumbnail(npc, preferences):
logger_sys.log_message(f"INFO: Printing NPC '{npc}' thumbnail")
with open(program_dir + '/temp/imgs/' + npc + ".txt") as f:
cout(apply_yaml_data_color_code(f.read()))
def print_enemy_thumbnail(enemy, preferences):
logger_sys.log_message(f"INFO: Printing enemy '{enemy}' thumbnail")
with open(program_dir + '/temp/imgs/' + enemy + ".txt") as f:
to_print = cout(apply_yaml_data_color_code(f.read()))
def a_an_check(word):
logger_sys.log_message(f"INFO: Checking correct grammar of 'a' in front of '{word}'")
global to_return
vowels = ['a', 'e', 'i', 'o', 'u']
if str(word.lower()[0]) in vowels:
to_return = "an " + word
elif str(word.lower()[0]) == 'h':
if str(word.lower()[1]) in vowels:
to_return = "an " + word
else:
to_return = "a " + word
logger_sys.log_message(f"INFO: Checked correct grammar of 'a' in front of '{word}': '{to_return}'")
return to_return
def print_item_thumbnail(to_print):
cout(apply_yaml_data_color_code(to_print))
return to_print
def multiple_items_in_list_formatting(list_to_format):
output_list = []
different_items = []
# Get different items in the list and then
# count how many times these items appear
# in the list, to after add the formatted
# output in the formatted list
for i in list_to_format:
if i not in different_items:
different_items += [i]
for i in different_items:
number_of_items = list_to_format.count(i)
if number_of_items > 1:
output_list += [f"{i}X{number_of_items}"]
else:
output_list += [f"{i}"]
return output_list
def transform_negative_number_to_positive(number):
if str(number).startswith("-"):
number = int(str(number).replace("-", ""))
return number
def print_map_art(item_data):
# Get the path of the file
# Then, each lines of the file one by one and
# prettify them, to after print the final result
# to the player's UI
path = f"{program_dir}/temp/imgs/{item_data['map']}.txt"
with open(path, 'r') as f:
art = f.readlines()
human_civilizations = ['⌂', '⟰', '⤊', '±', '⇭']
for line in art:
line = line.replace('\n', '')
line = line.replace('≈', '\033[38;2;250;223;199m' + "≈" + '\033[38;2;255;208;166m')
for character in human_civilizations:
line = line.replace(character, '\033[38;2;255;195;141m' + character + '\033[38;2;255;208;166m')
cout("║" + '\033[38;2;255;208;166m' + line.replace('\n', '') + COLOR_RESET_ALL + "║")
return art
def print_moving_text(text):
clear_prompt()
finished = False
text = text.split("\n")
columns, lines = terminal_handling.get_size()
count = 0
while not finished:
try:
for i in range(lines):
cout(text[count])
count += 1
count2 = 0
while (count + count2) < len(text) - 1:
time.sleep(random.uniform(1, 2))
cout(text[count+count2])
count2 += 1
finished = True
except EOFError as stop:
finished = True
time.sleep(2.5)