Commit 3af959e0 authored by Andrei Mihu's avatar Andrei Mihu
Browse files

Ensure tournament reset callbacks carry the correct ID.

parent 7c043759
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -15,6 +15,7 @@ The format is based on [keep a changelog](http://keepachangelog.com) and this pr

### Fixed
- Correctly calculate 'can enter' field for newly created tournaments.
- Ensure tournament reset callbacks carry the correct ID.

## [2.9.1] - 2020-01-14
### Changed
+1 −0
Original line number Diff line number Diff line
@@ -564,6 +564,7 @@ func calculateTournamentDeadlines(startTime, endTime, duration int64, resetSched

		return startActiveUnix, endActiveUnix, expiryUnix
	}

	endActiveUnix := int64(0)
	if startTime <= t.Unix() {
		endActiveUnix = startTime + duration
+23 −38
Original line number Diff line number Diff line
@@ -398,83 +398,68 @@ func (l *LocalLeaderboardCache) CreateTournament(ctx context.Context, id string,
		return nil, fmt.Errorf("cannot create tournament as leaderboard is already in use")
	}

	params := make([]interface{}, 0)
	paramsIndex := make(map[string]string)

	params = append(params, id)
	paramsIndex["id"] = strconv.Itoa(len(params))

	params = append(params, true)
	paramsIndex["authoritative"] = strconv.Itoa(len(params))

	params = append(params, sortOrder)
	paramsIndex["sort_order"] = strconv.Itoa(len(params))

	params = append(params, operator)
	paramsIndex["operator"] = strconv.Itoa(len(params))

	params = append(params, duration)
	paramsIndex["duration"] = strconv.Itoa(len(params))
	params := []interface{}{id, true, sortOrder, operator, duration}
	columns := "id, authoritative, sort_order, operator, duration"
	values := "$1, $2, $3, $4, $5"

	if resetSchedule != "" {
		params = append(params, resetSchedule)
		paramsIndex["reset_schedule"] = strconv.Itoa(len(params))
		columns += ", reset_schedule"
		values += ", $" + strconv.Itoa(len(params))
	}

	if metadata != "" {
		params = append(params, metadata)
		paramsIndex["metadata"] = strconv.Itoa(len(params))
		columns += ", metadata"
		values += ", $" + strconv.Itoa(len(params))
	}

	if category >= 0 {
		params = append(params, category)
		paramsIndex["category"] = strconv.Itoa(len(params))
		columns += ", category"
		values += ", $" + strconv.Itoa(len(params))
	}

	if description != "" {
		params = append(params, description)
		paramsIndex["description"] = strconv.Itoa(len(params))
		columns += ", description"
		values += ", $" + strconv.Itoa(len(params))
	}

	if endTime > 0 {
		params = append(params, time.Unix(int64(endTime), 0).UTC())
		paramsIndex["end_time"] = strconv.Itoa(len(params))
		columns += ", end_time"
		values += ", $" + strconv.Itoa(len(params))
	}

	if joinRequired {
		params = append(params, joinRequired)
		paramsIndex["join_required"] = strconv.Itoa(len(params))
		columns += ", join_required"
		values += ", $" + strconv.Itoa(len(params))
	}

	if maxSize > 0 {
		params = append(params, maxSize)
		paramsIndex["max_size"] = strconv.Itoa(len(params))
		columns += ", max_size"
		values += ", $" + strconv.Itoa(len(params))
	}

	if maxNumScore > 0 {
		params = append(params, maxNumScore)
		paramsIndex["max_num_score"] = strconv.Itoa(len(params))
		columns += ", max_num_score"
		values += ", $" + strconv.Itoa(len(params))
	}

	if title != "" {
		params = append(params, title)
		paramsIndex["title"] = strconv.Itoa(len(params))
		columns += ", title"
		values += ", $" + strconv.Itoa(len(params))
	}

	if startTime > 0 {
		params = append(params, time.Unix(int64(startTime), 0).UTC())
		paramsIndex["start_time"] = strconv.Itoa(len(params))
	}

	columns := ""
	values := ""
	for k, v := range paramsIndex {
		if columns != "" {
			columns += ", "
			values += ", "
		}
		columns += k
		values += "$" + v
		columns += ", start_time"
		values += ", $" + strconv.Itoa(len(params))
	}

	query := "INSERT INTO leaderboard (" + columns + ") VALUES (" + values + ") RETURNING metadata, max_size, max_num_score, create_time, start_time, end_time"
+8 −6
Original line number Diff line number Diff line
@@ -307,8 +307,9 @@ func (ls *LocalLeaderboardScheduler) queueEndActiveElapse(t time.Time, ids []str
	go func() {
		// Process the current set of tournament ends.
		for _, id := range ids {
			currentId := id
			// Will block if the queue is full.
			ls.queue <- &LeaderboardSchedulerCallback{id: id, ts: ts}
			ls.queue <- &LeaderboardSchedulerCallback{id: currentId, ts: ts}
		}
	}()
}
@@ -341,6 +342,7 @@ func (ls *LocalLeaderboardScheduler) queueExpiryElapse(t time.Time, ids []string
		// Queue the current set of leaderboard and tournament resets.
		// Executes inside a goroutine to ensure further invocation timings are not skewed.
		for _, id := range ids {
			currentId := id
			leaderboard := ls.cache.Get(id)
			if leaderboard == nil {
				// Cached entry was deleted before it reached the scheduler here.
@@ -352,7 +354,7 @@ func (ls *LocalLeaderboardScheduler) queueExpiryElapse(t time.Time, ids []string
				continue
			}
			// Will block if queue is full.
			ls.queue <- &LeaderboardSchedulerCallback{leaderboard: leaderboard, ts: ts, t: tMinusOne}
			ls.queue <- &LeaderboardSchedulerCallback{id: currentId, leaderboard: leaderboard, ts: ts, t: tMinusOne}
		}
	}()
}
+2 −1
Original line number Diff line number Diff line
@@ -4751,7 +4751,7 @@ func (n *RuntimeLuaNakamaModule) tournamentList(l *lua.LState) int {

	tournaments := l.CreateTable(len(list.Tournaments), 0)
	for i, t := range list.Tournaments {
		tt := l.CreateTable(0, 16)
		tt := l.CreateTable(0, 17)

		tt.RawSetString("id", lua.LString(t.Id))
		tt.RawSetString("title", lua.LString(t.Title))
@@ -4766,6 +4766,7 @@ func (n *RuntimeLuaNakamaModule) tournamentList(l *lua.LState) int {
		tt.RawSetString("max_size", lua.LNumber(t.MaxSize))
		tt.RawSetString("max_num_score", lua.LNumber(t.MaxNumScore))
		tt.RawSetString("duration", lua.LNumber(t.Duration))
		tt.RawSetString("start_active", lua.LNumber(t.StartActive))
		tt.RawSetString("end_active", lua.LNumber(t.EndActive))
		tt.RawSetString("can_enter", lua.LBool(t.CanEnter))
		tt.RawSetString("next_reset", lua.LNumber(t.NextReset))