コード例 #1
0
def add_user(user, password):
	user = util.trim(user)
	login_id = util.alphanums(user)
	
	if len(login_id) == 0:
		return 0
	
	start = get_next_start_point()
	
	sector = str(start[0]) + '^' + str(start[1])
	location = str(start[2]) + '^' + str(start[3])
	
	user_id = sql.insert('INSERT INTO `user` (`name`, `login_id`, `password`, `hq_sector`, `hq_loc`, `research`) values (%s,%s,%s,%s,%s, 20)',
		(user, login_id, password, sector, location))
	
	sql.insert("INSERT INTO `resource_status` (`user_id`) VALUES (" + str(user_id) + ")")
	# place HQ
	build.do_build(
		user_id,
		util.md5(str(time.time()) + "automated")[:16],
		0,
		sector,
		location,
		'hq',
		False)
	
	return user_id
コード例 #2
0
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"}
コード例 #3
0
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" }
コード例 #4
0
def add_user(user, password):
    user = util.trim(user)
    login_id = util.alphanums(user)

    if len(login_id) == 0:
        return 0

    start = get_next_start_point()

    sector = str(start[0]) + '^' + str(start[1])
    location = str(start[2]) + '^' + str(start[3])

    user_id = sql.insert(
        'INSERT INTO `user` (`name`, `login_id`, `password`, `hq_sector`, `hq_loc`, `research`) values (%s,%s,%s,%s,%s, 20)',
        (user, login_id, password, sector, location))

    sql.insert("INSERT INTO `resource_status` (`user_id`) VALUES (" +
               str(user_id) + ")")
    # place HQ
    build.do_build(user_id,
                   util.md5(str(time.time()) + "automated")[:16], 0, sector,
                   location, 'hq', False)

    return user_id
コード例 #5
0
def do_demolish(user_id, last_id, sector, loc, client_token):
    #last_id = util.parseInt(last_id)
    if sector == None or loc == None:
        return {'success': False, "message": "Malformed input"}
    if client_token == None:
        return {'success': False, 'message': "Missing client token"}
    sx, sy = util.tuple_from_coord(sector)
    x, y = util.tuple_from_coord(loc)
    poll_afterwards = last_id > 0

    # TODO: distinguish between involuntary demolition and damage

    # get building data

    target = -1
    buildings = sql.query("SELECT * FROM `structure` WHERE `user_id` = " +
                          str(user_id))

    for building in buildings:
        bx, by = util.tuple_from_coord(building['loc_xy'])
        bsx, bsy = util.tuple_from_coord(building['sector_xy'])
        type = building['type']
        id = building['structure_id']

        if type == 'hq': continue

        size = util.get_structure_size(type)

        for px in range(bx, bx + size):
            tsx = bsx + px // 60
            px = px % 60
            for py in range(by, by + size):
                tsy = bsy + py // 60
                py = py % 60
                if px == x and py == y and sx == tsx and sy == tsy:
                    target = id

                    # convert coordinates to be north corner
                    sx = bsx
                    sy = bsy
                    x = bx
                    y = by
                    break
            if target != -1: break

        if target != -1: break

    if target == -1:
        return {'success': False, 'message': "Invalid target"}

    building_id = target

    sector = str(sx) + '^' + str(sy)
    loc = str(x) + '^' + str(y)
    data = "Demolish:" + loc

    token_exists = sql.query(
        "SELECT `event_id` FROM `event` WHERE `client_token` = %s LIMIT 1",
        (client_token, ))
    if token_exists:
        pass
    else:
        event_id = sql.insert(
            ' '.join([
                "INSERT INTO `event`",
                "(`sector_xy`,`client_token`,`user_id`,`data`)", "VALUES",
                "(%s, %s, " + str(user_id) + ", %s)"
            ]), (sector, client_token, data))

        sql.query("DELETE FROM `structure` WHERE `structure_id` = " +
                  str(building_id) + " LIMIT 1")
        # no polling, I guess. This gets really complex since the target can be in a different
        # sector than the original request. Let updates come through the normal polling pipeline
    return {'success': True}
コード例 #6
0
def do_demolish(user_id, last_id, sector, loc, client_token):
	#last_id = util.parseInt(last_id)
	if sector == None or loc == None: return { 'success': False, "message": "Malformed input" }
	if client_token == None: return { 'success': False, 'message': "Missing client token" }
	sx, sy = util.tuple_from_coord(sector)
	x, y = util.tuple_from_coord(loc)
	poll_afterwards = last_id > 0
	
	# TODO: distinguish between involuntary demolition and damage
	
	# get building data
	
	target = -1
	buildings = sql.query("SELECT * FROM `structure` WHERE `user_id` = " + str(user_id))
	
	for building in buildings:
		bx, by = util.tuple_from_coord(building['loc_xy'])
		bsx, bsy = util.tuple_from_coord(building['sector_xy'])
		type = building['type']
		id = building['structure_id']
		
		if type == 'hq': continue
		
		size = util.get_structure_size(type)
		
		for px in range(bx, bx + size):
			tsx = bsx + px // 60
			px = px % 60
			for py in range(by, by + size):
				tsy = bsy + py // 60
				py = py % 60
				if px == x and py == y and sx == tsx and sy == tsy:
					target = id
	
					# convert coordinates to be north corner
					sx = bsx
					sy = bsy
					x = bx
					y = by
					break
			if target != -1: break
		
		if target != -1: break
	
	if target == -1:
		return { 'success': False, 'message': "Invalid target" }
	
	building_id = target
	
	
	
	
	sector = str(sx) + '^' + str(sy)
	loc = str(x) + '^' + str(y)
	data = "Demolish:" + loc
	
	token_exists = sql.query("SELECT `event_id` FROM `event` WHERE `client_token` = %s LIMIT 1", (client_token,))
	if token_exists:
		pass
	else:
		event_id = sql.insert(' '.join([
			"INSERT INTO `event`",
			"(`sector_xy`,`client_token`,`user_id`,`data`)",
			"VALUES",
			"(%s, %s, " + str(user_id) + ", %s)"]),
			(sector, client_token, data))
		
		sql.query("DELETE FROM `structure` WHERE `structure_id` = " + str(building_id) + " LIMIT 1")
		# no polling, I guess. This gets really complex since the target can be in a different
		# sector than the original request. Let updates come through the normal polling pipeline
	return { 'success': True }