go fmt and support monthly/yearly commands

This commit is contained in:
Justin Hawkins 2020-11-19 15:13:30 +10:30
parent 8abf5c7af3
commit 2a715ac172

View File

@ -14,7 +14,6 @@ import (
"time" "time"
) )
type OpenTTDServer struct { type OpenTTDServer struct {
connection net.Conn connection net.Conn
rconDaily []string rconDaily []string
@ -87,7 +86,7 @@ const (
// var conn int // var conn int
// Connect to the OpenTTD server on the admin port // 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) // fmt.Printf("array: %v (%T) %d\n", toSend, toSend, size)
connectString := fmt.Sprintf("%s:%d", host, port) 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 // register for daily updates
updateDateCmd := make([]byte, 2) updateDateCmd := make([]byte, 2)
binary.LittleEndian.PutUint16(updateDateCmd,adminUpdateDATE) binary.LittleEndian.PutUint16(updateDateCmd, adminUpdateDATE)
updateDateDaily := make([]byte, 2) updateDateDaily := make([]byte, 2)
binary.LittleEndian.PutUint16(updateDateDaily,adminFrequencyDAILY) binary.LittleEndian.PutUint16(updateDateDaily, adminFrequencyDAILY)
toSend = []byte{} toSend = []byte{}
toSend = append(toSend, updateDateCmd...) 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 // 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" { if period == "daily" {
server.rconDaily = append(server.rconDaily, command) server.rconDaily = append(server.rconDaily, command)
} else if period == "monthly" { } else if period == "monthly" {
@ -153,13 +152,29 @@ func ( server *OpenTTDServer ) RegisterDateChange (period string, command string
return return
} }
func (server *OpenTTDServer ) dateChanged(dt time.Time) { func (server *OpenTTDServer) dateChanged(dt time.Time) {
// do every daily one
for _, rconCommand := range server.rconDaily { for _, rconCommand := range server.rconDaily {
server.rconCommand(rconCommand) 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 var rconCommand []byte
rconCommand = append(rconCommand, command...) rconCommand = append(rconCommand, command...)
@ -168,7 +183,7 @@ func (server OpenTTDServer ) rconCommand(command string) {
server.sendSocket(adminPacketAdminRCON, rconCommand) 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) 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 toSend := make([]byte, 3) // start with 3 bytes for the length and protocol
size := uint16(len(data) + 3) // size 2 bytes, plus 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) server.connection.Write(toSend)
} }
func ( server *OpenTTDServer ) listenSocket() { func (server *OpenTTDServer) listenSocket() {
fmt.Printf("Listening to socket...\n") fmt.Printf("Listening to socket...\n")
var chunk []byte var chunk []byte