telnetroulette/client.go
2024-06-08 11:52:12 +02:00

56 lines
1.2 KiB
Go

package main
import (
"github.com/BRNSystems/go-telnet"
"strconv"
)
type Client struct {
Client telnet.Client
Username string
ID int
History []string
HistoryIndex int
context telnet.Context
writer telnet.Writer
reader telnet.Reader
// Add more fields as needed
}
func NewClient(context telnet.Context, writer telnet.Writer, reader telnet.Reader) int {
client := Client{}
clientsMutex.Lock()
clients = append(clients, &client)
clientID := len(clients) - 1
defer clientsMutex.Unlock()
clients[clientID].ID = clientID
clients[clientID].Username = "Client #" + strconv.Itoa(clientID)
clients[clientID].context = context
clients[clientID].writer = writer
clients[clientID].reader = reader
return clientID
}
func RemoveClient(clientID int) {
clientsMutex.Lock()
defer clientsMutex.Unlock()
clients = append(clients[:clientID], clients[clientID+1:]...)
}
func (client Client) send(message string) {
client.writer.Write([]byte(message))
}
func getIDByName(name string) int {
clientsMutex.Lock()
defer clientsMutex.Unlock()
for _, client := range clients {
if client.Username == name {
return client.ID
}
}
return -1
}