from peewee import * import json def singleton(cls): _instance = {} def inner(id=0, config_flag=True): if cls not in _instance: _instance[cls] = cls(id, config_flag) return _instance[cls] return inner @singleton class global_model(): def __init__(self, id=0, config_flag=True): ''' Database ''' print("[Model]Global_model has been created!") self.db = SqliteDatabase('my_database.db') self.id = 0 class BaseModel(Model): class Meta: database = self.db class GameData(BaseModel): id = AutoField(primary_key=True) intro = TextField() npc = TextField() value_system = TextField() win_condition = TextField() value_for_now = TextField() events = TextField() if_end = BooleanField() story = TextField() style = TextField() intro_declare = TextField() input_system_template = TextField() input_human_template = TextField() event_declare = TextField() event_system_template = TextField() event_human_template = TextField() value_declare = TextField() value_system_template = TextField() value_human_template = TextField() evaluate_declare = TextField() evaluate_system_template = TextField() evaluate_human_template = TextField() format_system_template = TextField() format_human_template = TextField() self.db.connect() self.db.create_tables([GameData]) self.GameData = GameData print("[Model]Database initialized!") ''' Input Config ''' with open('config/input_config.json', 'r', encoding='utf-8') as f: data = json.load(f) self.story = data[ "story"] if id == 0 and config_flag else self.GameData.get_by_id( id).story self.style = data[ "style"] if id == 0 and config_flag else self.GameData.get_by_id( id).style self.intro_declare = data[ "intro_declare"] if id == 0 and config_flag else self.GameData.get_by_id( id).intro_declare self.input_system_template = data[ "system_template"] if id == 0 and config_flag else self.GameData.get_by_id( id).input_system_template self.input_human_template = data[ "human_template"] if id == 0 and config_flag else self.GameData.get_by_id( id).input_human_template ''' Event Config ''' with open('config/event_config.json', 'r', encoding='utf-8') as f: data = json.load(f) self.event_declare = data[ "event_declare"] if id == 0 and config_flag else self.GameData.get_by_id( id).event_declare self.event_system_template = data[ "system_template"] if id == 0 and config_flag else self.GameData.get_by_id( id).event_system_template self.event_human_template = data[ "human_template"] if id == 0 and config_flag else self.GameData.get_by_id( id).event_human_template ''' Value Config ''' with open('config/value_config.json', 'r', encoding='utf-8') as f: data = json.load(f) self.value_declare = data[ "value_declare"] if id == 0 and config_flag else self.GameData.get_by_id( id).value_declare self.value_system_template = data[ "system_template"] if id == 0 and config_flag else self.GameData.get_by_id( id).value_system_template self.value_human_template = data[ "human_template"] if id == 0 and config_flag else self.GameData.get_by_id( id).value_human_template ''' Evaluate Config ''' with open('config/evaluate_config.json', 'r', encoding='utf-8') as f: data = json.load(f) self.evaluate_declare = data[ "evaluate_declare"] if id == 0 and config_flag else self.GameData.get_by_id( id).evaluate_declare self.evaluate_system_template = data[ "system_template"] if id == 0 and config_flag else self.GameData.get_by_id( id).evaluate_system_template self.evaluate_human_template = data[ "human_template"] if id == 0 and config_flag else self.GameData.get_by_id( id).evaluate_human_template ''' Format Config ''' with open('config/format_config.json', 'r', encoding='utf-8') as f: data = json.load(f) self.format_system_template = data[ "system_template"] if id == 0 and config_flag else self.GameData.get_by_id( id).format_system_template self.format_human_template = data[ "human_template"] if id == 0 and config_flag else self.GameData.get_by_id( id).format_human_template if id != 0: self.id = id def get_intro_str(self): intro = "" intro += "故事简介:" + self.get_intro() + "," intro += "角色设定:" + str(self.get_npc()) + "," intro += "数值系统:" + str(self.get_value_system()) + "," intro += "游戏通关所需条件:" + self.get_win_condition() + "," intro += "玩家当前数值:" + str(self.get_value_for_now()) + "," intro += "已经发生的事件:" + str(self.get_events()) return intro def input_init(self, intro, npc, value_system, win_condition): new_model = self.GameData.create( intro=intro, npc=str(npc), value_system=str(value_system), win_condition=win_condition, value_for_now=str([]), events=str([]), if_end=False, story=self.story, style=self.style, intro_declare=str(self.intro_declare), input_system_template=self.input_system_template, input_human_template=self.input_human_template, event_declare=str(self.event_declare), event_system_template=self.event_system_template, event_human_template=self.event_human_template, value_declare=str(self.value_declare), value_system_template=self.value_system_template, value_human_template=self.value_human_template, evaluate_declare=str(self.evaluate_declare), evaluate_system_template=self.evaluate_system_template, evaluate_human_template=self.evaluate_human_template, format_system_template=self.format_system_template, format_human_template=self.format_human_template) self.id = new_model.id def get_intro(self): try: intro = self.GameData.get(self.GameData.id == self.id).intro except: intro = "" return intro def get_npc(self): try: npc = eval(self.GameData.get(self.GameData.id == self.id).npc) except: npc = [] return npc def get_value_system(self): try: value_system = eval( self.GameData.get(self.GameData.id == self.id).value_system) except: value_system = [] return value_system def get_win_condition(self): try: win_condition = self.GameData.get( self.GameData.id == self.id).win_condition except: win_condition = "" return win_condition def get_value_for_now(self): try: value_for_now = eval( self.GameData.get(self.GameData.id == self.id).value_for_now) except: value_for_now = [] return value_for_now def get_events(self): try: events = eval( self.GameData.get(self.GameData.id == self.id).events) except: events = [] return events def add_event(self, event): try: events = eval( self.GameData.get(self.GameData.id == self.id).events) except: events = [] events.append(event) self.GameData.update(events=str(events)).where( self.GameData.id == self.id).execute() def update_events(self, events): self.GameData.update(events=str(events)).where( self.GameData.id == self.id).execute() def update_value_for_now(self, value_for_now): self.GameData.update(value_for_now=str(value_for_now)).where( self.GameData.id == self.id).execute() def update_if_end(self, if_end): self.GameData.update(if_end=if_end).where( self.GameData.id == self.id).execute() def get_if_end(self): try: if_end = self.GameData.get(self.GameData.id == self.id).if_end except: if_end = False return if_end