-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsession.go
198 lines (168 loc) · 3.8 KB
/
session.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package main
import (
"bufio"
"fmt"
"math/rand"
"net"
"strings"
"time"
)
var connected = make(map[*Connection]bool, 32)
type Connection struct {
net.Conn
*bufio.Reader
player *Player
location *System
lastScan time.Time
lastBomb time.Time
kills int
dead bool
money int64
mining bool
colonies []*System
bombs int
}
func NewConnection(conn net.Conn) *Connection {
c := &Connection{
Conn: conn,
Reader: bufio.NewReader(conn),
bombs: 1,
}
connected[c] = true
return c
}
func (c *Connection) Login() {
for {
fmt.Fprintf(c, "what is your name, adventurer?\n")
name, err := c.ReadString('\n')
if err == nil {
name = strings.TrimSpace(name)
} else {
log_error("player failed to connect: %v", err)
return
}
if !ValidName(name) {
fmt.Fprintf(c, "that name is illegal.\n")
continue
}
log_info("player connected: %v", name)
player, err := loadPlayer(name)
if err != nil {
log_error("could not read player: %v", err)
player = &Player{name: name}
if err := player.Create(); err != nil {
}
fmt.Fprintf(c, "you look new around these parts, %s.\n", player.name)
fmt.Fprintf(c, `if you'd like a description of how to play, type the "help" command`)
} else {
c.player = player
fmt.Fprintf(c, "welcome back, %s.\n", player.name)
}
break
}
}
func (c *Connection) SetSystem(s *System) {
c.location = s
}
func (c *Connection) System() *System {
return c.location
}
func (c *Connection) Close() error {
log_info("player disconnecting: %s", c.PlayerName())
delete(connected, c)
return c.Conn.Close()
}
func (c *Connection) PlayerName() string {
if c.player == nil {
return ""
}
return c.player.name
}
func (c *Connection) InTransit() bool {
return c.location == nil
}
func (c *Connection) RecordScan() {
fmt.Fprintln(c, "scanning known systems for signs of life")
c.lastScan = time.Now()
After(1*time.Minute, func() {
fmt.Fprintln(c, "scanner ready")
})
}
func (c *Connection) RecordBomb() {
c.lastBomb = time.Now()
After(15*time.Second, func() {
fmt.Fprintln(c, "bomb arsenal reloaded")
})
}
func (c *Connection) CanScan() bool {
return time.Since(c.lastScan) > 1*time.Minute
}
func (c *Connection) CanBomb() bool {
return time.Since(c.lastBomb) > 15*time.Second
}
func (c *Connection) NextScan() time.Duration {
return -time.Since(c.lastScan.Add(time.Minute))
}
func (c *Connection) NextBomb() time.Duration {
return -time.Since(c.lastBomb.Add(15 * time.Second))
}
func (c *Connection) MadeKill(victim *Connection) {
c.kills += 1
if c.kills == 3 {
c.Win()
}
}
func (c *Connection) StartMining() {
fmt.Fprintf(c, "now mining %s with a payout rate of %v\n", c.System().name, c.System().miningRate)
fmt.Fprintln(c, "(press enter to stop mining)")
c.mining = true
}
func (c *Connection) StopMining() {
fmt.Fprintf(c, "done mining\n")
c.mining = false
}
func (c *Connection) IsMining() bool {
return c.mining
}
func (c *Connection) Payout() {
if c.dead {
return
}
reward := int64(rand.NormFloat64()*5.0 + 100.0*c.System().miningRate)
c.Deposit(reward)
fmt.Fprintf(c, "mined: %d space duckets. total: %d\n", reward, c.money)
}
func (c *Connection) Withdraw(n int64) {
c.money -= n
}
func (c *Connection) Deposit(n int64) {
c.money += n
if c.money >= 25000 {
c.Win()
}
}
func (c *Connection) Win() {
for conn, _ := range connected {
fmt.Fprintf(conn, "player %s has won.\n", c.PlayerName())
conn.Close()
}
}
func (c *Connection) Die() {
fmt.Fprintf(c, "you were bombed. You will respawn in 1 minutes.\n")
c.dead = true
c.System().Leave(c)
After(30*time.Second, func() {
fmt.Fprintf(c, "respawn in 30 seconds.\n")
})
After(time.Minute, c.Respawn)
}
func (c *Connection) Respawn() {
c.dead = false
WUT:
s, err := randomSystem()
if err != nil {
log_error("error in respawn: %v", err)
goto WUT
}
s.Arrive(c)
}