-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutil.py
190 lines (165 loc) · 5.86 KB
/
util.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
import time, json, sys, time, random, os, datetime
import savesengine, value, constant
# timestamps
def getTimeStamp() -> float:
"""
Gets you current timestamp\n
Note: timestamp is amount of seconds since Epoch\n
---
Epoch was in `1 Jan 1970 00:00`
"""
return time.time();
def getTimeStampNS() -> float:
"""
Gets you current timestamp in nano seconds\n
Note: timestamp is amount of seconds since Epoch\n
---
Epoch was in `1 Jan 1970 00:00`\n
A nanosecond is `one billionth of a second`.
"""
return time.time_ns();
def saveAndExit(save : dict):
"""
Saves a save and exits immediately.\n
If save went wrong somewhy, it will ask user\n
for further actions.
"""
if savesengine.validateSave(save):
result = savesengine.writeSave(save)
if result: exit(0)
if result==False:
print("Save wasn't saved somewhy, exit anyway? (Y/n)")
prompt = userChoice("", ["y", "n"])
if prompt == "y": exit(0)
if prompt == "n":
print("Print json-formatted save so you can save it manually? (Y/n)")
prompt = userChoice("", ["y", "n"])
if prompt == "y": print(str(save).replace("\'", "\""))
exit();
def eraseFileContents(path: str = ""):
"""Erase file contents that is determinied in `path` argument."""
if path == "": raise Exception("It looks like you forgot to give me the path")
open(path, "w").close()
def ask_restart():
"""Prints `value.plsrestart`"""
print(value.plsrestart)
def check_pin(pin : int) -> None:
"""Check pin"""
inputpin=-1;
while inputpin == -1:
try: inputpin = int(input(value.pincodeprompt.replace("%PINCODE%", str(pin)))) # .replace("%PINCODE%", pin) is doing the placeholder
except Exception: print(value.pincodenotint)
if inputpin != pin:
print(value.pincodeswrong)
sys.exit(pin-1)
def sleepforawhile(max : int = 2) -> None:
"""Sleep for a while for making it more human-like"""
time.sleep(random.randrange(0, max))
def percentual(max : int) -> int:
"""
The Max value that you want to use should be the limit that is included\n
Example: I want to do a probability within `1` and `100`, `100` included, so I write that `max` is `100`
"""
#global perc # NEVER DO THAT. NEVER.
return random.randrange(1, max+1)
def fancyPrint(out: str, end="\n", speed=0.05) -> None:
"""
Prints a value to console, with a little delay between each letter\n
Except space and `\\n`.
"""
listt = list(out)
for i in listt:
print(i, end="")
if i != "\n" or i != " ":
time.sleep(random.uniform(0, speed))
print(end=end)
def fastFancyPrint(out: str, end="\n") -> None:
"""
Same as fancyPrint but faster.
"""
listt = list(out)
for i in listt:
print(i, end="")
if i != "\n" or i != " ":
time.sleep(random.uniform(0, 0.01))
print(end=end)
def userInput(prompt: str = "") -> str:
"""Get user input and then lowercase it"""
return input(str).lower()
def dinput(fprint: bool = False) -> str:
"""Default user input(with fancy priny)"""
fastFancyPrint(value.defaultprompt, end="")
return userInput()
def userChoice(msg: str, choices: list, wrongchoice=value.wrongchoice) -> str:
"""
Provides fast user choice interface.\n
`msg` is the message you want the user to see,\n
and the `choices` is the list of possible choices.
"""
fastFancyPrint(msg)
while True:
ans = dinput()
for i in choices:
if ans == str(i):
return ans;
print(wrongchoice)
def isint(string: str) -> bool:
"""
`isint("abc")` -> `False`\n
`isint("one23")` -> `False`\n
`isint("123")` -> `True`\n
`isint("З")` -> `False` (because its a cyrillic letter instead of number)
"""
try:
int(string);
return True;
except Exception:
return False;
def cls() -> None:
"""Runs `cls` cmd command if you are using windows nt."""
if os.name == 'nt':
os.system('cls')
def gamelogo():
"""Prints a game logo that determines in values.py"""
cls()
for i in value.logo.split("\n"):
fancyPrint(i, speed=0.00001)
print(" ", end="")
time.sleep(0.75)
fancyPrint("RPGame studios (C)", speed=0.3)
time.sleep(5)
cls()
def verbose(msg: str, msgend: str = "\n", saveLog: bool = True, cmdPrint: bool = False, fprint: bool = False):
"""
msg = Message to write to verbose. Cannot be an empty str.\n
msgend = End of the message. Used by both file and print.\n
saveLog = save to log file.\n
cmdPrint = if you want to print your message to console.\n
fprint = use `fancyPrint` if true.\n
"""
if msg == "": raise Exception("Message must contain data!")
if saveLog:
# Check if file name contains placeholder
if ("%TIME%" in constant.LOGFILENAME) == False:
raise Exception("constant.LOGFILENAME must contain %TIME%\n constant.LOGFILENAME = " + constant.LOGFILENAME)
# Generate time string & replace placeholder
now = datetime.datetime.now()
timestr = f"{now.hour}:{now.second}_{now.month}.{now.day}.{now.year}"
fname = constant.LOGFILENAME.replace("%TIME%", timestr)
fp = f"{constant.LOGFILESDIR}/{fname}.{constant.LOGFILEEXTN}"
# Do file magic
f = open(fp, "r", encoding="utf-8")
buffer = f.read()
f.close()
f = open(fp, "w+", encoding="utf-8")
f.write(buffer+msgend+msg)
f.close()
f=None # clean memory
if cmdPrint:
task_completed = False
if fprint:
fancyPrint(msg, end=msgend)
task_completed = True
if task_completed is False:
print(msg)
task_completed = True