Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

action: Add new action to 'RemovePlayer' #27

Merged
merged 1 commit into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ type Action struct {
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"`
AddPlayer *AddPlayerPayload `json:"add_player, omitempty"`
RemovePlayer *RemovePlayerPayload `json:"remove_player, omitempty"`
JoinRoom *JoinRoomPayload `json:"join_room, omitempty"`
UpdateState *UpdateStatePayload `json:"update_state, omitempty"`
}

type CursorMovePayload struct {
Expand Down Expand Up @@ -249,6 +250,21 @@ func NewAddPlayer(r, id, name string, lid int, ws *websocket.Conn) *Action {
}
}

type RemovePlayerPayload struct {
ID string
Room string
}

func NewRemovePlayer(r, id string) *Action {
return &Action{
Type: RemovePlayer,
RemovePlayer: &RemovePlayerPayload{
ID: id,
Room: r,
},
}
}

type UpdateStatePayload struct {
Players *UpdateStatePlayersPayload
Towers *UpdateStateTowersPayload
Expand Down Expand Up @@ -277,8 +293,9 @@ type UpdateStateTowersPayload struct {
type UpdateStateTowerPayload struct {
utils.Object

Type string
LineID int
Type string
LineID int
PlayerID string
}

type UpdateStateUnitsPayload struct {
Expand Down
1 change: 1 addition & 0 deletions action/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ const (
// Specific to WS
JoinRoom
AddPlayer
RemovePlayer
UpdateState
)
20 changes: 12 additions & 8 deletions action/type_string.go

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

8 changes: 7 additions & 1 deletion server/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ func (ac *ActionDispatcher) AddPlayer(sid, id, name string, lid int, ws *websock
ac.dispatcher.Dispatch(npa)
}

func (ac *ActionDispatcher) RemovePlayer(rn, sid string) {
rpa := action.NewRemovePlayer(rn, sid)
rpa.Room = rn
ac.dispatcher.Dispatch(rpa)
}

func (ac *ActionDispatcher) IncomeTick(rooms *RoomsStore) {
ita := action.NewIncomeTick()
ac.dispatcher.Dispatch(ita)
Expand All @@ -55,7 +61,7 @@ func (ac *ActionDispatcher) UpdateState(rooms *RoomsStore) {
if id == idp {
uspp.Current = true
}
players[id] = &uspp
players[idp] = &uspp
}

// Towers
Expand Down
11 changes: 8 additions & 3 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,14 @@ func wsHandler(rooms *RoomsStore) func(http.ResponseWriter, *http.Request) {
// we kick the user
err := ws.ReadJSON(&msg)
if err != nil {
fmt.Printf("Error when reading the WS message: %w\n", err)
// TODO: Remove the player from Room and Players
ws.Close()
fmt.Printf("Error when reading the WS message: %s\n", err)

for rn, r := range rooms.GetState().(RoomsState).Rooms {
if uid, ok := r.Connections[ws.RemoteAddr().String()]; ok {
actionDispatcher.RemovePlayer(rn, uid)
break
}
}
break
}

Expand Down
27 changes: 21 additions & 6 deletions server/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ type RoomsState struct {
type Room struct {
Name string

muCons sync.RWMutex
Players map[string]*websocket.Conn
muPlayers sync.RWMutex
Players map[string]*websocket.Conn

Connections map[string]string

Game *Game
}
Expand Down Expand Up @@ -54,14 +56,27 @@ func (rs *RoomsStore) Reduce(state, a interface{}) interface{} {
rd := flux.NewDispatcher()
if _, ok := rstate.Rooms[act.JoinRoom.Room]; !ok {
rstate.Rooms[act.JoinRoom.Room] = &Room{
Name: act.JoinRoom.Room,
Players: make(map[string]*websocket.Conn),
Game: NewGame(rd),
Name: act.JoinRoom.Room,
Players: make(map[string]*websocket.Conn),
Connections: make(map[string]string),
Game: NewGame(rd),
}
}
case action.AddPlayer:
rstate.Rooms[act.AddPlayer.Room].Players[act.AddPlayer.ID] = act.AddPlayer.Websocket
fallthrough
rstate.Rooms[act.AddPlayer.Room].Connections[act.AddPlayer.Websocket.RemoteAddr().String()] = act.AddPlayer.ID

rstate.Rooms[act.Room].Game.Dispatch(act)
case action.RemovePlayer:
ws := rstate.Rooms[act.RemovePlayer.Room].Players[act.RemovePlayer.ID]
delete(rstate.Rooms[act.RemovePlayer.Room].Players, act.RemovePlayer.ID)
delete(rstate.Rooms[act.RemovePlayer.Room].Connections, ws.RemoteAddr().String())

rstate.Rooms[act.Room].Game.Dispatch(act)

if len(rstate.Rooms[act.Room].Players) == 0 {
delete(rstate.Rooms, act.Room)
}
default:
if r, ok := rstate.Rooms[act.Room]; ok {
r.Game.Dispatch(act)
Expand Down
14 changes: 12 additions & 2 deletions store/players.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ func (ps *Players) Reduce(state, a interface{}) interface{} {
Income: 25,
Gold: 40,
}
case action.RemovePlayer:
delete(pstate.Players, act.RemovePlayer.ID)
case action.StealLive:
fp := pstate.Players[act.StealLive.FromPlayerID]
fp.Lives -= 1
Expand All @@ -99,9 +101,17 @@ func (ps *Players) Reduce(state, a interface{}) interface{} {
case action.UnitKilled:
pstate.Players[act.UnitKilled.PlayerID].Gold += unit.Units[act.UnitKilled.UnitType].Income
case action.UpdateState:
for _, p := range act.UpdateState.Players.Players {
pids := make(map[string]struct{})
for id := range pstate.Players {
pids[id] = struct{}{}
}
for id, p := range act.UpdateState.Players.Players {
delete(pids, id)
np := Player(*p)
pstate.Players[p.ID] = &np
pstate.Players[id] = &np
}
for id := range pids {
delete(pstate.Players, id)
}
pstate.IncomeTimer = act.UpdateState.Players.IncomeTimer
}
Expand Down
24 changes: 20 additions & 4 deletions store/towers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ type TowersState struct {
type Tower struct {
utils.Object

Type string
LineID int
Type string
LineID int
PlayerID string
}

func (t *Tower) Image() image.Image {
Expand Down Expand Up @@ -65,14 +66,29 @@ func (ts *Towers) Reduce(state, a interface{}) interface{} {
X: float64(act.PlaceTower.X), Y: float64(act.PlaceTower.Y),
W: w, H: h,
},
Type: act.PlaceTower.Type,
LineID: p.LineID,
Type: act.PlaceTower.Type,
LineID: p.LineID,
PlayerID: p.ID,
}
case action.UpdateState:
tids := make(map[string]struct{})
for id := range tstate.Towers {
tids[id] = struct{}{}
}
for id, t := range act.UpdateState.Towers.Towers {
delete(tids, id)
nt := Tower(*t)
tstate.Towers[id] = &nt
}
for id := range tids {
delete(tstate.Towers, id)
}
case action.RemovePlayer:
for id, t := range tstate.Towers {
if t.PlayerID == act.RemovePlayer.ID {
delete(tstate.Towers, id)
}
}
default:
}
return tstate
Expand Down
14 changes: 14 additions & 0 deletions store/units.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,10 +132,24 @@ func (us *Units) Reduce(state, a interface{}) interface{} {
u.Health = 0
}
case action.UpdateState:
uids := make(map[string]struct{})
for id := range ustate.Units {
uids[id] = struct{}{}
}
for id, u := range act.UpdateState.Units.Units {
delete(uids, id)
nu := Unit(*u)
ustate.Units[id] = &nu
}
for id := range uids {
delete(ustate.Units, id)
}
case action.RemovePlayer:
for id, u := range ustate.Units {
if u.PlayerID == act.RemovePlayer.ID {
delete(ustate.Units, id)
}
}
default:
}
return ustate
Expand Down