Skip to content

Commit

Permalink
Redis increment of weight for clans
Browse files Browse the repository at this point in the history
  • Loading branch information
heynemann committed Dec 14, 2016
1 parent e9b7c3c commit 2c5bdf6
Show file tree
Hide file tree
Showing 7 changed files with 647 additions and 48 deletions.
23 changes: 23 additions & 0 deletions api/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ type App struct {
Background bool
Fast bool
Redsync *redsync.Redsync
Redis redis.Conn
NewRelic newrelic.Application
}

Expand Down Expand Up @@ -169,6 +170,25 @@ func (app *App) OnErrorHandler(err error, stack []byte) {
raven.CaptureError(e, tags)
}

func (app *App) configureRedis() error {
redisURL := app.Config.GetString("redis.url")
l := app.Logger.With(
zap.String("operation", "configureRedis"),
zap.String("redis.url", app.Config.GetString("redis.url")),
)

conn, err := redis.DialURL(redisURL)
if err != nil {
log.E(l, "Failed to connect to redis.", func(cm log.CM) {
cm.Write(zap.Error(err))
})
return err
}

app.Redis = conn
return nil
}

func (app *App) configureRedsync() error {
redisURL := app.Config.GetString("redis.url")
maxIdle := app.Config.GetInt("redis.maxIdle")
Expand Down Expand Up @@ -317,8 +337,11 @@ func (app *App) configureApplication() error {
//Donations routes
a.Post("/games/:gameID/donation-requests/:donationRequestID", CreateDonationHandler(app))

a.Get("/games/:gameID/donation-weight-by-clan", GetDonationWeightByClanHandler(app))

app.configureMongoDB()
app.configureRedsync()
app.configureRedis()

l.Debug("Application configured successfully.")

Expand Down
53 changes: 52 additions & 1 deletion api/donation.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package api
import (
"fmt"
"net/http"
"strconv"
"time"

"github.com/topfreegames/donations/log"
"github.com/topfreegames/donations/models"
Expand Down Expand Up @@ -162,10 +164,11 @@ func CreateDonationHandler(app *App) func(c echo.Context) error {
return err
}

err = donationRequest.Donate(payload.Player, payload.Amount, payload.MaxWeightPerPlayer, app.MongoDb, app.Logger)
err = donationRequest.Donate(payload.Player, payload.Amount, payload.MaxWeightPerPlayer, app.Redis, app.MongoDb, app.Logger)
if err != nil {
return err
}

return nil
})
if err != nil {
Expand All @@ -183,3 +186,51 @@ func CreateDonationHandler(app *App) func(c echo.Context) error {
return c.String(http.StatusOK, "{\"success\":true}")
}
}

func toTime(val string) int64 {
var err error
var res int64
if val != "" {
res, err = strconv.ParseInt(val, 10, 64)
if err != nil {
res = time.Now().UTC().Unix()
}
} else {
res = time.Now().UTC().Unix()
}

return res
}

//GetDonationWeightByClanHandler is the handler responsible for creating donation requests
func GetDonationWeightByClanHandler(app *App) func(c echo.Context) error {
return func(c echo.Context) error {
l := app.Logger.With(
zap.String("source", "GetDonationWeightByClanHandler"),
zap.String("operation", "GetDonationWeightByClan"),
)
c.Set("route", "CreateDonation")
gameID := c.Param("gameID")
clanID := c.QueryParam("clanID")
resetTypeStr := c.QueryParam("type")
var resetType models.ResetType
switch resetTypeStr {
case "daily":
resetType = models.DailyReset
case "weekly":
resetType = models.WeeklyReset
case "monthly":
resetType = models.MonthlyReset
default:
resetType = models.NoReset
}

log.D(l, "Getting clan weight...")
weight, err := models.GetDonationWeightForClan(gameID, clanID, time.Now(), resetType, app.Redis, app.Logger)
if err != nil {
return FailWith(500, err.Error(), c)
}

return c.String(http.StatusOK, fmt.Sprintf("{\"success\":true, \"weight\": %d}", weight))
}
}
2 changes: 1 addition & 1 deletion config/local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ healthcheck:
mongo:
host: localhost
port: 9999
db: donations
db: donations-test

redis:
url: redis://localhost:6379/0
Expand Down

0 comments on commit 2c5bdf6

Please sign in to comment.