diff --git a/lib/cli.py b/lib/cli.py index edd1b62ad..26d346597 100644 --- a/lib/cli.py +++ b/lib/cli.py @@ -1,28 +1,224 @@ # lib/cli.py +from models.world import World +from models.player import Player +from helpers import exit_program -from helpers import ( - exit_program, - helper_1 -) - - +selected_world = None +selected_player = None def main(): while True: menu() - choice = input("> ") - if choice == "0": + choice = input(">") + if choice == "1": + world_options() + elif choice == "2": + player_options() + elif choice == "3": + login_menu() + # Would be a menu() + # while True: + # login_menu() + # Choice == "1": + # select_player() + # choice == "2": + # select_world() + elif choice == "4": exit_program() - elif choice == "1": - helper_1() else: - print("Invalid choice") - + print("Please Make A Valid Choice") def menu(): - print("Please select an option:") - print("0. Exit the program") - print("1. Some useful function") + print(""" + .,-:;//;:=, + . :H@@@MM@M#H/.,+%;, + ,/X+ +M@@M@MM%=,-%HMMM@X/, + -+@MM; $M@@MH+-,;XMMMM@MMMM@+- + ;@M@@M- XM@X;. -+XXXXXHHH@M@M#@/. + ,%MM@@MH ,@%= .---=-=:=,. + -@#@@@MX ., -%HX$$%%%+; + =-./@M@M$ .;@MMMM@MM: + X@/ -$MM/ .+MM@@@M$ +,@M@H: :@: . -X#@@@@- +,@@@MMX, . /H- ;@M@M= +.H@@@@M@+, %MM+..%#$. + /MMMM@MMH/. XM@MH; -; + /%+%$XHH@$= , .H@@@@MX, + .=--------. -%H.,@@@@@MX, + .%MM@@@HHHXX$$$%+- .:$MMX -M@@MM%. + =XMMM@MM@MM#H;,-+HMM@M+ /MMMX= + =%@M@M#@$-.=$@MM@@@M; %M%= + ,:+$+-,/H#MMMMMMM@- -, + =++%%%%+/:-. +""") + print("________________________") + print("Please select an option") + print("------------------------") + print("1. World Options") + print("2. Player Options") + print("3. Login Screen?") + print("4. Exit the program") + +def world_options(): + while True: + world_menu() + print(selected_world) + choice = input(">") + if choice == "1": + create_world_menu() + elif choice == "2": + if World.all() == []: + print("") + print("There Are No Worlds\n Please Create One") + else: + print(World.all()) + select_world() + elif choice == "3": + print("Select World Id to Delete") + id = input("->") + world = World.find_by_id(id) + world.shatter() + elif choice == "4": + break + else: + print("Please Make A Valid Choice") + +def login_menu(): + print("Theres Nothing Here Right Now") + print(f"World Selected:{selected_world}") + print(f"Player Selected:{selected_player}") + +def world_menu(): + print("________________________") + print("World Menu") + print("------------------------") + print("1. Create World") + print("2. Display Worlds") + print("3. Delete World") + print("4. Back to Main Menu") + +def player_options(): + while True: + player_menu() + choice = input(">") + if choice == "1": + create_player_menu() + elif choice == "2": + if Player.all() == []: + print("") + print("There Are No Players\n Please Create One") + else: + print(Player.all()) + select_player() + elif choice == "3": + id = input("Select Player Id to Delete:") + player = Player.find_by_id(id) + player.deletes() + elif choice == '4': + name = input('Find your character by their name: ') + player = Player.find_by_name(name) + print(player) + elif choice == '5': + id = input('Please input a id: ') + name= input('Please input a name: ') + if id and name: + player = Player.find_by_name(name) + world_instance = World.find_by_id(id) + if player and world_instance: + player.login(world_instance) + else: + print('Player or world not found') + else: + print('Please enter a valid world id and player name') + elif choice == '6': + print(Player.all()) + name = input('please input player name ') + if name: + player = Player.find_by_name(name) + if player: + print(player.worlds()) + else: + print('Player not found') + else: + print(' please enter a valid name') + elif choice == "7": + break + else: + print("Please Make A Valid Choice") + +def create_player_menu(): + while True: + print("Press 0 to go back") + choice = input("What Is Your Name Traveler?:>") + if choice == "0": + break + elif choice == "": + print("____________________") + print("Please Name Yourself") + elif len(choice) > 11: + print("___________________________") + print("Please Enter A Shorter Name") + else: + print(Player.create(choice)) + break +def create_world_menu(): + while True: + print("____________________") + print("Press 0 to go back") + choice = input("What Is Your World Called Traveler? >") + if choice == "0": + break + elif choice == "": + print("________________________________________") + print("No Input Detected: Please Name The World") + elif len(choice) > 16: + print("___________________________") + print("Please Enter A Shorter Name") + else: + print(World.create(choice)) + break +def player_menu(): + print("________________________") + print("Player Menu") + print("------------------------") + print('''1. + / __|| |_ ___ ___ ___ ___ _ _ ___ _ _ _ _ / __|| |_ __ _ _ _ __ _ __ | |_ ___ _ _ + | (__ | ' \ / _ \/ _ \(_-") + if selection == "0": + break + elif selection == "": + print("Please Select a world ID or backout with 0: >") + elif int(selection) <= len(World.all()): + global selected_world + selected_world = World.find_by_id(selection) + print(f'Selected\n{selected_world}') + else: + print("Please Select a valid world ID") +def select_player(): + while True: + selection = input("Select a player id or backout with 0: >") + if selection == "0": + break + elif selection == "": + print("Please Select a Player with ID or backout with 0") + elif int(selection) <= len(Player.all()): + global selected_player + selected_player = Player.find_by_id(selection) + print(f'Selected\n{selected_player}') + else: + print("Please Select a valid Player ID") if __name__ == "__main__": main() diff --git a/lib/debug.py b/lib/debug.py index 6fadf6e66..d42eaee6b 100644 --- a/lib/debug.py +++ b/lib/debug.py @@ -1,8 +1,11 @@ #!/usr/bin/env python3 # lib/debug.py +from models.player import Player +from models.world import World from models.__init__ import CONN, CURSOR import ipdb - - +player1 = +# world1 = World('Howling Castle') +# world2 = World('Mt Everwood') ipdb.set_trace() diff --git a/lib/models/__init__.py b/lib/models/__init__.py index d5b061e1e..1098a2f26 100644 --- a/lib/models/__init__.py +++ b/lib/models/__init__.py @@ -1,4 +1,4 @@ import sqlite3 -CONN = sqlite3.connect('company.db') +CONN = sqlite3.connect('game.db') CURSOR = CONN.cursor() diff --git a/lib/models/model_1.py b/lib/models/model_1.py deleted file mode 100644 index e69de29bb..000000000 diff --git a/lib/models/player.py b/lib/models/player.py new file mode 100644 index 000000000..b7d5c997e --- /dev/null +++ b/lib/models/player.py @@ -0,0 +1,136 @@ +from models.__init__ import CONN, CURSOR +from models.world import World +class Player: + def __init__(self, name, strength = 1, hp = 10, id=None ): + self.name = name + self.strength = strength + self.hp = hp + self.id = id + + @property + def name(self): + return self._name + @name.setter + def name(self, new_name): + if not hasattr(self, "_name"): + if isinstance(new_name, str) and 1 <= len(new_name) <= 10: + self._name = new_name + + @classmethod + def create_table(cls): + sql= ''' + CREATE TABLE players( + id INTEGER PRIMARY KEY, + name TEXT, + strength INTEGER, + HP INTEGER + ) + ''' + CURSOR.execute(sql) + + def save(self): + if self.id: + sql: f'UPDATE players SET name = ?, strength = ?, hp = ? WHERE id = ?' + param_tuples = (self.name, self.strength, self.hp, self.id) + CURSOR.execute(sql, param_tuples) + CONN.commit() + else: + sql = 'INSERT INTO players (name,strength,hp) VALUES (?, ?, ? )' + param_tuples = (self.name, self.strength, self.hp ) + CURSOR.execute(sql, param_tuples) + CONN.commit() + id_sql = 'SELECT LAST_INSERT_ROWID() FROM players' + new_id_tuple = CURSOR.execute( id_sql ).fetchone() + self.id = new_id_tuple[0] + + #Deletes player from db + def deletes( self ): + sql = 'DELETE FROM players WHERE id = ?' + params_tuple = ( self.id, ) + CURSOR.execute( sql, params_tuple ) + CONN.commit() + self.id = None +# V makes database display , work to create a instance with + @classmethod + def all(cls): + sql = 'SELECT * FROM players' + list_of_tuples = CURSOR.execute( sql ).fetchall() + return [Player.from_db(row) for row in list_of_tuples] + + @classmethod + def from_db(cls,row_tuple): + player_instance = Player( row_tuple[1],row_tuple[2],row_tuple[3]) + player_instance.id = row_tuple[0] + return player_instance + + def __repr__(self): + return f'\n\n.' + + @classmethod + def find_by_id(cls,id): + sql = ''' + SELECT * FROM players WHERE id = ? + ''' + row = CURSOR.execute(sql,(id,)).fetchone() + return cls.from_db(row) if row else None + + @classmethod + def find_by_name(cls, name): + sql = ''' + SELECT * FROM players WHERE name = ? + ''' + row = CURSOR.execute(sql,(name,)).fetchone() + return cls.from_db(row) if row else None + + @classmethod + def create(cls,name): + player = cls(name) + player.save() + return player + + def login(self, world): + sql = 'INSERT INTO login (world_id, player_id) VALUES(?,?)' + params_tuple = (world.id, self.id) + CURSOR.execute(sql,params_tuple) + CONN.commit() + def worlds(self): + sql = ''' + SELECT DISTINCT worlds.* FROM WORLDS + JOIN login ON login.world_id = worlds.id + WHERE login.player_id = ? + ''' + params_tuple = (self.id,) + list_of_tuples = CURSOR.execute(sql,params_tuple).fetchall() + return [World.from_db(row) for row in list_of_tuples] + + #CREATE TABLE login(id INTEGER PRIMARY KEY,player_id INTEGER,world_id INTEGER); + @classmethod + def create_table2(cls): + sql = 'CREATE TABLE login(id INTEGER PRIMARY KEY,player_id INTEGER,world_id INTEGER)' + CURSOR.execute(sql) + + @classmethod + def all_player_login(cls): + sql = ''' + SELECT * FROM login WHERE player_id = ? + ''' + list_of_tuples = CURSOR.execute(sql).fetchone() + return [Player.login_db(row) for row in list_of_tuples] + @classmethod + def login_db(cls,row_tuple): + player_instance = ( row_tuple[1],) + player_instance.id = row_tuple[0] + return player_instance + # must create a table(will throw seed at project later) + # need a player instace, and a world instance to login. +# ipdb> player1 = Player("Test7") +# ipdb> world1 = World("test4") +# ipdb> player1.save() +# ipdb> world1.save() +# ipdb> player1.login_history(world1) +# *** AttributeError: 'Player' object has no attribute 'login_history' +# ipdb> player1.login(world1) +# ipdb> player1.worlds() +# [ +# +# .] \ No newline at end of file diff --git a/lib/models/world.py b/lib/models/world.py new file mode 100644 index 000000000..55867f1fc --- /dev/null +++ b/lib/models/world.py @@ -0,0 +1,76 @@ +from models.__init__ import CONN, CURSOR +class World: + def __init__(self, location,id=None): + self.location = location + self.id = id + + @property + def location(self): + return self._location + + @location.setter + def location(self,new_location): + if not hasattr(self, '_location'): + if type(new_location) == str and 1 <= len(new_location) <= 15: + self._location = new_location + + @classmethod + def create_table(cls): + sql= ''' + CREATE TABLE worlds( + id INTEGER PRIMARY KEY, + location TEXT + ) + ''' + CURSOR.execute(sql) + + def save(self): + if self.id: + sql: f'UPDATE worlds SET location = ? WHERE id = ?' + param_tuples = (self.location, self.id) + CURSOR.execute(sql, param_tuples) + CONN.commit() + else: + sql = 'INSERT INTO worlds (location) VALUES (?)' + param_tuples = (self.location, ) + CURSOR.execute(sql, param_tuples) + CONN.commit() + id_sql = 'SELECT LAST_INSERT_ROWID() FROM worlds' + new_id_tuple = CURSOR.execute( id_sql ).fetchone() + self.id = new_id_tuple[0] +# Deletes World from db + def shatter( self ): + sql = 'DELETE FROM worlds WHERE id = ?' + params_tuple = ( self.id, ) + CURSOR.execute( sql, params_tuple ) + CONN.commit() + self.id = None +# V makes database display , work to create a instance with + @classmethod + def all(cls): + sql = 'SELECT * FROM worlds' + list_of_tuples = CURSOR.execute( sql ).fetchall() + return [World.from_db(row) for row in list_of_tuples] + + @classmethod + def from_db(cls,row_tuple): + world_instance = World( row_tuple[1]) + world_instance.id = row_tuple[0] + return world_instance + + def __repr__(self): + return f'\n\n.' + + @classmethod + def find_by_id(cls, id): + sql = ''' + SELECT * FROM worlds WHERE id = ? + ''' + row = CURSOR.execute(sql, (id,)).fetchone() + return cls.from_db(row) if row else None + + @classmethod + def create(cls,location): + world = cls(location) + world.save() + return world