Skip to content

Commit

Permalink
Merge pull request #3 from Pixel-Tactics/refactor/structure
Browse files Browse the repository at this point in the history
refactor: change match folder structure
  • Loading branch information
Emyr298 authored Jun 26, 2024
2 parents d5eef93 + 4c684ca commit 1b9b94b
Show file tree
Hide file tree
Showing 29 changed files with 551 additions and 394 deletions.
4 changes: 2 additions & 2 deletions src/handlers/session_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,15 @@ func (handler *SessionHandler) CreateSession(req *types.Request, res *types.Resp
},
})

if !session.GetRunning() {
if !session.GetRunningSync() {
res.NotifyOtherClient(body.OpponentId, &types.Message{
Action: types.ACTION_INVITE_SESSION,
Body: map[string]interface{}{
"playerId": body.PlayerId,
},
})
} else {
sessionMap := session.GetData()
sessionMap := session.GetDataSync()
res.NotifyOtherClient(body.OpponentId, &types.Message{
Action: types.ACTION_START_SESSION,
Body: map[string]interface{}{
Expand Down
116 changes: 0 additions & 116 deletions src/matches/actions.go

This file was deleted.

68 changes: 68 additions & 0 deletions src/matches/actions/attack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package matches_actions

import (
"errors"

matches_algorithms "pixeltactics.com/match/src/matches/algorithms"
matches_constants "pixeltactics.com/match/src/matches/constants"
matches_interfaces "pixeltactics.com/match/src/matches/interfaces"
)

type AttackLog struct {
srcHero matches_interfaces.IHero
trgHero matches_interfaces.IHero
damage int
}

type AttackLogData struct {
HeroName string `json:"heroName"`
PlayerId string `json:"playerId"`
TargetName string `json:"targetName"`
}

func (log *AttackLog) Apply(session matches_interfaces.ISession) error {
if !log.srcHero.CanAttack() {
return errors.New("hero cannot attack")
}

attackRange := log.srcHero.GetBaseStats().AttackRange
damage := log.srcHero.GetBaseStats().Damage

dist, err := matches_algorithms.CheckDistance(session.GetMatchMap().Structure, log.srcHero.GetPos(), log.trgHero.GetPos())
if dist > attackRange || err != nil {
return errors.New("target out of range")
}

log.damage = damage
log.srcHero.SetLastAttackTurn(session.GetCurrentTurn())
log.trgHero.SetHealth(max(log.trgHero.GetHealth()-damage, 0))
return nil
}

func (log *AttackLog) GetSourcePlayerId() string {
return log.srcHero.GetPlayer().GetId()
}

func (log *AttackLog) GetData() map[string]interface{} {
return map[string]interface{}{
"hero": log.srcHero.GetName(),
"target": log.trgHero.GetName(),
"playerId": log.srcHero.GetPlayer().GetId(),
"damage": log.damage,
}
}

func (log *AttackLog) GetName() string {
return matches_constants.ATTACK_LOG
}

func (log *AttackLog) GetSourceHero() matches_interfaces.IHero {
return log.srcHero
}

func NewAttackLog(srcHero matches_interfaces.IHero, trgHero matches_interfaces.IHero) *AttackLog {
return &AttackLog{
srcHero: srcHero,
trgHero: trgHero,
}
}
74 changes: 74 additions & 0 deletions src/matches/actions/move.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package matches_actions

import (
"errors"

"pixeltactics.com/match/src/exceptions"
matches_interfaces "pixeltactics.com/match/src/matches/interfaces"
matches_physics "pixeltactics.com/match/src/matches/physics"
)

type MoveLog struct {
srcHero matches_interfaces.IHero
directionList []string
}

type MoveLogData struct {
HeroName string `json:"heroName"`
PlayerId string `json:"playerId"`
DirectionList []string `json:"directionList"`
}

func (log *MoveLog) Apply(session matches_interfaces.ISession) error {
if !log.srcHero.CanMove() {
return errors.New("hero already moved this turn")
}

if log.srcHero.GetHealth() == 0 {
return exceptions.HeroIsDead()
}

if len(log.directionList) > log.srcHero.GetBaseStats().MoveRange {
return errors.New("invalid movement range")
}

curPos := log.srcHero.GetPos()
for _, dir := range log.directionList {
dirPoint := matches_physics.GetPointFromDirection(dir)
curPos = curPos.Add(dirPoint)
if !session.IsPointOpen(curPos) {
return errors.New("point is occupied")
}
}

log.srcHero.SetPos(curPos)
log.srcHero.SetLastMoveTurn(session.GetCurrentTurn())
return nil
}

func (log *MoveLog) GetSourcePlayerId() string {
return log.srcHero.GetPlayer().GetId()
}

func (log *MoveLog) GetData() map[string]interface{} {
return map[string]interface{}{
"hero": log.srcHero.GetName(),
"playerId": log.srcHero.GetPlayer().GetId(),
"directionList": log.directionList,
}
}

func (log *MoveLog) GetName() string {
return "move"
}

func (log *MoveLog) GetSourceHero() matches_interfaces.IHero {
return log.srcHero
}

func NewMoveLog(hero matches_interfaces.IHero, directionList []string) *MoveLog {
return &MoveLog{
srcHero: hero,
directionList: directionList,
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package matches
package matches_algorithms

import (
"errors"

llq "github.com/emirpasic/gods/queues/linkedlistqueue"

physics "pixeltactics.com/match/src/matches/physics"
)

type BFSElement struct {
point Point
point physics.Point
dist int
}

func checkDistanceValidity(mp [][]int, src Point, dest Point) error {
func checkDistanceValidity(mp [][]int, src physics.Point, dest physics.Point) error {
if len(mp) == 0 || len(mp[0]) == 0 {
return errors.New("invalid map structure")
}
Expand Down Expand Up @@ -44,12 +46,12 @@ func enqueueIfAvailable(queue *llq.Queue, mp [][]int, visited [][]bool, x int, y

visited[y][x] = true
queue.Enqueue(BFSElement{
point: Point{X: x, Y: y},
point: physics.Point{X: x, Y: y},
dist: dist,
})
}

func CheckDistance(mp [][]int, src Point, dest Point) (int, error) {
func CheckDistance(mp [][]int, src physics.Point, dest physics.Point) (int, error) {
err := checkDistanceValidity(mp, src, dest)
if err != nil {
return 0, nil
Expand Down
6 changes: 6 additions & 0 deletions src/matches/constants/action_constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package matches_constants

const (
ATTACK_LOG = "attack"
MOVE_LOG = "move"
)
58 changes: 0 additions & 58 deletions src/matches/heroes.go

This file was deleted.

Loading

0 comments on commit 1b9b94b

Please sign in to comment.