Class BasicGameServer

This is a basic game server that can be used to create a simple game. Many of the methods are overridable so you can customize the behavior of the game server. Look at the specific method documentation for more information.

Constructors

Accessors

  • get postId(): string
  • This is a helper method to get the current post id.

    Returns string

  • get reddit(): RedditAPIClient
  • This is a helper method to get the Reddit API client. You can use this to interact with Reddit.

    Returns RedditAPIClient

  • get redis(): RedisClient
  • This is a helper method to get the Redis client. All of your game state should be stored in Redis.

    Returns RedisClient

  • get userId(): null | string
  • This is a helper method to get the current user id. This will be null if the user is not logged in, or if this is a TimerEvent handler.

    Returns null | string

Methods

  • You can use this method to broadcast a message to all of your players.

    Parameters

    • channel: string
    • msg: JSONValue

    Returns Promise<any>

  • This must be called to build the game server. This will add the game server to the Devvit instance.

    Returns typeof Devvit

    The Devvit instance with the game server added.

  • Cancels a scheduled timer event

    Parameters

    Returns Promise<void>

  • A player has joined the game. By default, this will

    1. broadcast a notification to the postId channel, and
    2. subscribe the player to the postId channel.

    Returns Promise<any>

  • This method is called when a new post is created. You can use this to initialize the game state.

    Parameters

    • post: PostInfo

      The post that was created.

    Returns Promise<any>

  • This method is called when a message is received from the broadcast channel. By default, this will send the message to the webview. You can override this method to customize the behavior.

    Parameters

    Returns Promise<any>

  • This gets called whenever a timer event is triggered. You can use this to update the game state, broadcast changes, etc. By default, this method broadcasts the timer event to the postId channel.

    Parameters

    Returns Promise<void>

  • This method is called when a message is received from the webview. You can use this to update the game state. By default, this method will broadcast the message to the postId channel. You can override this method to customize the behavior.

    Parameters

    • msg: JSONValue

      The message that was sent from the webview.

    Returns Promise<any>

  • Assuming you're in an individual user's context, you can use this method to send a message to the webview. If you're in a more global context, like a timer event, there won't be a webview to send a message to, so this will do nothing.

    Parameters

    • msg: JSONValue

      The message to send to the webview. This should be a JSONable object.

    Returns Promise<any>

  • Schedules a recurring timer event on the server. You should prefer this to naive setInterval, if you want a long-running interval. This method will handle server restarts and other issues that could cause a naive setInterval to drift or stop.

    Parameters

    • name: string

      Unique identifier for the timer, used for cancellation

    • millis: number

      Interval in milliseconds between timer events

    • Optionaldata: JSONValue

      Optional data to pass to the timer event

    Returns Promise<TimerEvent>

    A TimerEvent object representing the scheduled timer

    Error if no postId is present in the context

  • Schedules a one-time timer event. You should prefer this to naive setTimeout, if you want a long-running timer. This method will handle server restarts and other issues that could cause a naive setTimeout to drift or stop.

    Parameters

    • name: string

      Unique identifier for the timer, used for cancellation

    • millis: number

      Delay in milliseconds before the timer fires

    • Optionaldata: JSONValue

      Optional data to pass to the timer event

    Returns Promise<TimerEvent>

    A TimerEvent object representing the scheduled timer

    Error if no postId is present in the context

  • This subscribes the current player to a channel.

    Parameters

    • channel: string

    Returns Promise<any>

  • This unsubscribes the current player from a channel.

    Parameters

    • channel: string

    Returns Promise<any>