Skip to content
Snippets Groups Projects
gamesettings.dart 3.64 KiB
Newer Older
Sunny Singh's avatar
Sunny Singh committed
import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'gamelobbyscreen.dart';

class GameSettingsScreen extends StatefulWidget {
  final String playerName;

  const GameSettingsScreen({super.key, required this.playerName});

  @override
  GameSettingsScreenState createState() => GameSettingsScreenState();
Sunny Singh's avatar
Sunny Singh committed
}

class GameSettingsScreenState extends State<GameSettingsScreen> {
Sunny Singh's avatar
Sunny Singh committed
  final TextEditingController _numQuestionsController = TextEditingController();
  final List<String> _categories = [
    'Compass',
    'GPS sensor',
    'History',
    'Literature',
    'Mathematics',
    'Pop Culture',
    'Science & Nature',
    'Sound',
    'Sound Sensor',
    'Technology'
  ];
  final List<String> _selectedCategories = [];

  Future<void> _createGame() async {
    if (_numQuestionsController.text.isEmpty || _selectedCategories.isEmpty) {
      ScaffoldMessenger.of(context).showSnackBar(
        const SnackBar(content: Text('Please select number of questions and categories')),
      );
      return;
    }

    try {
      final numQuestions = int.parse(_numQuestionsController.text);
      final gameCode = (100000 + DateTime.now().millisecondsSinceEpoch % 900000).toString();

      // Create game document
      await FirebaseFirestore.instance.collection('games').doc(gameCode).set({
        'createdAt': FieldValue.serverTimestamp(),
        'status': 'waiting',
        'host': widget.playerName,
        'numQuestions': numQuestions,
        'categories': _selectedCategories,
      });

      if (!mounted) return;
      Navigator.pushReplacement(
        context,
        MaterialPageRoute(
          builder: (context) => GameLobbyScreen(
            gameCode: gameCode,
            playerName: widget.playerName,
            isHost: true,
          ),
        ),
      );
    } catch (e) {
      if (!mounted) return;
      ScaffoldMessenger.of(context).showSnackBar(
        SnackBar(content: Text('Error creating game: $e')),
      );
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Game Settings'),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            TextField(
              controller: _numQuestionsController,
              decoration: const InputDecoration(
                labelText: 'Number of Questions',
                border: OutlineInputBorder(),
              ),
              keyboardType: TextInputType.number,
            ),
            const SizedBox(height: 16),
            const Text('Select Categories:'),
            Expanded(
              child: ListView.builder(
                itemCount: _categories.length,
                itemBuilder: (context, index) {
                  final category = _categories[index];
                  return CheckboxListTile(
                    title: Text(category),
                    value: _selectedCategories.contains(category),
                    onChanged: (bool? value) {
                      setState(() {
                        if (value == true) {
                          _selectedCategories.add(category);
                        } else {
                          _selectedCategories.remove(category);
                        }
                      });
                    },
                  );
                },
              ),
            ),
            const SizedBox(height: 16),
            ElevatedButton(
              onPressed: _createGame,
              child: const Text('Create Game'),
            ),
          ],
        ),
      ),
    );
  }
}