hermeticum/server/cmd/main.go

208 lines
4.5 KiB
Go
Raw Normal View History

2022-07-07 22:07:03 +00:00
package main
import (
"context"
"flag"
"fmt"
2022-07-22 21:10:12 +00:00
"io"
2022-07-07 22:07:03 +00:00
"log"
"net"
2022-07-22 21:57:15 +00:00
"sync"
2022-07-07 22:07:03 +00:00
"github.com/vilmibm/hermeticum/proto"
"github.com/vilmibm/hermeticum/server/db"
2022-07-07 22:07:03 +00:00
"google.golang.org/grpc"
)
var (
tls = flag.Bool("tls", false, "Connection uses TLS if true, else plain TCP")
certFile = flag.String("cert_file", "", "The TLS cert file")
keyFile = flag.String("key_file", "", "The TLS key file")
port = flag.Int("port", 6666, "The server port")
)
func _main() (err error) {
l, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
if err != nil {
return err
}
fmt.Printf("DBG %#v\n", l)
var opts []grpc.ServerOption
if *tls {
log.Fatal("tls unsupported")
/*
// TODO base some stuff on the data package in the examples to get tls working
if *certFile == "" {
*certFile = data.Path("x509/server_cert.pem")
}
if *keyFile == "" {
*keyFile = data.Path("x509/server_key.pem")
}
creds, err := credentials.NewServerTLSFromFile(*certFile, *keyFile)
if err != nil {
log.Fatalf("Failed to generate credentials %v", err)
}
opts = []grpc.ServerOption{grpc.Creds(creds)}
*/
}
grpcServer := grpc.NewServer(opts...)
2022-07-28 01:45:21 +00:00
srv, err := newServer()
if err != nil {
return err
}
proto.RegisterGameWorldServer(grpcServer, srv)
2022-07-07 22:07:03 +00:00
grpcServer.Serve(l)
return nil
}
type gameWorldServer struct {
proto.UnimplementedGameWorldServer
2022-07-22 21:57:15 +00:00
2022-07-28 01:45:21 +00:00
db db.DB
2022-07-22 21:57:15 +00:00
mu sync.Mutex // for msgRouter
msgRouter map[string]func(*proto.ClientMessage) error
2022-07-07 22:07:03 +00:00
}
2022-07-28 01:45:21 +00:00
func newServer() (*gameWorldServer, error) {
// TODO read from env or whatever
db, err := db.NewDB("postgres://vilmibm:vilmibm@localhost:5432/hermeticum")
if err != nil {
return nil, err
}
2022-07-22 21:57:15 +00:00
s := &gameWorldServer{
msgRouter: make(map[string]func(*proto.ClientMessage) error),
2022-07-28 01:45:21 +00:00
db: db,
2022-07-22 21:57:15 +00:00
}
2022-07-28 01:45:21 +00:00
return s, nil
2022-07-07 22:07:03 +00:00
}
2022-07-22 21:10:12 +00:00
func (s *gameWorldServer) Commands(stream proto.GameWorld_CommandsServer) error {
2022-07-28 01:45:21 +00:00
var sid string
2022-07-22 21:10:12 +00:00
for {
cmd, err := stream.Recv()
if err == io.EOF {
2022-07-28 02:05:48 +00:00
// TODO this doesn't really do anything. if a client
// disconnects without warning there's no EOF.
2022-07-28 01:45:21 +00:00
return s.db.EndSession(sid)
2022-07-22 21:10:12 +00:00
}
if err != nil {
return err
}
2022-07-28 01:45:21 +00:00
sid = cmd.SessionInfo.SessionID
2022-07-28 02:05:48 +00:00
log.Printf("verb %s in session %s", cmd.Verb, sid)
if cmd.Verb == "quit" || cmd.Verb == "q" {
s.msgRouter[sid] = nil
log.Printf("ending session %s", sid)
return s.db.EndSession(sid)
}
2022-07-22 21:57:15 +00:00
send := s.msgRouter[sid]
msg := &proto.ClientMessage{
Type: proto.ClientMessage_OVERHEARD,
Text: fmt.Sprintf("%s sent command %s with args %s",
sid, cmd.Verb, cmd.Rest),
}
speaker := "ECHO"
msg.Speaker = &speaker
err = send(msg)
if err != nil {
log.Printf("failed to send %v to %s: %s", msg, sid, err)
}
2022-07-22 21:10:12 +00:00
// TODO find the user who ran action via SessionInfo
// TODO get area of effect, which should include the sender
// TODO dispatch the command to each affected object
}
}
2022-07-07 22:07:03 +00:00
func (s *gameWorldServer) Ping(ctx context.Context, _ *proto.SessionInfo) (*proto.Pong, error) {
pong := &proto.Pong{
When: "TODO",
}
return pong, nil
}
2022-07-09 07:15:24 +00:00
func (s *gameWorldServer) Messages(si *proto.SessionInfo, stream proto.GameWorld_MessagesServer) error {
2022-07-22 21:57:15 +00:00
s.mu.Lock()
s.msgRouter[si.SessionID] = stream.Send
s.mu.Unlock()
// TODO this is clearly bad but it works. I should refactor this so that messages are received on a channel.
for {
2022-07-09 07:15:24 +00:00
}
}
func (s *gameWorldServer) Register(ctx context.Context, auth *proto.AuthInfo) (si *proto.SessionInfo, err error) {
2022-07-28 03:30:23 +00:00
var account *db.Account
account, err = s.db.CreateAccount(auth.Username, auth.Password)
if err != nil {
2022-07-28 03:30:23 +00:00
return
}
2022-08-01 15:15:18 +00:00
var sessionID string
sessionID, err = s.db.StartSession(*account)
2022-07-28 03:30:23 +00:00
if err != nil {
2022-08-01 15:15:18 +00:00
return nil, err
}
2022-08-01 15:15:18 +00:00
log.Printf("started session for %s", account.Name)
2022-08-01 15:15:18 +00:00
av, err := s.db.AvatarBySessionID(sessionID)
if err != nil {
return nil, err
}
2022-07-28 03:30:23 +00:00
2022-08-01 15:15:18 +00:00
bedroom, err := s.db.BedroomBySessionID(sessionID)
if err != nil {
return nil, err
}
2022-07-28 03:30:23 +00:00
2022-08-01 15:15:18 +00:00
err = s.db.MoveInto(*av, *bedroom)
if err != nil {
return nil, err
}
2022-08-01 15:15:18 +00:00
// TODO send room info, avatar info to client (need to figure this out and update proto)
si = &proto.SessionInfo{SessionID: sessionID}
return
}
func (s *gameWorldServer) Login(ctx context.Context, auth *proto.AuthInfo) (si *proto.SessionInfo, err error) {
var a *db.Account
2022-07-28 01:45:21 +00:00
a, err = s.db.ValidateCredentials(auth.Username, auth.Password)
if err != nil {
return
}
var sessionID string
2022-07-28 01:45:21 +00:00
sessionID, err = s.db.StartSession(*a)
if err != nil {
return
}
si = &proto.SessionInfo{SessionID: sessionID}
return
2022-07-07 22:07:03 +00:00
}
// TODO other server functions
func main() {
err := _main()
if err != nil {
2022-07-07 22:12:22 +00:00
log.Fatal(err.Error())
2022-07-07 22:07:03 +00:00
}
}