Skip to content

Commit

Permalink
setup link between frontend and client to send move actions
Browse files Browse the repository at this point in the history
  • Loading branch information
zthacker committed Oct 25, 2023
1 parent fe35dca commit 2a1d89c
Show file tree
Hide file tree
Showing 5 changed files with 312 additions and 127 deletions.
17 changes: 17 additions & 0 deletions actions/actions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package actions

import (
"github.com/google/uuid"
"time"
)

type Action interface{}

type Direction int

type MoveAction struct {
Action
Direction Direction
ID uuid.UUID
Created time.Time
}
22 changes: 20 additions & 2 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package client

import (
"context"
"gameclient/actions"
"gameclient/frontend"
"gameclient/proto"
"github.com/google/uuid"
Expand All @@ -13,10 +14,11 @@ type GameClient struct {
CurrentPlayer uuid.UUID
Stream proto.GameBackend_StreamClient
View *frontend.View
actionChannel chan actions.Action
}

func NewGameClient() *GameClient {
return &GameClient{}
func NewGameClient(actionChannel chan actions.Action) *GameClient {
return &GameClient{actionChannel: actionChannel}
}

func (gc *GameClient) Connect(grpcClient proto.GameBackendClient, playerID uuid.UUID, password string, playerName string) error {
Expand Down Expand Up @@ -50,6 +52,7 @@ func (gc *GameClient) Connect(grpcClient proto.GameBackendClient, playerID uuid.
func (gc *GameClient) Start() {
//write a loop to handle changes like moves, etc

//receiving from game server
go func() {
for {
resp, err := gc.Stream.Recv()
Expand All @@ -66,6 +69,21 @@ func (gc *GameClient) Start() {
}
}()

// little test func for sending to server to validate stream
go func() {
for {
action := <-gc.actionChannel
switch action.(type) {
case actions.MoveAction:
d := action.(actions.MoveAction).Direction
err := gc.Stream.Send(&proto.Request{Action: &proto.Request_Move{Move: &proto.Move{Direction: proto.Direction(d)}}})
if err != nil {
logrus.Error(err)
}
}
}
}()

//for {
// logrus.Infof("Client: %s is Connected", gc.CurrentPlayer)
// time.Sleep(10 * time.Second)
Expand Down
33 changes: 26 additions & 7 deletions frontend/frontend.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package frontend

import (
"gameclient/actions"
"github.com/gdamore/tcell/v2"
"github.com/google/uuid"
"github.com/rivo/tview"
"github.com/sirupsen/logrus"
"time"
)

Expand All @@ -23,16 +23,18 @@ type View struct {
pages *tview.Pages
drawCallbacks []func()
viewPort tview.Primitive
actionChannel chan actions.Action
Done chan error
}

func NewView() *View {
func NewView(actionChannel chan actions.Action) *View {
app := tview.NewApplication()
pages := tview.NewPages()
view := &View{
App: app,
pages: pages,
drawCallbacks: make([]func(), 0),
actionChannel: actionChannel,
Done: make(chan error),
}

Expand Down Expand Up @@ -96,13 +98,30 @@ func setupViewPort(view *View) {
box.SetInputCapture(func(e *tcell.EventKey) *tcell.EventKey {
switch e.Key() {
case tcell.KeyUp:
logrus.Info("Up key was pressed")
view.actionChannel <- actions.MoveAction{
Direction: 0,
ID: view.CurrentPlayer,
Created: time.Now(),
}
case tcell.KeyDown:
logrus.Info("Down key was pressed")
case tcell.KeyRight:
logrus.Info("Right key was pressed")
view.actionChannel <- actions.MoveAction{
Direction: 1,
ID: view.CurrentPlayer,
Created: time.Now(),
}
case tcell.KeyLeft:
logrus.Info("Left key was pressed")
view.actionChannel <- actions.MoveAction{
Direction: 2,
ID: view.CurrentPlayer,
Created: time.Now(),
}
case tcell.KeyRight:
view.actionChannel <- actions.MoveAction{
Direction: 3,
ID: view.CurrentPlayer,
Created: time.Now(),
}

}
return e
})
Expand Down
7 changes: 5 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"gameclient/actions"
"gameclient/client"
"gameclient/frontend"
"gameclient/proto"
Expand Down Expand Up @@ -82,7 +83,9 @@ func main() {
app := loginApp(&info)
app.Run()

view := frontend.NewView()
actionChannel := make(chan actions.Action, 1)

view := frontend.NewView(actionChannel)

var opts []grpc.DialOption
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
Expand All @@ -94,7 +97,7 @@ func main() {
defer conn.Close()

grpcClient := proto.NewGameBackendClient(conn)
c := client.NewGameClient()
c := client.NewGameClient(actionChannel)
playerID := uuid.New()
logrus.Info(playerID)

Expand Down
Loading

0 comments on commit 2a1d89c

Please sign in to comment.