summaryrefslogtreecommitdiff
path: root/asciifarm/client/start.py
blob: fef5d9e230733b92a124a50426e2859844fc4e9d (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

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
    
    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")
        nonlocal closeMessage
        closeMessage = client.closeMessage
    
    curses.wrapper(start)
    
    if closeMessage:
        print(closeMessage, file=sys.stderr)