/**
 * Parse a price string from the shop interface to an integer.
 *
 * For example, the string <pre>10 m 15 g 90 s 40 c</pre> would become
 * <pre>100159040</pre>
 * @param text The string to parse
 * @return An integer representation of the price string. */
int shop_price2int(char *text)
{
	int pos = 0, total_value = 0;
	char *word, value_buf[MAX_BUF];

	while ((word = get_word_from_string(text, &pos)))
	{
		int i = 0, flag = *word;

		while (*(word + i) != '\0')
		{
			if (*(word + i) < '0' || *(word + i) > '9')
			{
				flag = 0;
			}

			i++;
		}

		/* If still set, we have a valid number in the word string */
		if (flag)
		{
			int value = atoi(word);

			/* A valid number - now let's look if we have a valid money keyword */
			if (value > 0 && value < 100000)
			{
				if ((word = get_word_from_string(text, &pos)) && *word != '\0')
				{
					int len = (int) strlen(word);

					for (i = 0; i < COINS_ARRAY_SIZE; i++)
					{
						if (!strncasecmp(coins[i].name, word, len))
						{
							total_value += value * coins[i].value;

							break;
						}
					}
				}
			}
		}
	}

	snprintf(value_buf, sizeof(value_buf), "%d", total_value);

	if (total_value > MAX_PRICE_VALUE || atoi(value_buf) < 1)
	{
		return 0;
	}

	return total_value;
}
Exemple #2
0
/******************************************************************************
* dot_delim_string()
*
* Parameters
* ---------
* - a string
*
* Returns
* -------
* - a new string
*
* Notes
* -----
* Takes a whitespace delimited string and make it dot delimited
* there is NO leading or trailing dot
******************************************************************************/
char *dot_delim_string(char *s1)
{

    int num_words, j;
    char *ret_str;

    if (s1 == NULL ) print_error("NULL string passed to dot_delim_string");
    num_words = num_words_in_string(s1);
    ret_str = get_word_from_string(s1,1);
    if (num_words > 1){
        for (j=2;j<=num_words;j++){
            // note memorey leak, should use a temp string
            // so can free old ret_str bf reassigning.....
            ret_str = concat_2(char_to_string('.'),get_word_from_string(s1,j) );
        }
    }
    return(ret_str);
}
Exemple #3
0
/* Inserts a word to a dictionary and returns true if query is correct
 * and word doesn't exist in a dictionary; otherwise query is ignored and
 * function returns false. */
bool insert(char *params) {
    bool query_result = false;
    char *word = calloc(LINE_LENGTH, sizeof(char));
    int insert_result = -1;
    if (get_word_from_string(&params, word) && is_string_blank(&params)
        && (insert_result = trie_insert(word)) != -1) {
            printf("word number: %d\n", insert_result);
            query_result = true;
    }
    free(word);
    return query_result;
}
Exemple #4
0
/******************************************************************************
* string_to_dbl_array_fmt()
* Convert a formatted string to an array of doubles
*
* Parameters
* ---------
* - a formatted string
* - nrow is number of rows in converted arrays
* - ncols is the number of columns
*
* Returns
* -------
* - new array
*
* Notes
* -----
* This will take a string with formatting and convert it to an arrays
* Semi-colons are used to deliniate rows of an array.
******************************************************************************/
double *string_to_dbl_array_fmt(char *str, int *nrow, int *ncol )
{
    int     n,j,k;
    double *arr;
    char   *temp;
    char  **strarr;
    //char *dummy;

    if (str == NULL){
        *nrow = 0;
        *ncol = 0;
        return (NULL);
    }

    strarr    = split_string(str, ";",   nrow, DEFAULT);
    strarr[0] = clean_string(strarr[0], NOCOPY);
    *ncol     = num_words_in_string(strarr[0]);

    for (j=1;j<*nrow;j++){
        strarr[j] = clean_string(strarr[j], NOCOPY);
        if( *ncol != num_words_in_string(strarr[j])){
            buff_print("Error: rows and cols dont match\n");
            *nrow = 1; *ncol = 1;
            clear_str_arr(strarr);
            return (NULL);
        }
    }

    n = (*nrow) * (*ncol);
    if (n == 0) {
        clear_str_arr(strarr);
        return( (double *)NULL );
    }
    arr = new_array(n, double);

    n = 0;
    for(j=0;j<*nrow;j++){
        for(k=0;k<*ncol;k++){
            temp = get_word_from_string(strarr[j],k+1);
            arr[n] = atof(temp);
            free(temp);
            n++;
        }
    }
    clear_str_arr(strarr);
    return(&arr[0]);
}
Exemple #5
0
/******************************************************************************
* string_to_dbl_array()
* Convert a string to an array of doubles
*
* Parameters
* ---------
* - a string
* - n is the count on the length of the array
*
* Returns
* -------
* - a new array of doubles
*
* Notes
* -----
* Take a string of whitespace delim doubles and make into an array
* Note any words in the string which cant be interpretted as a number
* result in a zero value (this is a property of atof)!
* Could use the string_to_num function to include error checking??
*
******************************************************************************/
double *string_to_dbl_array(char *str, int *n )
{
    int     num_words, j;
    double *arr;
    char   *temp;
    //char *dummy;

    num_words = num_words_in_string(str);
    *n = num_words;
    if (num_words == 0) return( (double *)NULL );

    arr = new_array(num_words, double);
    for(j=0;j<num_words;j++){
        temp = get_word_from_string(str,j+1);
        arr[j] = atof(temp);
        //arr[j] = strtod(temp,&dummy);
        //buff_print("%s\n",temp);
        free(temp);
    }
    return(&arr[0]);
}
Exemple #6
0
/**
 * Deposit money to player's bank object.
 * @param op Player.
 * @param bank Bank object in player's inventory.
 * @param text What was said to trigger this.
 * @retval 1 Money deposited successfully.
 * @retval 0 Failed to deposit money.
 * @retval -1 The text parameter was invalid. */
int bank_deposit(object *op, object *bank, char *text)
{
	int pos = 0;
	_money_block money;

	get_word_from_string(text, &pos);
	get_money_from_string(text + pos , &money);

	if (!money.mode)
	{
		new_draw_info(NDI_UNIQUE, op, "Deposit what?\nUse 'deposit all' or 'deposit 40 gold, 20 silver...'");
		return -1;
	}
	else if (money.mode == MONEYSTRING_ALL)
	{
		bank->value += remove_money_type(op, op, -1, 0);
		fix_player(op);
	}
	else
	{
		if (money.mithril)
		{
			if (query_money_type(op, coins_arch[0]->clone.value) < money.mithril)
			{
				new_draw_info(NDI_UNIQUE, op, "You don't have that many mithril coins.");
				return 0;
			}
		}

		if (money.gold)
		{
			if (query_money_type(op, coins_arch[1]->clone.value) < money.gold)
			{
				new_draw_info(NDI_UNIQUE, op, "You don't have that many gold coins.");
				return 0;
			}
		}

		if (money.silver)
		{
			if (query_money_type(op, coins_arch[2]->clone.value) < money.silver)
			{
				new_draw_info(NDI_UNIQUE, op, "You don't have that many silver coins.");
				return 0;
			}
		}

		if (money.copper)
		{
			if (query_money_type(op, coins_arch[3]->clone.value) < money.copper)
			{
				new_draw_info(NDI_UNIQUE, op, "You don't have that many copper coins.");
				return 0;
			}
		}

		if (money.mithril)
		{
			remove_money_type(op, op, coins_arch[0]->clone.value, money.mithril);
		}

		if (money.gold)
		{
			remove_money_type(op, op, coins_arch[1]->clone.value, money.gold);
		}

		if (money.silver)
		{
			remove_money_type(op, op, coins_arch[2]->clone.value, money.silver);
		}

		if (money.copper)
		{
			remove_money_type(op, op, coins_arch[3]->clone.value, money.copper);
		}

		bank->value += money.mithril * coins_arch[0]->clone.value + money.gold * coins_arch[1]->clone.value + money.silver * coins_arch[2]->clone.value + money.copper * coins_arch[3]->clone.value;
		fix_player(op);
	}

	return 1;
}
Exemple #7
0
/**
 * Get money from a string.
 * @param text Text to get money from.
 * @param money Money block structure.
 * @return One of @ref MONEYSTRING_xxx. */
int get_money_from_string(char *text, struct _money_block *money)
{
	int pos = 0;
	char *word;

	memset(money, 0, sizeof(struct _money_block));

	/* Kill all whitespace */
	while (*text !='\0' && (isspace(*text) || !isprint(*text)))
	{
		text++;
	}

	/* Easy, special case: all money */
	if (!strncasecmp(text, "all", 3))
	{
		money->mode = MONEYSTRING_ALL;
		return money->mode;
	}

	money->mode = MONEYSTRING_NOTHING;

	while ((word = get_word_from_string(text, &pos)))
	{
		int i = 0, flag = *word;

		while (*(word + i) != '\0')
		{
			if (*(word + i) < '0' || *(word + i) > '9')
			{
				flag = 0;
			}

			i++;
		}

		/* If still set, we have a valid number in the word string */
		if (flag)
		{
			int value = atoi(word);

			/* A valid number - now lets look we have a valid money keyword */
			if (value > 0 && value < 1000000)
			{
				if ((word = get_word_from_string(text, &pos)) && *word != '\0')
				{
					size_t len = strlen(word);

					if (!strncasecmp("mithril", word, len))
					{
						money->mode = MONEYSTRING_AMOUNT;
						money->mithril += value;
					}
					else if (!strncasecmp("gold", word, len))
					{
						money->mode = MONEYSTRING_AMOUNT;
						money->gold += value;
					}
					else if (!strncasecmp("silver", word, len))
					{
						money->mode = MONEYSTRING_AMOUNT;
						money->silver += value;
					}
					else if (!strncasecmp("copper", word, len))
					{
						money->mode = MONEYSTRING_AMOUNT;
						money->copper += value;
					}
				}
			}
		}
	}

	return money->mode;
}
Exemple #8
0
/**
 * Withdraw money player previously stored in bank object.
 * @param op Player.
 * @param bank Bank object in player's inventory.
 * @param text What was said to trigger this.
 * @retval 1 Money withdrawn successfully.
 * @retval 0 Failed to withdraw money.
 * @retval -1 The text parameter was invalid. */
int bank_withdraw(object *op, object *bank, char *text)
{
	int pos = 0;
	sint64 big_value;
	_money_block money;

	get_word_from_string(text, &pos);
	get_money_from_string(text + pos , &money);

	if (!money.mode)
	{
		new_draw_info(NDI_UNIQUE, op, "Withdraw what?\nUse 'withdraw all' or 'withdraw 30 gold, 20 silver...'");
		return -1;
	}
	else if (money.mode == MONEYSTRING_ALL)
	{
		sell_item(NULL, op, bank->value);
		bank->value = 0;
		fix_player(op);
	}
	else
	{
		/* Just to set a border.... */
		if (money.mithril > 100000 || money.gold > 100000 || money.silver > 1000000 || money.copper > 1000000)
		{
			new_draw_info(NDI_UNIQUE, op, "Withdraw values are too high.");
			return 1;
		}

		big_value = money.mithril * coins_arch[0]->clone.value + money.gold * coins_arch[1]->clone.value + money.silver * coins_arch[2]->clone.value + money.copper * coins_arch[3]->clone.value;

		if (big_value > bank->value)
		{
			return 0;
		}

		if (money.mithril)
		{
			insert_money_in_player(op, &coins_arch[0]->clone, money.mithril);
		}

		if (money.gold)
		{
			insert_money_in_player(op, &coins_arch[1]->clone, money.gold);
		}

		if (money.silver)
		{
			insert_money_in_player(op, &coins_arch[2]->clone, money.silver);
		}

		if (money.copper)
		{
			insert_money_in_player(op, &coins_arch[3]->clone, money.copper);
		}

		bank->value -= big_value;
		fix_player(op);
	}

	return 1;
}