Partage
  • Partager sur Facebook
  • Partager sur Twitter

Problème avec mon server en golang

    7 novembre 2023 à 8:36:44

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Hangman Web</title>
        <link rel="stylesheet" href="../static/style.css">
        <link rel="shortcut icon" href="../static/LogoPendu.png" type="image/x-icon">
    </head>
    <body>
        <header>
            <div class="banner">
                <a href="home.html">
                    <img src="../static/LogoPendu.png" alt="logo" class="logo">
                </a>
                <a href="./connexion.html">
                    <img src="../static/SignIn.png" class="logo_SignIn" alt="Logo SignIn" id="Logo">
                </a>
                <input type="text" placeholder="Search a game.." class="search">
            </div>
        </header>
        <h1>Hangman Game</h1>
        <section>
            <div class="conteneur">
                <div><a class="box" href="game.html" onclick="startNewGame()">Play</a></div>
            <script>
                function startNewGame() {
                    var xhr = new XMLHttpRequest();
                    xhr.open("GET", "/new-game", true); // Utilisez l'URL de la route correcte pour la fonction NewGame.
                    xhr.send();
                    xhr.onreadystatechange = function() {
                        if (xhr.readyState === 4 && xhr.status === 200) {
                            // La requête a réussi, vous pouvez effectuer des actions supplémentaires si nécessaire.
                        }
                    };
                }
            </script>
                    <div><a class="box" href="Language.html">Languages</a></div>
                    <div><a class="box" href="Difficulty.html">Difficulty</a></div>
                 </div>
        </section>
            <div>
                <main>
                    <section>
                    <h2>Rules:</h2>
                    <p class="rules">The rules are simple. You have to guess a word chosen at random. You'll have a certain number of letters to reveal, depending on the word. From then on, you'll have to guess the other letters that make up the word, revealing them little by little until the word is completely revealed. To do this, your attempts are limited to 10. Good luck!</p>
                    </section>
                </main>
            </div>
        <footer>
            <div class="footer">
                <h4>© 2023 Hangman Web</h4>
                <h5>Who we are?</h5>
                <p>Emmanuel</p>
                <p>Guillaume</p>
                <p>Edgar</p>
            </div>
        </footer>
    </body>
    </html>
        <!DOCTYPE html>
            <html lang="en">
            <head>
                <link rel="stylesheet" href="../static/style.css">
                <meta charset="UTF-8">
                <meta name="viewport" content="width=device-width, initial-scale=1.0">
                <title>Hangman-Web</title>
            </head>
            <body>
                <h1>The Hangman Game</h1>
                <div>
                    <p><strong>You have {{.AttemptsRemaining}} attempts remaining!</strong></p>
                    <p>False letter : {{.FalseLetter}}</p>
                    <p>Word : {{.Display}}</p>
                    <form action="/form" method="post">
                        <input type="text" placeholder="Guess a letter" name="letter"/>
                        <br>
                        <input type="submit" value="Submit"/>
                        <br>
                        <button><a href="home.html">Home</a></button>
                </div>
            </body>
            </html>
    
    // Description: Server web for the hangman game
    package main
    
    import (
    	"fmt"
    	"github.com/GuillaumeAntier/hangman"
    	"html/template"
    	"log"
    	"net/http"
    )
    
    type HangmanGameData struct {
    	AttemptsRemaining int
    	FalseLetter       string
    	Display           string
    }
    
    func NewGame(w http.ResponseWriter, r *http.Request) {
        // Choose a word
        words := []string{"apple", "banana", "cherry", "date", "elderberry"}
        word := hangman.RandomWord(words)
        displayWord := hangman.DisplayWord(word)
        attemptsRemaining := 10
        falseLetter := ""
    
        // Create an instance of HangmanGameData with the masked word
        gameData := HangmanGameData{
            AttemptsRemaining: attemptsRemaining,
            FalseLetter:       falseLetter,
            Display:           displayWord,
        }
    
        // Load the HTML template from the file
        tmpl, err := template.ParseFiles("HTML/game.html")
        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
            return
        }
    
        // Execute the template with the game data
        err = tmpl.Execute(w, gameData)
        if err != nil {
            http.Error(w, err.Error(), http.StatusInternalServerError)
        }
    }
    
    func formHandler(w http.ResponseWriter, r *http.Request) {
    	// Parse the form
    	if err := r.ParseForm(); err != nil {
    		http.Error(w, fmt.Sprintf("ParseForm() err: %v", err), http.StatusInternalServerError)
    		return
    	}
    
    	// Get the letter from the form
    	//letter := r.FormValue("letter")
    
    	// Process the letter here, update the game state (e.g., in a global variable).
    
    	// Redirect to the "/game" (game) page
    	http.Redirect(w, r, "/game.html", http.StatusSeeOther)
    }
    
    func homeHandler(w http.ResponseWriter, r *http.Request) {
    	// Serve the file "home.html" as the "home" page
    	http.ServeFile(w, r, "HTML/home.html")
    }
    
    func gameHandler(w http.ResponseWriter, r *http.Request) {
    	// Serve the file "game.html" as the "game" page
    	http.ServeFile(w, r, "HTML/game.html")
    }
    func langageHandler(w http.ResponseWriter, r *http.Request) {
    	// Serve the file "langage.html" as the "langage" page
    	http.ServeFile(w, r, "HTML/langage.html")
    }
    func difficultyHandler(w http.ResponseWriter, r *http.Request) {
    	// Serve the file "difficulty.html" as the "difficulty" page
    	http.ServeFile(w, r, "HTML/difficulty.html")
    }
    
    func main() {
    	// Server of the HTML files
    	fileServer := http.FileServer(http.Dir("HTML/"))
    	http.Handle("/HTML/", http.StripPrefix("/HTML/", fileServer))
    
    	// Gestionnary of the page "home"
    	http.HandleFunc("/", homeHandler)
    
    	// Gestionnary of the page "game"
    	http.HandleFunc("/game.html", gameHandler)
    
    	// Gestionnary of the page "new-game"
        http.HandleFunc("/new-game", NewGame)
    
    	// Gestionnary of the page "form"
    	http.HandleFunc("/form", formHandler)
    
    	// Gestionnary of the page "langage"
    	http.HandleFunc("/langage.html", langageHandler)
    
    	// Gestionnary of the page "difficulty"
    	http.HandleFunc("/difficulty.html", difficultyHandler)
    
    	// Server of the static files
    	staticDir := http.FileServer(http.Dir("static"))
    	http.Handle("/static/", http.StripPrefix("/static/", staticDir))
    
    	// Launch the server
    	fmt.Printf("Starting server at port 8080\n")
    	if err := http.ListenAndServe(":8080", nil); err != nil {
    		log.Fatal(err)
    	}
    }
    
    Bonjour je souhaite faire un projet hangman avec un server en golang sauf que j'ai un problème au niveau des variables dynamiques que j'ai mise dans mon HTML, quand je lance mon serveur et que je vais dans l'onglet localhost:8080/game.html les valeurs ne se change pas avec les valeurs que j'ai donné. Auriez vous des réponses à mon problème ?


    • Partager sur Facebook
    • Partager sur Twitter
      7 novembre 2023 à 10:38:06

      Je ne connais pas vraiment le golang, mais il me semble que tu perds le mot à trouver (tu le tires au sort dans newgame, mais tu ne le passes à personne, à part sous sa forme "masquée" via hangman.DisplayWord(word)
      • Partager sur Facebook
      • Partager sur Twitter

      Problème avec mon server en golang

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      • Editeur
      • Markdown