summaryrefslogtreecommitdiff
path: root/asciifarm/client/main.py
blob: fa8a07485efbfe4569c82f69efa42295eb6d8768 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#! /usr/bin/python3


import argparse
import getpass
import json
import os
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[:-3] for name in os.listdir(keybindingsPath) if name[-3:] == ".hy"]


defaultAdresses = {
    "abstract": "asciifarm",
    "unix": "asciifarm.socket",
    "inet": "localhost:9021",
    }

def main(argv=None):

    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 script with the keybindings. This file is a snippet of hy script.', default="keybindings")
    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")
    parser.add_argument('-o', '--logfile', help='All game messages will be written to this file.'.format(standardCharFiles), 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)
    
    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 + ".hy")
    with open(keyFile, 'r') as kf:
        keybindings = kf.read()
    
    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))
    
    colours = True
    if args.colours:
        colours = True
    elif args.nocolours:
        colours = False
    
    clientmain(args.name, args.socket, address, keybindings, charMap, colours, args.logfile)