<commit_before>package main

import (
	"fmt"
	"log"
	"net/http"
	"strconv"

	"github.com/gorilla/mux"
	"github.com/gorilla/schema"
)

type event struct {
	Type string
	Data interface{}
}

type moveData struct {
	Row    int
	Col    int
	Player int
	Turn   int
}

func newGame(w http.ResponseWriter, r *http.Request) {
	numOfPlayer := len(b.subscribers)
	if numOfPlayer > 1 {
		fmt.Fprint(w, 0)
	} else if numOfPlayer == 0 {
		fmt.Fprint(w, `{"gameStatus": "waiting", "playerNum": 1}`)
	} else {
		fmt.Fprint(w, `{"gameStatus": "started", "playerNum": 2}`)
		var eventData = event{
			"game",
			"started",
		}
		publishEvent(eventData)
		setupGame()
	}
}

func move(w http.ResponseWriter, r *http.Request) {
	var move moveData
	vars := mux.Vars(r)
	player, err := strconv.Atoi(vars["player"])
	if err != nil {
		log.Fatal(err)
	}
	if player > 0 {
		move.Player = player
		err = r.ParseForm()
		if err != nil {
			log.Fatal(err)
		}

		decoder := schema.NewDecoder()
		decoder.Decode(&move, r.PostForm)

		if !movePiece(move) {
			fmt.Fprintf(w, "Player %v: Invalid Move", move.Player)
		} else {

			move.Turn = theGame.turn
			var eventData = event{
				"move",
				move,
			}
			publishEvent(eventData)

			fmt.Fprintf(w, "Player %v: Move Initiated", move.Player)
		}

	}
}

func events(w http.ResponseWriter, r *http.Request) {
	f := w.(http.Flusher)
	ch := Subscribe()
	defer Unsubscribe(ch)

	w.Header().Set("Content-Type", "text/event-stream")
	w.Header().Set("Cache-Control", "no-cache")
	w.Header().Set("Connection", "keep-alive")

	fmt.Fprintln(w, "Connected To EventHandler!")
	f.Flush()

	cn := w.(http.CloseNotifier)

	for {
		select {
		case m := <-ch:
			msg := fmt.Sprintf("data: %s\n\n", m)<commit_msg>Validating move via player turn<commit_after>package main

import (
	"fmt"
	"log"
	"net/http"
	"strconv"

	"github.com/gorilla/mux"
	"github.com/gorilla/schema"
)

type event struct {
	Type string
	Data interface{}
}

type moveData struct {
	Row    int
	Col    int
	Player int
	Turn   int
}

func newGame(w http.ResponseWriter, r *http.Request) {
	numOfPlayer := len(b.subscribers)
	if numOfPlayer > 1 {
		fmt.Fprint(w, 0)
	} else if numOfPlayer == 0 {
		fmt.Fprint(w, `{"gameStatus": "waiting", "playerNum": 1}`)
	} else {
		fmt.Fprint(w, `{"gameStatus": "started", "playerNum": 2}`)
		var eventData = event{
			"game",
			"started",
		}
		publishEvent(eventData)
		setupGame()
	}
}

func move(w http.ResponseWriter, r *http.Request) {
	var move moveData
	vars := mux.Vars(r)
	player, err := strconv.Atoi(vars["player"])
	if err != nil {
		log.Fatal(err)
	}
	if player > 0 && player == theGame.turn {
		move.Player = player
		err = r.ParseForm()
		if err != nil {
			log.Fatal(err)
		}

		decoder := schema.NewDecoder()
		decoder.Decode(&move, r.PostForm)

		if !movePiece(move) {
			fmt.Fprintf(w, "Player %v: Invalid Move", move.Player)
		} else {

			move.Turn = theGame.turn
			var eventData = event{
				"move",
				move,
			}
			publishEvent(eventData)

			fmt.Fprintf(w, "Player %v: Move Initiated", move.Player)
		}

	} else {
		fmt.Fprintln(w, "Invalid Player or Player out of turn")
	}
}

func events(w http.ResponseWriter, r *http.Request) {
	f := w.(http.Flusher)
	ch := Subscribe()
	defer Unsubscribe(ch)

	w.Header().Set("Content-Type", "text/event-stream")
	w.Header().Set("Cache-Control", "no-cache")
	w.Header().Set("Connection", "keep-alive")

	fmt.Fprintln(w, "Connected To EventHandler!")
	f.Flush()

	cn := w.(http.CloseNotifier)

	for {
		select {
		case m := <-ch:
			msg := fmt.Sprintf("data: %s\n\n", m)
{'fraction_non_alphanumeric': 0.16835871404399322, 'fraction_numerical': 0.00338409475465313, 'mean_word_length': 3.450439146800502, 'pattern_counts': {'":': 8, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2386976', 'n_tokens_mistral': 1513, 'n_tokens_neox': 1378, 'n_words': 312}