go fmt and support monthly/yearly commands
This commit is contained in:
parent
8abf5c7af3
commit
2a715ac172
33
pkg/admin.go
33
pkg/admin.go
@ -14,7 +14,6 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
|
||||
type OpenTTDServer struct {
|
||||
connection net.Conn
|
||||
rconDaily []string
|
||||
@ -87,7 +86,7 @@ const (
|
||||
// var conn int
|
||||
|
||||
// Connect to the OpenTTD server on the admin port
|
||||
func ( server *OpenTTDServer ) Connect(host string, port int, password string, botName string, botVersion string) {
|
||||
func (server *OpenTTDServer) Connect(host string, port int, password string, botName string, botVersion string) {
|
||||
|
||||
// fmt.Printf("array: %v (%T) %d\n", toSend, toSend, size)
|
||||
connectString := fmt.Sprintf("%s:%d", host, port)
|
||||
@ -117,9 +116,9 @@ func ( server *OpenTTDServer ) Connect(host string, port int, password string, b
|
||||
|
||||
// register for daily updates
|
||||
updateDateCmd := make([]byte, 2)
|
||||
binary.LittleEndian.PutUint16(updateDateCmd,adminUpdateDATE)
|
||||
binary.LittleEndian.PutUint16(updateDateCmd, adminUpdateDATE)
|
||||
updateDateDaily := make([]byte, 2)
|
||||
binary.LittleEndian.PutUint16(updateDateDaily,adminFrequencyDAILY)
|
||||
binary.LittleEndian.PutUint16(updateDateDaily, adminFrequencyDAILY)
|
||||
|
||||
toSend = []byte{}
|
||||
toSend = append(toSend, updateDateCmd...)
|
||||
@ -140,7 +139,7 @@ func ( server *OpenTTDServer ) Connect(host string, port int, password string, b
|
||||
}
|
||||
|
||||
// RegisterDateChange to send a command periodically
|
||||
func ( server *OpenTTDServer ) RegisterDateChange (period string, command string) {
|
||||
func (server *OpenTTDServer) RegisterDateChange(period string, command string) {
|
||||
if period == "daily" {
|
||||
server.rconDaily = append(server.rconDaily, command)
|
||||
} else if period == "monthly" {
|
||||
@ -153,13 +152,29 @@ func ( server *OpenTTDServer ) RegisterDateChange (period string, command string
|
||||
return
|
||||
}
|
||||
|
||||
func (server *OpenTTDServer ) dateChanged(dt time.Time) {
|
||||
func (server *OpenTTDServer) dateChanged(dt time.Time) {
|
||||
// do every daily one
|
||||
for _, rconCommand := range server.rconDaily {
|
||||
server.rconCommand(rconCommand)
|
||||
}
|
||||
|
||||
// monthly ones on the 1st
|
||||
if dt.Day() == 1 {
|
||||
for _, rconCommand := range server.rconMonthly {
|
||||
server.rconCommand(rconCommand)
|
||||
}
|
||||
}
|
||||
|
||||
// yearly on the 1st of jan
|
||||
if dt.Day() == 1 && dt.Month() == 1 {
|
||||
for _, rconCommand := range server.rconYearly {
|
||||
server.rconCommand(rconCommand)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (server OpenTTDServer ) rconCommand(command string) {
|
||||
func (server OpenTTDServer) rconCommand(command string) {
|
||||
|
||||
var rconCommand []byte
|
||||
rconCommand = append(rconCommand, command...)
|
||||
@ -168,7 +183,7 @@ func (server OpenTTDServer ) rconCommand(command string) {
|
||||
server.sendSocket(adminPacketAdminRCON, rconCommand)
|
||||
}
|
||||
|
||||
func ( server *OpenTTDServer ) sendSocket( protocol int, data []byte) {
|
||||
func (server *OpenTTDServer) sendSocket(protocol int, data []byte) {
|
||||
fmt.Printf("Going to send using protocol %v this data: %v\n", protocol, data)
|
||||
toSend := make([]byte, 3) // start with 3 bytes for the length and protocol
|
||||
size := uint16(len(data) + 3) // size 2 bytes, plus protocol
|
||||
@ -180,7 +195,7 @@ func ( server *OpenTTDServer ) sendSocket( protocol int, data []byte) {
|
||||
server.connection.Write(toSend)
|
||||
}
|
||||
|
||||
func ( server *OpenTTDServer ) listenSocket() {
|
||||
func (server *OpenTTDServer) listenSocket() {
|
||||
fmt.Printf("Listening to socket...\n")
|
||||
|
||||
var chunk []byte
|
||||
|
Loading…
x
Reference in New Issue
Block a user