put initialisation code in somewhat more sensible modules

This commit is contained in:
troido 2019-09-16 21:57:46 +02:00
parent 655f739bd9
commit cacb7563b3
3 changed files with 111 additions and 119 deletions

View File

@ -1,74 +1,51 @@
#! /usr/bin/python3
import argparse
import getpass
import curses
import json
import os
import os.path
from .start import main as clientmain
#from .paths import keybindingsPath, charmapPath
from . import loaders
#standardCharFiles = [name[:-5] for name in os.listdir(charMapPath) if name[-5:] == ".json"]
#standardKeyFiles = [name[:-5] for name in os.listdir(keybindingsPath) if name[-5:] == ".json"]
defaultAdresses = {
"abstract": "asciifarm",
"unix": "asciifarm.socket",
"inet": "localhost:9021",
}
import getpass
import sys
from .connection import Connection
from .gameclient import Client
from .display.display import Display
from .parseargs import parse_args
def main(argv=None):
(name, socketType, address, keybindings, characters, colours, logfile) = parse_args(argv)
connection = Connection(socketType)
try:
connection.connect(address)
except ConnectionRefusedError:
print("ERROR: Could not connect to server.\nAre you sure that the server is running and that you're connecting to the right address?", file=sys.stderr)
return
error = None
closeMessage = None
os.environ.setdefault("ESCDELAY", "25")
def start(stdscr):
display = Display(stdscr, characters, colours)
client = Client(stdscr, display, name, connection, keybindings, logfile)
try:
client.start()
except KeyboardInterrupt:
client.close("^C caught, goodbye")
except Exception as e:
# throw the execption outside ncurses
# so the cleanup can happen first
nonlocal error
error = e
nonlocal closeMessage
closeMessage = client.closeMessage
parser = argparse.ArgumentParser(description="The client to AsciiFarm. Run this to connect to to the server.", epilog="""
Gameplay information:
Walk around and explore the rooms.
Kill the goblins and plant the seeds.
~troido""", formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('-n', '--name', help='Your player name (must be unique!). Defaults to username on inet sockets and tildename on (unix socket (including abstract)', default=None)
parser.add_argument("-a", "--address", help="The address of the socket. When the socket type is 'abstract' this is just a name. When it is 'unix' this is a filename. When it is 'inet' is should be in the format 'address:port', eg 'localhost:8080'. Defaults depends on the socket type")
parser.add_argument("-s", "--socket", help="the socket type. 'unix' is unix domain sockets, 'abstract' is abstract unix domain sockets and 'inet' is inet sockets. ", choices=["abstract", "unix", "inet"], default="abstract")
parser.add_argument('-k', '--keybindings', help='The file with the keybinding configuration. This file is a JSON file.', default="default")
parser.add_argument('-c', '--characters', help='The file with the character mappings for the graphics. If it is either of these names: {} it will be loaded from the charmaps directory.'.format(list(loaders.standardCharFiles.keys())), default="default")
parser.add_argument('-o', '--logfile', help='All game messages will be written to this file.', default=None)
colourGroup = parser.add_mutually_exclusive_group()
colourGroup.add_argument('-l', '--colours', '--colors', help='enable colours! :)', action="store_true")
colourGroup.add_argument('-b', '--nocolours', '--nocolors', help='disable colours! :)', action="store_true")
args = parser.parse_args(argv)
charmap = loaders.loadCharmap(args.characters)
keybindings = loaders.loadKeybindings(args.keybindings)
address = args.address
if address is None:
address = defaultAdresses[args.socket]
if args.socket == "abstract":
address = '\0' + address
elif args.socket == "inet":
hostname, sep, port = address.partition(':')
address = (hostname, int(port))
colours = True
if args.colours:
colours = True
elif args.nocolours:
colours = False
name = args.name
if name is None:
username = getpass.getuser()
if args.socket == "unix" or args.socket == "abstract":
name = "~"+username
else:
name = username
clientmain(name, args.socket, address, keybindings, charmap, colours, args.logfile)
curses.wrapper(start)
if error is not None:
raise error
if closeMessage:
print(closeMessage, file=sys.stderr)

View File

@ -0,0 +1,67 @@
import argparse
import getpass
import json
import os
import os.path
from .start import main as clientmain
from . import loaders
defaultAdresses = {
"abstract": "asciifarm",
"unix": "asciifarm.socket",
"inet": "localhost:9021",
}
def parse_args(argv):
parser = argparse.ArgumentParser(description="The client to AsciiFarm. Run this to connect to to the server.", epilog="""
Gameplay information:
Walk around and explore the rooms.
Kill the goblins and plant the seeds.
~troido""", formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('-n', '--name', help='Your player name (must be unique!). Defaults to username on inet sockets and tildename on (unix socket (including abstract)', default=None)
parser.add_argument("-a", "--address", help="The address of the socket. When the socket type is 'abstract' this is just a name. When it is 'unix' this is a filename. When it is 'inet' is should be in the format 'address:port', eg 'localhost:8080'. Defaults depends on the socket type")
parser.add_argument("-s", "--socket", help="the socket type. 'unix' is unix domain sockets, 'abstract' is abstract unix domain sockets and 'inet' is inet sockets. ", choices=["abstract", "unix", "inet"], default="abstract")
parser.add_argument('-k', '--keybindings', help='The file with the keybinding configuration. This file is a JSON file.', default="default")
parser.add_argument('-c', '--characters', help='The file with the character mappings for the graphics. If it is either of these names: {} it will be loaded from the charmaps directory.'.format(list(loaders.standardCharFiles.keys())), default="default")
parser.add_argument('-o', '--logfile', help='All game messages will be written to this file.', default=None)
colourGroup = parser.add_mutually_exclusive_group()
colourGroup.add_argument('-l', '--colours', '--colors', help='enable colours! :)', action="store_true")
colourGroup.add_argument('-b', '--nocolours', '--nocolors', help='disable colours! :)', action="store_true")
args = parser.parse_args(argv)
charmap = loaders.loadCharmap(args.characters)
keybindings = loaders.loadKeybindings(args.keybindings)
address = args.address
if address is None:
address = defaultAdresses[args.socket]
if args.socket == "abstract":
address = '\0' + address
elif args.socket == "inet":
hostname, sep, port = address.partition(':')
address = (hostname, int(port))
colours = True
if args.colours:
colours = True
elif args.nocolours:
colours = False
name = args.name
if name is None:
username = getpass.getuser()
if args.socket == "unix" or args.socket == "abstract":
name = "~"+username
else:
name = username
return (name, args.socket, address, keybindings, charmap, colours, args.logfile)

View File

@ -1,52 +0,0 @@
import curses
import json
import os
import getpass
import sys
from .connection import Connection
from .gameclient import Client
from .display.display import Display
defaultAdresses = {
"abstract": "asciifarm",
"unix": "asciifarm.socket",
"inet": "localhost:9021",
}
def main(name, socketType, address, keybindings, characters, colours=False, logfile=None):
connection = Connection(socketType)
try:
connection.connect(address)
except ConnectionRefusedError:
print("ERROR: Could not connect to server.\nAre you sure that the server is running and that you're connecting to the right address?", file=sys.stderr)
return
error = None
closeMessage = None
os.environ.setdefault("ESCDELAY", "25")
def start(stdscr):
display = Display(stdscr, characters, colours)
client = Client(stdscr, display, name, connection, keybindings, logfile)
try:
client.start()
except KeyboardInterrupt:
client.close("^C caught, goodbye")
except Exception as e:
# throw the execption outside ncurses
# so the cleanup can happen first
nonlocal error
error = e
nonlocal closeMessage
closeMessage = client.closeMessage
curses.wrapper(start)
if error is not None:
raise error
if closeMessage:
print(closeMessage, file=sys.stderr)