-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.go
98 lines (84 loc) · 2.16 KB
/
player.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
package sirpent
import (
"errors"
"fmt"
"sync"
"time"
)
type Player struct {
Name string `json:"name"`
// Address to open a TCP socket to.
ServerAddress string `json:"server_address"`
connection *jsonSocket
ready_wg *sync.WaitGroup
// Is the Player alive after the most recent tick?
Alive bool `json:"alive"`
// What killed the player?
DiedFrom CauseOfDeath `json:"died_from"`
}
func NewPlayer(name string, server_address string) *Player {
// @TODO: Ensure CauseOfDeath will deny every cause of death unless modified.
return &Player{
Name: name,
ServerAddress: server_address,
connection: nil,
Alive: true,
}
}
func (p *Player) Connect(game *Game, player_connection_timeout time.Duration, err_chan chan error) {
// 1. Simultaneously all Players connect and send the player ID.
connection, err := newJsonSocket(p.ServerAddress, player_connection_timeout)
if err != nil {
err_chan <- err
return
}
p.connection = connection
err = p.connection.sendOrTimeout(p.Name)
if err != nil {
err_chan <- err
return
}
err = p.connection.sendOrTimeout(game.World)
if err != nil {
err_chan <- err
return
}
err = p.connection.sendOrTimeout(game.Players)
if err != nil {
err_chan <- err
return
}
err_chan <- nil
}
func (p *Player) PlayTurn(game *Game, action_chan chan *PlayerAction, err_chan chan error) {
if !p.Alive {
err_chan <- errors.New("Player cannot take a turn for they are already dead.")
return
}
previous_game_state := game.LatestTick()
// Player turn loop:
// 1. Send current game state.
// 2. Receive chosen move.
// 3. Update player state.
// 4. Wait for global turn operations.
// 5. Go to 1 unless player is dead.
// 1. Send current game state.
err := p.connection.sendOrTimeout(previous_game_state)
if err != nil {
err_chan <- err
return
}
// 2. Receive chosen action.
var action *PlayerAction
err = p.connection.receiveOrTimeout(&action)
if err != nil {
err_chan <- err
return
}
action_chan <- action
}
func (p *Player) ErrorKillPlayer(err error) {
p.Alive = false
p.DiedFrom.DiagnoseError(err)
fmt.Printf("---\nDIED: Player %s died from %s---\n", p.Name, p.DiedFrom.Spew())
}