Skip to content

Commit

Permalink
server|client|store: Created a multiplayer server
Browse files Browse the repository at this point in the history
Abstracted the most important stores from the client so they can be reused on the server.

Right now not all stores are on the server but just the ones that make sense to be
  • Loading branch information
xescugc committed Sep 26, 2023
1 parent 6560313 commit 21e8197
Show file tree
Hide file tree
Showing 31 changed files with 1,081 additions and 422 deletions.
157 changes: 132 additions & 25 deletions action/action.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package action

import (
"github.com/gorilla/websocket"
"github.com/xescugc/ltw/utils"
)

type Action struct {
Type Type `json:"type"`
Type Type `json:"type"`
Room string `json:"room"`

CursorMove *CursorMovePayload `json:"cursor_move,omitempty"`
SummonUnit *SummonUnitPayload `json:"summon_unit,omitempty"`
Expand All @@ -10,10 +16,14 @@ type Action struct {
CameraZoom *CameraZoomPayload `json:"camera_zoom,omitempty"`
SelectTower *SelectTowerPayload `json:"select_tower,omitempty"`
PlaceTower *PlaceTowerPayload `json:"place_tower,omitempty"`
SelectedTowerInvalid *SelectedTowerInvalidPayload `json:"selected_tower_invalid",omitempty"`
TowerAttack *TowerAttackPayload `json:"tower_attack",omitempty`
UnitKilled *UnitKilledPayload `json:"unit_killed",omitempty`
WindowResizing *WindowResizingPayload `json:"window_resizing",omitempty"`
SelectedTowerInvalid *SelectedTowerInvalidPayload `json:"selected_tower_invalid,omitempty"`
TowerAttack *TowerAttackPayload `json:"tower_attack,omitempty"`
UnitKilled *UnitKilledPayload `json:"unit_killed,omitempty"`
WindowResizing *WindowResizingPayload `json:"window_resizing,omitempty"`

AddPlayer *AddPlayerPayload `json:"add_player, omitempty"`
JoinRoom *JoinRoomPayload `json:"join_room, omitempty"`
UpdateState *UpdateStatePayload `json:"update_state, omitempty"`
}

type CursorMovePayload struct {
Expand All @@ -33,12 +43,12 @@ func NewCursorMove(x, y int) *Action {

type SummonUnitPayload struct {
Type string
PlayerID int
PlayerID string
PlayerLineID int
CurrentLineID int
}

func NewSummonUnit(t string, pid, plid, clid int) *Action {
func NewSummonUnit(t, pid string, plid, clid int) *Action {
return &Action{
Type: SummonUnit,
SummonUnit: &SummonUnitPayload{
Expand All @@ -57,10 +67,10 @@ func NewMoveUnit() *Action {
}

type RemoveUnitPayload struct {
UnitID int
UnitID string
}

func NewRemoveUnit(uid int) *Action {
func NewRemoveUnit(uid string) *Action {
return &Action{
Type: RemoveUnit,
RemoveUnit: &RemoveUnitPayload{
Expand All @@ -70,11 +80,11 @@ func NewRemoveUnit(uid int) *Action {
}

type StealLivePayload struct {
FromPlayerID int
ToPlayerID int
FromPlayerID string
ToPlayerID string
}

func NewStealLive(fpid, tpid int) *Action {
func NewStealLive(fpid, tpid string) *Action {
return &Action{
Type: StealLive,
StealLive: &StealLivePayload{
Expand All @@ -98,20 +108,20 @@ func NewCameraZoom(d int) *Action {
}

type PlaceTowerPayload struct {
Type string
LineID int
X int
Y int
Type string
PlayerID string
X int
Y int
}

func NewPlaceTower(t string, x, y, lid int) *Action {
func NewPlaceTower(t, pid string, x, y int) *Action {
return &Action{
Type: PlaceTower,
PlaceTower: &PlaceTowerPayload{
Type: t,
LineID: lid,
X: x,
Y: y,
Type: t,
PlayerID: pid,
X: x,
Y: y,
},
}
}
Expand Down Expand Up @@ -159,10 +169,10 @@ func NewIncomeTick() *Action {
}

type TowerAttackPayload struct {
UnitID int
UnitID string
}

func NewTowerAttack(uid int) *Action {
func NewTowerAttack(uid string) *Action {
return &Action{
Type: TowerAttack,
TowerAttack: &TowerAttackPayload{
Expand All @@ -172,11 +182,11 @@ func NewTowerAttack(uid int) *Action {
}

type UnitKilledPayload struct {
PlayerID int
PlayerID string
UnitType string
}

func NewUnitKilled(pid int, ut string) *Action {
func NewUnitKilled(pid, ut string) *Action {
return &Action{
Type: UnitKilled,
UnitKilled: &UnitKilledPayload{
Expand All @@ -200,3 +210,100 @@ func NewWindowResizing(w, h int) *Action {
},
}
}

type JoinRoomPayload struct {
Room string
Name string
}

func NewJoinRoom(room, name string) *Action {
return &Action{
Type: JoinRoom,
JoinRoom: &JoinRoomPayload{
Room: room,
Name: name,
},
}
}

type AddPlayerPayload struct {
ID string
Name string
LineID int
Websocket *websocket.Conn
Room string
}

func NewAddPlayer(r, id, name string, lid int, ws *websocket.Conn) *Action {
return &Action{
Type: AddPlayer,
AddPlayer: &AddPlayerPayload{
ID: id,
Name: name,
LineID: lid,
Websocket: ws,
Room: r,
},
}
}

type UpdateStatePayload struct {
Players *UpdateStatePlayersPayload
Towers *UpdateStateTowersPayload
Units *UpdateStateUnitsPayload
}

type UpdateStatePlayersPayload struct {
Players map[string]*UpdateStatePlayerPayload
IncomeTimer int
}

type UpdateStatePlayerPayload struct {
ID string
Name string
Lives int
LineID int
Income int
Gold int
Current bool
}

type UpdateStateTowersPayload struct {
Towers map[string]*UpdateStateTowerPayload
}

type UpdateStateTowerPayload struct {
utils.Object

Type string
LineID int
}

type UpdateStateUnitsPayload struct {
Units map[string]*UpdateStateUnitPayload
}

type UpdateStateUnitPayload struct {
utils.MovingObject

Type string
PlayerID string
PlayerLineID int
CurrentLineID int

Health float64

Path []utils.Step
}

// TODO: or make the action.Action separated or make the store.Player separated
func NewUpdateState(players *UpdateStatePlayersPayload, towers *UpdateStateTowersPayload, units *UpdateStateUnitsPayload) *Action {
return &Action{
Type: UpdateState,
UpdateState: &UpdateStatePayload{
Players: players,
Towers: towers,
Units: units,
},
}
}
5 changes: 5 additions & 0 deletions action/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ const (
TowerAttack
UnitKilled
WindowResizing

// Specific to WS
JoinRoom
AddPlayer
UpdateState
)
20 changes: 16 additions & 4 deletions action/type_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes
18 changes: 18 additions & 0 deletions assets/assets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package assets

import (
_ "embed"
_ "image/png"
)

//go:embed cyclope/Faceset.png
var CyclopeFaceset_png []byte

//go:embed TilesetHouse.png
var TilesetHouse_png []byte

//go:embed cyclope/Cyclopes.png
var Cyclopes_png []byte

//go:embed maps/1v1.png
var M_1v1_png []byte
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes
File renamed without changes
Loading

0 comments on commit 21e8197

Please sign in to comment.