# Error creating tournament with registerTournamentEnd function

**URL:** <https://forum.heroiclabs.com/t/error-creating-tournament-with-registertournamentend-function/3038>\
**Category:** Runtime Framework\
**Tags:** typescript\
**Created:** [September 12, 2022, 3:52pm UTC](https://forum.heroiclabs.com/t/error-creating-tournament-with-registertournamentend-function/3038 "2022-09-12T15:52:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![himmelattack](https://avatars.discourse-cdn.com/v4/letter/h/839c29/32.png) [@himmelattack](https://forum.heroiclabs.com/u/himmelattack)\
**Post date:** [September 12, 2022, 3:52pm UTC](https://forum.heroiclabs.com/t/error-creating-tournament-with-registertournamentend-function/3038/1 "2022-09-12T15:52:43Z")

</div>

I’m been going through the Typescript guide and testing out Nakama, so far so good!

I’ve run into a problem with tournaments, however. They seem to create ok, but I can’t seem register hook to handle when it ends. I get the following error in the log:

```auto
{"level":"info","ts":"2022-09-12T15:38:31.171Z","caller":"server/core_tournament.go:59","msg":"Tournament created","id":"dailyChallenge"}
{"level":"error","ts":"2022-09-12T15:38:31.175Z","caller":"server/runtime_javascript_logger.go:94","msg":"Error creating tournament%!(EXTRA map[string]interface {}=map[value:js registerTournamentEnd function key could not be extracted: not found])"}

```

Here’s a snipped of my server code:

```auto
const distributeTournamentRewards: nkruntime.TournamentEndFunction = function (
  ctx: nkruntime.Context,
  logger: nkruntime.Logger,
  nk: nkruntime.Nakama,
  tournament: nkruntime.Tournament,
  end: number,
  reset: number
) {
  // I commented everything out here...
};

const InitModule: nkruntime.InitModule = function (
  ctx: nkruntime.Context,
  logger: nkruntime.Logger,
  nk: nkruntime.Nakama,
  initializer: nkruntime.Initializer
) {
  // ...

  // Create tournament
  {
    const id = "dailyChallenge";
    const authoritative = false;
    const sortOrder = nkruntime.SortOrder.DESCENDING;
    const operator = nkruntime.Operator.BEST;
    const resetSchedule = "*/10 * * * *"; // Every 10 minutes
    const duration = 1000; // Until reset
    const metadata = {
      weatherConditions: "rain",
    };
    const metadata = {};
    const title = "Daily Challenge";
    const description = "The top score for today's challenge";

    try {
      nk.tournamentCreate(
        id,
        authoritative,
        sortOrder,
        operator,
        duration,
        resetSchedule,
        metadata,
        title,
        description
      );

      initializer.registerTournamentEnd(distributeTournamentRewards);

      logger.info("Tournament created");
    } catch (error) {
      logger.error("Error creating tournament", error);
    }
  }
};

```

1. Versions: Nakama 3.13 on Docker
2. Server Framework Runtime language: TS

Any help would be appreciated. Thanks!

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [September 12, 2022, 5:59pm UTC](https://forum.heroiclabs.com/t/error-creating-tournament-with-registertournamentend-function/3038/2 "2022-09-12T17:59:15Z")

</div>

Hello @himmelattack, I can’t tell by the snippet, is `initializer.registerTournamentEnd` invoked at the top level of your `InitModule` function?

---

<div class="post-metadata">

**Author:** ![himmelattack](https://avatars.discourse-cdn.com/v4/letter/h/839c29/32.png) [@himmelattack](https://forum.heroiclabs.com/u/himmelattack)\
**Post date:** [September 13, 2022, 7:55am UTC](https://forum.heroiclabs.com/t/error-creating-tournament-with-registertournamentend-function/3038/3 "2022-09-13T07:55:29Z")

</div>

Ok, so I eventually resolved out the problem, by adding `duration`, `startTime`, and `endTime` when creating the tournament. The corrected code is below.

I also had to remove the previous tournament with the same ID in order to change the `resetSchedule`. It appears that the schedule is cached otherwise…

```typescript
const distributeTournamentRewards: nkruntime.TournamentEndFunction = function (
  ctx: nkruntime.Context,
  logger: nkruntime.Logger,
  nk: nkruntime.Nakama,
  tournament: nkruntime.Tournament,
  end: number,
  reset: number
) {
  logger.info("In distributeTournamentRewards for tournament", tournament.id);
};

const InitModule: nkruntime.InitModule = function (
  ctx: nkruntime.Context,
  logger: nkruntime.Logger,
  nk: nkruntime.Nakama,
  initializer: nkruntime.Initializer
) {
  logger.info("Starting custom module");

  initializer.registerTournamentEnd(distributeTournamentRewards);

  // Create tournament
  {
    const id = "dailyChallenge";
    const authoritative = false;
    const sortOrder = nkruntime.SortOrder.DESCENDING;
    const operator = nkruntime.Operator.BEST;
    const resetSchedule = "* * * * *"; // Every minute
    const duration = 60; // 60 seconds 
    const metadata = {
      weatherConditions: "rain",
    };
    const title = "Daily Challenge";
    const description = "The top score for today's challenge";
    const category = 1; // For filtering tournaments
    const startTime = 0; // Start now.
    const endTime = 0; // Never end, repeat the tournament each day forever.

    try {
      nk.tournamentCreate(
        id,
        authoritative,
        sortOrder,
        operator,
        duration,
        resetSchedule,
        metadata,
        title,
        description,
        category,
        startTime,
        endTime
      );

      logger.info("Tournament created");
    } catch (error) {
      logger.error("Error creating tournament", error);
    }
  }
};

```

---

<div class="post-metadata">

**Author:** ![himmelattack](https://avatars.discourse-cdn.com/v4/letter/h/839c29/32.png) [@himmelattack](https://forum.heroiclabs.com/u/himmelattack)\
**Post date:** [September 13, 2022, 7:58am UTC](https://forum.heroiclabs.com/t/error-creating-tournament-with-registertournamentend-function/3038/4 "2022-09-13T07:58:03Z")

</div>

Hi @sesposito! I just figured it out, as in my response below. However, I’m still confused about a few things:

- Are `duration`, `startTime`, and `endTime` mandatory when creating a tournament?
- Is there a way to “update” a tournament in order to change its settings? Or is the correct pattern to delete the tournament if it exists before creating it again?

Thanks a lot!

---

<div class="post-metadata">

**Author:** ![sesposito](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.heroiclabs.com/sesposito/32/2594_2.png) [@sesposito](https://forum.heroiclabs.com/u/sesposito)\
**Post date:** [September 13, 2022, 2:11pm UTC](https://forum.heroiclabs.com/t/error-creating-tournament-with-registertournamentend-function/3038/5 "2022-09-13T14:11:22Z")

</div>

- Duration is mandatory and has to be \> 0 - it dictates for how long the tournament is active and accepts scores, starting from Start Time.
- Start Time can be set to 0 (meaning it’ll start effective immediately).
- End Time can be set to 0, meaning the tournament’s end is dictated by `duration` alone. If it is set to a value \> 0 AND a reset schedule is present, then it should be a value **after** at least one cycle from Start Time to the first reset.

There is no way to update a created tournament, to change its configuration it has to be deleted and recreated.
