Skip to content

Commit

Permalink
fix(#267): Added server side fight object
Browse files Browse the repository at this point in the history
  • Loading branch information
lxgr-linux committed Oct 10, 2024
1 parent d7aec13 commit a93374e
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 17 deletions.
10 changes: 9 additions & 1 deletion pkg/server/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/lxgr-linux/pokete/bs_rpc/msg"
"github.com/lxgr-linux/pokete/server/config"
"github.com/lxgr-linux/pokete/server/options"
"github.com/lxgr-linux/pokete/server/pokete/fight"
"github.com/lxgr-linux/pokete/server/pokete/user"
"github.com/lxgr-linux/pokete/server/resources"
)
Expand Down Expand Up @@ -36,9 +37,10 @@ const (
contextKey_id
contextKey_positions
contextKey_options
contextKey_fights
)

func PoketeContext(users Users, r *resources.Resources, config *config.Config, client *bs_rpc.Client, connectionId uint64, positions Positions, options *options.Options) context.Context {
func PoketeContext(users Users, r *resources.Resources, fights *fight.Fights, config *config.Config, client *bs_rpc.Client, connectionId uint64, positions Positions, options *options.Options) context.Context {
ctx := context.Background()
ctx = context.WithValue(ctx, contextKey_users, users)
ctx = context.WithValue(ctx, contextKey_resources, r)
Expand All @@ -47,6 +49,7 @@ func PoketeContext(users Users, r *resources.Resources, config *config.Config, c
ctx = context.WithValue(ctx, contextKey_id, connectionId)
ctx = context.WithValue(ctx, contextKey_positions, positions)
ctx = context.WithValue(ctx, contextKey_options, options)
ctx = context.WithValue(ctx, contextKey_fights, fights)

return ctx
}
Expand Down Expand Up @@ -85,3 +88,8 @@ func OptionsFromContext(ctx context.Context) (*options.Options, bool) {
o, ok := ctx.Value(contextKey_options).(*options.Options)
return o, ok
}

func FightsFromContext(ctx context.Context) (*fight.Fights, bool) {
f, ok := ctx.Value(contextKey_options).(*fight.Fights)
return f, ok
}
13 changes: 13 additions & 0 deletions pkg/server/pokete/fight/fight.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package fight

import "github.com/lxgr-linux/pokete/server/pokete/user"

type Fight struct {
ID FightID
playerA *user.User
playerB *user.User
}

func New(playerA *user.User, playerB *user.User) Fight {
return Fight{playerA: playerA, playerB: playerB}
}
34 changes: 34 additions & 0 deletions pkg/server/pokete/fight/fights.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package fight

import "errors"

var FIGHT_DOESNT_EXIST = errors.New("fight with id doesnt exist")

type FightID uint64

type Fights struct {
currFightID FightID
fights map[FightID]*Fight
}

func (f *Fights) incID() {
f.currFightID += 1
}

func (f *Fights) Add(fight *Fight) {
f.incID()
fight.ID = f.currFightID
f.fights[fight.ID] = fight
}

func (f *Fights) Get(id FightID) (*Fight, error) {
fight, ok := f.fights[id]
if !ok {
return nil, FIGHT_DOESNT_EXIST
}
return fight, nil
}

func NewFights() Fights {
return Fights{0, make(map[FightID]*Fight)}
}
8 changes: 6 additions & 2 deletions pkg/server/pokete/msg/fight/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package fight
import (
"context"
"fmt"
"log/slog"

"github.com/lxgr-linux/pokete/bs_rpc/msg"
pctx "github.com/lxgr-linux/pokete/server/context"
"github.com/lxgr-linux/pokete/server/pokete/fight"
error2 "github.com/lxgr-linux/pokete/server/pokete/msg/error"
"log/slog"
)

const RequestType msg.Type = "pokete.fight.request"
Expand All @@ -27,6 +29,7 @@ func NewRequest(name string) Request {
func (r Request) CallForResponse(ctx context.Context) (msg.Body, error) {
u, _ := pctx.UsersFromContext(ctx)
conId, _ := pctx.ConnectionIdFromContext(ctx)
fights, _ := pctx.FightsFromContext(ctx)

slog.Info("Received request")

Expand All @@ -49,7 +52,8 @@ func (r Request) CallForResponse(ctx context.Context) (msg.Body, error) {
switch resp.GetType() {
case ResponseType:
dataResp := resp.(Response)
// Add fight object here
f := fight.New(attacker, enemy)
fights.Add(&f)
return dataResp, nil
default:
slog.Warn("big uff")
Expand Down
9 changes: 5 additions & 4 deletions pkg/server/pokete/msg/handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package msg
import (
"context"
"fmt"
"github.com/lxgr-linux/pokete/server/pokete/fight"

"github.com/lxgr-linux/pokete/server/pokete/poke"
"github.com/lxgr-linux/pokete/server/pokete/troll"

"github.com/lxgr-linux/pokete/server/pokete/msg/map_info"
Expand All @@ -20,9 +21,9 @@ const HandshakeType msg.Type = "pokete.handshake"

type Handshake struct {
msg.BaseMsg
UserName string `json:"user_name"`
Version string `json:"version"`
Pokes []fight.Poke `json:"pokes"`
UserName string `json:"user_name"`
Version string `json:"version"`
Pokes []poke.Instance `json:"pokes"`
}

func (h Handshake) GetType() msg.Type {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package fight
package poke

type Nature struct {
Nature string `json:"nature"`
Grade uint `json:"grade"`
}

type Poke struct {
type Instance struct {
Name string `json:"name"`
Xp uint32 `json:"xp"`
Hp uint32 `json:"hp"`
Expand Down
4 changes: 4 additions & 0 deletions pkg/server/pokete/pokete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pokete
import (
"github.com/lxgr-linux/pokete/server/config"
"github.com/lxgr-linux/pokete/server/options"
"github.com/lxgr-linux/pokete/server/pokete/fight"
"github.com/lxgr-linux/pokete/server/pokete/positions"
"github.com/lxgr-linux/pokete/server/pokete/users"
"github.com/lxgr-linux/pokete/server/resources"
Expand All @@ -14,6 +15,7 @@ type Pokete struct {
users *users.Users
options *options.Options
positions *positions.Positions
fights *fight.Fights
}

func New(config *config.Config, resources *resources.Resources, opts ...options.Func) (*Pokete, error) {
Expand All @@ -22,12 +24,14 @@ func New(config *config.Config, resources *resources.Resources, opts ...options.
return nil, err
}
p := positions.NewPositions()
f := fight.NewFights()

return &Pokete{
config: config,
resources: resources,
users: users.NewUsers(p),
options: o,
positions: p,
fights: &f,
}, nil
}
7 changes: 4 additions & 3 deletions pkg/server/pokete/troll/poke.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package troll

import (
"fmt"

"github.com/lxgr-linux/pokete/resources/base"
"github.com/lxgr-linux/pokete/server/pokete/fight"
"github.com/lxgr-linux/pokete/server/pokete/poke"
)

func CheckPokes(protos map[string]base.Poke, pokes []fight.Poke) error {
func CheckPokes(protos map[string]base.Poke, pokes []poke.Instance) error {
for _, poke := range pokes {
err := CheckPoke(protos, poke)
if err != nil {
Expand All @@ -16,7 +17,7 @@ func CheckPokes(protos map[string]base.Poke, pokes []fight.Poke) error {
return nil
}

func CheckPoke(pokes map[string]base.Poke, poke fight.Poke) error {
func CheckPoke(pokes map[string]base.Poke, poke poke.Instance) error {
proto, ok := pokes[poke.Name]
if !ok {
return fmt.Errorf("poke %s not found", poke.Name)
Expand Down
10 changes: 5 additions & 5 deletions pkg/server/pokete/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package user

import (
"github.com/lxgr-linux/pokete/bs_rpc"
"github.com/lxgr-linux/pokete/server/pokete/fight"
"github.com/lxgr-linux/pokete/server/pokete/poke"
)

type User struct {
Name string `json:"name"`
Position Position `json:"position"`
Client *bs_rpc.Client `json:"client"` // TODO: Maybe remove
Pokes []fight.Poke `json:"pokes"`
Name string `json:"name"`
Position Position `json:"position"`
Client *bs_rpc.Client `json:"client"` // TODO: Maybe remove
Pokes []poke.Instance `json:"pokes"`
}

0 comments on commit a93374e

Please sign in to comment.