Refactor interfaces

This commit is contained in:
Hri7566 2024-04-20 05:27:00 -04:00
parent 8049c9a288
commit 97e07e41df
7 changed files with 119 additions and 88 deletions

16
data.go
View File

@ -2,12 +2,11 @@ package main
import ( import (
"log" "log"
"strconv"
badger "github.com/dgraph-io/badger/v4" badger "github.com/dgraph-io/badger/v4"
) )
var db leveldb.DB var db badger.DB
func SetupDb() { func SetupDb() {
db, err := badger.Open(badger.DefaultOptions("./bot2024.db")) db, err := badger.Open(badger.DefaultOptions("./bot2024.db"))
@ -20,14 +19,11 @@ func SetupDb() {
} }
func GetBalance(uid string) (float64, error) { func GetBalance(uid string) (float64, error) {
s, err := db.Get([]byte("userbal~" + uid), nil) // TODO thing
log.Println("here") return 0, nil
bal, _ := strconv.ParseFloat(string(s), 64)
return bal, err
} }
func SetBalance(uid string, bal float64) (error) { func SetBalance(uid string, bal float64) error {
s := strconv.FormatFloat(bal, 'f', -1, 64) // TODO other thing
err := db.Put([]byte("userbal~" + uid), []byte(s), nil) return nil
return err
} }

95
main.go
View File

@ -12,67 +12,14 @@ import (
"strings" "strings"
"time" "time"
"github.com/Hri7566/mpp-client-go/mpp"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"github.com/joho/godotenv" "github.com/joho/godotenv"
) )
var addr = flag.String("addr", "mppclone.com:8443", "websocket address") var addr = flag.String("addr", "mppclone.com:8443", "websocket address")
type User struct {
Uid string `json:"_id"`
Name string `json:"name"`
Color string `json:"color"`
}
type Participant struct {
User
Id string `json:"id"`
}
type MppMessage struct {
Type string `json:"m"`
}
type HiMessage struct {
MppMessage
Timestamp json.Number `json:"t"`
User User `json:"u"`
Motd string `json:"motd"`
}
type NqMessage struct {
MppMessage
Allowance json.Number `json:"allowance"`
Max json.Number `json:"max"`
MaxHistLen json.Number `json:"maxHistLen"`
}
type ChatMessage struct {
MppMessage
Message string `json:"a"`
Part Participant `json:"p"`
Timestamp json.Number `json:"t"`
}
type TMessage struct {
MppMessage
Timestamp json.Number `json:"t"`
Echo json.Number `json:"e"`
}
type Channel struct {
Id string `json:"_id"`
Id2 string `json:"id"`
}
type ChMessage struct {
MppMessage
Channel Channel `json:"ch"`
Participants []Participant `json:"ppl"`
}
type AnyMppMessage []interface{}
var token string var token string
func main() { func main() {
@ -113,9 +60,9 @@ func StartSocket(address *string) {
go func() { go func() {
for { for {
select { select {
case <- serverTicker.C: case <-serverTicker.C:
SendTimeMessage(ws) SendTimeMessage(ws)
case <- serverTickerQuit: case <-serverTickerQuit:
serverTicker.Stop() serverTicker.Stop()
} }
} }
@ -195,18 +142,18 @@ func ReceiveMessage(ws *websocket.Conn, data []byte) {
_ = json.Unmarshal(raw, &header) _ = json.Unmarshal(raw, &header)
switch header.Type { switch header.Type {
case "hi": case "hi":
msg := HiMessage{} msg := mpp.HiMppMessage{}
_ = json.Unmarshal(raw, &msg) _ = json.Unmarshal(raw, &msg)
ReceiveHiMessage(ws, &msg) ReceiveHiMessage(ws, &msg)
case "nq": case "nq":
msg := NqMessage{} msg := mpp.NoteQuotaMppMessage{}
_ = json.Unmarshal(raw, &msg) _ = json.Unmarshal(raw, &msg)
ReceiveNqMessage(&msg) ReceiveNqMessage(&msg)
case "t": case "t":
msg := TMessage{} msg := mpp.TimeMppMessage{}
_ = json.Unmarshal(raw, &msg) _ = json.Unmarshal(raw, &msg)
case "a": case "a":
msg := ChatMessage{} msg := mpp.ChatMppMessage{}
_ = json.Unmarshal(raw, &msg) _ = json.Unmarshal(raw, &msg)
ReceiveChatMessage(ws, &msg) ReceiveChatMessage(ws, &msg)
case "b": case "b":
@ -224,7 +171,7 @@ func ReceiveMessage(ws *websocket.Conn, data []byte) {
// } // }
} }
func ReceiveNqMessage(msg *NqMessage) { func ReceiveNqMessage(msg *mpp.NoteQuotaMppMessage) {
// println(msg.MaxHistLen) // println(msg.MaxHistLen)
} }
@ -240,10 +187,10 @@ func ReceiveBMessage(ws *websocket.Conn) {
// sentHi = true // sentHi = true
} }
func ReceiveHiMessage(ws *websocket.Conn, msg *HiMessage) { func ReceiveHiMessage(ws *websocket.Conn, msg *mpp.HiMppMessage) {
println("I am " + msg.User.Name) println("I am " + msg.User.Name)
chstr := "[{\"m\":\"ch\",\"_id\":\"Sans' Comedy Corner\"}]" chstr := "[{\"m\":\"ch\",\"_id\":\"✧𝓓𝓔𝓥 𝓡𝓸𝓸𝓶✧\"}]"
// println("Sending ch message: " + chstr) // println("Sending ch message: " + chstr)
err2 := ws.WriteMessage(1, []byte(chstr)) err2 := ws.WriteMessage(1, []byte(chstr))
if err2 != nil { if err2 != nil {
@ -252,7 +199,7 @@ func ReceiveHiMessage(ws *websocket.Conn, msg *HiMessage) {
} }
type CommandData struct { type CommandData struct {
ChatMessage mpp.ChatMppMessage
Args []string Args []string
Cmd string Cmd string
} }
@ -267,19 +214,19 @@ var helpCommands []Command
var commands []Command = []Command{ var commands []Command = []Command{
{ {
Id: "help", Id: "help",
Callback: func (ws *websocket.Conn, data *CommandData) string { Callback: func(ws *websocket.Conn, data *CommandData) string {
output := "Commands: " output := "Commands: "
for _, cmd := range helpCommands { for _, cmd := range helpCommands {
output += cmd.Id + ", " output += cmd.Id + ", "
} }
return strings.Trim(output[:len(output) - 2], " ") return strings.Trim(output[:len(output)-2], " ")
}, },
}, },
{ {
Id: "about", Id: "about",
Callback: func (ws *websocket.Conn, data *CommandData) string { Callback: func(ws *websocket.Conn, data *CommandData) string {
return "written in go version go1.22.2 linux/amd64" return "written in go version go1.22.2 linux/amd64"
}, },
}, },
@ -296,11 +243,17 @@ var commands []Command = []Command{
return "Your balance: " + strconv.FormatFloat(bal, 'f', -1, 64) return "Your balance: " + strconv.FormatFloat(bal, 'f', -1, 64)
}, },
}, },
{
Id: "nu",
Callback: func(ws *websocket.Conn, data *CommandData) string {
return "I'd just like to interject for a moment. What you're refering to as Linux, is in fact, GNU/Linux, or as I've recently taken to calling it, GNU plus Linux. Linux is not an operating system unto itself, but rather another free component of a fully functioning GNU system made useful by the GNU corelibs, shell utilities and vital system components comprising a full OS as defined by POSIX."
},
},
} }
var botPrefix string = "g" var botPrefix string = "g"
func ReceiveChatMessage(ws *websocket.Conn, msg *ChatMessage) { func ReceiveChatMessage(ws *websocket.Conn, msg *mpp.ChatMppMessage) {
log.Println(msg.Part.Uid[0:6] + " <" + msg.Part.Name + ">: " + msg.Message) log.Println(msg.Part.Uid[0:6] + " <" + msg.Part.Name + ">: " + msg.Message)
if strings.HasPrefix(msg.Message, botPrefix) { if strings.HasPrefix(msg.Message, botPrefix) {
@ -308,7 +261,7 @@ func ReceiveChatMessage(ws *websocket.Conn, msg *ChatMessage) {
cmd := args[0][1:] cmd := args[0][1:]
var data CommandData = CommandData{ var data CommandData = CommandData{
ChatMessage: *msg, ChatMppMessage: *msg,
Args: args, Args: args,
Cmd: cmd, Cmd: cmd,
} }
@ -338,5 +291,5 @@ func SendTimeMessage(ws *websocket.Conn) {
} }
func SendChat(ws *websocket.Conn, message string) { func SendChat(ws *websocket.Conn, message string) {
ws.WriteMessage(1, []byte("[{\"m\":\"a\",\"message\":\"\u034f" + message + "\"}]")) ws.WriteMessage(1, []byte("[{\"m\":\"a\",\"message\":\"\u034f"+message+"\"}]"))
} }

19
mpp/channel.go Normal file
View File

@ -0,0 +1,19 @@
package mpp
type MppChannel struct {
Id string `json:"_id"`
Id2 string `json:"id"`
Crown MppCrown `json:"crown"`
Settings MppChannelSettings `json:"settings"`
}
type MppCrown struct {
ParticipantId string `json:"participantId"`
UserId string `json:"userId"`
StartPos Vector2 `json:"startPos"`
EndPos Vector2 `json:"endPos"`
}
type MppChannelSettings struct {
// TODO
}

42
mpp/message.go Normal file
View File

@ -0,0 +1,42 @@
package mpp
import "encoding/json"
type MppMessage struct {
Type string `json:"m"`
}
type MppTimeMessage struct {
MppMessage
Timestamp json.Number `json:"t"`
}
type HiMppMessage struct {
MppTimeMessage
User MppUser `json:"u"`
Motd string `json:"motd"`
}
type NoteQuotaMppMessage struct {
MppMessage
Allowance json.Number `json:"allowance"`
Max json.Number `json:"max"`
MaxHistLen json.Number `json:"maxHistLen"`
}
type ChatMppMessage struct {
MppTimeMessage
Message string `json:"a"`
Part MppParticipant `json:"p"`
}
type TimeMppMessage struct {
MppTimeMessage
Echo json.Number `json:"e"`
}
type ChannelMppMessage struct {
MppMessage
Channel MppChannel `json:"ch"`
Participants []MppParticipant `json:"ppl"`
}

12
mpp/user.go Normal file
View File

@ -0,0 +1,12 @@
package mpp
type MppUser struct {
Uid string `json:"_id"`
Name string `json:"name"`
Color string `json:"color"`
}
type MppParticipant struct {
MppUser
Id string `json:"id"`
}

8
mpp/vec.go Normal file
View File

@ -0,0 +1,8 @@
package mpp
import "encoding/json"
type Vector2 struct {
X json.Number `json:"x"`
Y json.Number `json:"y"`
}

1
user.go Normal file
View File

@ -0,0 +1 @@