File: networkclient.py

package info (click to toggle)
unknown-horizons 2019.1-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 347,832 kB
  • sloc: python: 46,804; xml: 3,137; sql: 1,189; sh: 736; makefile: 40; perl: 35
file content (322 lines) | stat: -rwxr-xr-x 8,275 bytes parent folder | download | duplicates (4)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#!/usr/bin/env python3

# ###################################################
# Copyright (C) 2008-2017 The Unknown Horizons Team
# team@unknown-horizons.org
# This file is part of Unknown Horizons.
#
# Unknown Horizons is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
# ###################################################


from __future__ import print_function
import getopt
import os
import sys
import platform
import signal
import logging
import logging.config
import logging.handlers

sys.path.append(os.getcwd())
from horizons.network.client import Client, ClientMode
import horizons.network

#-------------------------------------------------------------------------------


class AlarmException(Exception):
	pass


def alarmhandler(signum, frame):
	raise AlarmException


def nbrawinput(prompt='', timeout=1):
	signal.signal(signal.SIGALRM, alarmhandler)
	signal.alarm(timeout)
	try:
		text = raw_input(prompt)
		signal.alarm(0)
		return text
	except AlarmException:
		"""nothing in here"""
	signal.signal(signal.SIGALRM, signal.SIG_IGN)
	return None

#-------------------------------------------------------------------------------


def usage():
	print("Usage: {} -h host -p port".format(sys.argv[0]))


def onquit(*args):
	try:
		client.disconnect()
	except horizons.network.NetworkException:
		"""ignore the errors"""
	sys.exit(0)


def onconnect(*args):
	global client
	client.connect()


def ondisconnect(*args):
	global client
	client.disconnect()


def onlist(*args):
	global client
	games = client.listgames(*args)
	if games:
		print("[GAMESLIST]")
		for game in games:
			print("  [{}] map={} maxplayers={:d} playercnt={:d} name={}"
			      .format(game.uuid, game.mapname, game.maxplayers, game.playercnt, game.name))
	else:
		print("No games available")


def oncreate(*args):
	global client
	if len(args) != 3:
		print("Syntax: create <mapname> <maxplayers> <gamename>")
		return
	try:
		maxplayers = int(args[1])
		game = client.creategame(unicode(args[0]), maxplayers, unicode(args[2]))
		print("[GAME] [{}] mapname={} maxplayers={:d} playercnt={:d}"
		      .format(game.uuid, game.mapname, game.maxplayers, game.playercnt))
		for player in game.players:
			print("  Player: {} ({})".format(player.name, player.sid))
	except (ValueError, IndexError):
		print("Maxplayers must be an integer")


def onjoin(*args):
	global client
	if len(args) != 1:
		print("Syntax: join <uuid>")
		return
	try:
		game = client.joingame(unicode(args[0]))
		print("[GAME] [{}] mapname={} maxplayers={:d} playercnt={:d}"
		      .format(game.uuid, game.mapname, game.maxplayers, game.playercnt))
		for player in game.players:
			print("  Player: {} ({})".format(player.name, player.sid))
	except ValueError:
		print("Invalid UUID")


def onleave(*args):
	global client
	client.leavegame()


def onchat(*args):
	global client
	client.chat(u' '.join(args))


def cb_onchat(game, player, msg):
	print("[ONCHAT] [{}] {}: {}".format(game.uuid, player, msg))


def cb_onjoin(game, player):
	print("[ONJOIN] [{}] {} joins".format(game.uuid, player))


def cb_onleave(game, player):
	print("[ONLEAVE] [{}] {} leaves".format(game.uuid, player))


def cb_onchangename(game, oldplayer, newplayer, myself):
	global name
	print("[ONCHANGENAME] [{}] {} changed name to {}"
	      .format(game.uuid, oldplayer.name, newplayer.name))
	if myself:
		name = newplayer.name
		print("[NAME] My new name is {}".format(name))


def cb_ongameprepare(game):
	print("[ONGAMEPREPARE]")


def cb_ongamestarts(game):
	print("[ONGAMESTART]")


def cb_ongamedata(data):
	print("[ONGAMEDATA]: {}".format(data))


def onauto(*args):
	global client
	mapname = u"autocreated"
	gamename = u"mygame"
	maxplayers = 4
	if len(args) >= 1:
		mapname = unicode(args[0])
	if len(args) >= 2:
		try:
			maxplayers = int(args[1])
		except (ValueError, IndexError):
			print("Maxplayers must be an integer")
			return
	client.connect()
	games = client.listgames(mapname, maxplayers)
	if games:
		game = client.joingame(games[0].uuid)
	else:
		game = client.creategame(mapname, maxplayers, gamename)
	print("[GAME] [{}] mapname={} maxplayers={:d} playercnt={:d}"
	      .format(game.uuid, game.mapname, game.maxplayers, game.playercnt))
	for player in game.players:
		print("  Player: {}".format(player.name))
	client.chat("I am here guys. Game can start")


def ongamedata(*args):
	global client
	if client.mode is not ClientMode.Game:
		print("[ERROR] Client not in game mode")
		return
	client.send(u' '.join(args))


def onname(*args):
	global name, client
	if len(args) == 1:
		# see documentation for client.changename() why this code is like that
		if not client.changename(unicode(args[0])):
			return
		name = unicode(args[0])
	print("[NAME] My name is {}".format(name))


def onstatus(*args):
	global name, client
	statusstr = "[STATUS] name={} mode={}".format(name, "GAME" if client.mode is ClientMode.Game else "Server")
	statusstr += " connected={}".format("yes" if client.isconnected() else "no")
	statusstr += " server={}".format(client.serveraddress)
	print(statusstr)
	if client.isconnected():
		if client.game is not None:
			print("[STATUS] game: uuid={} mapname={} maxplayers={:d} playercnt={:d}"
			      .format(client.game.uuid, client.game.mapname, client.game.maxplayers, client.game.playercnt))
		for player in client.game.players:
			print("[STATUS]  Player: {}".format(player.name))


def onhelp(*args):
	global commands, prompt
	print("Available commands:")
	for command in sorted(commands.iterkeys()):
		print("  {}".format(command))

#-------------------------------------------------------------------------------


host = None
port = 0
commands = {
	'help':       onhelp,
	'connect':    onconnect,
	'disconnect': ondisconnect,
	'list':       onlist,
	'create':     oncreate,
	'join':       onjoin,
	'leave':      onleave,
	'chat':       onchat,
	'quit':       onquit,
	'auto':       onauto,
	'gamedata':   ongamedata,
	'name':       onname,
	'status':     onstatus,
}
prompt = ">>>"

if platform.system() == "Windows":
	print("Testclient doesn't run on windows")
	sys.exit(1)

try:
	opts, args = getopt.getopt(sys.argv[1:], 'h:p:')
except getopt.GetoptError as err:
	print(str(err))
	usage()
	sys.exit(1)

try:
	for (key, value) in opts:
		if key == '-h':
			host = value
		if key == '-p':
			port = int(value)
except (ValueError, IndexError):
	port = 0

if host is None or port is None or port <= 0:
	usage()
	sys.exit(1)

logging.config.fileConfig(os.path.join('content', 'logging.conf'))
logging.getLogger().addHandler(logging.StreamHandler(sys.stderr))
logging.getLogger("network").setLevel(logging.DEBUG)

client = None
version = u"0.512a"
name = u"client-{}".format(os.getpid())
onname()
client = Client(name, version, [host, port], None)
client.register_callback("lobbygame_chat", cb_onchat)
client.register_callback("lobbygame_join", cb_onjoin)
client.register_callback("lobbygame_leave", cb_onleave)
client.register_callback("lobbygame_changename", cb_onchangename)
client.register_callback("lobbygame_starts", cb_ongameprepare)
client.register_callback("game_starts", cb_ongamestarts)
client.register_callback("game_data", cb_ongamedata)

print(prompt, end=' ')
while True:
	try:
		if client.isconnected():
			client.ping()

		text = nbrawinput()
		if text is None:
			continue

		pieces = filter(lambda x: len(x.strip()) > 0, text.strip().split(' '))
		if len(pieces) <= 0:
			print(prompt, end=' ')
			continue

		cmd = pieces.pop(0)
		if cmd not in commands:
			print("[ERROR] Unknown command")
		else:
			commands[cmd](*pieces)
	except horizons.network.NetworkException as e:
		print("[ERROR] {}".format(e))
	print(prompt, end=' ')