2018-12-28 04:06:37 +00:00
#!/usr/bin/env python3
#
# Oberon: an application for managing correspondance gameplay
# for board style games
# by Brian Evans
#_
import sys
import os
2018-12-29 20:11:59 +00:00
import sqlite3 as sql
2018-12-28 04:06:37 +00:00
import re
import subprocess
2018-12-29 20:11:59 +00:00
import json
2018-12-28 04:06:37 +00:00
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 ( )
2018-12-29 20:11:59 +00:00
self . db_path = ' /home/sloum/oberon.sqlite '
self . game = Game ( )
2018-12-28 04:06:37 +00:00
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 ) :
2018-12-29 20:11:59 +00:00
select_player = ' '
2018-12-28 04:06:37 +00:00
p2 = ' '
2018-12-29 20:11:59 +00:00
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
2018-12-28 04:06:37 +00:00
def list_games ( self ) :
2018-12-29 20:11:59 +00:00
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 \n To 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 )
2018-12-28 04:06:37 +00:00
def get_game_list ( ) :
# query for available open games and current games for player
2018-12-29 20:11:59 +00:00
print ( ' \n Coming soon... \n ' )
self . screen = ' menu '
2018-12-28 04:06:37 +00:00
def show_history ( self ) :
# query for this palyers win records
# display them
# options for back or quit
2018-12-29 20:11:59 +00:00
print ( ' \n Coming soon... \n ' )
self . screen = ' menu '
2018-12-28 04:06:37 +00:00
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 :
2018-12-29 20:11:59 +00:00
def __init__ ( self ) :
2018-12-28 04:06:37 +00:00
self . game_id = None
self . game_board = None
self . turn = None
2018-12-29 20:11:59 +00:00
self . create_game_board ( )
def create_game_board ( self ) :
self . game_board = [ [ ' · ' for y in range ( 15 ) ] for x in range ( 15 ) ]
2018-12-28 04:06:37 +00:00
2018-12-29 20:11:59 +00:00
def get_game_board ( self ) :
2018-12-28 04:06:37 +00:00
pass
2018-12-29 20:11:59 +00:00
def validate_turn ( self ) :
2018-12-28 04:06:37 +00:00
pass
2018-12-29 20:11:59 +00:00
def make_move ( self ) :
2018-12-28 04:06:37 +00:00
pass
2018-12-29 20:11:59 +00:00
def validate_move ( self ) :
2018-12-28 04:06:37 +00:00
pass
2018-12-29 20:11:59 +00:00
def check_win_state ( self ) :
2018-12-28 04:06:37 +00:00
pass
2018-12-29 20:11:59 +00:00
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
2018-12-28 04:06:37 +00:00
if __name__ == ' __main__ ' :
2018-12-29 20:11:59 +00:00
check_and_build_db ( ' /home/sloum/oberon.sqlite ' )
game = Interface ( )
2018-12-28 04:06:37 +00:00