Skip to content

Commit

Permalink
store/units: The units now are correctly renered
Browse files Browse the repository at this point in the history
No more glitches not performances needed for now. The server is creating the unit and
the client is moving it and checking if any change needs to apply on it
  • Loading branch information
xescugc committed Jan 19, 2024
1 parent 6d62286 commit c7525cb
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 13 deletions.
Binary file modified server/assets/wasm/maze-wars.wasm
Binary file not shown.
12 changes: 6 additions & 6 deletions server/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,28 +173,28 @@ func wsHandler(s *Store) func(http.ResponseWriter, *http.Request) {
}

func startLoop(ctx context.Context, s *Store) {
secondTicker := time.NewTicker(time.Second)
stateTicker := time.NewTicker(time.Second / 4)
incomeTicker := time.NewTicker(time.Second)
// The default TPS on of Ebiten client if 60 so to
// emulate that we trigger the move action every TPS
moveTicker := time.NewTicker(time.Second / 60)
tpsTicker := time.NewTicker(time.Second / 60)
usersTicker := time.NewTicker(5 * time.Second)
for {
select {
case <-stateTicker.C:
actionDispatcher.SyncState(s.Rooms)
case <-incomeTicker.C:
case <-secondTicker.C:
actionDispatcher.IncomeTick(s.Rooms)
actionDispatcher.WaitRoomCountdownTick()
actionDispatcher.SyncWaitingRoom(s.Rooms)
case <-moveTicker.C:
case <-tpsTicker.C:
actionDispatcher.TPS(s.Rooms)
case <-usersTicker.C:
actionDispatcher.SyncUsers(s.Users)
case <-ctx.Done():
stateTicker.Stop()
incomeTicker.Stop()
moveTicker.Stop()
secondTicker.Stop()
tpsTicker.Stop()
usersTicker.Stop()
goto FINISH
}
Expand Down
9 changes: 5 additions & 4 deletions server/rooms.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,16 +179,17 @@ func (rs *RoomsStore) Reduce(state, a interface{}) interface{} {
rs.mxRooms.Lock()
defer rs.mxRooms.Unlock()

if r, ok := rstate.Rooms[act.Room]; ok {
r.Game.Dispatch(act)
}
// If no room means that is a broadcast
if act.Room == "" {
for _, r := range rstate.Rooms {
if r.Name != rstate.CurrentWaitingRoom {
r.Game.Dispatch(act)
go r.Game.Dispatch(act)
}
}
} else {
if r, ok := rstate.Rooms[act.Room]; ok {
go r.Game.Dispatch(act)
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions store/unit_astar.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package store

import (
"container/heap"
"fmt"
"strconv"

"github.com/xescugc/maze-wars/utils"
)
Expand Down Expand Up @@ -121,7 +121,7 @@ func straightPathToEnd(m *Map, lid int, s utils.Step) utils.Object {
}

func calculateObjectKey(o utils.Object) string {
return fmt.Sprintf("%.0f%.0f", o.X, o.Y)
return strconv.FormatFloat(o.X, 'x', -1, 64) + strconv.FormatFloat(o.Y, 'x', -1, 64)
}

type queueItem struct {
Expand Down
13 changes: 12 additions & 1 deletion store/units.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,19 @@ func (us *Units) Reduce(state, a interface{}) interface{} {
for id, u := range act.SyncState.Units.Units {
delete(uids, id)
nu := Unit(*u)

ou, ok := ustate.Units[id]

if ok {
//If the unit already exists and have the same Hash then ignore the server
//coordinates and path
if ou.HashPath == nu.HashPath {
nu.Path = ou.Path
nu.X = ou.X
nu.Y = ou.Y
}
}
ustate.Units[id] = &nu

}
for id := range uids {
delete(ustate.Units, id)
Expand Down

0 comments on commit c7525cb

Please sign in to comment.