diff options
| author | troido <troido@hotmail.com> | 2017-10-28 12:31:42 +0200 |
|---|---|---|
| committer | troido <troido@hotmail.com> | 2017-10-28 12:31:42 +0200 |
| commit | a8fd6fc77a3c5af06727a68684e61c63553679a8 (patch) | |
| tree | cde31f96a13907b3b5889d630667084e8dfdbcec /asciifarm/client/__init__.py | |
| parent | 4fe76206a7b183eaf46268e9dc03916de093f3b9 (diff) | |
better use of __main__ and __init__
Diffstat (limited to 'asciifarm/client/__init__.py')
| -rwxr-xr-x[-rw-r--r--] | asciifarm/client/__init__.py | 84 |
1 files changed, 58 insertions, 26 deletions
diff --git a/asciifarm/client/__init__.py b/asciifarm/client/__init__.py index f2814d7..92e1648 100644..100755 --- a/asciifarm/client/__init__.py +++ b/asciifarm/client/__init__.py @@ -1,12 +1,22 @@ +#! /usr/bin/python3 -import curses + +import argparse +import getpass import json import os -import getpass -import sys -from .connection import Connection -from .client import Client -from .display import Display +import os.path + +from .start import main as clientmain + +thisPath = os.path.dirname(__file__) +farmsPath = os.path.join(thisPath, "..") +charMapPath = os.path.join(farmsPath, "charmaps") +keybindingsPath = os.path.join(farmsPath, "keybindings") + +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", @@ -14,28 +24,50 @@ defaultAdresses = { "inet": "localhost:9021", } -def main(name, socketType, address, keybindings, characters, colours=False): +def main(): + + 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', default=getpass.getuser()) + 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 keybindings. If it is either of these names: {} it will be loaded from the keybindings directory.'.format(standardKeyFiles), 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(standardCharFiles), default="default") - 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 + 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() - caught_ctrl_c = False + charFile = args.characters + if charFile in standardCharFiles: + charFile = os.path.join(charMapPath, charFile + ".json") + with open(charFile, 'r') as cf: + charMap = json.load(cf) + keyFile = args.keybindings + if keyFile in standardKeyFiles: + keyFile = os.path.join(keybindingsPath, keyFile + ".json") + with open(keyFile, 'r') as kf: + keybindings = json.load(kf) - def start(stdscr): - display = Display(stdscr, characters, colours) - client = Client(stdscr, display, name, connection, keybindings) - nonlocal caught_ctrl_c - try: - client.start() - except KeyboardInterrupt: - caught_ctrl_c = True - client.keepAlive = False + address = args.address + if address == 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)) - curses.wrapper(start) + colours = True + if args.colours: + colours = True + elif args.nocolours: + colours = False - if caught_ctrl_c: - print('^C caught, goodbye!') + clientmain(args.name, args.socket, address, keybindings, charMap, colours) + |
