-
Notifications
You must be signed in to change notification settings - Fork 0
/
uno.go
133 lines (126 loc) · 3.28 KB
/
uno.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package game
import (
"bytes"
"fmt"
"math/rand"
"time"
"github.com/feel-easy/hole-server/consts"
"github.com/feel-easy/hole-server/models"
"github.com/feel-easy/hole-server/utils/logs"
"github.com/feel-easy/uno/card/color"
"github.com/feel-easy/uno/event"
"github.com/feel-easy/uno/game"
)
type Uno struct{}
func (g *Uno) Next(user *models.User) (consts.StateID, error) {
room := models.GetRoom(user.RoomID)
if room == nil {
return 0, user.WriteError(consts.ErrorsExist)
}
game := room.RoomGame.(*models.UnoGame)
buf := bytes.Buffer{}
buf.WriteString(fmt.Sprintf(
"WELCOME TO %s%s%s!!!\n",
color.Red.Paint("U"),
color.Yellow.Paint("N"),
color.Blue.Paint("O"),
))
buf.WriteString(fmt.Sprintf("Your Cards: %s\n", game.Game.GetPlayerCards(user.ID)))
_ = user.WriteString(buf.String())
for {
if room.State == consts.Waiting {
return consts.StateWaiting, nil
}
state := <-game.States[user.ID]
switch state {
case stateFirstCard:
if msg := game.Game.PlayFirstCard(); msg != "" {
models.Broadcast(room.ID, msg)
}
pc := game.Game.Players().Next()
game.States[pc.ID()] <- statePlay
case statePlay:
err := handlePlayUno(room, user, game)
if err != nil {
logs.Error(err)
return 0, err
}
case stateWaiting:
return consts.StateWaiting, nil
default:
return 0, consts.ErrorsChanClosed
}
}
}
func (g *Uno) Exit(user *models.User) consts.StateID {
room := models.GetRoom(user.RoomID)
if room == nil {
return consts.StateHome
}
models.LeaveRoom(room.ID, user.ID)
return consts.StateHome
}
func handlePlayUno(room *models.Room, user *models.User, game *models.UnoGame) error {
p := game.Game.Current()
if p.ID() != user.ID {
game.States[p.ID()] <- statePlay
return nil
}
if !game.HavePlay(user) {
pc := game.Game.Players().Next()
game.States[pc.ID()] <- statePlay
}
gameState := game.Game.ExtractState(p)
card, err := p.Play(gameState, game.Game.Deck())
if err != nil || card == nil {
event.PlayerPassed.Emit(event.PlayerPassedPayload{
PlayerName: p.Name(),
})
pc := game.Game.Players().Next()
game.States[pc.ID()] <- statePlay
return err
}
game.Game.Pile().Add(card)
event.CardPlayed.Emit(event.CardPlayedPayload{
PlayerName: p.Name(),
Card: card,
})
if msg := game.Game.PerformCardActions(card); msg != "" {
models.Broadcast(room.ID, msg)
}
if p.NoCards() || game.NeedExit() {
models.Broadcast(room.ID, fmt.Sprintf("%s wins! \n", p.Name()))
room.Lock()
room.State = consts.Waiting
room.Unlock()
for _, userId := range game.Users {
game.States[userId] <- stateWaiting
}
return nil
}
pc := game.Game.Players().Next()
game.States[pc.ID()] <- statePlay
return nil
}
func InitUnoGame(room *models.Room) (*models.UnoGame, error) {
users := make([]int, 0)
roomUsers := room.Users
unoPlayers := make([]game.Player, 0)
states := map[int]chan int{}
for userId := range roomUsers {
user := models.GetUser(userId)
users = append(users, user.ID)
unoPlayers = append(unoPlayers, models.NewUnoPlayer(user))
states[userId] = make(chan int, 1)
}
rand.Seed(time.Now().UnixNano())
unoGame := game.New(unoPlayers)
unoGame.DealStartingCards()
states[int(unoGame.Current().ID())] <- stateFirstCard
return &models.UnoGame{
Room: room,
Users: users,
States: states,
Game: unoGame,
}, nil
}