You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
exo/session.go

93 lines
1.6 KiB
Go

10 years ago
package main
import (
"bufio"
"fmt"
"net"
"strings"
"time"
10 years ago
)
type Connection struct {
net.Conn
*bufio.Reader
player *Player
location *System
lastScan time.Time
10 years ago
}
func NewConnection(conn net.Conn) *Connection {
return &Connection{
Conn: conn,
Reader: bufio.NewReader(conn),
}
}
func (c *Connection) Login() {
for {
fmt.Fprintf(c, "what is your name, adventurer?\n")
name, err := c.ReadString('\n')
if err == nil {
name = strings.TrimSpace(name)
} else {
log_error("player failed to connect: %v", err)
return
}
if !ValidName(name) {
fmt.Fprintf(c, "that name is illegal.\n")
continue
}
log_info("player connected: %v", name)
10 years ago
player, err := loadPlayer(name)
if err != nil {
log_error("could not read player: %v", err)
player = &Player{name: name}
if err := player.Create(); err != nil {
}
fmt.Fprintf(c, "godspeed, %s.\n", player.name)
} else {
c.player = player
fmt.Fprintf(c, "welcome back, %s.\n", player.name)
}
10 years ago
break
}
10 years ago
10 years ago
}
func (c *Connection) SetSystem(s *System) {
c.location = s
}
func (c *Connection) System() *System {
return c.location
}
10 years ago
func (c *Connection) Close() error {
10 years ago
log_info("player disconnecting: %s", c.PlayerName())
10 years ago
return c.Conn.Close()
}
func (c *Connection) PlayerName() string {
10 years ago
if c.player == nil {
return ""
}
10 years ago
return c.player.name
}
func (c *Connection) InTransit() bool {
return c.location == nil
}
func (c *Connection) RecordScan() {
c.lastScan = time.Now()
}
func (c *Connection) CanScan() bool {
return time.Since(c.lastScan) > 1*time.Minute
}
func (c *Connection) NextScan() time.Duration {
return -time.Since(c.lastScan.Add(time.Minute))
}