Skip to content

Commit

Permalink
Merge pull request #1 from ysoldak/dev
Browse files Browse the repository at this point in the history
First usable version
  • Loading branch information
ysoldak authored Jul 5, 2024
2 parents 543ac84 + 2989fc1 commit 765cc6e
Show file tree
Hide file tree
Showing 21 changed files with 699 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@

# Go workspace file
go.work
build
35 changes: 35 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# VERSION := $(shell git describe --tags)
# LD_FLAGS := -ldflags="-X 'main.Version=$(VERSION)'"

SRC := ./cmd/lady
BIN := build/fpvc-lady

clean:
@rm -rf build
@mkdir -p build

build-darwin-amd64:
GOOS=darwin GOARCH=amd64 go build $(LD_FLAGS) -o build/fpvc-lady-darwin-amd64 $(SRC)

build-windows-386:
GOOS=windows GOARCH=386 go build $(LD_FLAGS) -o build/fpvc-lady-windows-386.exe $(SRC)

build-windows-amd64:
GOOS=windows GOARCH=amd64 go build $(LD_FLAGS) -o build/fpvc-lady-windows-amd64.exe $(SRC)

build-linux-386:
GOOS=linux GOARCH=386 go build $(LD_FLAGS) -o build/fpvc-lady-linux-386 $(SRC)

build-linux-amd64:
GOOS=linux GOARCH=amd64 go build $(LD_FLAGS) -o build/fpvc-lady-linux-amd64 $(SRC)

build-linux-arm:
GOOS=linux GOARCH=arm go build $(LD_FLAGS) -o build/fpvc-lady-linux-arm $(SRC)

build-linux-arm64:
GOOS=linux GOARCH=arm64 go build $(LD_FLAGS) -o build/fpvc-lady-linux-arm64 $(SRC)

build: clean build-darwin-amd64 build-windows-386 build-windows-amd64 build-linux-386 build-linux-amd64 build-linux-arm build-linux-arm64

run:
go run $(SRC)
90 changes: 90 additions & 0 deletions cmd/lady/comment.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package main

import (
"fmt"
"io"
"os"
"time"

"github.com/urfave/cli/v2"
"github.com/ysoldak/fpvc-lady/internal/csp"
"github.com/ysoldak/fpvc-lady/internal/game"
"github.com/ysoldak/fpvc-lady/internal/tts"
"github.com/ysoldak/fpvc-lady/internal/utils"
)

func commentAction(cc *cli.Context) (err error) {

// TTS
speech := cc.String(flagSpeech)
speakerChan := make(chan string, 100)
ttsEngine := tts.NewByName(speech, speakerChan)
go ttsEngine.Run()

// Serial
serial := io.ReadWriter(os.Stdin)
port := cc.String(flagPort)
if port != "none" {
serial, err = utils.NewSerial(port)
if err != nil {
return err
}
}

// CSP
eventsChan := make(chan interface{}, 100)
cspEngine := csp.New(serial, eventsChan)
go cspEngine.Run()

// Game
g := game.NewGame()

// Temporary for testing
if port == "none" {
go func() {
for {
time.Sleep(5 * time.Second)
eventsChan <- csp.Hit{
PlayerID: 0xA1,
Lives: 10,
}
time.Sleep(100 * time.Millisecond)
eventsChan <- csp.Claim{
PlayerID: 0xB1,
Power: 3,
}
}
}()
}

// Main loop
speakerChan <- "The lady is ready."
for {
event := <-eventsChan
switch event := event.(type) {
case csp.Beacon:
player, new := g.Beacon(event)
if new {
speakerChan <- fmt.Sprintf("%s registered", player.Name)
}
// fmt.Printf("%v Beacon: %X %s %s\n", time.Now(), event.PlayerID, event.Name, event.Description)
case csp.Hit:
g.Hit(event)
// println("Hit: ", event.PlayerID, event.Lives)
case csp.Claim:
victim, ok := g.Claim(event)
if !ok {
continue
}
attacker, _ := g.Player(event.PlayerID)
speakerChan <- fmt.Sprintf("%s was hit by %s. %d lives left.", victim.Name, attacker.Name, victim.Lives)
// println("Claim: ", event.PlayerID, event.Power)
}
println()
println()
println()
for _, line := range g.Table() {
println(line)
}
}
}
29 changes: 29 additions & 0 deletions cmd/lady/flags.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"github.com/urfave/cli/v2"
)

const (
flagPort = "port"
flagSpeech = "speech"
)

func getFlags() []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: flagPort,
Usage: "Port name",
EnvVars: []string{"PORT"},
Required: false,
Value: "",
},
&cli.StringFlag{
Name: flagSpeech,
Usage: "Speech command: [system], google, none or any other command to convert text to speech.",
EnvVars: []string{"SPEECH"},
Required: false,
Value: "system",
},
}
}
20 changes: 20 additions & 0 deletions cmd/lady/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

import (
"log"
"os"

"github.com/urfave/cli/v2"
)

func main() {
app := cli.NewApp()
app.Name = "fpvc-lady"

app.Flags = getFlags()
app.Action = commentAction

if err := app.Run(os.Args); err != nil {
log.Fatalf("error: %s\n", err.Error())
}
}
17 changes: 17 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module github.com/ysoldak/fpvc-lady

go 1.22.4

require (
github.com/hegedustibor/htgo-tts v0.0.0-20211106065519-4b33b08f698f
github.com/urfave/cli/v2 v2.27.2
go.bug.st/serial v1.6.2
)

require (
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/creack/goselect v0.1.2 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 // indirect
golang.org/x/sys v0.21.0 // indirect
)
24 changes: 24 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/goselect v0.1.2 h1:2DNy14+JPjRBgPzAd1thbQp4BSIihxcBf0IXhQXDRa0=
github.com/creack/goselect v0.1.2/go.mod h1:a/NhLweNvqIYMuxcMOuWY516Cimucms3DglDzQP3hKY=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/hegedustibor/htgo-tts v0.0.0-20211106065519-4b33b08f698f h1:9hj9NB/nSMz1AF/uw1J51gNmbfInP8oQ446C/50o1gE=
github.com/hegedustibor/htgo-tts v0.0.0-20211106065519-4b33b08f698f/go.mod h1:Uqnv3qFrs2WtaeO2/+PQ35x4HdD4u2ZAX/PcQEEP5VY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI=
github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
go.bug.st/serial v1.6.2 h1:kn9LRX3sdm+WxWKufMlIRndwGfPWsH1/9lCWXQCasq8=
go.bug.st/serial v1.6.2/go.mod h1:UABfsluHAiaNI+La2iESysd9Vetq7VRdpxvjx7CmmOE=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
15 changes: 15 additions & 0 deletions internal/csp/beacon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package csp

type Beacon struct {
PlayerID byte
Name string
Description string
}

func NewBeacon(data []byte) Beacon {
return Beacon{
PlayerID: data[0],
Name: string(data[1:11]),
Description: string(data[11:31]),
}
}
13 changes: 13 additions & 0 deletions internal/csp/claim.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package csp

type Claim struct {
PlayerID byte
Power byte
}

func NewClaim(data []byte) Claim {
return Claim{
PlayerID: data[0],
Power: data[1],
}
}
96 changes: 96 additions & 0 deletions internal/csp/csp.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package csp

import "io"

const (
COMMAND_BEACON byte = 0x71

COMMAND_HIT byte = 0x82
COMMAND_CLAIM byte = 0x84
)

const (
// States
STATE_IDLE byte = iota
STATE_HEADER
STATE_LENGTH
STATE_COMMAND
STATE_DATA
STATE_CHECKSUM
)

type CSP struct {
serial io.ReadWriter
events chan interface{}

state byte
message Message
}

func New(serial io.ReadWriter, events chan interface{}) *CSP {
return &CSP{
serial: serial,
events: events,
}
}

func (csp *CSP) Run() {
buf := make([]byte, 1000)
for {
n, err := csp.serial.Read(buf)
if err != nil {
panic(err)
}
if n == 0 {
continue
}
for i := 0; i < n; i++ {
b := buf[i]
switch csp.state {
case STATE_IDLE:
if b == '$' {
csp.message.header[0] = b
csp.state = STATE_HEADER
}
case STATE_HEADER:
if b == 'C' {
csp.message.header[1] = b
csp.state = STATE_LENGTH
} else {
csp.state = STATE_IDLE
}
case STATE_LENGTH:
csp.message.length = b
csp.message.checksum = b
csp.state = STATE_COMMAND
case STATE_COMMAND:
csp.message.command = b
csp.message.checksum ^= b
csp.state = STATE_DATA
case STATE_DATA:
csp.message.data = append(csp.message.data, b)
csp.message.checksum ^= b
if len(csp.message.data) == int(csp.message.length) {
csp.state = STATE_CHECKSUM
}
case STATE_CHECKSUM:
if csp.message.checksum == b {
csp.emitEvent()
}
csp.message = Message{}
csp.state = STATE_IDLE
}
}
}
}

func (csp *CSP) emitEvent() {
switch csp.message.command {
case COMMAND_BEACON:
csp.events <- NewBeacon(csp.message.data)
case COMMAND_HIT:
csp.events <- NewHit(csp.message.data)
case COMMAND_CLAIM:
csp.events <- NewClaim(csp.message.data)
}
}
13 changes: 13 additions & 0 deletions internal/csp/hit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package csp

type Hit struct {
PlayerID byte
Lives byte
}

func NewHit(data []byte) Hit {
return Hit{
PlayerID: data[0],
Lives: data[1],
}
}
9 changes: 9 additions & 0 deletions internal/csp/message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package csp

type Message struct {
header [2]byte // '$' + 'C'
length byte // Length of the data
command byte // 0x82 = Claim, 0x83 = Hit
data []byte // Data
checksum byte // XOR of all bytes from length to the end of data
}
Loading

0 comments on commit 765cc6e

Please sign in to comment.