Skip to content

Commit

Permalink
renames, part 2.
Browse files Browse the repository at this point in the history
  • Loading branch information
schwarzlichtbezirk committed Nov 17, 2024
1 parent e3a6284 commit cc8a413
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 83 deletions.
38 changes: 20 additions & 18 deletions api/endpoint.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package core
package api

import (
"encoding/xml"
"time"
)

var Admin AuthResp

type (
ArgSignIs struct {
XMLName xml.Name `json:"-" yaml:"-" xml:"arg"`
Expand Down Expand Up @@ -37,28 +39,28 @@ type (
}
)

func ApiSignIs(email string) (user User, err error) {
func ReqSignIs(email string) (user User, err error) {
var arg = ArgSignIs{
Email: email,
}
var ret RetSignIs
ret, _, err = HttpPost[ArgSignIs, RetSignIs]("/user/is", admin.Access, &arg)
ret, _, err = HttpPost[ArgSignIs, RetSignIs]("/user/is", Admin.Access, &arg)
user.UID = ret.UID
user.Email = ret.Email
user.Name = ret.Name
return
}

func ApiSignIn(email, secret string) (ret AuthResp, err error) {
func ReqSignIn(email, secret string) (ret AuthResp, err error) {
var arg ArgSignIn
arg.Email = email
arg.Secret = secret
ret, _, err = HttpPost[ArgSignIn, AuthResp]("/signin", "", &arg)
return
}

func ApiRefresh() (ret AuthResp, err error) {
ret, _, err = HttpPost[any, AuthResp]("/refresh", admin.Access, nil)
func ReqRefresh() (ret AuthResp, err error) {
ret, _, err = HttpPost[any, AuthResp]("/refresh", Admin.Access, nil)
return
}

Expand Down Expand Up @@ -87,16 +89,16 @@ type (
}
)

func ApiClubList() (ret RetClubList, err error) {
ret, _, err = HttpPost[any, RetClubList]("/club/list", admin.Access, nil)
func ReqClubList() (ret RetClubList, err error) {
ret, _, err = HttpPost[any, RetClubList]("/club/list", Admin.Access, nil)
return
}

func ApiClubInfo(cid uint64) (ret RetClubInfo, err error) {
func ReqClubInfo(cid uint64) (ret RetClubInfo, err error) {
var arg = ArgClubInfo{
CID: cid,
}
ret, _, err = HttpPost[ArgClubInfo, RetClubInfo]("/club/info", admin.Access, &arg)
ret, _, err = HttpPost[ArgClubInfo, RetClubInfo]("/club/info", Admin.Access, &arg)
return
}

Expand Down Expand Up @@ -138,13 +140,13 @@ type (
}
)

func ApiPropGet(cid, uid uint64) (p Props, err error) {
func ReqPropGet(cid, uid uint64) (p Props, err error) {
var arg = ArgPropGet{
CID: cid,
UID: uid,
}
var ret RetPropGet
ret, _, err = HttpPost[ArgPropGet, RetPropGet]("/prop/get", admin.Access, &arg)
ret, _, err = HttpPost[ArgPropGet, RetPropGet]("/prop/get", Admin.Access, &arg)
p = Props{
Wallet: ret.Wallet,
Access: ret.Access,
Expand All @@ -154,37 +156,37 @@ func ApiPropGet(cid, uid uint64) (p Props, err error) {
return
}

func ApiWalletGet(cid, uid uint64) (sum float64, err error) {
func ReqWalletGet(cid, uid uint64) (sum float64, err error) {
var arg = ArgPropGet{
CID: cid,
UID: uid,
}
var ret RetWalletGet
ret, _, err = HttpPost[ArgPropGet, RetWalletGet]("/prop/wallet/get", admin.Access, &arg)
ret, _, err = HttpPost[ArgPropGet, RetWalletGet]("/prop/wallet/get", Admin.Access, &arg)
sum = ret.Wallet
return
}

func ApiAccessGet(cid, uid uint64, all bool) (al AL, err error) {
func ReqAccessGet(cid, uid uint64, all bool) (al AL, err error) {
var arg = ArgAccessGet{
CID: cid,
UID: uid,
All: all,
}
var ret RetAccessGet
ret, _, err = HttpPost[ArgAccessGet, RetAccessGet]("/prop/al/get", admin.Access, &arg)
ret, _, err = HttpPost[ArgAccessGet, RetAccessGet]("/prop/al/get", Admin.Access, &arg)
al = ret.Access
return
}

func ApiRtpGet(cid, uid uint64, all bool) (mrtp float64, err error) {
func ReqRtpGet(cid, uid uint64, all bool) (mrtp float64, err error) {
var arg = ArgRtpGet{
CID: cid,
UID: uid,
All: all,
}
var ret RetRtpGet
ret, _, err = HttpPost[ArgRtpGet, RetRtpGet]("/prop/rtp/get", admin.Access, &arg)
ret, _, err = HttpPost[ArgRtpGet, RetRtpGet]("/prop/rtp/get", Admin.Access, &arg)
mrtp = ret.MRTP
return
}
2 changes: 1 addition & 1 deletion api/http.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core
package api

import (
"bytes"
Expand Down
41 changes: 14 additions & 27 deletions api/logic.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package core
package api

import (
"strings"
"time"

cfg "github.com/slotopol/balance/config"
Expand Down Expand Up @@ -36,40 +35,28 @@ type (

var (
Clubs = map[string]uint64{}
Users = map[string]User{}
Users = map[string]*User{}
Cfg = cfg.Cfg // shortcut
)

func GetProp(cid uint64, user *User) (p Props, err error) {
func (p Props) Expired() bool {
const lag = 20 * time.Millisecond // for refresh synchronization
p = user.props[curcid]
var d = time.Since(p.last)
if d < Cfg.PropUpdateTick-lag {
return // return cached
}
if p, err = ApiPropGet(curcid, user.UID); err != nil {
return d < Cfg.PropUpdateTick-lag
}

func (u *User) GetProps(cid uint64) (p Props, ok bool) {
if u.props == nil {
u.props = map[uint64]Props{} // make new empty map
return
}
user.props[curcid] = p
p, ok = u.props[cid]
return
}

func FormatAL(al AL) string {
var items = make([]string, 0, 5)
if al&ALmem != 0 {
items = append(items, "member")
}
if al&ALgame != 0 {
items = append(items, "game")
}
if al&ALuser != 0 {
items = append(items, "user")
}
if al&ALclub != 0 {
items = append(items, "club")
}
if al&ALadmin != 0 {
items = append(items, "admin")
func (u *User) SetProps(cid uint64, p Props) {
if u.props == nil {
u.props = map[uint64]Props{} // make new empty map
}
return strings.Join(items, ", ")
u.props[cid] = p
}
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package main
import (
"fyne.io/fyne/v2/app"

"github.com/slotopol/balance/core"
"github.com/slotopol/balance/ui"
)

func main() {
var a = app.NewWithID("slotopol.balance")
core.Lifecycle(a)
var frame = &core.Frame{}
ui.Lifecycle(a)
var frame = &ui.Frame{}
frame.CreateWindow(a)
frame.ShowAndRun()
}
62 changes: 47 additions & 15 deletions ui/frame.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package core
package ui

import (
"fmt"
"log"
"strconv"
"strings"

"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/widget"
"github.com/slotopol/balance/api"
cfg "github.com/slotopol/balance/config"
)

Expand All @@ -18,7 +20,11 @@ var Foreground bool

var (
curcid uint64 // current selected at tab club ID
cural AL // access level of loggined account for current selected club
cural api.AL // access level of loggined account for current selected club
)

var (
Cfg = cfg.Cfg // shortcut
)

// Label compatible with ToolbarItem interface to insert into Toolbar.
Expand Down Expand Up @@ -78,6 +84,37 @@ func (l FitLayout) MinSize(objects []fyne.CanvasObject) fyne.Size {
return minSize
}

func GetProp(cid uint64, user *api.User) (p api.Props, err error) {
if p, _ = user.GetProps(curcid); !p.Expired() {
return // return cached
}
if p, err = api.ReqPropGet(curcid, user.UID); err != nil {
return
}
user.SetProps(curcid, p)
return
}

func FormatAL(al api.AL) string {
var items = make([]string, 0, 5)
if al&api.ALmem != 0 {
items = append(items, "member")
}
if al&api.ALgame != 0 {
items = append(items, "game")
}
if al&api.ALuser != 0 {
items = append(items, "user")
}
if al&api.ALclub != 0 {
items = append(items, "club")
}
if al&api.ALadmin != 0 {
items = append(items, "admin")
}
return strings.Join(items, ", ")
}

type Frame struct {
fyne.Window
MainPage
Expand Down Expand Up @@ -155,7 +192,7 @@ func (p *MainPage) Create() {
var err error

var label = cell.(*widget.Label)
var user, ok = Users[cfg.UserList[id.Row]]
var user, ok = api.Users[cfg.UserList[id.Row]]
if !ok {
label.SetText("error")
return
Expand All @@ -164,12 +201,12 @@ func (p *MainPage) Create() {
label.SetText(cfg.UserList[id.Row])
return
}
if cural&ALuser == 0 {
if cural&api.ALuser == 0 {
label.SetText("N/A")
return
}
var prop Props
if prop, err = GetProp(curcid, &user); err != nil {
var prop api.Props
if prop, err = GetProp(curcid, user); err != nil {
label.SetText("error")
return
}
Expand All @@ -191,7 +228,7 @@ func (p *MainPage) Create() {
if id.Row < 0 {
label.SetText(colhdr[id.Col])
} else if id.Col < 0 {
var user, ok = Users[cfg.UserList[id.Row]]
var user, ok = api.Users[cfg.UserList[id.Row]]
if !ok {
label.SetText("error")
return
Expand All @@ -204,11 +241,6 @@ func (p *MainPage) Create() {
ShowHeaderRow: true,
ShowHeaderColumn: true,
}
p.userTable.SetColumnWidth(0, 180) // email
p.userTable.SetColumnWidth(1, 100) // wallet
p.userTable.SetColumnWidth(2, 50) // mtrp
p.userTable.SetColumnWidth(3, 150) // access
p.userTable.ExtendBaseWidget(p.userTable)

// Main page
p.mainPage = container.NewStack(
Expand All @@ -229,9 +261,9 @@ func (p *MainPage) RefreshContent() {

var label = p.clubTabs.Selected().Content.(*widget.Label)
var bank, fund, deposit = "N/A", "N/A", "N/A"
if cural&ALclub != 0 {
var info RetClubInfo
if info, err = ApiClubInfo(curcid); err != nil {
if cural&api.ALclub != 0 {
var info api.RetClubInfo
if info, err = api.ReqClubInfo(curcid); err != nil {
return
}
bank, fund, deposit = fmt.Sprintf("%.2f", info.Bank), fmt.Sprintf("%.2f", info.Fund), fmt.Sprintf("%.2f", info.Lock)
Expand Down
2 changes: 1 addition & 1 deletion ui/res.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package core
package ui

import (
_ "embed"
Expand Down
Loading

0 comments on commit cc8a413

Please sign in to comment.