<commit_before>package terra

import (
	"github.com/arukim/terrarium/helpers"
	"log"
	"time"
)

type TurnSummary struct {
	Turn int
}

type PlayerTurn struct {
}

type PlayerInfo struct {
	Name          string
	TurnSummaryCh chan TurnSummary
	PlayerTurnCh  chan PlayerTurn
	Score         int
}

type Game struct {
	// settings
	maxPlayers  int
	maxTurns    int
	turnTimeout time.Duration
	// current info
	players      []PlayerInfo
	playersCount int
	turn         int
}

func NewGame(maxPlayers int, maxTurns int, turnTimeout time.Duration) *Game {
	g := new(Game)
	g.maxPlayers = maxPlayers
	g.maxTurns = maxTurns
	g.turnTimeout = turnTimeout
	return g
}

func (g *Game) Start() chan PlayerInfo {
	connectQueue := make(chan PlayerInfo)
	go func() {
		log.Printf("Waiting for players\n")
		g.players = make([]PlayerInfo, g.maxPlayers, g.maxPlayers)
		g.playersCount = 0
		for g.playersCount < g.maxPlayers {
			player := <-connectQueue
			log.Printf("Player %s connected\n", player.Name)
			g.players[g.playersCount] = player
			g.playersCount++
		}

		log.Printf("Starting the game\n")
		go func() {
			for g.turn < g.maxTurns {
				g.Turn()
			}
			g.FindWinner()
		}()
	}()
	return connectQueue
}

func (g *Game) FindWinner() {
	maxScore := 0
	for _, player := range g.players {
		if player.Score > maxScore {
			maxScore = player.Score
		}
	}

	for _, player := range g.players {
		if player.Score == maxScore {
			log.Printf("%s is winner with %v score", player.Name, player.Score)
		}
	}
}

func (g *Game) Turn() {
	log.Printf("Turn %d\n", g.turn)
	for i, _ := range g.players {
		player := &g.players[i]

		// check for old turns
		select {
		case <-player.PlayerTurnCh:
		default:
		}

		// start timeout, send turn summary
		var timeout = helpers.NewTimeout(g.turnTimeout)
		player.TurnSummaryCh <- TurnSummary{Turn: g.turn}

		// wait for player answer
		select {<commit_msg>added food generation<commit_after>package terra

import (
	"github.com/arukim/terrarium/helpers"
	"log"
	"math/rand"
	"time"
)

type TurnSummary struct {
	Turn int
}

type PlayerTurn struct {
}

type PlayerInfo struct {
	Name          string
	TurnSummaryCh chan TurnSummary
	PlayerTurnCh  chan PlayerTurn
	Score         int
}

type Point struct {
	X int
	Y int
}

type Game struct {
	// settings
	maxPlayers    int
	maxTurns      int
	mapWidth      int
	mapHeight     int
	foodSpawnRate int
	turnTimeout   time.Duration
	// current info
	players      []PlayerInfo
	playersCount int
	turn         int
	food         map[Point]int
}

func NewGame(maxPlayers int, maxTurns int, turnTimeout time.Duration) *Game {
	g := new(Game)
	g.maxPlayers = maxPlayers
	g.maxTurns = maxTurns
	g.turnTimeout = turnTimeout
	g.mapWidth = 100
	g.mapHeight = 100
	g.foodSpawnRate = 20

	g.food = make(map[Point]int)
	return g
}

func (g *Game) Start() chan PlayerInfo {
	connectQueue := make(chan PlayerInfo)
	go func() {
		log.Printf("Waiting for players\n")
		g.players = make([]PlayerInfo, g.maxPlayers, g.maxPlayers)
		g.playersCount = 0
		for g.playersCount < g.maxPlayers {
			player := <-connectQueue
			log.Printf("Player %s connected\n", player.Name)
			g.players[g.playersCount] = player
			g.playersCount++
		}

		log.Printf("Starting the game\n")
		go func() {
			for g.turn < g.maxTurns {
				g.Turn()
			}
			g.PrintFoodMap()
			g.FindWinner()
		}()
	}()
	return connectQueue
}

func (g *Game) FindWinner() {
	maxScore := 0
	for _, player := range g.players {
		if player.Score > maxScore {
			maxScore = player.Score
		}
	}

	for _, player := range g.players {
		if player.Score == maxScore {
			log.Printf("%s is winner with %v score", player.Name, player.Score)
		}
	}
}

func (g *Game) SpawnFood() {
	for i := 0; i < g.foodSpawnRate; i++ {
		var point = Point{X: rand.Intn(g.mapWidth),
			Y: rand.Intn(g.mapHeight)}
		var currValue = g.food[point]
		currValue++
		g.food[point] = currValue
	}
}

func (g *Game) PrintFoodMap() {
	for key, value := range g.food {
		log.Printf("%v - %v", value, key)
	}
}

func (g *Game) Turn() {
	log.Printf("Turn %d\n", g.turn)
	for i, _ := range g.players {
		g.SpawnFood()

		player := &g.players[i]

		// check for old turns
		select {
		case <-player.PlayerTurnCh:
		default:
		}

		// start timeout, send turn summary
		var timeout = helpers.NewTimeout(g.turnTimeout)
		player.TurnSummaryCh <- TurnSummary{Turn: g.turn}

		// wait for player answer
		select {
{'fraction_non_alphanumeric': 0.12445763873030372, 'fraction_numerical': 0.00296871431833752, 'mean_word_length': 3.066852367688022, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 5, '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': '2680047', 'n_tokens_mistral': 1741, 'n_tokens_neox': 1606, 'n_words': 411}