Skip to content

Commit

Permalink
refactor: types across everything :)
Browse files Browse the repository at this point in the history
  • Loading branch information
dd84ai committed Nov 1, 2023
1 parent 1a82e8d commit 3aaae21
Show file tree
Hide file tree
Showing 48 changed files with 319 additions and 320 deletions.
25 changes: 14 additions & 11 deletions configurator/alerts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package configurator

import "darkbot/configurator/models"
import (
"darkbot/configurator/models"
"darkbot/settings/types"
)

type iConfiguratorBoolAlert interface {
Enable(channelID string) *ConfiguratorError
Expand Down Expand Up @@ -60,7 +63,7 @@ type CfgAlertBaseHealthIsDecreasing = IConfiguratorAlertBool[models.AlertBaseIfH
type CfgAlertBaseIsUnderAttack = IConfiguratorAlertBool[models.AlertBaseIfUnderAttack]
type CfgAlertPingMessage = IConfiguratorAlertString[models.AlertPingMessage]

func (c IConfiguratorAlertThreshold[T]) Set(channelID string, value int) *ConfiguratorError {
func (c IConfiguratorAlertThreshold[T]) Set(channelID types.DiscordChannelID, value int) *ConfiguratorError {
c.Unset(channelID)
obj := T{
AlertTemplate: models.AlertTemplate{ChannelID: channelID},
Expand All @@ -70,14 +73,14 @@ func (c IConfiguratorAlertThreshold[T]) Set(channelID string, value int) *Config
return (&ConfiguratorError{}).AppendSQLError(result2)
}

func (c IConfiguratorAlertThreshold[T]) Unset(channelID string) *ConfiguratorError {
func (c IConfiguratorAlertThreshold[T]) Unset(channelID types.DiscordChannelID) *ConfiguratorError {
objs := []T{}
result := c.db.Unscoped().Where("channel_id = ?", channelID).Find(&objs)
result = c.db.Unscoped().Delete(&objs)
return (&ConfiguratorError{}).AppendSQLError(result)
}

func (c IConfiguratorAlertThreshold[T]) Status(channelID string) (*int, *ConfiguratorError) {
func (c IConfiguratorAlertThreshold[T]) Status(channelID types.DiscordChannelID) (*int, *ConfiguratorError) {
var obj T
result := c.db.Where("channel_id = ?", channelID).First(&obj)
if result.Error != nil {
Expand All @@ -90,22 +93,22 @@ func (c IConfiguratorAlertThreshold[T]) Status(channelID string) (*int, *Configu

///////////////////////////

func (c IConfiguratorAlertBool[T]) Enable(channelID string) *ConfiguratorError {
func (c IConfiguratorAlertBool[T]) Enable(channelID types.DiscordChannelID) *ConfiguratorError {
obj := T{
AlertTemplate: models.AlertTemplate{ChannelID: channelID},
}
result := c.db.Create(&obj)
return (&ConfiguratorError{}).AppendSQLError(result)
}

func (c IConfiguratorAlertBool[T]) Disable(channelID string) *ConfiguratorError {
func (c IConfiguratorAlertBool[T]) Disable(channelID types.DiscordChannelID) *ConfiguratorError {
objs := []T{}
result := c.db.Unscoped().Where("channel_id = ?", channelID).Find(&objs)
result = c.db.Unscoped().Delete(&objs)
return (&ConfiguratorError{}).AppendSQLError(result)
}

func (c IConfiguratorAlertBool[T]) Status(channelID string) (bool, *ConfiguratorError) {
func (c IConfiguratorAlertBool[T]) Status(channelID types.DiscordChannelID) (bool, *ConfiguratorError) {
obj := T{}
result := c.db.Where("channel_id = ?", channelID).First(&obj)

Expand All @@ -114,7 +117,7 @@ func (c IConfiguratorAlertBool[T]) Status(channelID string) (bool, *Configurator

////////////////////////////

func (c IConfiguratorAlertString[T]) Set(channelID string, value string) *ConfiguratorError {
func (c IConfiguratorAlertString[T]) Set(channelID types.DiscordChannelID, value string) *ConfiguratorError {
c.Unset(channelID)
obj := T{
AlertTemplate: models.AlertTemplate{ChannelID: channelID},
Expand All @@ -124,20 +127,20 @@ func (c IConfiguratorAlertString[T]) Set(channelID string, value string) *Config
return (&ConfiguratorError{}).AppendSQLError(result2)
}

func (c IConfiguratorAlertString[T]) Unset(channelID string) *ConfiguratorError {
func (c IConfiguratorAlertString[T]) Unset(channelID types.DiscordChannelID) *ConfiguratorError {
objs := []T{}
result := c.db.Unscoped().Where("channel_id = ?", channelID).Find(&objs)
result = c.db.Unscoped().Delete(&objs)
return (&ConfiguratorError{}).AppendSQLError(result)
}

func (c IConfiguratorAlertString[T]) Status(channelID string) (string, *ConfiguratorError) {
func (c IConfiguratorAlertString[T]) Status(channelID types.DiscordChannelID) (types.PingMessage, *ConfiguratorError) {
var obj T
result := c.db.Where("channel_id = ?", channelID).First(&obj)
if result.Error != nil {
return "", (&ConfiguratorError{}).AppendSQLError(result)
}

str := obj.GetValue()
return str, (&ConfiguratorError{}).AppendSQLError(result)
return types.PingMessage(str), (&ConfiguratorError{}).AppendSQLError(result)
}
15 changes: 8 additions & 7 deletions configurator/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,43 @@ package configurator

import (
"darkbot/configurator/models"
"darkbot/settings/logus"
"darkbot/settings/types"
"darkbot/settings/utils"
"darkbot/settings/utils/logger"
)

type ConfiguratorChannel struct {
Configurator
}

func (c ConfiguratorChannel) Add(channelID string) *ConfiguratorError {
func (c ConfiguratorChannel) Add(channelID types.DiscordChannelID) *ConfiguratorError {

result := c.db.Table("channels").Where("channel_id = ? AND deleted_at IS NOT NULL", channelID).Update("deleted_at", nil)
if result.RowsAffected > 0 {
return (&ConfiguratorError{}).AppendSQLError(result)
}

if result.Error != nil {
logger.Info("channels.Add.Error1=", result.Error.Error())
logus.Info("channels.Add", logus.OptError(result.Error))
}

channel := models.Channel{ChannelID: channelID}
result = c.db.Create(&channel)
if result.Error != nil {
logger.Info("channels.Add.Error2=", result.Error.Error())
logus.Info("channels.Add.Error2=", logus.OptError(result.Error))
}
return (&ConfiguratorError{}).AppendSQLError(result)
}

func (c ConfiguratorChannel) Remove(channelID string) *ConfiguratorError {
func (c ConfiguratorChannel) Remove(channelID types.DiscordChannelID) *ConfiguratorError {
result := c.db.Where("channel_id = ?", channelID).Delete(&models.Channel{})
return (&ConfiguratorError{}).AppendSQLError(result)
}

func (c ConfiguratorChannel) List() ([]string, *ConfiguratorError) {
func (c ConfiguratorChannel) List() ([]types.DiscordChannelID, *ConfiguratorError) {
objs := []models.Channel{}
result := c.db.Find(&objs)

return utils.CompL(objs,
func(x models.Channel) string { return x.ChannelID }), (&ConfiguratorError{}).AppendSQLError(result)
func(x models.Channel) types.DiscordChannelID { return x.ChannelID }), (&ConfiguratorError{}).AppendSQLError(result)
}
4 changes: 2 additions & 2 deletions configurator/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ package configurator
import (
"darkbot/configurator/models"
"darkbot/settings"
"darkbot/settings/logus"
"darkbot/settings/types"
"darkbot/settings/utils/logger"

"gorm.io/driver/sqlite"
"gorm.io/gorm"
Expand All @@ -26,7 +26,7 @@ func NewConfigurator(dbpath types.Dbpath) Configurator {
db, err := gorm.Open(
sqlite.Open(string(dbpath)+"?cache=shared&mode=rwc&_journal_mode=WAL"), &gorm.Config{},
)
logger.CheckPanic(err, "failed to connect database at dbpath=", string(settings.Dbpath))
logus.CheckFatal(err, "failed to connect database at dbpath=", logus.Dbpath(settings.Dbpath))

return Configurator{db: db}
}
Expand Down
4 changes: 2 additions & 2 deletions configurator/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ func FixtureMigrator(callback func(dbpath types.Dbpath)) Configurator {
return cfg
}

func FixtureChannel(dbpath types.Dbpath) (string, ConfiguratorChannel) {
channelID := "123"
func FixtureChannel(dbpath types.Dbpath) (types.DiscordChannelID, ConfiguratorChannel) {
channelID := types.DiscordChannelID("123")
configurator_ := FixtureConfigurator(dbpath)
cfg_channel := ConfiguratorChannel{Configurator: configurator_}
cfg_channel.Add(channelID)
Expand Down
10 changes: 7 additions & 3 deletions configurator/models/model_templates.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package models

import "gorm.io/gorm"
import (
"darkbot/settings/types"

"gorm.io/gorm"
)

type TagTemplate struct {
gorm.Model
ChannelID string
ChannelID types.DiscordChannelID
Channel Channel `gorm:"references:ChannelID"`
Tag string
}
Expand All @@ -19,6 +23,6 @@ type AlertPlayerMoreThan struct {

type AlertTemplate struct {
gorm.Model
ChannelID string
ChannelID types.DiscordChannelID
Channel Channel `gorm:"references:ChannelID,unique"`
}
8 changes: 6 additions & 2 deletions configurator/models/models.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package models

import "gorm.io/gorm"
import (
"darkbot/settings/types"

"gorm.io/gorm"
)

type Channel struct {
gorm.DeletedAt
ChannelID string `gorm:"primarykey"` // Discord channel reference
ChannelID types.DiscordChannelID `gorm:"primarykey"` // Discord channel reference
}

// ================== Tag Tracking ====================
Expand Down
25 changes: 13 additions & 12 deletions configurator/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ package configurator

import (
"darkbot/configurator/models"
"darkbot/settings/logus"
"darkbot/settings/types"
"darkbot/settings/utils"
"darkbot/settings/utils/logger"
"fmt"
)

type IConfiguratorTags interface {
TagsAdd(channelID string, tags ...string) *ConfiguratorError
TagsRemove(channelID string, tags ...string) *ConfiguratorError
TagsList(channelID string) ([]string, *ConfiguratorError)
TagsClear(channelID string) *ConfiguratorError
TagsAdd(channelID types.DiscordChannelID, tags ...string) *ConfiguratorError
TagsRemove(channelID types.DiscordChannelID, tags ...string) *ConfiguratorError
TagsList(channelID types.DiscordChannelID) ([]string, *ConfiguratorError)
TagsClear(channelID types.DiscordChannelID) *ConfiguratorError
}

type taggable interface {
Expand All @@ -34,7 +35,7 @@ type ConfiguratorPlayerFriend = ConfiguratorTags[models.TagPlayerFriend]
type ConfiguratorPlayerEnemy = ConfiguratorTags[models.TagPlayerEnemy]

// T =
func (c ConfiguratorTags[T]) TagsAdd(channelID string, tags ...string) *ConfiguratorError {
func (c ConfiguratorTags[T]) TagsAdd(channelID types.DiscordChannelID, tags ...string) *ConfiguratorError {
objs := []T{}
errors := &ConfiguratorError{}
for _, tag := range tags {
Expand All @@ -58,31 +59,31 @@ func (c ConfiguratorTags[T]) TagsAdd(channelID string, tags ...string) *Configur
}

res := c.db.Create(objs)
logger.CheckWarn(res.Error, "unsuccesful result of c.db.Create")
logus.CheckWarn(res.Error, "unsuccesful result of c.db.Create")
errors.AppendSQLError(res)
return errors
}

func (c ConfiguratorTags[T]) TagsRemove(channelID string, tags ...string) *ConfiguratorError {
func (c ConfiguratorTags[T]) TagsRemove(channelID types.DiscordChannelID, tags ...string) *ConfiguratorError {
errors := &ConfiguratorError{}
for _, tag := range tags {
result := c.db.Where("channel_id = ? AND tag = ?", channelID, tag).Delete(&T{})
logger.CheckWarn(result.Error, "unsuccesful result of c.db.Delete")
logus.CheckWarn(result.Error, "unsuccesful result of c.db.Delete")
errors.AppendSQLError(result)
}
return errors
}

func (c ConfiguratorTags[T]) TagsList(channelID string) ([]string, *ConfiguratorError) {
func (c ConfiguratorTags[T]) TagsList(channelID types.DiscordChannelID) ([]string, *ConfiguratorError) {
objs := []T{}
result := c.db.Where("channel_id = ?", channelID).Find(&objs)
logger.CheckWarn(result.Error, "unsuccesful result of c.db.Find")
logus.CheckWarn(result.Error, "unsuccesful result of c.db.Find")

return utils.CompL(objs,
func(x T) string { return x.GetTag() }), (&ConfiguratorError{}).AppendSQLError(result)
}

func (c ConfiguratorTags[T]) TagsClear(channelID string) *ConfiguratorError {
func (c ConfiguratorTags[T]) TagsClear(channelID types.DiscordChannelID) *ConfiguratorError {
tags := []T{}
result := c.db.Unscoped().Where("channel_id = ?", channelID).Find(&tags)
fmt.Println("Clear.Find.rowsAffected=", result.RowsAffected)
Expand Down
2 changes: 1 addition & 1 deletion consoler/commands/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func (t *AlertSetStringCommand[T]) CreateStatusCmd() {
}
}

helper.Printer{Cmd: cmd}.Println("OK value is = " + str)
helper.Printer{Cmd: cmd}.Println("OK value is = " + string(str))
},
}
t.CurrentCmd.AddCommand(command)
Expand Down
2 changes: 1 addition & 1 deletion consoler/helper/channelInfo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package helper
import "darkbot/settings/types"

type ChannelInfo struct {
ChannelID string
ChannelID types.DiscordChannelID
Dbpath types.Dbpath
}
Loading

0 comments on commit 3aaae21

Please sign in to comment.