diff --git a/docs/guides/best-practices/game-score-comments.mdx b/docs/guides/best-practices/game-score-comments.mdx new file mode 100644 index 000000000..a3ec6ff9d --- /dev/null +++ b/docs/guides/best-practices/game-score-comments.mdx @@ -0,0 +1,128 @@ +import GameScoreCommentDemo from "@site/src/components/GameScoreCommentDemo"; + +# Commenting Game Scores + +Players may want to share game scores, but repeated score-only comments can +crowd the conversation. Keep generic score updates in one pinned thread and make +sharing an explicit player choice. Submitting on the player's behalf also lets +them report or delete their comment. + +## Score comment examples + +Compare a score-only reply, a score with optional player commentary, and a +rejected top-level comment from the app account. + + + +In the **Optional player comment** tab, leave the field blank or add a message, +then select **Post comment** to preview where the score appears. + +## What gets rejected + +A game score-comment flow will be rejected when it: + +- Posts a score automatically when a game starts, ends, or reaches a milestone. +- Posts the score from the app account instead of the logged-in player. +- Posts a score-only comment at the top level instead of replying to one + stickied comment. +- Combines commenting with another action, such as **Play again**, **Continue**, + or **Subscribe**. +- Requires a comment to keep playing or unlock a feature. + +## What is acceptable + +For a score-only comment: + +- Show a separate **Comment my score** or **Post comment** action. +- Submit the comment only after the player selects that action. +- Submit it on behalf of the player with runAs: 'USER'. +- Reply to one stickied comment on the game post. + +If the player adds their own message, the comment may be posted at the top +level. If the optional field is blank, the comment is score-only and must be a +reply to the pinned prompt. In either case, the player must choose to post it +and it must be submitted on their behalf. + +## How to fix it + +Create one prompt comment per game post, then use it as the parent for score-only +replies. + +#### Don’t + +Post a score automatically as the app account when the game ends: + +~~~ts +import { reddit } from '@devvit/web/server'; + +async function onGameEnded(postId: string, score: number) { + await reddit.submitComment({ + id: postId, + text: 'Score: ' + score, + runAs: 'APP', + }); +} +~~~ + +This runs without the player's choice and creates a top-level comment from the +app account. + +#### Do + +Create and pin one prompt comment on the game post: + +~~~ts +import { reddit } from '@devvit/web/server'; + +const comment = await reddit.submitComment({ + id: postId, + text: 'Reply here with your score after you finish the game.', + runAs: 'APP', +}); + +await comment.distinguish(true); // distinguish + sticky to the top of the thread +~~~ + +Use the returned comment.id as the parent ID for score-only replies. +Call the following handler only after the player selects **Post comment**. When +the player entered a message, post at the top level; when the field is blank, +reply to the pinned prompt: + +~~~ts +import { reddit } from '@devvit/web/server'; + +async function onPostScoreClick( + postId: string, + pinnedCommentId: string, + score: number, + customMessage: string, +) { + const message = customMessage.trim(); + const text = message + ? 'Score: ' + score + '\n\n' + message + : 'Score: ' + score; + + await reddit.submitComment({ + id: message ? postId : pinnedCommentId, + text, + runAs: 'USER', + }); +} +~~~ + +Enable the SUBMIT_COMMENT permission. See the [User Actions +requirements](../../capabilities/server/userActions#requirements) and [game +score guidance](../../capabilities/server/userActions#adding-user-actions-to-games). +For API details, see [submitComment](../../api/redditapi/RedditClient/classes/RedditClient.md#submitcomment) +and [distinguish](../../api/redditapi/models/classes/Comment.md#distinguish). + +## Review checklist + +Before submitting, test both outcomes in a real post: + +- Submit with the optional field blank; the score should appear under the pinned + prompt as a reply. +- Add a custom message and submit; the comment should appear at the top level + under the player's username. +- Confirm the player can finish without commenting and the comment action is + separate from other game actions. diff --git a/sidebars.ts b/sidebars.ts index 77fa6a58f..eac1d8f6a 100644 --- a/sidebars.ts +++ b/sidebars.ts @@ -314,6 +314,7 @@ const sidebars: SidebarsConfig = { items: [ "guides/best-practices/community_games", "guides/best-practices/scroll-traps", + "guides/best-practices/game-score-comments", "guides/best-practices/mod_resources", "capabilities/server/text_fallback", ], diff --git a/src/components/GameScoreCommentDemo/index.tsx b/src/components/GameScoreCommentDemo/index.tsx new file mode 100644 index 000000000..57646ffef --- /dev/null +++ b/src/components/GameScoreCommentDemo/index.tsx @@ -0,0 +1,270 @@ +import React, { useState } from "react"; + +import styles from "./styles.module.css"; + +type Example = "incorrect" | "recommended" | "optional"; + +const examples: Array<{ id: Example; label: string }> = [ + { id: "incorrect", label: "Top-Level App Comment" }, + { id: "recommended", label: "Pinned Comment Reply" }, + { id: "optional", label: "Optional Player Comment" }, +]; + +export default function GameScoreCommentDemo(): React.ReactElement { + const [activeExample, setActiveExample] = useState("incorrect"); + const [optionalMessage, setOptionalMessage] = useState(""); + const [postedMessage, setPostedMessage] = useState(null); + const isIncorrect = activeExample === "incorrect"; + const isRecommended = activeExample === "recommended"; + const isOptional = activeExample === "optional"; + const showScoreReply = + isRecommended || (isOptional && postedMessage === ""); + const showCustomTopLevel = + isOptional && postedMessage !== null && postedMessage.length > 0; + + const routeHint = + postedMessage === null + ? optionalMessage.trim() + ? "With your message, the score can appear as a top-level comment." + : "Leave this blank to reply to the pinned score prompt." + : postedMessage + ? "Your score and message appear as a top-level comment." + : "Your score appears as a reply under the pinned prompt."; + + function handlePostComment(event: React.FormEvent) { + event.preventDefault(); + setPostedMessage(optionalMessage.trim()); + } + + function handleMessageChange(value: string) { + setOptionalMessage(value); + setPostedMessage(null); + } + + return ( +
+
+ {examples.map((example) => ( + + ))} +
+ +
+

+ {isIncorrect + ? "The app account posts a generic score as a top-level comment." + : isOptional + ? "Recommended: add a message to post at the top level, or leave it blank to reply under the pinned prompt." + : "Recommended: the player chooses to reply to the pinned prompt."} +

+ + {isOptional ? ( + + ) : null} + +
+ {!isOptional ? ( + + ) : null} +
+ Comments +
+ + {isIncorrect ? ( +
+ +

+ u/puzzle_player: Score 7/10 · Solved in 22 seconds +

+ +
+ ) : ( + <> + + {showCustomTopLevel ? ( +
+ +

+ Score 7/10 · Solved in 22 seconds +
+ {postedMessage} +

+ +
+ ) : null} + + )} +
+
+
+ ); +} + +function ScoreApp({ + message, + onMessageChange, + onPostComment, + routeHint, +}: { + message: string; + onMessageChange: (value: string) => void; + onPostComment: (event: React.FormEvent) => void; + routeHint: string; +}) { + return ( +
+ +
+
+

Comment your score

+

+ Your score: 7/10 · Solved in 22 seconds +

+
+ +