Skip to content
Snippets Groups Projects
main.go 1.68 KiB
Newer Older
  • Learn to ignore specific revisions
  • Alex Constantin-Gomez's avatar
    Alex Constantin-Gomez committed
    package main
    
    import (
    	"encoding/json"
    	"fmt"
    	"log"
    	"net/http"
    )
    
    /******************** DATA STRUCTS ***********************/
    
    // Player represents a player
    type Player struct {
    
    	ID   int
    	Name string
    
    Alex Constantin-Gomez's avatar
    Alex Constantin-Gomez committed
    }
    
    // Game represents a game object in the database
    type Game struct {
    
    	ID       int
    	Player1  Player
    	Player2  Player
    	Channels [2]chan string
    
    Alex Constantin-Gomez's avatar
    Alex Constantin-Gomez committed
    }
    
    // Move represents a move in the game
    type Move struct {
    	GameID   int    `json:"game_id"`
    
    Alex Constantin-Gomez's avatar
    Alex Constantin-Gomez committed
    	Player   int    `json:"player"`
    
    Alex Constantin-Gomez's avatar
    Alex Constantin-Gomez committed
    	MoveData string `json:"move"`
    	GameOver bool   `json:"game_over"`
    }
    
    var (
    
    Alex Constantin-Gomez's avatar
    Alex Constantin-Gomez committed
    	gameData map[int]Game
    
    Alex Constantin-Gomez's avatar
    Alex Constantin-Gomez committed
    )
    
    /************** MAIN FUNCTION **********************/
    
    func main() {
    
    Alex Constantin-Gomez's avatar
    Alex Constantin-Gomez committed
    	gameData = make(map[int]Game)
    
    Alex Constantin-Gomez's avatar
    Alex Constantin-Gomez committed
    
    	http.HandleFunc("/", hello)
    
    	http.HandleFunc("/move", move)
    	http.HandleFunc("/create", createGame)
    	http.HandleFunc("/join", joinGame)
    
    	err := http.ListenAndServe(":8080", nil)
    	if err != nil {
    		log.Println("could not start server")
    	}
    }
    
    /************************ HANDLERS ************************/
    
    func hello(w http.ResponseWriter, r *http.Request) {
    	fmt.Fprintf(w, "Big Tac Toe")
    }
    
    func move(w http.ResponseWriter, r *http.Request) {
    	jsonData := readJsonFromRequest(r)
    
    	var move Move
    	err := json.Unmarshal(jsonData, &move)
    	if err != nil {
    		log.Println("could not decode json data into Move struct")
    	}
    
    
    	game := gameData[move.GameID]
    	sendChan := game.Channels[move.Player]
    	recChan := game.Channels[1-move.Player]
    	sendChan <- move.MoveData
    
    
    Alex Constantin-Gomez's avatar
    Alex Constantin-Gomez committed
    	if move.GameOver {
    
    		fmt.Fprintf(w, "Game over!")
    		return
    
    	response := <-recChan
    	fmt.Fprintf(w, response)
    
    Alex Constantin-Gomez's avatar
    Alex Constantin-Gomez committed
    }
    
    func createGame(w http.ResponseWriter, r *http.Request) {
    	fmt.Fprintf(w, "hello")
    }
    
    func joinGame(w http.ResponseWriter, r *http.Request) {
    	fmt.Fprintf(w, "hello")
    }