Skip to content

Commit

Permalink
Stock: Rename attribute functions for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
eric15342335 committed Jun 8, 2024
1 parent 48697e0 commit 0b9fa57
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
4 changes: 2 additions & 2 deletions include/events.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ enum stock_modifiers {
};

/// @brief Multiplier for mean
const float meanMultiplier = 0.3;
constexpr float meanMultiplier = 0.3f;

/// @brief Multiplier for standard deviation
const float sdMultiplier = 15.0;
Expand All @@ -67,7 +67,7 @@ const float defaultLowerLimit = -5;
const float defaultUpperLimit = 5;

/// @brief Default mean
const float defaultMean = 0.1;
constexpr float defaultMean = 0.5f / meanMultiplier;

/// @brief Rate of decrease of duration
const unsigned int durationDecreaseMultiplier = 2;
Expand Down
14 changes: 8 additions & 6 deletions include/stock.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const float STOCK_PRICE_LIMIT = 1000.0f;
* // What is the name of the stock?
* std::string name = stock.get_name();
* // Get the upper limit of the percentage change of the stock price:
* float upper_limit = stock.getTotalAttribute(stock_modifiers::upper_limit);
* float upper_limit = stock.get_total_attribute(stock_modifiers::upper_limit);
* @endcode
*/
class Stock {
Expand Down Expand Up @@ -123,13 +123,13 @@ class Stock {
/**
* @brief Get the total change of attribute of the stock due to events only.
* Getter function. \n Example usage: @code {.cpp}
* stock.get_attribute(stock_modifiers::standard_deviation);
* stock.get_base_attribute(stock_modifiers::standard_deviation);
* @endcode
* @param attribute The attribute to get.
* @return Total change of attribute due to Stock_event. Does not include the
* base value.
*/
float sum_attribute(stock_modifiers attribute);
float get_event_attribute(stock_modifiers attribute);

/**
* @brief Call this when the game proceed to next round.
Expand Down Expand Up @@ -188,7 +188,9 @@ class Stock {
* lower_limit, upper_limit
* @return Base value of stock_attributes as float.
*/
float get_attribute(stock_modifiers attribute) { return attributes[attribute]; }
float get_base_attribute(stock_modifiers attribute) {
return attributes[attribute];
}

/**
* @brief Get size of the history.
Expand Down Expand Up @@ -233,11 +235,11 @@ class Stock {
bool can_add_event(const Stock_event & event);

/**
* @brief Sums up get_attribute() and sum_attribute().
* @brief Sums up get_base_attribute() and get_event_attribute().
* @param attribute The attribute to get.
* @return Total value of the attribute. `float` type.
*/
float getTotalAttribute(stock_modifiers attribute);
float get_total_attribute(stock_modifiers attribute);

/**
* @brief Calculate the total value of the stocks the player is holding.
Expand Down
4 changes: 2 additions & 2 deletions src/events.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ const std::vector<Stock_event> all_stock_events = {
/** mutually_exclusive_events */ {},
/** text */ "Economic Recession: Market Downturn Signals Investor Concerns",
/** duration */ 7,
/** percentage_permille */ 4,
/** percentage_permille */ 2,
/** type_of_event */ all_stocks,
/** category */ 0,
/** modifiers*/
{{standard_deviation, 0.2}, {mean, -50}, {lower_limit, -50}, {upper_limit, 0}},
{{standard_deviation, 0.2}, {mean, -30}, {lower_limit, -30}, {upper_limit, 0}},
},
{
/** event_id */ 3,
Expand Down
12 changes: 6 additions & 6 deletions src/random_price.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ unsigned int random_integer(unsigned int max_integer) {
}

std::map<stock_modifiers, float> getProcessedModifiers(Stock stock) {
float trueMean = meanMultiplier * (stock.getTotalAttribute(mean));
float trueSD = sdMultiplier * (stock.getTotalAttribute(standard_deviation));
float trueMean = meanMultiplier * (stock.get_total_attribute(mean));
float trueSD = sdMultiplier * (stock.get_total_attribute(standard_deviation));
unsigned int rounds_passed = stock.get_history_size();
if (stock.get_price() < stock.get_initial_price() / 10) {
// Force the return of a significantly higher mean normal dist, by a bit over
Expand All @@ -74,11 +74,11 @@ std::map<stock_modifiers, float> getProcessedModifiers(Stock stock) {
// of the stock.
float temp = 100 * std::abs(stock.get_initial_price() - stock.get_price()) /
stock.get_price();
float upper_lim = stock.get_attribute(upper_limit) +
stock.sum_attribute(upper_limit) * upperLimitMultiplier +
float upper_lim = stock.get_base_attribute(upper_limit) +
stock.get_event_attribute(upper_limit) * upperLimitMultiplier +
std::min(static_cast<int>(rounds_passed / 3), 10) + temp;
float lower_lim = stock.get_attribute(lower_limit) +
stock.sum_attribute(lower_limit) * lowerLimitMultiplier -
float lower_lim = stock.get_base_attribute(lower_limit) +
stock.get_event_attribute(lower_limit) * lowerLimitMultiplier -
std::min(static_cast<int>(rounds_passed / 3), 10) - temp;
// Standardize the upper and lower limit
float zScoreUpLimit = (upper_limit - trueMean) / trueSD;
Expand Down
20 changes: 10 additions & 10 deletions src/stock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ void Stock::remove_obselete_event(void) {
}
}

float Stock::sum_attribute(stock_modifiers attribute) {
float Stock::get_event_attribute(stock_modifiers attribute) {
float sum = 0;
std::list<Stock_event>::iterator event_itr = events.begin();
while (event_itr != events.end()) {
Expand Down Expand Up @@ -314,8 +314,8 @@ std::vector<unsigned int> Stock::get_event_ids(void) {
return event_ids;
}

float Stock::getTotalAttribute(stock_modifiers attribute) {
return attributes[attribute] + sum_attribute(attribute);
float Stock::get_total_attribute(stock_modifiers attribute) {
return attributes[attribute] + get_event_attribute(attribute);
}

void sortStocksList(std::vector<Stock> & stocks_list, SortingMethods sortMethod,
Expand All @@ -339,25 +339,25 @@ void sortStocksList(std::vector<Stock> & stocks_list, SortingMethods sortMethod,
break;
case by_sd:
std::sort(stocks_list.begin(), stocks_list.end(), [](Stock a, Stock b) {
return a.getTotalAttribute(standard_deviation) <
b.getTotalAttribute(standard_deviation);
return a.get_total_attribute(standard_deviation) <
b.get_total_attribute(standard_deviation);
});
break;
case by_mean:
std::sort(stocks_list.begin(), stocks_list.end(), [](Stock a, Stock b) {
return a.getTotalAttribute(mean) < b.getTotalAttribute(mean);
return a.get_total_attribute(mean) < b.get_total_attribute(mean);
});
break;
case by_lower_limit:
std::sort(stocks_list.begin(), stocks_list.end(), [](Stock a, Stock b) {
return a.getTotalAttribute(lower_limit) <
b.getTotalAttribute(lower_limit);
return a.get_total_attribute(lower_limit) <
b.get_total_attribute(lower_limit);
});
break;
case by_upper_limit:
std::sort(stocks_list.begin(), stocks_list.end(), [](Stock a, Stock b) {
return a.getTotalAttribute(upper_limit) <
b.getTotalAttribute(upper_limit);
return a.get_total_attribute(upper_limit) <
b.get_total_attribute(upper_limit);
});
break;
default:
Expand Down

0 comments on commit 0b9fa57

Please sign in to comment.