<commit_before>package main

import (
	"encoding/json"
	"fmt"
	"log"
	"sync"
	"time"
)

const (
	initialEnergy      = 100
	initialLife        = 100
	initialTemperature = -53.5
	initialRadiation   = 500
	initialSolarFlare  = false
	maxTemperature     = 35.00
	minTemperature     = -142.00
	maxRadiation       = 1000
	minRadiation       = 0
)

// GameInfo contains information about the state of the game
type GameInfo struct {
	// Running defines whether the game is running or not
	Running      bool      `json:"running"`
	StartedAt    time.Time `json:"startedAt"`
	Reading      Reading   `json:"readings"`
	Teams        []Team    `json:"teams"`
	adminToken   string
	autoReadings bool
	start        chan TokenRequest
	stop         chan TokenRequest
	reset        chan TokenRequest
	join         chan JoinRequest
	kick         chan KickRequest
	readings     chan ReadingsRequest
	exit         chan []byte
	shield       chan ShieldRequest
}

// Team contains information about a team
type Team struct {
	Name   string `json:"name"`
	token  string
	Energy int64 `json:"energy"`
	Life   int64 `json:"life"`
	Shield bool  `json:"shield"`
}

var game = GameInfo{
	Running:      false,
	autoReadings: false,
	start:        make(chan TokenRequest),
	stop:         make(chan TokenRequest),
	reset:        make(chan TokenRequest),
	join:         make(chan JoinRequest),
	kick:         make(chan KickRequest),
	shield:       make(chan ShieldRequest),
	readings:     make(chan ReadingsRequest),
	exit:         make(chan []byte),
	Reading:      Reading{SolarFlare: initialSolarFlare, Temperature: initialTemperature, Radiation: initialRadiation},
	Teams:        []Team{},
}

func (game *GameInfo) run(adminToken string) {
	game.adminToken = adminToken
	var wg sync.WaitGroup
	ticker := time.NewTicker(time.Second * 1)
	for {
		select {
		case req := <-game.start:
			success, message := game.startGame(req.token)
			if success && game.autoReadings {
				go game.getReadings(&wg)
				go game.runEngine(&wg)
			}
			req.Response <- GameResponse{success: success, message: message}
			close(req.Response)
		case req := <-game.stop:
			success, message := game.stopGame(req.token)
			if success && game.autoReadings {
				wg.Wait()
			}
			req.Response <- GameResponse{success: success, message: message}
			close(req.Response)
		case req := <-game.join:
			success, message := game.joinGame(req.name)
			req.Response <- GameResponse{success: success, message: message}
			close(req.Response)
		case req := <-game.kick:
			success, message := game.kickTeam(req.token, req.name)
			req.Response <- GameResponse{success: success, message: message}
			close(req.Response)
		case req := <-game.shield:
			success, message := game.enableShield(req.token, req.enable)
			req.Response <- GameResponse{success: success, message: message}
			close(req.Response)
		case req := <-game.reset:
			success, message := game.resetGame(req.token)
			req.Response <- GameResponse{success: success, message: message}
			close(req.Response)
		case req := <-game.readings:
			success, message := game.updateReadings(req.readings, req.token)
			req.Response <- GameResponse{success: success, message: message}
			close(req.Response)
		case <-ticker.C:
			if game.Running && game.isOver() {
				log.Println("Game is over. Only one team is left.")
				game.Running = false
				wg.Wait()
			}

			m, err := json.Marshal(&game)
			if err != nil {
				log.Println("Error parsing to JSON.", err)
				continue
			}
			log.Println(string(m))<commit_msg>added timestamp to json output<commit_after>package main

import (
	"encoding/json"
	"fmt"
	"log"
	"sync"
	"time"
)

const (
	initialEnergy      = 100
	initialLife        = 100
	initialTemperature = -53.5
	initialRadiation   = 500
	initialSolarFlare  = false
	maxTemperature     = 35.00
	minTemperature     = -142.00
	maxRadiation       = 1000
	minRadiation       = 0
)

// GameInfo contains information about the state of the game
type GameInfo struct {
	// Running defines whether the game is running or not
	Running      bool      `json:"running"`
	StartedAt    time.Time `json:"startedAt"`
	Timestamp    time.Time `json:"timestamp"`
	Reading      Reading   `json:"readings"`
	Teams        []Team    `json:"teams"`
	adminToken   string
	autoReadings bool
	start        chan TokenRequest
	stop         chan TokenRequest
	reset        chan TokenRequest
	join         chan JoinRequest
	kick         chan KickRequest
	readings     chan ReadingsRequest
	exit         chan []byte
	shield       chan ShieldRequest
}

// Team contains information about a team
type Team struct {
	Name   string `json:"name"`
	token  string
	Energy int64 `json:"energy"`
	Life   int64 `json:"life"`
	Shield bool  `json:"shield"`
}

var game = GameInfo{
	Running:      false,
	autoReadings: false,
	start:        make(chan TokenRequest),
	stop:         make(chan TokenRequest),
	reset:        make(chan TokenRequest),
	join:         make(chan JoinRequest),
	kick:         make(chan KickRequest),
	shield:       make(chan ShieldRequest),
	readings:     make(chan ReadingsRequest),
	exit:         make(chan []byte),
	Reading:      Reading{SolarFlare: initialSolarFlare, Temperature: initialTemperature, Radiation: initialRadiation},
	Teams:        []Team{},
}

func (game *GameInfo) run(adminToken string) {
	game.adminToken = adminToken
	var wg sync.WaitGroup
	ticker := time.NewTicker(time.Second * 1)
	for {
		select {
		case req := <-game.start:
			success, message := game.startGame(req.token)
			if success && game.autoReadings {
				go game.getReadings(&wg)
				go game.runEngine(&wg)
			}
			req.Response <- GameResponse{success: success, message: message}
			close(req.Response)
		case req := <-game.stop:
			success, message := game.stopGame(req.token)
			if success && game.autoReadings {
				wg.Wait()
			}
			req.Response <- GameResponse{success: success, message: message}
			close(req.Response)
		case req := <-game.join:
			success, message := game.joinGame(req.name)
			req.Response <- GameResponse{success: success, message: message}
			close(req.Response)
		case req := <-game.kick:
			success, message := game.kickTeam(req.token, req.name)
			req.Response <- GameResponse{success: success, message: message}
			close(req.Response)
		case req := <-game.shield:
			success, message := game.enableShield(req.token, req.enable)
			req.Response <- GameResponse{success: success, message: message}
			close(req.Response)
		case req := <-game.reset:
			success, message := game.resetGame(req.token)
			req.Response <- GameResponse{success: success, message: message}
			close(req.Response)
		case req := <-game.readings:
			success, message := game.updateReadings(req.readings, req.token)
			req.Response <- GameResponse{success: success, message: message}
			close(req.Response)
		case <-ticker.C:
			if game.Running && game.isOver() {
				log.Println("Game is over. Only one team is left.")
				game.Running = false
				wg.Wait()
			}

			game.Timestamp = time.Now()

			m, err := json.Marshal(&game)
			if err != nil {
				log.Println("Error parsing to JSON.", err)
				continue
			}
			log.Println(string(m))
{'fraction_non_alphanumeric': 0.11497326203208556, 'fraction_numerical': 0.008725021108922038, 'mean_word_length': 3.156140350877193, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 3, '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': '5074471', 'n_tokens_mistral': 2536, 'n_tokens_neox': 2198, 'n_words': 594}