Skip to content

Commit

Permalink
Factor out commodity lookup code
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen M. Cameron <[email protected]>
  • Loading branch information
smcameron committed Aug 13, 2021
1 parent e5bbec7 commit 0e6c8b3
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
10 changes: 10 additions & 0 deletions commodities.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,16 @@ int add_commodity(struct commodity **c, int *ncommodities, const char *category,
return n - 1;
}

int lookup_commodity(struct commodity *c, int ncommodities, const char *commodity_name)
{
int i;

for (i = 0; i < ncommodities; i++)
if (strcasecmp(commodity_name, c[i].name) == 0)
return i;
return -1;
}

const char *commodity_category(int cat)
{
if (cat < 0 || cat >= ncategories)
Expand Down
2 changes: 2 additions & 0 deletions commodities.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ float commodity_calculate_price(struct commodity *c,
int add_commodity(struct commodity **c, int *ncommodities, const char *category, const char *name, const char *unit,
const char *scans_as, float base_price, float volatility, float legality, int odds);

int lookup_commodity(struct commodity *c, int ncommodities, const char *commodity_name);

const char *commodity_category(int cat);
const char *commodity_category_description(int cat);
const int ncommodity_categories(void);
Expand Down
12 changes: 5 additions & 7 deletions snis_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -21403,13 +21403,11 @@ static int l_lookup_commodity(lua_State *l)
const char *lua_commodity_name = luaL_checkstring(l, 1);
int i;

for (i = 0; i < ncommodities; i++) {
if (strcasecmp(lua_commodity_name, commodity[i].name) == 0) {
lua_pushnumber(l, (float) i);
return 1;
}
}
lua_pushnil(l);
i = lookup_commodity(commodity, ncommodities, lua_commodity_name);
if (i < 0)
lua_pushnil(l);
else
lua_pushnumber(l, (float) i);
return 1;
}

Expand Down

0 comments on commit 0e6c8b3

Please sign in to comment.