Example #1
0
	def api(self, *args, **kwargs):
		if len(args) == 0:
			return error(404)
		elif args[0] == 'getMap':
			jsonGameMap = {
				'size': [config.load_var('map_x_size'), config.load_var('map_y_size')],
				'limits': {
					'players': config.load_var('max_players'),
					'entities': config.load_var('max_entities')
				},
				'objects': []
			}
			for i in MainGameLoop.gameMap.gameObjects:
				jsonGameMap['objects'].append(i.getDict())
			return json.dumps(jsonGameMap)
		elif args[0] == 'addObj':
			if set(['loc','type']).issubset(kwargs):
				loc = [int(kwargs['loc'].split(',')[0]), int(kwargs['loc'].split(',')[1])]
				myObject = GameObject(loc, kwargs['type'], cherrypy.session['userid'])
				myObject.velocity = [1,1]
				MainGameLoop.gameMap.addObject(myObject)
				return json.dumps(myObject.getDict())
		elif args[0] == 'writeMsg':
			if 'msg' in kwargs:
				print(kwargs['msg'])
		elif args[0] == 'changeDirection':
			if set(['loc','ID']).issubset(kwargs):
				loc = [int(kwargs['loc'].split(',')[0]), int(kwargs['loc'].split(',')[1])]
				Movement.newVelocity(MainGameLoop.gameMap.getObject(kwargs['ID']), loc)
		elif args[0] == 'gotoPoint':
			if set(['loc','ID']).issubset(kwargs):
				if kwargs['ID'].split('-')[0] == cherrypy.session['userid']:
					loc = [int(kwargs['loc'].split(',')[0]), int(kwargs['loc'].split(',')[1])]
					MainGameLoop.gameMap.getObject(kwargs['ID']).current_cmd = Command('gotoPoint', dest_pt=loc)
		else:
			return error(message="API method does not exist")
Example #2
0
				Movement.newVelocity(MainGameLoop.gameMap.getObject(kwargs['ID']), loc)
		elif args[0] == 'gotoPoint':
			if set(['loc','ID']).issubset(kwargs):
				if kwargs['ID'].split('-')[0] == cherrypy.session['userid']:
					loc = [int(kwargs['loc'].split(',')[0]), int(kwargs['loc'].split(',')[1])]
					MainGameLoop.gameMap.getObject(kwargs['ID']).current_cmd = Command('gotoPoint', dest_pt=loc)
		else:
			return error(message="API method does not exist")

	@cherrypy.expose
	def default(self, *args, **kwargs):
		return error(404)

if __name__ == '__main__':
	#Just for fun, the following two lines count the number of times this code has been run and saves it in variables.ini
	init_count = config.load_var(var_name='init_count', section='global_stats')
	config.save_var_string(var_name='init_count', section='global_stats', var_string=str(init_count + 1))

	conf = {
		'/': {
			'tools.sessions.on': True,
			'tools.sessions.storage_type': "file",
			'tools.sessions.storage_path': os.path.join(os.getcwd(), "sessions"),
			'tools.staticdir.root': os.path.abspath(os.getcwd())
		},
		'/static': {
			'tools.staticdir.on': True,
			'tools.staticdir.dir': 'static'
		}
	}
	cherrypy.config.update({