Asciifarm/asciifarm/client/start.py

53 lines
1.4 KiB
Python
Raw Normal View History

2017-10-28 10:31:42 +00:00
import curses
import json
import os
import getpass
import sys
from .connection import Connection
from .gameclient import Client
2017-12-28 21:47:36 +00:00
from .display.display import Display
2017-10-28 10:31:42 +00:00
defaultAdresses = {
"abstract": "asciifarm",
"unix": "asciifarm.socket",
"inet": "localhost:9021",
}
2017-11-01 11:41:38 +00:00
def main(name, socketType, address, keybindings, characters, colours=False, logfile=None):
2017-10-28 10:31:42 +00:00
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
2017-10-28 10:31:42 +00:00
2018-04-19 08:51:21 +00:00
os.environ.setdefault("ESCDELAY", "25")
2017-10-28 10:31:42 +00:00
def start(stdscr):
display = Display(stdscr, characters, colours)
2017-11-01 11:41:38 +00:00
client = Client(stdscr, display, name, connection, keybindings, logfile)
2017-10-28 10:31:42 +00:00
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
2017-10-28 10:31:42 +00:00
curses.wrapper(start)
if error is not None:
raise error
if closeMessage:
print(closeMessage, file=sys.stderr)