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
|
|
|
|
|
2018-05-22 17:09:47 +00:00
|
|
|
error = None
|
2018-04-18 10:43:19 +00:00
|
|
|
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:
|
2018-04-18 10:43:19 +00:00
|
|
|
client.close("^C caught, goodbye")
|
2018-05-22 17:09:47 +00:00
|
|
|
except Exception as e:
|
|
|
|
# throw the execption outside ncurses
|
|
|
|
# so the cleanup can happen first
|
|
|
|
nonlocal error
|
|
|
|
error = e
|
2018-04-18 10:43:19 +00:00
|
|
|
nonlocal closeMessage
|
|
|
|
closeMessage = client.closeMessage
|
2018-05-22 17:09:47 +00:00
|
|
|
|
2017-10-28 10:31:42 +00:00
|
|
|
curses.wrapper(start)
|
|
|
|
|
2018-05-22 17:09:47 +00:00
|
|
|
if error is not None:
|
|
|
|
raise error
|
|
|
|
|
2018-04-18 10:43:19 +00:00
|
|
|
if closeMessage:
|
|
|
|
print(closeMessage, file=sys.stderr)
|