Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
namrapatel committed Jul 31, 2023
1 parent d6f0a2a commit dd1eeeb
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cardinal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ func main() {
))

// Register the systems
world.AddSystem(systems.AddPlayerSystem)
world.AddSystem(systems.MoveSystem)
world.AddSystem(systems.ProcessMovesSystem)
world.AddSystem(systems.AddPlayerSystem)
world.AddSystem(systems.RemovePlayerSystem)
world.AddSystem(systems.SpawnCoinsSystem)

Expand Down
8 changes: 6 additions & 2 deletions cardinal/read/read_player_coins.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package read

import (
"encoding/json"
"errors"
"github.com/argus-labs/new-game/components"
"github.com/argus-labs/new-game/game"
"github.com/argus-labs/new-game/types"
Expand Down Expand Up @@ -52,8 +51,13 @@ func readPlayerCoins(world *ecs.World, m []byte) ([]byte, error) {
}
}
if foundPlayer == false {
// TODO: put the errors back in
log.Error().Msg("ReadPlayerCoins: Player with given name not found.")
return nil, errors.New("ReadPlayerCoins: Player with given name not found")
coins := make([]types.NearbyCoin, 0)
var returnMsg []byte
returnMsg, err = json.Marshal(coins)
return returnMsg, nil
//return nil, errors.New("ReadPlayerCoins: Player with given name not found")
}

// Get the Player's Component
Expand Down
33 changes: 31 additions & 2 deletions cardinal/read/read_player_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package read

import (
"encoding/json"
"fmt"
"github.com/argus-labs/new-game/components"
"github.com/argus-labs/new-game/types"
"github.com/argus-labs/world-engine/cardinal/ecs"
"github.com/argus-labs/world-engine/cardinal/ecs/storage"
"github.com/rs/zerolog/log"
Expand Down Expand Up @@ -37,8 +37,37 @@ func readPlayerState(world *ecs.World, m []byte) ([]byte, error) {
}
}
if foundPlayer == false {
// TODO: put these errors back in
log.Error().Msgf("ReadPlayerState: Player with name %s not found", msg.PlayerName)
return nil, fmt.Errorf("ReadPlayerState: Player with name %s not found", msg.PlayerName)
tempPlayer := &components.PlayerComponent{
Name: msg.PlayerName,
Health: 100,
Coins: 0,
Weapon: 0,
Loc: types.Pair[float64, float64]{
First: -1,
Second: -1,
},
Dir: types.Pair[float64, float64]{
First: 0,
Second: 0,
},
LastMove: types.Pair[float64, float64]{
First: 0,
Second: 0,
},
Extract: types.Pair[float64, float64]{
First: 0,
Second: 0,
},
IsRight: true,
MoveNum: 0,
}

var returnMsg []byte
returnMsg, err = json.Marshal(tempPlayer)
return returnMsg, nil
//return returnMsg, fmt.Errorf("ReadPlayerState: Player with name %s not found", msg.PlayerName)
}

// Get the Player's Component
Expand Down
34 changes: 23 additions & 11 deletions cardinal/systems/move_player.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ package systems
import (
"fmt"
"github.com/argus-labs/new-game/components"
"github.com/argus-labs/new-game/read"
"github.com/argus-labs/new-game/tx"
"github.com/argus-labs/world-engine/cardinal/ecs/storage"
"math"
"strconv"

"github.com/argus-labs/new-game/game"
"github.com/argus-labs/new-game/types"
Expand All @@ -22,7 +25,7 @@ func diff(a, b bool) float64 {
}

// adjusts player directions based on their movement
func MoveSystem(World *ecs.World, q *ecs.TransactionQueue) error {
func MoveSystem(world *ecs.World, q *ecs.TransactionQueue) error {
// playerId -> Move Directions Struct mapping
moveMap := make(map[string][]msg.MovePlayerMsg)

Expand All @@ -36,17 +39,26 @@ func MoveSystem(World *ecs.World, q *ecs.TransactionQueue) error {
}

for id, moveList := range moveMap {
entityID, contains := game.Players[id]
var contains bool = false
var entityID storage.EntityID
playerPairs := read.ReadPlayers(world)
for _id, _comp := range playerPairs {
strId := strconv.Itoa(_id)
if id == strId {
contains = true
entityID = _comp.ID
}
}

// Check if the player making the move is registered
if !contains {
str := ""

for key, _ := range game.Players {
str += " " + key
}

return fmt.Errorf("Cardinal: unregistered player attempting to move " + str)
//str := ""
//
//for key, _ := range game.Players {
// str += " " + key
//}
//continue
return fmt.Errorf("Cardinal: unregistered player attempting to move ")
}

var dir types.Pair[float64, float64]
Expand Down Expand Up @@ -74,7 +86,7 @@ func MoveSystem(World *ecs.World, q *ecs.TransactionQueue) error {
}

// Update the player's direction in their PlayerComponent on Cardinal
components.Player.Update(World, entityID, func(comp components.PlayerComponent) components.PlayerComponent {
components.Player.Update(world, entityID, func(comp components.PlayerComponent) components.PlayerComponent {
comp.Dir = dir // Adjust the player's move directions
comp.MoveNum = moveList[len(moveList)-1].InputSequenceNumber // Set the player's latest input sequence number
comp.LastMove = lastMove // Update the player's last movement
Expand All @@ -93,7 +105,7 @@ func MoveSystem(World *ecs.World, q *ecs.TransactionQueue) error {
continue
}

components.Player.Update(World, entityID, func(comp components.PlayerComponent) components.PlayerComponent {
components.Player.Update(world, entityID, func(comp components.PlayerComponent) components.PlayerComponent {
comp.Dir = comp.LastMove

return comp
Expand Down
2 changes: 1 addition & 1 deletion nakama/local.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
logger:
level: DEBUG
level: INFO
14 changes: 9 additions & 5 deletions nakama/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (m *Match) MatchJoinAttempt(ctx context.Context, logger runtime.Logger, db
}

func (m *Match) MatchJoin(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, dispatcher runtime.MatchDispatcher, tick int64, state interface{}, presences []runtime.Presence) interface{} {
time.Sleep(100 * time.Millisecond)
if presences == nil {
return fmt.Errorf("Nakama: no presence exists in MatchJoin")
}
Expand Down Expand Up @@ -235,18 +236,21 @@ func (m *Match) MatchLoop(ctx context.Context, logger runtime.Logger, db *sql.DB
playerState, err := CallRPCs["read-player-state"](ctx, logger, db, nk, "{\"player_name\":\""+pp.GetUserId()+"\"}")

if err != nil { // assume that an error here means the player is dead
kickList = append(kickList, pp.GetUserId())
//kickList = append(kickList, pp.GetUserId()) TODO: put this error back in
logger.Info("Error with ReadPlayerState")
} else { // send everyone player state & send player its nearby coins
err = dispatcher.BroadcastMessage(LOCATION, []byte(playerState), nil, nil, true) // idk what the boolean is for the last argument of BroadcastMessage, but it isn't listed in the docs

if err != nil {
return err
}

nearbyCoins, err := CallRPCs["read-player-coins"](ctx, logger, db, nk, "{\"player_coins\":\""+pp.GetUserId()+"\"}")
nearbyCoins, err := CallRPCs["read-player-coins"](ctx, logger, db, nk, "{\"player_name\":\""+pp.GetUserId()+"\"}")

if err != nil {
return err
// TODO: put this error back in
//return err
logger.Info("Error with ReadPlayerCoins")
}

err = dispatcher.BroadcastMessage(COINS, []byte(nearbyCoins), []runtime.Presence{pp}, nil, true)
Expand Down Expand Up @@ -296,7 +300,7 @@ func makeTxEndpoint(currEndpoint string, makeURL func(string) string) func(conte
return func(ctx context.Context, logger runtime.Logger, db *sql.DB, nk runtime.NakamaModule, payload string) (string, error) {

signedPayload := &sign.SignedPayload{
PersonaTag: "bullshit",
PersonaTag: "sender's tag",
Namespace: "0",
Nonce: 1000,
Signature: "pk",
Expand All @@ -305,7 +309,7 @@ func makeTxEndpoint(currEndpoint string, makeURL func(string) string) func(conte

logger.Debug("Got request for %q, payload: %s", currEndpoint, payload)
payloadStr, err := json.Marshal(signedPayload)
logger.Debug("PayloadStr: %v ", payloadStr)
//logger.Debug("PayloadStr: %v ", payloadStr)
readerStr := strings.NewReader(string(payloadStr))
logger.Debug("string from Reader: %s", readerStr)

Expand Down

0 comments on commit dd1eeeb

Please sign in to comment.