-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbots.go
96 lines (77 loc) · 1.6 KB
/
bots.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
// Copyright (c) 2016, RetailNext, Inc.
// This material contains trade secrets and confidential information of
// RetailNext, Inc. Any use, reproduction, disclosure or dissemination
// is strictly prohibited without the explicit written permission
// of RetailNext, Inc.
// All rights reserved.
package main
import (
"math/rand"
"time"
)
type Bot interface {
Client
Run()
}
type bot struct {
server *server
name string
enhanceCount int
}
func (b *bot) Name() string {
return b.name
}
func (b *bot) SendCommand(c string, args interface{}) error {
return nil
}
func (b *bot) Run() {
for {
time.Sleep(time.Millisecond * (5000 + time.Duration(rand.Intn(30000))))
msg := messageArgs{
Sender: b.name,
}
enhanceMessage(b.name, &msg, b.enhanceCount)
b.enhanceCount++
b.server.sendMessage(b, msg)
}
}
func (s *server) initBots() {
addBot := func(b Bot) {
s.clients[b.Name()] = b
s.clientStats[b.Name()] = &clientStats{
ConnectionCount: 1,
}
go b.Run()
}
for _, name := range names {
addBot(&bot{s, name, 0})
}
addBot(romulan{s})
}
type romulan struct {
server *server
}
func (r romulan) Name() string {
return "not_romulan"
}
func (r romulan) SendCommand(c string, args interface{}) error {
return nil
}
func (r romulan) Run() {
for i := 0; true; i++ {
if rand.Intn(10000) == 0 {
r.server.Lock()
time.Sleep(2 * time.Second)
r.server.Unlock()
}
msg := messageArgs{
Sender: r.Name(),
Private: true,
Recipient: r.Name(),
Message: "death to the federation",
}
if err := r.server.sendMessage(r, msg); err != nil {
break
}
}
}