Skip to content

Commit

Permalink
trace + demo mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ysoldak committed Jul 10, 2024
1 parent 2e777ea commit b8e18bc
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@
# Go workspace file
go.work
build

*.csv
86 changes: 66 additions & 20 deletions cmd/lady/comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package main
import (
"fmt"
"io"
"os"
"strings"
"time"

"github.com/urfave/cli/v2"
"github.com/ysoldak/fpvc-lady/internal/game"
Expand All @@ -23,9 +24,9 @@ func commentAction(cc *cli.Context) (err error) {
go ttsEngine.Run()

// Serial
serial := io.ReadWriter(os.Stdin)
var serial io.ReadWriter
port := cc.String(flagPort)
if port != "none" {
if port != "demo" {
serial, err = utils.NewSerial(port)
if err != nil {
return err
Expand All @@ -34,52 +35,97 @@ func commentAction(cc *cli.Context) (err error) {

// CSP
messageChan := make(chan csp.Message, 10)
listener := net.NewListener(serial, messageChan)
go listener.Run()
if serial != nil {
listener := net.NewListener(serial, messageChan)
go listener.Run()
} else {
go demo(messageChan)
}

// Game
g := game.NewGame()

// Trace
go trace()

// Main loop
fmt.Println()
fmt.Println("The lady is ready.")
fmt.Println()
fmt.Println("Listening to combat events...")
fmt.Println()

speakerChan <- "The lady is ready."
for {
message := <-messageChan
traceChan <- "START"

for message := range messageChan {

switch message.Command {
case csp.CommandBeacon:
event := csp.NewBeaconFromMessage(&message)
player, new := g.Beacon(event)
if new {
speakerChan <- fmt.Sprintf("%s registered", player.Name)
traceChan <- fmt.Sprintf("REG %02X %s", player.ID, strings.ReplaceAll(player.Name, " ", "_"))
}
case csp.CommandHit:
if message.IsRequest() {
event := csp.NewHitRequestFromMessage(&message)
traceChan <- fmt.Sprintf("HIT %02X %d", event.ID, event.Lives)
g.HitRequest(event)
phrase := fmt.Sprintf("%s was hit.", g.Victim.Name)
if cc.Bool(flagSpeakLives) {
phrase += fmt.Sprintf(" %d lives left.", g.Victim.Lives)
}
speakerChan <- phrase
}
if message.IsResponse() {
event := csp.NewHitResponseFromMessage(&message)
victim, ok := g.HitResponse(event)
if !ok {
continue
traceChan <- fmt.Sprintf("CLM %02X %d", event.ID, event.Power)
if g.HitResponse(event) {
attacker, _ := g.Player(event.ID)
speakerChan <- fmt.Sprintf("Score to %s.", attacker.Name)
}
attacker, _ := g.Player(event.ID)
phrase := fmt.Sprintf("%s was hit by %s.", victim.Name, attacker.Name)
if cc.Bool(flagSpeakLives) {
phrase += fmt.Sprintf(" %d lives left.", victim.Lives)
}
speakerChan <- phrase
}
}
println()
println()
println()

fmt.Println()
fmt.Println()
fmt.Println()
for _, line := range g.Table() {
println(line)
fmt.Println(line)
}
}

return nil
}

func demo(messageChan chan csp.Message) {
a := csp.NewBeacon(0xA1, "ALICE ", "fake fake fake fake ")
b := csp.NewBeacon(0xB1, "BARTOLOMEO", "fake fake fake fake ")
time.Sleep(2 * time.Second)

messageChan <- *a.Message()
time.Sleep(1 * time.Second)
messageChan <- *b.Message()

time.Sleep(6 * time.Second)

messageChan <- *a.Message()
time.Sleep(1 * time.Second)
messageChan <- *b.Message()

time.Sleep(5 * time.Second)

lives := byte(5)
for {
messageChan <- *csp.NewHitRequest(0xA1, lives).Message()
time.Sleep(100 * time.Millisecond)
messageChan <- *csp.NewHitResponse(0xB1, 3).Message()
time.Sleep(3 * time.Second)
lives--
if lives == 0 {
break
}
}
}
4 changes: 2 additions & 2 deletions cmd/lady/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ func getFlags() []cli.Flag {
},
&cli.StringFlag{
Name: flagSpeak,
Usage: "Speech command: [system], google, none or any other command to convert text to speech.",
EnvVars: []string{"SPEECH"},
Usage: "Text-to-speech command: [system], google, none or any other command to convert text to speech.",
EnvVars: []string{"SPEAK"},
Required: false,
Value: "system",
},
Expand Down
26 changes: 26 additions & 0 deletions cmd/lady/trace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package main

import (
"log"
"os"
)

var traceChan = make(chan string, 10)

func trace() {

filename := "fpvcc-log.csv"
f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Println(err)
}
defer f.Close()

tracer := log.New(f, "", log.Ldate|log.Ltime|log.Lmicroseconds)

for {
event := <-traceChan
tracer.Println(event)
}

}
13 changes: 6 additions & 7 deletions internal/game/game.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@ func (g *Game) Beacon(event *csp.Beacon) (player *Player, new bool) {

func (g *Game) HitRequest(event *csp.HitRequest) {
victim, _ := g.Player(event.ID)
victim.Lives = event.Lives
victim.Lives = event.Lives - 1
victim.Deaths++
g.Victim = victim
}

func (g *Game) HitResponse(event *csp.HitResponse) (victim *Player, ok bool) {
func (g *Game) HitResponse(event *csp.HitResponse) (ok bool) {
attacker, _ := g.Player(event.ID)
if attacker == nil {
attacker = &Player{
Expand All @@ -43,14 +44,12 @@ func (g *Game) HitResponse(event *csp.HitResponse) (victim *Player, ok bool) {
}
g.Players = append(g.Players, attacker)
}
victim = g.Victim
if g.Victim != nil {
g.Victim = nil
attacker.Kills++
victim.Deaths++
victim.Lives--
g.Victim = nil
return true
}
return victim, victim != nil
return false
}

func (g *Game) Player(id byte) (player *Player, isNew bool) {
Expand Down

0 comments on commit b8e18bc

Please sign in to comment.