269 lines
7.4 KiB
Python
269 lines
7.4 KiB
Python
#!/usr/bin/env python3
|
|
#
|
|
# Oberon: an application for managing correspondance gameplay
|
|
# for board style games
|
|
# by Brian Evans
|
|
#_
|
|
|
|
import sys
|
|
import os
|
|
import sqlite3 as sql
|
|
import re
|
|
import subprocess
|
|
import json
|
|
|
|
class c:
|
|
black = ''
|
|
red = '\033[0;31m'
|
|
b_red = '\033[1;31m'
|
|
yellow = '\033[1;33m'
|
|
green = '\033[0;32m'
|
|
b_green = '\033[1;32m'
|
|
cyan = '\033[0;36m'
|
|
b_cyan = '\033[1;36m'
|
|
purple = '\033[1;35m'
|
|
blue = '\033[0;34m'
|
|
b_blue = '\033[1;34m'
|
|
white = '\033[1;37m'
|
|
end = '\033[0m'
|
|
|
|
#--------->
|
|
|
|
class User:
|
|
def __init__(self):
|
|
self.home_folder = os.path.expanduser('~')
|
|
self.name = os.path.split(self.home_folder)[-1]
|
|
self.current_games = self.get_games()
|
|
self.history = self.get_history()
|
|
|
|
|
|
def get_games(self):
|
|
db_data = False # do db query
|
|
# Retrieve current games
|
|
return db_data
|
|
|
|
|
|
def get_history(self):
|
|
db_data = False # do db query
|
|
# Retrieve history for this user
|
|
return db_data
|
|
|
|
#-------->
|
|
|
|
class Interface:
|
|
def __init__(self):
|
|
self.user = User()
|
|
self.screen = 'menu'
|
|
self.userlist = self.get_user_list()
|
|
self.db_path = '/home/sloum/oberon.sqlite'
|
|
self.game = Game()
|
|
self.mainloop()
|
|
|
|
|
|
def main_menu(self):
|
|
menu = [
|
|
'{:^80}'.format('1) Create Game'),
|
|
'{:^80}'.format('2) List Games'),
|
|
'{:^80}'.format('3) View history'),
|
|
'{:^80}'.format('4) Quit/Exit')
|
|
]
|
|
|
|
print('\n{:^80}'.format('O B E R O N'))
|
|
print('{:^80}\n'.format('Correspondence gaming system'))
|
|
for x in menu:
|
|
print('{:^80}'.format(x))
|
|
selection = ''
|
|
while selection not in ['1', '2', '3', '4', 'q', 'quit', 'exit']:
|
|
selection = input(' oberon > ')
|
|
if selection == '1':
|
|
self.screen = 'create'
|
|
elif selection == '2':
|
|
self.screen = 'list'
|
|
elif selection == '3':
|
|
self.screen = 'history'
|
|
elif selection in ['4','q','quit','exit']:
|
|
sys.exit(0)
|
|
|
|
|
|
def get_user_list(self):
|
|
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 create_game(self):
|
|
select_player = ''
|
|
p2 = ''
|
|
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):
|
|
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
|
|
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
|
|
|
|
|
|
def play_game(self):
|
|
# acts as a router to game logic functions
|
|
# based on which game is selected
|
|
pass
|
|
|
|
|
|
def mainloop(self):
|
|
while True:
|
|
if self.screen == 'menu':
|
|
self.main_menu()
|
|
elif self.screen == 'create':
|
|
self.create_game()
|
|
elif self.screen == 'list':
|
|
self.list_games()
|
|
elif self.screen == 'history':
|
|
self.show_history()
|
|
elif self.screen == 'play':
|
|
self.play_game()
|
|
|
|
|
|
class Game:
|
|
def __init__(self):
|
|
self.game_id = None
|
|
self.game_board = None
|
|
self.turn = None
|
|
self.create_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(self):
|
|
pass
|
|
|
|
|
|
def make_move(self):
|
|
pass
|
|
|
|
|
|
def validate_move(self):
|
|
pass
|
|
|
|
|
|
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__':
|
|
check_and_build_db('/home/sloum/oberon.sqlite')
|
|
game = Interface()
|
|
|