Пример #1
0
// ReportLeaders returns a string of the leaderboard
func ReportLeaders(ag *accessors.AccessorGroup) string {
	users := ag.GetAllUsers()
	pValues := []models.PortfolioValue{}

	// Compile portfolio data
	for _, user := range users {
		portfolio := ag.GetPortfolio(user.UserID)
		worth := portfolio.Turnips

		if worth != 1000000 && len(portfolio.Investments) > 0 {
			for _, value := range portfolio.Investments {
				if value.Quantity > 0 {
					price := models.CheckStock(value.Ticker).Price
					worth = worth + price*value.Quantity
				}
			}

			pValues = append(pValues, models.PortfolioValue{UserID: user.UserID, Username: user.Username, Value: worth})
		}
	}

	// Sort the portfolios by value
	sort.Sort(models.SortedPortfolioValue(pValues))

	message := []string{}
	message = append(message, fmt.Sprintf("*End of the Day Leaderboard*"))
	// Run through the sorted values and compile the message
	for _, pValue := range pValues {
		message = append(message, fmt.Sprintf("<@%s|%s> has a net worth of %s turnips.", pValue.UserID, pValue.Username, Comma(pValue.Value)))
	}

	response := strings.Join(message, "\\n") // Double escape the newline because Slack incoming webhooks are obsessive with JSON formatting while the /slash-command "endpoints" are now

	return response
}
Пример #2
0
func main() {
	// Construct the dsn used for the database
	dsn := os.Getenv("DATABASE_USERNAME") + ":" + os.Getenv("DATABASE_PASSWORD") + "@tcp(" + os.Getenv("DATABASE_HOST") + ":" + os.Getenv("DATABASE_PORT") + ")/" + os.Getenv("DATABASE_NAME")

	// Construct a new AccessorGroup and connects it to the database
	ag := new(accessors.AccessorGroup)
	ag.ConnectToDB("mysql", dsn)

	// Constructs a new ControllerGroup and gives it the AccessorGroup
	cg := new(controllers.ControllerGroup)
	cg.Accessors = ag

	c := cron.New()
	c.AddFunc("0 0 20 * * 1-5", func() { // Run at 2:00pm MST (which is 21:00 UTC) Monday through Friday
		helpers.Webhook(helpers.ReportLeaders(ag))
	})
	c.Start()

	goji.Get("/health", cg.Health)
	goji.Get("/leaderboard", cg.ReportLeaders)
	goji.Post("/slack", cg.Slack) // The main endpoint that Slack hits
	goji.Post("/play", cg.User)
	goji.Post("/portfolio", cg.Portfolio)
	goji.Get("/check/:symbol", cg.Check)
	goji.Post("/buy/:quantity/:symbol", cg.Buy)
	goji.Post("/sell/:quantity/:symbol", cg.Sell)
	goji.Serve()
}
Пример #3
0
// Portfolio gets the given user's portfolio and returns it in string form
func Portfolio(userID string, ag *accessors.AccessorGroup) string {
	portfolio := ag.GetPortfolio(userID)
	message := []string{}
	worth := portfolio.Turnips

	message = append(message, fmt.Sprintf("You have %s turnips in your wallet.", Comma(portfolio.Turnips)))

	for _, value := range portfolio.Investments {
		if value.Quantity > 0 {
			price := models.CheckStock(value.Ticker).Price
			worth = worth + price*value.Quantity
			message = append(message, fmt.Sprintf("You have %s share(s) of %s worth %s turnips total.", Comma(value.Quantity), value.Ticker, Comma(price*value.Quantity)))
		}
	}

	message = append(message, fmt.Sprintf("Your net worth is %s turnips.", Comma(worth)))

	response := strings.Join(message, "\n")

	return response
}
Пример #4
0
// Buy buys a given number of stocks for the user
func Buy(userID string, quantity int, symbol string, ag *accessors.AccessorGroup) string {
	if !MarketOpen() {
		return fmt.Sprintf("The Stalk Market is currently closed.")
	}

	stock := models.CheckStock(symbol)
	user := ag.GetUser(userID)

	if stock.Name == "N/A" || stock.Price == 0 {
		return fmt.Sprintf("%s does not appear to be a valid stock...\n", strings.ToUpper(symbol)) // Return the price through the API endpoint
	}

	// Make sure they have enough turnips to buy
	if user.Turnips < stock.Price*quantity {
		return fmt.Sprintf("%s shares of %s costs %s turnips and you have %s turnips.\n", Comma(quantity), strings.ToUpper(symbol), Comma(stock.Price*quantity), Comma(user.Turnips)) // Return information about a user's portfolio
	}

	ag.SubtractTurnips(userID, stock.Price*quantity)

	if quantity == 0 {
		quantity = 1
	}

	ag.AddShares(userID, symbol, quantity)

	Webhook(fmt.Sprintf("<@%s|%s> purchased %s share(s) of %s for %s turnips.", user.UserID, user.Username, Comma(quantity), strings.ToUpper(symbol), Comma(stock.Price*quantity)))

	return fmt.Sprintf("%s turnips were spent to add %s share(s) of %s to your portfolio.\n", Comma(stock.Price*quantity), Comma(quantity), strings.ToUpper(symbol)) // Return information about a user's portfolio
}
Пример #5
0
// MakeUser creates the given user and puts them into the database
func MakeUser(userID string, username string, ag *accessors.AccessorGroup) string {
	user := ag.GetUser(userID)
	if len(user.Username) > 0 {
		return fmt.Sprintf("Your account already exists. You have %s turnips.\n", Comma(user.Turnips))
	}

	ag.MakeUser(userID, username)
	user = ag.GetUser(userID)

	return fmt.Sprintf("Your account has been created and supplied with %s turnips. Welcome to the Stalk Exchange!\n", Comma(user.Turnips))
}
Пример #6
0
// Sell sells a given number of the user's holdings in the symbol
func Sell(userID string, quantity int, symbol string, ag *accessors.AccessorGroup) string {
	if !MarketOpen() {
		return fmt.Sprintf("The Stalk Market is currently closed.")
	}

	stock := models.CheckStock(symbol)
	user := ag.GetUser(userID)

	if stock.Name == "N/A" || stock.Price == 0 {
		return fmt.Sprintf("%s does not appear to be a valid stock...\n", strings.ToUpper(symbol)) // Return the price through the API endpoint
	}

	holdings := ag.GetShare(userID, symbol)
	if holdings >= quantity { // If we successfully sell
		ag.SubtractShares(userID, symbol, quantity)
		ag.AddTurnips(userID, stock.Price*quantity) // Add turnips to our wallet
	} else { // Else return a human-readable error
		return fmt.Sprintf("You do not have enough shares of %s to sell %s. You have %s shares.\n", strings.ToUpper(symbol), Comma(quantity), Comma(holdings)) // Return information about a user's portfolio
	}

	Webhook(fmt.Sprintf("<@%s|%s> sold %s share(s) of %s for %s turnips.", user.UserID, user.Username, Comma(quantity), strings.ToUpper(symbol), Comma(stock.Price*quantity)))

	return fmt.Sprintf("%s share(s) of %s have been sold for a total of %s turnips.\n", Comma(quantity), strings.ToUpper(symbol), Comma(quantity*stock.Price)) // Return information about a user's portfolio
}