Skip to content

Commit

Permalink
Add events of ended game
Browse files Browse the repository at this point in the history
  • Loading branch information
rnixik committed Feb 12, 2018
1 parent 8b967b7 commit f7262e9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 29 deletions.
10 changes: 10 additions & 0 deletions events_game.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,13 @@ type GameFirstAttackerEvent struct {
ReasonCard *Card `json:"reason_card"`
AttackerIndex int `json:"attacker_index"`
}

// GamePlayerLeftEvent contains index of player who left the game
type GamePlayerLeftEvent struct {
PlayerIndex int `json:"player_index"`
}

// GameEndEvent contains index of player who left the game
type GameEndEvent struct {
WinnerIndex int `json:"winner_index"`
}
56 changes: 33 additions & 23 deletions game.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,30 +53,16 @@ type Game struct {
attackerIndex int
}

func newGame(id uint64, room *Room, owner *Player, players []*Player) *Game {
func newGame(id uint64, room *Room, players []*Player) *Game {
return &Game{
id: id,
room: room,
owner: owner,
playerActions: make(chan *PlayerAction),
status: GameStatusPreparing,
players: players,
}
}

func (g *Game) getActivePlayers() (activePlayers []*Player) {
for _, p := range g.players {
if p.IsActive {
activePlayers = append(activePlayers, p)
}
}
return activePlayers
}

func (g *Game) getName() string {
return g.owner.Name
}

func (g *Game) sendPlayersEvent() {
pe := GamePlayersEvent{
YourPlayerIndex: -1,
Expand All @@ -90,12 +76,11 @@ func (g *Game) sendPlayersEvent() {
}

func (g *Game) deal() {
activePlayers := g.getActivePlayers()
cardsLimit := 6
var lastCard *Card
lastPlayerIndex := -1
for cardIndex := 0; cardIndex < cardsLimit; cardIndex = cardIndex + 1 {
for playerIndex, p := range activePlayers {
for playerIndex, p := range g.players {
if len(p.cards) >= cardsLimit {
break
}
Expand Down Expand Up @@ -184,7 +169,7 @@ func (g *Game) findFirstAttacker() (firstAttackerIndex int, lowestTrumpCard *Car
firstAttackerIndex = -1
lowestTrumpCard = &Card{"A", g.trumpSuit}

for playerIndex, p := range g.getActivePlayers() {
for playerIndex, p := range g.players {
for _, c := range p.cards {
if c.Suit == g.trumpSuit && c.lte(lowestTrumpCard) {
firstAttackerIndex = playerIndex
Expand All @@ -198,7 +183,7 @@ func (g *Game) findFirstAttacker() (firstAttackerIndex int, lowestTrumpCard *Car
}

// fallback
for playerIndex, p := range g.getActivePlayers() {
for playerIndex, p := range g.players {
for _, c := range p.cards {
if c.lte(lowestTrumpCard) {
firstAttackerIndex = playerIndex
Expand All @@ -224,7 +209,10 @@ func (g *Game) begin() {
g.prepare()
for {
select {
case action := <-g.playerActions:
case action, ok := <-g.playerActions:
if !ok {
return
}
log.Printf("action: %#v", action)
g.onClientAction(action)
}
Expand All @@ -251,11 +239,33 @@ func (g *Game) broadcastEvent(event interface{}) {
}
}

func (g *Game) onGameEnded(winnerIndex int) {
gameEndEvent := &GameEndEvent{winnerIndex}
g.room.broadcastEvent(gameEndEvent, nil)
close(g.playerActions)
g.room.onGameEnded()
}

func (g *Game) onPlayerLeft(playerIndex int) {
gamePlayerLeft := &GamePlayerLeftEvent{playerIndex}
g.room.broadcastEvent(gamePlayerLeft, nil)

winnerIndex := -1
if len(g.players) == 2 {
if playerIndex == 0 {
winnerIndex = 1
} else {
winnerIndex = 0
}
g.onGameEnded(winnerIndex)
}
}

func (g *Game) onClientRemoved(client *Client) {
// TODO: implement logic
for _, p := range g.getActivePlayers() {
for index, p := range g.players {
if p.client.Id() == client.Id() {
//
g.onPlayerLeft(index)
return
}
}
}
21 changes: 15 additions & 6 deletions room.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,16 +103,16 @@ func (r *Room) addClient(client *Client) {
}

roomUpdatedEvent := &RoomUpdatedEvent{r.toRoomInfo()}
r.broadcastEvent(roomUpdatedEvent, member)
r.broadcastEvent(roomUpdatedEvent, member.client.(*Client))

roomJoinedEvent := RoomJoinedEvent{r.toRoomInfo()}
client.sendEvent(roomJoinedEvent)
}

func (r *Room) broadcastEvent(event interface{}, exceptMember *RoomMember) {
func (r *Room) broadcastEvent(event interface{}, exceptClient *Client) {
json, _ := eventToJSON(event)
for m := range r.members {
if exceptMember == nil || m.client.Id() != exceptMember.client.Id() {
if exceptClient == nil || m.client.Id() != exceptClient.Id() {
m.client.sendMessage(json)
}
}
Expand Down Expand Up @@ -226,14 +226,16 @@ func (r *Room) onStartGameCommand(c *Client) {

players := make([]*Player, 0)
for rm := range r.members {
player := newPlayer(rm.client, rm.isPlayer)
players = append(players, player)
if rm.isPlayer {
player := newPlayer(rm.client, rm.isPlayer)
players = append(players, player)
}
}

atomic.AddUint64(&lastGameId, 1)
lastGameIdSafe := atomic.LoadUint64(&lastGameId)

r.game = newGame(lastGameIdSafe, r, players[0], players)
r.game = newGame(lastGameIdSafe, r, players)
go r.game.begin()

roomUpdatedEvent := &RoomUpdatedEvent{r.toRoomInfo()}
Expand All @@ -259,6 +261,13 @@ func (r *Room) onClientCommand(cc *ClientCommand) {
}
}

func (r *Room) onGameEnded() {
r.game = nil
roomUpdatedEvent := &RoomUpdatedEvent{r.toRoomInfo()}
r.broadcastEvent(roomUpdatedEvent, nil)
r.lobby.sendRoomUpdate(r)
}

func (rm *RoomMember) memberToRoomMemberInfo() *RoomMemberInfo {
return &RoomMemberInfo{
Id: rm.client.Id(),
Expand Down

0 comments on commit f7262e9

Please sign in to comment.