2021-11-02 17:40:54 +01:00
|
|
|
import yaml
|
|
|
|
from yaml.loader import SafeLoader
|
2021-11-03 10:23:37 +01:00
|
|
|
from colorama import Fore, Back, Style
|
2021-11-02 17:40:54 +01:00
|
|
|
|
|
|
|
class Game:
|
|
|
|
def __init__(self,name:str,author:str,game:dict):
|
|
|
|
self.name = name
|
|
|
|
self.author = author
|
|
|
|
self.current = "start"
|
|
|
|
self.nodes = {}
|
|
|
|
for k in game:
|
|
|
|
self.nodes.update({k:game[k]})
|
|
|
|
|
|
|
|
def make_selection(self,selection):
|
2021-11-03 10:16:56 +01:00
|
|
|
if(selection is not int or selection >= len(self.nodes[self.current][selection]) or selection < 0):
|
2021-11-02 17:40:54 +01:00
|
|
|
print("Invalid selection")
|
2021-11-03 10:16:56 +01:00
|
|
|
return False
|
2021-11-02 17:40:54 +01:00
|
|
|
else:
|
|
|
|
self.current = self.nodes[self.current][selection]
|
2021-11-03 10:16:56 +01:00
|
|
|
return True
|
2021-11-02 17:40:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
def printme(self):
|
|
|
|
'''
|
|
|
|
Used to print out the current prompt with the options
|
|
|
|
'''
|
|
|
|
print(self.nodes[self.current]["text"])
|
|
|
|
print("")
|
|
|
|
ostring = ""
|
|
|
|
for i,option in enumerate(self.nodes[self.current]["actions"]):
|
|
|
|
ostring+=f"{i} - {self.nodes[option]['description']}\n"
|
|
|
|
print(ostring)
|
|
|
|
sel = input("Make a selection (number): ")
|
2021-11-03 10:16:56 +01:00
|
|
|
isWrong = self.make_selection(sel)
|
|
|
|
while isWrong == False:
|
|
|
|
sel = input("Make a selection (number): ")
|
|
|
|
isWrong = self.make_selection(sel)
|
|
|
|
self.printme()
|
|
|
|
|
2021-11-03 10:23:37 +01:00
|
|
|
def parse_colors(self,text:str) -> str:
|
|
|
|
'''
|
|
|
|
Used to convert color codes in string to colors from the colorama lib
|
|
|
|
'''
|
|
|
|
return text.replace("&b",Fore.CYAN)
|
|
|
|
|
2021-11-02 17:40:54 +01:00
|
|
|
|
|
|
|
def load(file_path):
|
|
|
|
'''Loads the game from a YAML file to a Game class'''
|
|
|
|
with open(file_path) as f:
|
|
|
|
data = yaml.load(f,Loader=SafeLoader)
|
|
|
|
g = Game(data["meta"]["name"],data["meta"]["creator"],data["game"])
|
|
|
|
return g
|