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

109
main.go
View File

@ -12,67 +12,14 @@ import (
"strings"
"time"
"github.com/Hri7566/mpp-client-go/mpp"
"github.com/gorilla/websocket"
"github.com/joho/godotenv"
)
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
func main() {
@ -113,9 +60,9 @@ func StartSocket(address *string) {
go func() {
for {
select {
case <- serverTicker.C:
case <-serverTicker.C:
SendTimeMessage(ws)
case <- serverTickerQuit:
case <-serverTickerQuit:
serverTicker.Stop()
}
}
@ -195,18 +142,18 @@ func ReceiveMessage(ws *websocket.Conn, data []byte) {
_ = json.Unmarshal(raw, &header)
switch header.Type {
case "hi":
msg := HiMessage{}
msg := mpp.HiMppMessage{}
_ = json.Unmarshal(raw, &msg)
ReceiveHiMessage(ws, &msg)
case "nq":
msg := NqMessage{}
msg := mpp.NoteQuotaMppMessage{}
_ = json.Unmarshal(raw, &msg)
ReceiveNqMessage(&msg)
case "t":
msg := TMessage{}
msg := mpp.TimeMppMessage{}
_ = json.Unmarshal(raw, &msg)
case "a":
msg := ChatMessage{}
msg := mpp.ChatMppMessage{}
_ = json.Unmarshal(raw, &msg)
ReceiveChatMessage(ws, &msg)
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)
}
@ -240,10 +187,10 @@ func ReceiveBMessage(ws *websocket.Conn) {
// sentHi = true
}
func ReceiveHiMessage(ws *websocket.Conn, msg *HiMessage) {
func ReceiveHiMessage(ws *websocket.Conn, msg *mpp.HiMppMessage) {
println("I am " + msg.User.Name)
chstr := "[{\"m\":\"ch\",\"_id\":\"Sans' Comedy Corner\"}]"
chstr := "[{\"m\":\"ch\",\"_id\":\"✧𝓓𝓔𝓥 𝓡𝓸𝓸𝓶✧\"}]"
// println("Sending ch message: " + chstr)
err2 := ws.WriteMessage(1, []byte(chstr))
if err2 != nil {
@ -252,13 +199,13 @@ func ReceiveHiMessage(ws *websocket.Conn, msg *HiMessage) {
}
type CommandData struct {
ChatMessage
mpp.ChatMppMessage
Args []string
Cmd string
Cmd string
}
type Command struct {
Id string
Id string
Callback func(ws *websocket.Conn, data *CommandData) string
}
@ -267,19 +214,19 @@ var helpCommands []Command
var commands []Command = []Command{
{
Id: "help",
Callback: func (ws *websocket.Conn, data *CommandData) string {
Callback: func(ws *websocket.Conn, data *CommandData) string {
output := "Commands: "
for _, cmd := range helpCommands {
output += cmd.Id + ", "
}
return strings.Trim(output[:len(output) - 2], " ")
return strings.Trim(output[:len(output)-2], " ")
},
},
{
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"
},
},
@ -287,7 +234,7 @@ var commands []Command = []Command{
Id: "me",
Callback: func(ws *websocket.Conn, data *CommandData) string {
bal, err := GetBalance(data.Part.Uid)
if err != nil {
log.Println(err)
return "An error has occurred."
@ -296,11 +243,17 @@ var commands []Command = []Command{
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"
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)
if strings.HasPrefix(msg.Message, botPrefix) {
@ -308,9 +261,9 @@ func ReceiveChatMessage(ws *websocket.Conn, msg *ChatMessage) {
cmd := args[0][1:]
var data CommandData = CommandData{
ChatMessage: *msg,
Args: args,
Cmd: cmd,
ChatMppMessage: *msg,
Args: args,
Cmd: cmd,
}
for _, command := range commands {
@ -338,5 +291,5 @@ func SendTimeMessage(ws *websocket.Conn) {
}
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 @@