{
  "name": "Snakes & Ladders Trivia Generator",
  "nodes": [
    {
      "parameters": {
        "path": "trivia-snakes-generator",
        "responseMode": "responseNode",
        "options": {}
      },
      "id": "webhook-trigger",
      "name": "Webhook Trigger",
      "type": "n8n-nodes-base.webhook",
      "typeVersion": 1.1,
      "position": [
        200,
        300
      ]
    },
    {
      "parameters": {
        "method": "POST",
        "url": "https://api.z.ai/v1/chat/completions",
        "authentication": "genericCredentialType",
        "genericAuthType": "httpHeaderAuth",
        "sendHeaders": true,
        "headerParameters": {
          "parameters": [
            {
              "name": "Authorization",
              "value": "Bearer {{$credentials.apiKey}}"
            },
            {
              "name": "Content-Type",
              "value": "application/json"
            }
          ]
        },
        "sendBody": true,
        "specifyBody": "json",
        "jsonBody": "={\n  \"model\": \"zai-pro\",\n  \"messages\": [\n    {\n      \"role\": \"system\",\n      \"content\": \"You are an expert trivia creator specializing in questions suitable for Snakes and Ladders board game. Generate fun, engaging trivia that works well with dice-rolling mechanics. Include some board game-themed questions.\"\n    },\n    {\n      \"role\": \"user\",\n      \"content\": \"Generate {{ $json.numQuestions }} trivia questions for a Snakes and Ladders game about {{ $json.topic || 'general knowledge' }} at {{ $json.difficulty }} difficulty level.\\n\\nRequirements:\\n1. Mix of question types: multiple-choice, true-false, text-input\\n2. Include some fun/random questions that are quick to answer\\n3. Include some board game themed questions\\n4. Questions should be solvable in 30 seconds or less\\n5. Include interesting explanations\\n6. Balance of easy, medium, and hard questions suitable for all ages\\n7. Include some lucky/bonus questions themed around snakes and ladders\\n\\nReturn in this exact JSON format:\\n{\\n  \\\"topic\\\": \\\"{{ $json.topic || 'General' }}\\\",\\n  \\\"difficulty\\\": \\\"{{ $json.difficulty }}\\\",\\n  \\\"questions\\\": [\\n    {\\n      \\\"id\\\": \\\"1\\\",\\n      \\\"type\\\": \\\"multiple-choice\\\",\\n      \\\"question\\\": \\\"In a standard game of Snakes and Ladders, what happens when you land on a snake?\\\",\\n      \\\"correctAnswer\\\": \\\"a\\\",\\n      \\\"options\\\": [\\n        {\\\"id\\\": \\\"a\\\", \\\"text\\\": \\\"You slide down to a lower square\\\"},\\n        {\\\"id\\\": \\\"b\\\", \\\"text\\\": \\\"You climb up to a higher square\\\"},\\n        {\\\"id\\\": \\\"c\\\", \\\"text\\\": \\\"You miss your next turn\\\"},\\n        {\\\"id\\\": \\\"d\\\", \\\"text\\\": \\\"You get an extra turn\\\"}\\n      ],\\n      \\\"explanation\\\": \\\"Landing on a snake sends you back down to a lower numbered square, making it a setback in the game.\\\",\\n      \\\"board_event\\\": \\\"snake\\\"\\n    }\\n  ],\\n  \\\"total_questions\\\": {{ $json.numQuestions }}\\n}\"\n    }\n  ],\n  \"temperature\": 0.7,\n  \"max_tokens\": 4000\n}"
      },
      "id": "z-ai-llm",
      "name": "Z.AI LLM - Snakes & Ladders Questions",
      "type": "n8n-nodes-base.httpRequest",
      "typeVersion": 4.2,
      "position": [
        500,
        300
      ]
    },
    {
      "parameters": {
        "jsCode": "const response = items[0].json;\nconst input = items[0].json;\n\n// Parse AI response\nlet parsedData;\ntry {\n  const content = response.choices[0].message.content;\n  const jsonMatch = content.match(/\\{[\\s\\S]*\\}/);\n  if (jsonMatch) {\n    parsedData = JSON.parse(jsonMatch[0]);\n  } else {\n    parsedData = JSON.parse(content);\n  }\n} catch (error) {\n  return [{\n    json: {\n      error: 'Failed to parse AI response',\n      details: error.message,\n      rawResponse: response\n    }\n  }];\n}\n\n// Snakes and Ladders specific categories\nconst categories = [\n  {\n    name: 'Board Game Trivia',\n    description: 'Questions about Snakes and Ladders and classic board games',\n    subcategories: ['Game Rules', 'History', 'Variations']\n  },\n  {\n    name: 'Quick Thinking',\n    description: 'Fast-paced trivia for exciting gameplay',\n    subcategories: ['Fun Facts', 'General Knowledge', 'Quick Questions']\n  },\n  {\n    name: input.topic || 'General Knowledge',\n    description: `Trivia about ${input.topic || 'general topics'}`,\n    subcategories: ['Key Facts', 'Interesting Tidbits', 'Surprising Facts']\n  }\n];\n\n// Format questions with board game enhancements\nconst formattedQuestions = parsedData.questions.map((q, index) => ({\n  id: q.id || (index + 1).toString(),\n  type: q.type,\n  question: q.question,\n  correctAnswer: q.correctAnswer,\n  explanation: q.explanation,\n  options: q.options,\n  acceptableAnswers: q.acceptableAnswers,\n  // Board game specific fields\n  board_event: q.board_event || null, // 'snake', 'ladder', 'bonus'\n  difficulty_level: q.difficulty_level || input.difficulty,\n  time_limit: q.type === 'text-input' ? 45 : 30, // Different time limits per type\n  points: calculatePoints(q.difficulty_level || input.difficulty)\n}));\n\n// Calculate points based on difficulty\nfunction calculatePoints(difficulty) {\n  const pointsMap = {\n    'easy': 100,\n    'medium': 150,\n    'hard': 200\n  };\n  return pointsMap[difficulty] || 150;\n}\n\n// Distribute questions across board positions\nconst boardDistribution = distributeQuestionsOnBoard(formattedQuestions);\n\nreturn [{\n  json: {\n    topic: parsedData.topic || 'Snakes and Ladders Trivia',\n    difficulty: input.difficulty || 'medium',\n    categories: categories,\n    questions: formattedQuestions,\n    total_questions: formattedQuestions.length,\n    difficulty_distribution: {\n      'easy': formattedQuestions.filter(q => q.difficulty_level === 'easy').length,\n      'medium': formattedQuestions.filter(q => q.difficulty_level === 'medium').length,\n      'hard': formattedQuestions.filter(q => q.difficulty_level === 'hard').length\n    },\n    // Snakes and Ladders specific\n    game_type: 'snakes-and-ladders',\n    board_distribution: boardDistribution,\n    has_board_events: formattedQuestions.some(q => q.board_event),\n    max_time_limit: 45\n  }\n}];\n\n// Distribute questions across board (100 squares)\nfunction distributeQuestionsOnBoard(questions) {\n  const distribution = {};\n  const squares = [1, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100];\n  \n  questions.forEach((q, i) => {\n    const square = squares[i % squares.length];\n    distribution[square] = {\n      question_id: q.id,\n      difficulty: q.difficulty_level,\n      points: q.points,\n      type: q.board_event || 'normal'\n    };\n  });\n  \n  return distribution;\n}"
      },
      "id": "format-response",
      "name": "Format Snakes & Ladders Response",
      "type": "n8n-nodes-base.code",
      "typeVersion": 2,
      "position": [
        800,
        300
      ]
    },
    {
      "parameters": {
        "respondWith": "json",
        "responseBody": "={{ $json }}"
      },
      "id": "respond-to-webhook",
      "name": "Respond to Webhook",
      "type": "n8n-nodes-base.respondToWebhook",
      "typeVersion": 1.1,
      "position": [
        1100,
        300
      ]
    }
  ],
  "connections": {
    "Webhook Trigger": {
      "main": [
        [
          {
            "node": "Z.AI LLM - Snakes and Ladders Questions",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Z.AI LLM - Snakes and Ladders Questions": {
      "main": [
        [
          {
            "node": "Format Snakes and Ladders Response",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Format Snakes and Ladders Response": {
      "main": [
        [
          {
            "node": "Respond to Webhook",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  },
  "settings": {
    "executionOrder": "v1"
  },
  "staticData": null,
  "tags": [],
  "triggerCount": 1,
  "updatedAt": "2024-01-22T12:00:00.000Z",
  "versionId": "1"
}