commit 79b25896d299250fe3f87f1f9fd6611862f04d8b
parent a33280069588ea007845c8c9592aeb4091a1045e
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Wed, 6 Dec 2023 06:58:51 -0500
command to transfer funds
Diffstat:
3 files changed, 53 insertions(+), 5 deletions(-)
diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go
@@ -117,6 +117,11 @@ func ParseInt64(v string) (int64, error) {
return strconv.ParseInt(v, 10, 64)
}
+// ParseInt shortcut for strconv.Atoi
+func ParseInt(v string) (int, error) {
+ return strconv.Atoi(v)
+}
+
// ParseInt64OrDefault ...
func ParseInt64OrDefault(v string, d int64) (out int64) {
var err error
@@ -133,6 +138,12 @@ func DoParseInt64(v string) (out int64) {
return
}
+// DoParseInt same as ParseInt but ignore errors
+func DoParseInt(v string) (out int) {
+ out, _ = ParseInt(v)
+ return
+}
+
// ParseF64 ...
func ParseF64(v string) (float64, error) {
return strconv.ParseFloat(v, 64)
diff --git a/pkg/web/handlers/interceptors/msgInterceptor.go b/pkg/web/handlers/interceptors/msgInterceptor.go
@@ -79,6 +79,7 @@ var memeRemoveRgx = regexp.MustCompile(`^/memerm ([a-zA-Z0-9_-]{3,50})$`)
var memesRgx = regexp.MustCompile(`^/memes$`)
var locateRgx = regexp.MustCompile(`^/locate ` + optAtGUser)
var chipsRgx = regexp.MustCompile(`^/chips ` + optAtGUser + ` (\d+)`)
+var chipsSendRgx = regexp.MustCompile(`^/chips-send ` + optAtGUser + ` (\d+)`)
type MsgInterceptor struct{}
diff --git a/pkg/web/handlers/interceptors/slashInterceptor.go b/pkg/web/handlers/interceptors/slashInterceptor.go
@@ -31,13 +31,18 @@ import (
// different behavior according to the type of error it holds.
// if c.err is set to ErrRedirect, the chat-bar iframe will refresh completely.
// if c.err is set to ErrStop, no further processing of the user input will be done,
-// and the chat iframe will be rendered instead of redirected.
-// This is useful to keep a prefix in the text box (eg: /pm user )
+//
+// and the chat iframe will be rendered instead of redirected.
+// This is useful to keep a prefix in the text box (eg: /pm user )
+//
// if c.err is set to an instance of ErrSuccess,
-// a green message will appear beside the text box.
+//
+// a green message will appear beside the text box.
+//
// otherwise if c.err is set to a different error,
-// text box is retested to original message,
-// and a red message will appear beside the text box.
+//
+// text box is retested to original message,
+// and a red message will appear beside the text box.
type SlashInterceptor struct{}
type CmdHandler func(c *command.Command) (handled bool)
@@ -84,6 +89,7 @@ var userCmdsMap = map[string]CmdHandler{
"/locate": handleLocateCmd,
"/error": handleErrorCmd,
"/chips": handleChipsBalanceCmd,
+ "/chips-send": handleChipsSendCmd,
}
var privateRoomCmdsMap = map[string]CmdHandler{
@@ -1296,6 +1302,36 @@ func handleProfileCmd(c *command.Command) (handled bool) {
return
}
+func handleChipsSendCmd(c *command.Command) (handled bool) {
+ if m := chipsSendRgx.FindStringSubmatch(c.Message); len(m) == 3 {
+ username := database.Username(m[1])
+ chips := utils.DoParseInt(m[2])
+ if chips <= 0 {
+ c.Err = errors.New("must send at least 1 chip")
+ return true
+ }
+ if chips > 1000000 {
+ c.Err = errors.New("cannot send more than 1000000 chips")
+ return true
+ }
+ if c.AuthUser.ChipsTest < chips {
+ c.Err = errors.New("you do not have enough chips")
+ return true
+ }
+ user, err := c.DB.GetUserByUsername(username)
+ if err != nil {
+ c.Err = errors.New("username does not exists")
+ return true
+ }
+ user.ChipsTest += chips
+ user.DoSave(c.DB)
+ c.DataMessage = "/chips-send " + username.String() + " "
+ c.Err = command.NewErrSuccess("chips sent")
+ return true
+ }
+ return
+}
+
func handleChipsBalanceCmd(c *command.Command) (handled bool) {
if c.Message == "/chips" {
c.ZeroMsg(fmt.Sprintf(`Balance: %d`, c.AuthUser.ChipsTest))