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/connection.go

265 lines
5.2 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
dest *System
travelRemaining int64
lastScan time.Time
lastBomb time.Time
kills int
dead bool
money int
colonies []*System
bombs int
state PlayerState // this is wrong...
10 years ago
}
type PlayerState int
const (
idle PlayerState = iota
dead
inTransit
mining
)
10 years ago
func NewConnection(conn net.Conn) *Connection {
10 years ago
c := &Connection{
10 years ago
Conn: conn,
Reader: bufio.NewReader(conn),
bombs: 1,
10 years ago
}
currentGame.Join(c)
10 years ago
return c
10 years ago
}
func (conn *Connection) Reset() {
*conn = Connection{
Conn: conn.Conn,
Reader: bufio.NewReader(conn.Conn),
bombs: 1,
player: conn.player,
}
currentGame.Join(conn)
}
10 years ago
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 {
log_error("unable to create player record: %v", err)
10 years ago
}
10 years ago
fmt.Fprintf(c, "you look new around these parts, %s.\n", player.name)
fmt.Fprintf(c, `if you'd like a description of how to play, type the "help" command`)
c.player = player
10 years ago
} else {
c.player = player
fmt.Fprintf(c, "welcome back, %s.\n", player.name)
}
10 years ago
break
}
currentGame.Register(c)
}
func (c *Connection) Dead() bool {
return false
}
func (c *Connection) Tick(frame int64) {
// fuck
switch c.state {
case idle:
case dead:
case inTransit:
c.travelRemaining -= 1
log_info("player %s has remaining travel: %v", c.PlayerName(), c.travelRemaining)
if c.travelRemaining == 0 {
c.land()
}
case mining:
sys := c.System()
if sys == nil {
log_error("a player is in the mining state with no system. what?")
break
}
if sys.money <= 0 {
fmt.Fprintf(c, "system %s is all out of space duckets.\n", sys.Label())
c.StopMining()
} else {
c.Deposit(1)
sys.money -= 1
log_info("%v", c.money)
}
default:
log_error("connection %v has invalid state wtf", c)
}
}
func (c *Connection) TravelTo(dest *System) {
fmt.Fprintf(c, "traveling to: %s\n", dest.Label())
dist := c.System().DistanceTo(dest)
c.travelRemaining = int64(dist / (options.lightSpeed * options.playerSpeed))
c.location = nil
c.dest = dest
c.state = inTransit // fuck everything about this
}
10 years ago
func (c *Connection) land() {
fmt.Fprintf(c, "you have arrived at %v\n", c.dest.Label())
c.location = c.dest
c.dest = nil
c.state = idle
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())
currentGame.Quit(c)
if c.Conn != nil {
return c.Conn.Close()
}
return nil
10 years ago
}
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() {
10 years ago
fmt.Fprintln(c, "scanning known systems for signs of life")
c.lastScan = time.Now()
time.AfterFunc(1*time.Minute, func() {
fmt.Fprintln(c, "scanner ready")
})
}
10 years ago
func (c *Connection) RecordBomb() {
c.lastBomb = time.Now()
time.AfterFunc(15*time.Second, func() {
fmt.Fprintln(c, "bomb arsenal reloaded")
})
10 years ago
}
func (c *Connection) CanScan() bool {
return time.Since(c.lastScan) > 1*time.Minute
}
10 years ago
func (c *Connection) CanBomb() bool {
10 years ago
return time.Since(c.lastBomb) > 15*time.Second
10 years ago
}
func (c *Connection) NextScan() time.Duration {
return -time.Since(c.lastScan.Add(time.Minute))
}
10 years ago
func (c *Connection) NextBomb() time.Duration {
10 years ago
return -time.Since(c.lastBomb.Add(15 * time.Second))
10 years ago
}
func (c *Connection) MadeKill(victim *Connection) {
c.kills += 1
if c.kills == 3 {
c.Win("military")
10 years ago
}
}
func (c *Connection) Mine() {
switch c.state {
case idle:
fmt.Fprintf(c, "now mining %s. %v space duckets remaining.\n", c.System().name, c.System().money)
fmt.Fprintln(c, "(press enter to stop mining)")
c.state = mining
default:
fmt.Fprintf(c, "no\n")
}
10 years ago
}
func (c *Connection) StopMining() {
fmt.Fprintf(c, "done mining\n")
c.state = idle
10 years ago
}
func (c *Connection) IsMining() bool {
return c.state == mining
10 years ago
}
func (c *Connection) Withdraw(n int) {
c.money -= n
}
func (c *Connection) Deposit(n int) {
c.money += n
if c.money >= options.economic {
c.Win("economic")
}
}
func (c *Connection) Win(method string) {
currentGame.Win(c, method)
10 years ago
}
func (c *Connection) Die() {
10 years ago
fmt.Fprintf(c, "you were bombed. You will respawn in 1 minutes.\n")
10 years ago
c.dead = true
c.System().Leave(c)
time.AfterFunc(30*time.Second, func() {
10 years ago
fmt.Fprintf(c, "respawn in 30 seconds.\n")
})
time.AfterFunc(time.Minute, c.Respawn)
10 years ago
}
func (c *Connection) Respawn() {
c.dead = false
WUT:
s, err := randomSystem()
if err != nil {
log_error("error in respawn: %v", err)
goto WUT
}
s.Arrive(c)
}