Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Version 1 #25

Open
wants to merge 27 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
8e5e10c
Initial Commit, created player and world class and tables
tashigyatso45 Nov 6, 2023
8b8d943
Touched some db stuff
Case652 Nov 7, 2023
2dc8fcd
played with menu a bit
Case652 Nov 7, 2023
8437168
Merge pull request #1 from tashigyatso45/sql_stuff
tashigyatso45 Nov 7, 2023
615ce50
find by id method
tashigyatso45 Nov 7, 2023
c7d7463
Merge pull request #2 from tashigyatso45/sql_stuff
Nov 7, 2023
5b89373
added Players find_by_id
theProNicole Nov 7, 2023
e7fc1f9
Merge pull request #3 from tashigyatso45/Nicole
Case652 Nov 7, 2023
550f42c
Menu Stuff
Case652 Nov 7, 2023
430c1fb
Merge pull request #4 from tashigyatso45/Cli_menu
tashigyatso45 Nov 7, 2023
1abe6c4
some changes
tashigyatso45 Nov 8, 2023
f5e89ce
Menu Tweaks
Case652 Nov 8, 2023
c5c0a04
Some minor Visuals
Case652 Nov 8, 2023
1269fbf
Worked with tashi on join's
Case652 Nov 8, 2023
2b7733e
Merge pull request #5 from tashigyatso45/MenuStuff
tashigyatso45 Nov 8, 2023
a60c65a
Progress
Case652 Nov 8, 2023
8da1cd1
started adding errors
Case652 Nov 8, 2023
7858fae
Some touchups
Case652 Nov 8, 2023
1c65e2d
worked for login
tashigyatso45 Nov 8, 2023
487f4ea
just some smalls tuff
Case652 Nov 8, 2023
1230213
Merge pull request #6 from tashigyatso45/login
tashigyatso45 Nov 8, 2023
e531c64
Merge pull request #7 from tashigyatso45/version-1
Case652 Nov 8, 2023
ccbd269
allows player to know which world he/she went
tashigyatso45 Nov 9, 2023
12cceb6
One change
tashigyatso45 Nov 9, 2023
ff7cbf1
Merge pull request #8 from tashigyatso45/version-1
Case652 Nov 9, 2023
778c249
whops
Case652 Nov 9, 2023
d1cbab9
menue 2 => option 5-6 debuggers included, 1 test ascii art
tashigyatso45 Nov 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
226 changes: 211 additions & 15 deletions lib/cli.py
Original file line number Diff line number Diff line change
@@ -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.
/ __|| |_ ___ ___ ___ ___ _ _ ___ _ _ _ _ / __|| |_ __ _ _ _ __ _ __ | |_ ___ _ _
| (__ | ' \ / _ \/ _ \(_-</ -_) | || |/ _ \| || || '_| | (__ | ' \ / _` || '_|/ _` |/ _|| _|/ -_)| '_|
\___||_||_|\___/\___//__/\___| \_, |\___/ \_,_||_| \___||_||_|\__,_||_| \__,_|\__| \__|\___||_|''')

print("2. Display Player's")
print("3. Remove Player")
print('4. Find Player by name')
print("5. Login A player to a world")
print("6. Search a Player Worlds")
print("7. Back to Main Menu")

def select_world():
while True:
selection = input("Select a world id or backout with 0: >")
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()
7 changes: 5 additions & 2 deletions lib/debug.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion lib/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sqlite3

CONN = sqlite3.connect('company.db')
CONN = sqlite3.connect('game.db')
CURSOR = CONN.cursor()
Empty file removed lib/models/model_1.py
Empty file.
136 changes: 136 additions & 0 deletions lib/models/player.py
Original file line number Diff line number Diff line change
@@ -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<Player id: {self.id} name: {self.name} strength: {self.strength} hp: {self.hp}>\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()
# [
# <World id: 4 name: test4>
# .]
Loading