Trying out a secondary build that seems simpler
This commit is contained in:
parent
20fefda39c
commit
4268d7125a
154
oberon.py
154
oberon.py
|
@ -7,9 +7,10 @@
|
|||
|
||||
import sys
|
||||
import os
|
||||
import sqlite3
|
||||
import sqlite3 as sql
|
||||
import re
|
||||
import subprocess
|
||||
import json
|
||||
|
||||
class c:
|
||||
black = ''
|
||||
|
@ -54,6 +55,8 @@ class Interface:
|
|||
self.user = User()
|
||||
self.screen = 'menu'
|
||||
self.userlist = self.get_user_list()
|
||||
self.db_path = '/home/sloum/oberon.sqlite'
|
||||
self.game = Game()
|
||||
self.mainloop()
|
||||
|
||||
|
||||
|
@ -91,47 +94,86 @@ class Interface:
|
|||
|
||||
|
||||
def create_game(self):
|
||||
if not len(self.userlist):
|
||||
self.screen = 'menu'
|
||||
print('\n')
|
||||
print('{}{:^80}{}'.format(c.white, 'There are no players available', c.end))
|
||||
print('\n')
|
||||
return False
|
||||
print('{:^80}'.format('The following players are available:\n'))
|
||||
for x in self.userlist:
|
||||
print('{:^80}'.format(x))
|
||||
print('\n')
|
||||
select_player = ''
|
||||
p2 = ''
|
||||
while not p2 in self.userlist:
|
||||
p2 = input('Who would you like to challenge? ')
|
||||
if not p2 in self.userlist:
|
||||
print('{}Invalid entry{}'.format(c.red, c.end))
|
||||
self.screen = 'play'
|
||||
game_type = 'gomoku'
|
||||
game_status = ''
|
||||
next_menu = 'menu'
|
||||
|
||||
while True:
|
||||
select_player = input('Do you want to challenge another player (y/n) > ')
|
||||
if select_player in ['y', 'yes', 'no', 'n']:
|
||||
break
|
||||
|
||||
if select_player in ['y', 'yes']:
|
||||
if not len(self.userlist):
|
||||
self.screen = 'menu'
|
||||
print('\n')
|
||||
print('{}{:^80}{}'.format(c.white, 'There are no players available', c.end))
|
||||
print('\n')
|
||||
return False
|
||||
print('{:^80}'.format('The following players are available:\n'))
|
||||
for x in self.userlist:
|
||||
print('{:^80}'.format(x))
|
||||
print('\n')
|
||||
while not p2 in self.userlist:
|
||||
p2 = input('Enter the name of the user would you like to challenge > ')
|
||||
if not p2 in self.userlist:
|
||||
print('{}Invalid entry{}'.format(c.red, c.end))
|
||||
continue
|
||||
game_status = 'playing'
|
||||
next_menu = 'play'
|
||||
else:
|
||||
p2 = None
|
||||
game_status = 'Open'
|
||||
next_menu = 'menu'
|
||||
q = "INSERT INTO games VALUES (?, ?, ?, ?, ?, ?, ?)"
|
||||
v = (self.user.name, p2, game_type, game_status, json.dumps(self.game.game_board), 1, None)
|
||||
db_do(q, v, True)
|
||||
self.screen = next_menu
|
||||
|
||||
|
||||
def list_games(self):
|
||||
# List games user is currently playing
|
||||
# List any open games
|
||||
game_id = input('Choice > ')
|
||||
game = validate_game(game_id)
|
||||
if game:
|
||||
if p1 == self.user or p2 == self.user:
|
||||
# THis is getting wonky
|
||||
pass
|
||||
elif not p2:
|
||||
pass
|
||||
else:
|
||||
pass
|
||||
game_id = ''
|
||||
q = "SELECT rowid, p1, p2, game_type, game_status FROM games WHERE winner is NULL"
|
||||
res = db_do(q)
|
||||
print('{:^8} {:^10} {:^10} {:^12}'.format('ID', 'Player 1', 'Player 2', 'Game Status'))
|
||||
print('-------- ---------- ---------- ------------\n')
|
||||
for x in res:
|
||||
print("{:^8} {:^10} {:^10} {:^12}".format(x[0], x[1], x[2] or '-', x[4]))
|
||||
if not len(res):
|
||||
print('There are no open games')
|
||||
print('')
|
||||
print('To join an open game, enter the game ID\nTo go back type "back"\n')
|
||||
while True:
|
||||
game_id = input('Choice > ')
|
||||
if game_id == 'back':
|
||||
self.screen = 'menu'
|
||||
return
|
||||
else:
|
||||
try:
|
||||
gid = int(game_id)
|
||||
if gid in [x[0] for x in res]:
|
||||
break
|
||||
except:
|
||||
print('{}Invalid entry{}'.format(c.red, c.end))
|
||||
q2 = "UPDATE games SET p2 = ?, game_status = 'playing' WHERE rowid = ?"
|
||||
v2 = (self.user.name, gid)
|
||||
db_do(q2, v2, True)
|
||||
|
||||
|
||||
def get_game_list():
|
||||
# query for available open games and current games for player
|
||||
pass
|
||||
print('\nComing soon...\n')
|
||||
self.screen = 'menu'
|
||||
|
||||
|
||||
def show_history(self):
|
||||
# query for this palyers win records
|
||||
# display them
|
||||
# options for back or quit
|
||||
print('\nComing soon...\n')
|
||||
self.screen = 'menu'
|
||||
pass
|
||||
|
||||
|
||||
|
@ -156,31 +198,71 @@ class Interface:
|
|||
|
||||
|
||||
class Game:
|
||||
def __init__(self, game_id):
|
||||
def __init__(self):
|
||||
self.game_id = None
|
||||
self.game_board = None
|
||||
self.turn = None
|
||||
self.create_game_board()
|
||||
|
||||
def get_game_board():
|
||||
def create_game_board(self):
|
||||
self.game_board = [[' · ' for y in range(15)] for x in range(15)]
|
||||
|
||||
def get_game_board(self):
|
||||
pass
|
||||
|
||||
|
||||
def validate_turn():
|
||||
def validate_turn(self):
|
||||
pass
|
||||
|
||||
|
||||
def make_move():
|
||||
def make_move(self):
|
||||
pass
|
||||
|
||||
|
||||
def validate_move():
|
||||
def validate_move(self):
|
||||
pass
|
||||
|
||||
|
||||
def check_win_state():
|
||||
def check_win_state(self):
|
||||
pass
|
||||
|
||||
|
||||
def check_and_build_db(db_path):
|
||||
if not os.path.isfile(db_path):
|
||||
conn = sql.connect(db_path)
|
||||
c = conn.cursor()
|
||||
|
||||
c.execute("CREATE TABLE games (p1 text NOT NULL, p2 text DEFAULT NULL, game_type text NOT NULL, game_status text DEFAULT NULL, game_board text, turn INTEGER, winner text DEFAULT NULL)")
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
def db_do(query, var=False, noresval=False):
|
||||
global messages
|
||||
db_path = '/home/sloum/oberon.sqlite'
|
||||
if os.path.isfile(db_path):
|
||||
conn = sql.connect(db_path)
|
||||
c = conn.cursor()
|
||||
if var:
|
||||
c.execute(query, var)
|
||||
else:
|
||||
c.execute(query)
|
||||
if noresval:
|
||||
out = c.rowcount
|
||||
else:
|
||||
out = []
|
||||
for row in c:
|
||||
out.append(row)
|
||||
conn.commit()
|
||||
conn.close()
|
||||
return out
|
||||
else:
|
||||
messages.append("{}ERROR:{} Database cannot be found or is corrupt".format(c.red, c.end))
|
||||
return False
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
game = Interface()
|
||||
check_and_build_db('/home/sloum/oberon.sqlite')
|
||||
game = Interface()
|
||||
|
||||
|
|
|
@ -0,0 +1,110 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import sys
|
||||
import sqlite3 as sql
|
||||
import os
|
||||
import subprocess
|
||||
import json
|
||||
|
||||
version = '0.2.0'
|
||||
home_folder = os.path.expanduser('~')
|
||||
username = os.path.split(home_folder)[-1]
|
||||
database = '/var/lib/oberon/oberon.sqlite'
|
||||
|
||||
def create_game(enemy):
|
||||
pass
|
||||
|
||||
|
||||
def play_game(gid):
|
||||
pass
|
||||
|
||||
|
||||
def show_game_record():
|
||||
pass
|
||||
|
||||
def list_games():
|
||||
pass
|
||||
|
||||
|
||||
def display_help():
|
||||
print('O B E R O N\nv{}'.format(version))
|
||||
print('syntax: oberon [command [option]]\n')
|
||||
print('{:25} {}'.format('help','display this message'))
|
||||
print('{:25} {}'.format('list', 'display your currently open games'))
|
||||
print('{:25} {}'.format('history', 'display your win/loss record'))
|
||||
print('{:25} {}'.format('create [user]', 'create a game against the user provided'))
|
||||
print('{:25} {}'.format('play [game_id]', 'make a move or view the board for the game id provided'))
|
||||
|
||||
|
||||
def parse_args():
|
||||
args = sys.argv
|
||||
arg_len = len(args)
|
||||
if arg_len == 1:
|
||||
print('Invalid arguments. Run "oberon help".')
|
||||
sys.exit(1)
|
||||
if arg_len == 2:
|
||||
if args[1] == 'list':
|
||||
list_games()
|
||||
sys.exit(0)
|
||||
elif args[1] == 'history':
|
||||
show_game_record()
|
||||
sys.exit(0)
|
||||
elif args[1] == 'create':
|
||||
print('Must include an opponent name.\nFind one in chat or cspc!')
|
||||
sys.exit(1)
|
||||
elif args[1] == 'play':
|
||||
print('Must include a game id.\nRun "oberon list" to view your open games.')
|
||||
sys.exit(1)
|
||||
elif args[1] == 'help':
|
||||
display_help()
|
||||
sys.exit(0)
|
||||
else:
|
||||
print('Invalid arguments. Run "oberon help".')
|
||||
sys.exit(1)
|
||||
elif arg_len == 3:
|
||||
if args[1] == 'create':
|
||||
if args[2] in get_user_list():
|
||||
create_game(args[2])
|
||||
sys.exit(0)
|
||||
else:
|
||||
print('User "{}" does not exist.'.format(args[2]))
|
||||
sys.exit(1)
|
||||
elif args[1] == 'play':
|
||||
try:
|
||||
game_id = int(args[2])
|
||||
except:
|
||||
print('Invalid game id.')
|
||||
sys.exit(1)
|
||||
play_game(game_id)
|
||||
sys.exit(0)
|
||||
else:
|
||||
print('Invalid arguments. Run "oberon help".')
|
||||
sys.exit(1)
|
||||
else:
|
||||
print('Unknown paramaters: {}'.format(' '.join(args[3:])))
|
||||
sys.exit(1)
|
||||
|
||||
|
||||
|
||||
def get_user_list():
|
||||
res = subprocess.run(['awk', '-F', ':', '{if ($7 == "/usr/local/bin/colorsh" && $1 != "brian")}', '/etc/passwd'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
||||
res_list = res.stdout.decode('utf-8').split('\n')
|
||||
if res_list[0] == '':
|
||||
return []
|
||||
return res_list
|
||||
|
||||
|
||||
def check_and_build_db(db_path):
|
||||
if not os.path.isfile(db_path):
|
||||
conn = sql.connect(db_path)
|
||||
c = conn.cursor()
|
||||
|
||||
c.execute("CREATE TABLE games (p1 text NOT NULL, p2 text DEFAULT NULL, game_type text NOT NULL, game_status text DEFAULT NULL, game_board text, turn INTEGER, winner text DEFAULT NULL)")
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
check_and_build_db(database)
|
||||
parse_args()
|
Loading…
Reference in New Issue