]> git.cworth.org Git - lmno-server/commitdiff
Empathy: Reject categories with more than 20 items
authorCarl Worth <cworth@cworth.org>
Fri, 12 Jun 2020 00:06:28 +0000 (17:06 -0700)
committerCarl Worth <cworth@cworth.org>
Fri, 12 Jun 2020 00:06:28 +0000 (17:06 -0700)
Just to avoid any smart aleck trying to get every player's browser to
try to render a million text input fields.

empathy.js

index 1da9b8ef07ce8fde9b221fcb65715ef11c00e9d4..25d8f77b975e86280a66c393c2587ed9c5c86941 100644 (file)
@@ -1,6 +1,8 @@
 const express = require("express");
 const Game = require("./game.js");
 
+const MAX_PROMPT_ITEMS = 20;
+
 class Empathy extends Game {
   constructor(id) {
     super(id);
@@ -55,6 +57,12 @@ class Empathy extends Game {
   }
 
   add_prompt(items, prompt_string) {
+    if (items > MAX_PROMPT_ITEMS)
+      return {
+        valid: false,
+        message: `Maximum number of items is ${MAX_PROMPT_ITEMS}`
+      };
+
     const prompt = new Prompt(this.next_prompt_id, items, prompt_string);
     this.next_prompt_id++;
 
@@ -62,7 +70,10 @@ class Empathy extends Game {
 
     this.broadcast_event_object('prompt', prompt);
 
-    return prompt;
+    return {
+      valid: true,
+      id: prompt.id
+    };
   }
 
   /* Returns true if vote toggled, false for player or prompt not found */
@@ -326,9 +337,9 @@ class Prompt {
 router.post('/prompts', (request, response) => {
   const game = request.game;
 
-  prompt = game.add_prompt(request.body.items, request.body.prompt);
+  const result = game.add_prompt(request.body.items, request.body.prompt);
 
-  response.json({ id: prompt.id});
+  response.json(result);
 });
 
 router.post('/vote/:prompt_id([0-9]+)', (request, response) => {