<commit_before>package main

import (
	"log"
	"time"
)

type User struct {
	client *Client
	player *Player
}

type Game struct {
	users          []*User
	pendingClients chan *Client
	leavingUsers   chan *User
	world          *World
}

func NewGame(w *World) *Game {
	g := new(Game)
	g.users = make([]*User, 0)
	g.pendingClients = make(chan *Client, 10)
	g.leavingUsers = make(chan *User, 10)
// 	g.world = NewWorld(0)
	g.world = w
	return g
}

// Thread safe
func (g *Game) Connect(c *Client) {
	g.pendingClients <- c
}

// Not thread safe (This better be called on the Games main thread)
func (g *Game) handlePendingClients() {
	for {
		select {
		case c := <-g.pendingClients:
			p := NewPlayer(g.world, c.name)
			user := new(User)
			user.client = c
			user.player = p
			g.users = append(g.users, user)
			g.world.AddEntity(p)
			g.Announce(c.name + " has joined the game!")
		default:
			return
		}
	}
}

func (g *Game) Disconnect(u *User) {
	g.leavingUsers <- u
}

func (g *Game) handleLeavingUsers() {
	for {
		select {
		case u := <-g.leavingUsers:
			i := g.findUser(u)
			if i == -1 {
				log.Println("[WARN] Attempt to disconnect user who is not connected.")
				return
			}
			g.users[i] = g.users[len(g.users) - 1]
			g.users = g.users[:len(g.users) - 1]

			g.Announce(u.client.name + " has left the game :(")
		default:
			return
		}
	}
}

func (g *Game) Announce(message string) {
	g.Chat("SERVER", message)
}

func (g *Game) Chat(user string, message string) {
	log.Println("[CHAT]", user + ":", message)<commit_msg>Remove the player from the world when disconnected<commit_after>package main

import (
	"log"
	"time"
)

type User struct {
	client *Client
	player *Player
}

type Game struct {
	users          []*User
	pendingClients chan *Client
	leavingUsers   chan *User
	world          *World
}

func NewGame(w *World) *Game {
	g := new(Game)
	g.users = make([]*User, 0)
	g.pendingClients = make(chan *Client, 10)
	g.leavingUsers = make(chan *User, 10)
// 	g.world = NewWorld(0)
	g.world = w
	return g
}

// Thread safe
func (g *Game) Connect(c *Client) {
	g.pendingClients <- c
}

// Not thread safe (This better be called on the Games main thread)
func (g *Game) handlePendingClients() {
	for {
		select {
		case c := <-g.pendingClients:
			p := NewPlayer(g.world, c.name)
			user := new(User)
			user.client = c
			user.player = p
			g.users = append(g.users, user)
			g.world.AddEntity(p)
			g.Announce(c.name + " has joined the game!")
		default:
			return
		}
	}
}

func (g *Game) Disconnect(u *User) {
	g.leavingUsers <- u
}

func (g *Game) handleLeavingUsers() {
	for {
		select {
		case u := <-g.leavingUsers:
			i := g.findUser(u)
			if i == -1 {
				log.Println("[WARN] Attempt to disconnect user who is not connected.")
				return
			}
			g.users[i] = g.users[len(g.users) - 1]
			g.users = g.users[:len(g.users) - 1]

			g.world.RemoveEntity(u.player)
			g.Announce(u.client.name + " has left the game :(")
		default:
			return
		}
	}
}

func (g *Game) Announce(message string) {
	g.Chat("SERVER", message)
}

func (g *Game) Chat(user string, message string) {
	log.Println("[CHAT]", user + ":", message)
{'fraction_non_alphanumeric': 0.15323351385791653, 'fraction_numerical': 0.005734310289901242, 'mean_word_length': 2.9797211660329532, 'pattern_counts': {'":': 2, '<': 11, '<?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': '6629377', 'n_tokens_mistral': 1324, 'n_tokens_neox': 1205, 'n_words': 325}