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.

142 lines
3.3 KiB
Go

10 years ago
package main
import (
10 years ago
"fmt"
10 years ago
"io"
"math/rand"
"net"
10 years ago
"os"
"strings"
10 years ago
"sync"
"time"
10 years ago
)
10 years ago
var (
dataPath = "/projects/exo/expl.speck"
)
10 years ago
func log_error(template string, args ...interface{}) {
10 years ago
fmt.Fprint(os.Stderr, "ERROR ")
fmt.Fprintf(os.Stderr, template+"\n", args...)
10 years ago
}
func log_info(template string, args ...interface{}) {
10 years ago
fmt.Fprint(os.Stdout, "INFO ")
fmt.Fprintf(os.Stdout, template+"\n", args...)
10 years ago
}
func bail(status int, template string, args ...interface{}) {
10 years ago
if status == 0 {
fmt.Fprintf(os.Stdout, template, args...)
} else {
fmt.Fprintf(os.Stderr, template, args...)
}
os.Exit(status)
10 years ago
}
10 years ago
func handleConnection(conn *Connection) {
var mu sync.Mutex
defer conn.Close()
conn.Login()
system, err := randomSystem()
10 years ago
if err != nil {
log_error("player %s failed to get random system: %v", conn.PlayerName(), err)
10 years ago
return
}
system.Arrive(conn)
if system.planets == 1 {
fmt.Fprintf(conn, "you are in the system %s. There is %d planet here.\n", system.name, system.planets)
10 years ago
} else {
fmt.Fprintf(conn, "you are in the system %s. There are %d planets here.\n", system.name, system.planets)
10 years ago
}
10 years ago
for {
line, err := conn.ReadString('\n')
switch err {
case io.EOF:
return
case nil:
break
default:
log_error("failed to read line from player %s: %v", conn.PlayerName(), err)
}
line = strings.TrimSpace(line)
parts := strings.Split(line, " ")
if isCommand(parts[0]) {
runCommand(conn, parts[0], parts[1:]...)
continue
}
10 years ago
switch parts[0] {
case "scan":
for _, otherSystem := range index {
if otherSystem.name == system.name {
10 years ago
continue
}
10 years ago
go func(p *System) {
dist := system.DistanceTo(p)
10 years ago
delay := time.Duration(int64(dist * 100000000))
time.Sleep(delay)
mu.Lock()
fmt.Fprintf(conn, "PONG from system %s (%v)\n", p.name, delay)
10 years ago
mu.Unlock()
}(otherSystem)
10 years ago
}
case "broadcast":
10 years ago
msg := strings.Join(parts[1:], " ")
log_info("player %s is broadcasting message %s", conn.PlayerName(), msg)
for _, otherSystem := range index {
if otherSystem.name == system.name {
log_info("skpping duplicate system %s", system.name)
10 years ago
continue
}
go func(s *System) {
log_info("message reached system %s with %d inhabitants", s.name, s.NumInhabitants())
dist := system.DistanceTo(s) * 0.5
10 years ago
delay := time.Duration(int64(dist * 100000000))
time.Sleep(delay)
s.EachConn(func(conn *Connection) {
fmt.Fprintln(conn, msg)
})
}(otherSystem)
}
10 years ago
// case "nearby":
// neighbors, err := system.Nearby(25)
// fmt.Fprintf(conn, "fetching nearby star systems\n")
// if err != nil {
// log_error("%v", err)
// break
// }
// fmt.Fprintf(conn, "found %d nearby systems\n", len(neighbors))
// for _, neighbor := range neighbors {
// other := index[neighbor.id]
// fmt.Fprintf(conn, "%s: %v\n", other.name, neighbor.distance)
// }
10 years ago
case "quit":
return
default:
fmt.Fprintf(conn, "hmm I'm not sure I know that one.\n")
}
}
}
10 years ago
func main() {
10 years ago
dbconnect()
10 years ago
rand.Seed(time.Now().UnixNano())
10 years ago
setupDb()
listener, err := net.Listen("tcp", ":9220")
if err != nil {
bail(E_No_Port, "unable to start server: %v", err)
}
for {
conn, err := listener.Accept()
if err != nil {
log_error("error accepting connection: %v", err)
continue
}
10 years ago
go handleConnection(NewConnection(conn))
}
10 years ago
}