Skip to content

Commit

Permalink
fix: market duplicate
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangliang committed Jan 10, 2022
1 parent 1dbbdfa commit ccc30ce
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 17 deletions.
6 changes: 3 additions & 3 deletions examples/hello/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import (
_ "embed"
"github.com/LilithGames/agent-go/pkg/agent"
"encoding/json"
"fmt"
"github.com/LilithGames/agent-go/pkg/agent"
"github.com/hasura/go-graphql-client"
"github.com/magicsea/behavior3go"
"github.com/magicsea/behavior3go/core"
Expand Down Expand Up @@ -49,7 +49,7 @@ func HelloA(tick agent.Ticker) (behavior3go.Status, error) {
one := tick.Marget().InviteOne()
if one == nil {
p = NewPlayer(xid.New().String())
tick.Marget().UseOne(p)
tick.Marget().UseOne(p)
fmt.Println("new player id", p.ID())
} else {
p = one.(*Player)
Expand Down Expand Up @@ -150,4 +150,4 @@ func NewPlayer(id string) *Player {

func (p *Player) ID() string {
return p.id
}
}
6 changes: 5 additions & 1 deletion examples/hello/task.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
plans:

- treeName: subHello
robotNum: 2
robotNum: 1
parallel: 1

- treeName: subHello
robotNum: 3
parallel: 1

environments:
Expand Down
4 changes: 2 additions & 2 deletions pkg/agent/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"go.opentelemetry.io/otel/metric/global"
)

var hc = metric.Must(global.Meter("agent")).NewInt64Counter("agent_stress_handler_outcome")
var hc = metric.Must(global.Meter("agent")).NewInt64Counter("agent_stress_handler_outcome")

type Handler func(ticker Ticker) (behavior3go.Status, error)

Expand Down Expand Up @@ -43,7 +43,7 @@ func (n *Action) OnTick(ticker core.Ticker) behavior3go.Status {
if err != nil {
outcome.Err = err.Error()
}

outcome.Name = name
if start.UnixNano() < tick.recvTime {
outcome.Consume = tick.sendTime - tick.recvTime
Expand Down
2 changes: 1 addition & 1 deletion pkg/agent/robot.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"go.opentelemetry.io/otel/metric/global"
)

var oc = metric.Must(global.Meter("agent")).NewInt64Counter("agent_stress_task_outcome")
var oc = metric.Must(global.Meter("agent")).NewInt64Counter("agent_stress_task_outcome")

type job struct {
statPID *actor.PID
Expand Down
24 changes: 14 additions & 10 deletions pkg/agent/tick.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,16 @@ type Market struct {
idx int64
hub chan One
amount int
roster []One
roster map[string]One
}

func newMarket(amount int) *Market {
amount = amount * 2
return &Market{amount: amount, hub: make(chan One, amount)}
return &Market{
amount: amount,
hub: make(chan One, amount),
roster: make(map[string]One),
}
}

func (h *Market) PushOne(one One) {
Expand Down Expand Up @@ -52,7 +56,7 @@ func (h *Market) JoinOne(one One) {
}

func (h *Market) UseOne(one One) {
h.roster = append(h.roster, one)
h.roster[one.ID()] = one
}

func (h *Market) InviteOne() One {
Expand All @@ -69,7 +73,7 @@ func (h *Market) reset() {
for _, o := range h.roster {
h.hub <- o
}
h.roster = make([]One, 0)
h.roster = map[string]One{}
}

func (h *Market) Index() int {
Expand All @@ -88,12 +92,12 @@ type Ticker interface {

type Tick struct {
core.Tick
market *Market
ctx context.Context
market *Market
ctx context.Context
actorRootContext *actor.RootContext
statPID *actor.PID
recvTime int64
sendTime int64
statPID *actor.PID
recvTime int64
sendTime int64
}

func NewTick() *Tick {
Expand Down Expand Up @@ -148,4 +152,4 @@ func strToInt64(s string) int64 {
return 0
}
return v
}
}

0 comments on commit ccc30ce

Please sign in to comment.