def do_build(user_id, client_token, last_id, sector, loc, type, poll_afterwards=True): sector = util.tsanitize(sector) loc = util.tsanitize(loc) last_id = util.parseInt(last_id) verify_position = sql.query("SELECT `hq_sector` FROM `user` WHERE `user_id` = " + str(user_id) + " LIMIT 1") if verify_position[0]["hq_sector"] == sector: if client_token: duplicate = sql.query("SELECT `client_token` FROM `event` WHERE `client_token` = %s", (client_token,)) if not duplicate and validate_building(type, sector, loc): error = verify_placement(type, sector, loc) if error == None: if type == "hq" or try_deplete_resources( user_id, settings.building_cost[type][0], settings.building_cost[type][1], settings.building_cost[type][2], settings.building_cost[type][3], settings.building_cost[type][4], settings.building_cost[type][5], ): data = "Build:%s,%s" % (type, loc) event_id = sql.insert( "INSERT INTO `event` (`client_token`, `sector_xy`, `user_id`, `data`) VALUES (%s, %s, %s, %s)", (client_token, sector, user_id, data), ) quarry_data = "" if type == "quarry": resources = "a c s".split(" ") distribution = [int(random.random() * 101)] rdist = int(random.random() * (100 - distribution[0] + 1)) distribution.append(rdist) distribution.append(100 - sum(distribution)) random.shuffle(distribution) quarry_data = ( "a" + str(distribution[0]) + "c" + str(distribution[1]) + "s" + str(distribution[2]) ) sql.insert( "INSERT INTO `structure` (`type`, `sector_xy`, `loc_xy`, `user_id`, `event_id`, `data`) VALUES (%s, %s, %s, %s, %s, %s)", (type, sector, loc, user_id, event_id, quarry_data), ) else: return {"success": False, "message": "Insufficient resources"} else: return {"success": False, "message": error} if poll_afterwards: return poll.do_poll(user_id, str(last_id) + "^" + sector) else: return {"success": False, "message": "missing client token"} else: return {"success": False, "message": "Cannot build this far from your HQ"}
def do_build(user_id, client_token, last_id, sector, loc, type, poll_afterwards=True): sector = util.tsanitize(sector) loc = util.tsanitize(loc) last_id = util.parseInt(last_id) verify_position = sql.query("SELECT `hq_sector` FROM `user` WHERE `user_id` = " + str(user_id) + " LIMIT 1") if verify_position[0]['hq_sector'] == sector: if client_token: duplicate = sql.query( 'SELECT `client_token` FROM `event` WHERE `client_token` = %s', (client_token,)) if not duplicate and validate_building(type, sector, loc): error = verify_placement(type, sector, loc) if error == None: if type == 'hq' or try_deplete_resources( user_id, settings.building_cost[type][0], settings.building_cost[type][1], settings.building_cost[type][2], settings.building_cost[type][3], settings.building_cost[type][4], settings.building_cost[type][5]): data = 'Build:%s,%s' % (type, loc) event_id = sql.insert( 'INSERT INTO `event` (`client_token`, `sector_xy`, `user_id`, `data`) VALUES (%s, %s, %s, %s)', (client_token, sector, user_id, data)) quarry_data = '' if type == 'quarry': resources = 'a c s'.split(' ') distribution = [int(random.random() * 101)] rdist = int(random.random() * (100 - distribution[0] + 1)) distribution.append(rdist) distribution.append(100 - sum(distribution)) random.shuffle(distribution) quarry_data = 'a' + str(distribution[0]) + 'c' + str(distribution[1]) + 's' + str(distribution[2]) sql.insert('INSERT INTO `structure` (`type`, `sector_xy`, `loc_xy`, `user_id`, `event_id`, `data`) VALUES (%s, %s, %s, %s, %s, %s)', (type, sector, loc, user_id, event_id, quarry_data)) else: return { 'success': False, 'message': "Insufficient resources" } else: return { 'success': False, 'message': error } if poll_afterwards: return poll.do_poll(user_id, str(last_id) + '^' + sector) else: return { 'success':False, 'message': "missing client token" } else: return { 'success': False, 'message': "Cannot build this far from your HQ" }
def do_things(action, args): if action == 'echo': return { 'success': True, 'data': args.get('data', None) } elif action == 'api_users': from serverlib import api return api.get_user_info() elif action == 'authenticate': return authenticate.heavy_authenticate(args.get('user', ''), args.get('password', ''), True) elif action == 'getuser': from serverlib import getuser return getuser.get_user(args.get('user_id_list', None)) else: user_id = util.parseInt(args.get('user_id', 0)) if authenticate.light_authenticate(user_id, args.get('password', '')): if action == 'poll': return poll.do_poll(user_id, args.get('sectors')) elif action == 'build': from serverlib import build return build.do_build( user_id, args.get('client_token'), args.get('last_id'), args.get('sector'), args.get('loc'), args.get('type')) elif action == 'demolish': from serverlib import demolish return demolish.do_demolish( user_id, args.get('last_id'), args.get('sector'), args.get('loc'), args.get('client_token')) elif action == 'radar': from serverlib import neighbors return neighbors.find_neighbors( user_id, args.get('rx', None), args.get('ry', None)) elif action == 'research': from serverlib import research return research.apply_research( user_id, args.get('type', None)) elif action == 'quarrydata': from serverlib import quarrydata return quarrydata.get_quarry_data( user_id, args.get('sector', None), args.get('xy', None)) elif action == 'buildbot': from serverlib import producebot return producebot.produce_bot( user_id, args.get('type', 0)) elif action == 'getbots': from serverlib import producebot return producebot.get_count(user_id) elif action == 'dispatchbots': from serverlib import producebot return producebot.dispatch(user_id) elif action == 'debug_resources': from serverlib import producebot return producebot.DEBUG_resources(user_id) elif action == 'start_research': from serverlib import research return research.apply_research(user_id, args.get('subject', None)) elif action == 'alienkill': from serverlib import serverbattle return serverbattle.award_resources( user_id, args.get('alientype', 'x')) elif action == 'attacksuccess': from serverlib import serverbattle return serverbattle.award_bytes( user_id, args.get('attacked', 0), args.get('numbytes', 0)) else: return { 'success': False, 'message': "Unrecognized command" } else: return { 'success': False, 'message': "Invalid username/password." }