<commit_before>package server

import (
	"github.com/trasa/watchmud/message"
	"github.com/trasa/watchmud/world"
	"github.com/trasa/watchmud/response"
)

//var GameServerInstance *GameServer

type GameServer struct {
	incomingMessageBuffer chan *message.IncomingMessage
	World                 *world.World
}

func NewGameServer() *GameServer {
	return &GameServer{
		incomingMessageBuffer: make(chan *message.IncomingMessage),
		World: world.New(),
	}
}

// Initialize the game server
//func Init() {
//	GameServerInstance = newGameServer()
//}

func (server *GameServer) Start() {
	// this is the loop that handles incoming requests
	// needs to be organized around TICKs
	for {
		select {
		// TODO add in tick time
		case message := <-server.incomingMessageBuffer:
			switch messageType := message.Body["msg_type"]; messageType {
			case "login":
				// login is special case, handled by server first and then
				// sent down to world for further initialization
				server.handleLogin(message) // TODO error handling

			default:
				server.World.HandleIncomingMessage(message)
			}
		}
	}
}

func (server *GameServer) Receive(message *message.IncomingMessage) {
	server.incomingMessageBuffer <- message
}


func (server *GameServer) handleLogin(msg *message.IncomingMessage) { // TODO error handling
	// is this connection already authenticated?
	// see if we can find an existing player ..
	if msg.Client.GetPlayer() != nil {
		// you've already got one
		msg.Client.Send(response.LoginResponse{
			Response: response.Response{
				MessageType: "login_response",
				Successful:  false,
				ResultCode:  "PLAYER_ALREADY_ATTACHED",
			},
		})
		return
	}
	// what if player is logged in on a different client?
	// TODO
	/*
		p := FindPlayerByClient(message.Client)
		if p != nil {<commit_msg>cleanup<commit_after>package server

import (
	"github.com/trasa/watchmud/message"
	"github.com/trasa/watchmud/world"
	"github.com/trasa/watchmud/response"
)


type GameServer struct {
	incomingMessageBuffer chan *message.IncomingMessage
	World                 *world.World
}

func NewGameServer() *GameServer {
	return &GameServer{
		incomingMessageBuffer: make(chan *message.IncomingMessage),
		World: world.New(),
	}
}

func (server *GameServer) Start() {
	// this is the loop that handles incoming requests
	// needs to be organized around TICKs
	for {
		select {
		// TODO add in tick time
		case msg := <-server.incomingMessageBuffer:
			switch messageType := msg.Body["msg_type"]; messageType {
			case "login":
				// login is special case, handled by server first and then
				// sent down to world for further initialization
				server.handleLogin(msg) // TODO error handling

			default:
				server.World.HandleIncomingMessage(msg)
			}
		}
	}
}

func (server *GameServer) Receive(message *message.IncomingMessage) {
	server.incomingMessageBuffer <- message
}


func (server *GameServer) handleLogin(msg *message.IncomingMessage) { // TODO error handling
	// is this connection already authenticated?
	// see if we can find an existing player ..
	if msg.Client.GetPlayer() != nil {
		// you've already got one
		msg.Client.Send(response.LoginResponse{
			Response: response.Response{
				MessageType: "login_response",
				Successful:  false,
				ResultCode:  "PLAYER_ALREADY_ATTACHED",
			},
		})
		return
	}
	// what if player is logged in on a different client?
	// TODO
	/*
		p := FindPlayerByClient(message.Client)
		if p != nil {
{'fraction_non_alphanumeric': 0.10749564206856478, 'fraction_numerical': 0.0, 'mean_word_length': 3.6029411764705883, 'pattern_counts': {'":': 2, '<': 7, '<?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': '7279687', 'n_tokens_mistral': 1151, 'n_tokens_neox': 1039, 'n_words': 295}