Skip to content

Commit

Permalink
atom module added, after & tick functions added.
Browse files Browse the repository at this point in the history
  • Loading branch information
schwarzlichtbezirk committed Sep 18, 2024
1 parent cc72068 commit 7ca8174
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 36 deletions.
6 changes: 4 additions & 2 deletions .luacheckrc
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ globals = {
}

read_globals = {
-- modules
"path", "atom",
-- variables
"quit",
"buildvers", "buildtime", "bindir", "scrdir", "tmpdir",
-- functions
"log", "checkfile", "bin2hex", "hex2bin", "milli2time", "time2milli",
"sleep", "thread",
"log", "checkfile", "bin2hex", "hex2bin", "milli2time", "time2milli", "sec2dur",
"after", "tick", "sleep", "thread",
}

std = { -- Lua 5.1 & GopherLua
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,9 @@ To start group of players emulation run followed script:
bot_win_x64 script/group.lua
```

In this script you can change the value of `usrnum` to determine how many players online your configuration can keep. `jobtime` defines the testing time.

During the testing each user starts randomly from 1 to 15 games, and makes a spin each 1.3 seconds (this emulates playing at several browser tabs). If spin have a win, its waits supposed win animation, and randomly starts double-up bonus game. Average every 50 spins user randomly can change bet value or selected bet lines at last spin game. Sometimes he pause randomly 3..15 seconds. If the money runs out, the admin tops up his balance with a random amount.

---
(c) schwarzlichtbezirk, 2024.
193 changes: 193 additions & 0 deletions luaatom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package main

import (
"sync"

lua "github.com/yuin/gopher-lua"
)

var (
intmap = map[string]int64{}
intmux sync.Mutex
nummap = map[string]float64{}
nummux sync.Mutex
)

// RegAtom is the module loader function.
func RegAtom(ls *lua.LState) {
var mod = ls.RegisterModule("atom", atomfuncs).(*lua.LTable)
_ = mod
}

var atomfuncs = map[string]lua.LGFunction{
"intinc": atomintinc,
"intdec": atomintdec,
"intadd": atomintadd,
"intand": atomintand,
"intor": atomintor,
"intxor": atomintxor,
"intget": atomintget,
"intset": atomintset,
"numinc": atomnuminc,
"numdec": atomnumdec,
"numadd": atomnumadd,
"numget": atomnumget,
"numset": atomnumset,
}

func atomintinc(ls *lua.LState) int {
var name = ls.CheckString(1)

intmux.Lock()
intmap[name]++
var v = intmap[name]
intmux.Unlock()

ls.Push(lua.LNumber(v))
return 1
}

func atomintdec(ls *lua.LState) int {
var name = ls.CheckString(1)

intmux.Lock()
intmap[name]--
var v = intmap[name]
intmux.Unlock()

ls.Push(lua.LNumber(v))
return 1
}

func atomintadd(ls *lua.LState) int {
var name = ls.CheckString(1)
var arg = ls.CheckInt64(2)

intmux.Lock()
intmap[name] += arg
var v = intmap[name]
intmux.Unlock()

ls.Push(lua.LNumber(v))
return 1
}

func atomintand(ls *lua.LState) int {
var name = ls.CheckString(1)
var arg = ls.CheckInt64(2)

intmux.Lock()
intmap[name] &= arg
var v = intmap[name]
intmux.Unlock()

ls.Push(lua.LNumber(v))
return 1
}

func atomintor(ls *lua.LState) int {
var name = ls.CheckString(1)
var arg = ls.CheckInt64(2)

intmux.Lock()
intmap[name] |= arg
var v = intmap[name]
intmux.Unlock()

ls.Push(lua.LNumber(v))
return 1
}

func atomintxor(ls *lua.LState) int {
var name = ls.CheckString(1)
var arg = ls.CheckInt64(2)

intmux.Lock()
intmap[name] ^= arg
var v = intmap[name]
intmux.Unlock()

ls.Push(lua.LNumber(v))
return 1
}

func atomintget(ls *lua.LState) int {
var name = ls.CheckString(1)

intmux.Lock()
var v = intmap[name]
intmux.Unlock()

ls.Push(lua.LNumber(v))
return 1
}

func atomintset(ls *lua.LState) int {
var name = ls.CheckString(1)
var arg = ls.CheckInt64(2)

intmux.Lock()
intmap[name] = arg
intmux.Unlock()

return 0
}

func atomnuminc(ls *lua.LState) int {
var name = ls.CheckString(1)

nummux.Lock()
nummap[name]++
var v = nummap[name]
nummux.Unlock()

ls.Push(lua.LNumber(v))
return 1
}

func atomnumdec(ls *lua.LState) int {
var name = ls.CheckString(1)

nummux.Lock()
nummap[name]--
var v = nummap[name]
nummux.Unlock()

ls.Push(lua.LNumber(v))
return 1
}

func atomnumadd(ls *lua.LState) int {
var name = ls.CheckString(1)
var arg = ls.CheckNumber(2)

nummux.Lock()
nummap[name] += float64(arg)
var v = nummap[name]
nummux.Unlock()

ls.Push(lua.LNumber(v))
return 1
}

func atomnumget(ls *lua.LState) int {
var name = ls.CheckString(1)

nummux.Lock()
var v = nummap[name]
nummux.Unlock()

ls.Push(lua.LNumber(v))
return 1
}

func atomnumset(ls *lua.LState) int {
var name = ls.CheckString(1)
var arg = ls.CheckInt64(2)

nummux.Lock()
nummap[name] = float64(arg)
nummux.Unlock()

return 0
}
9 changes: 3 additions & 6 deletions luapath.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,10 @@ import (
lua "github.com/yuin/gopher-lua"
)

// LoadPath is the module loader function.
func LoadPath(ls *lua.LState) int {
mod := ls.NewTable()
ls.SetFuncs(mod, pathfuncs)
// RegPath is the module loader function.
func RegPath(ls *lua.LState) {
var mod = ls.RegisterModule("path", pathfuncs).(*lua.LTable)
mod.RawSetString("sep", lua.LString("/"))
ls.Push(mod)
return 1
}

var pathfuncs = map[string]lua.LGFunction{
Expand Down
59 changes: 49 additions & 10 deletions runlvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,43 @@ func luatime2milli(ls *lua.LState) int {
return 1
}

func luasec2dur(ls *lua.LState) int {
var sec = float64(ls.CheckNumber(1))
var gran = float64(ls.OptNumber(2, 1))

var dur = time.Duration(sec * float64(time.Second))
var dg = time.Duration(gran * float64(time.Second))
dur = dur.Round(dg)
ls.Push(lua.LString(dur.String()))
return 1
}

func luaafter(ls *lua.LState) int {
var milli = ls.CheckInt64(1)
var ch = ls.CheckChannel(2)

go func() {
<-time.After(time.Duration(milli) * time.Millisecond)
<-ch
}()
return 0
}

func luatick(ls *lua.LState) int {
var milli = ls.CheckInt64(1)
var ch = ls.CheckChannel(2)

go func() {
var c = time.Tick(time.Duration(milli) * time.Millisecond)
for range c {
if _, ok := <-ch; !ok {
break
}
}
}()
return 0
}

func luasleep(ls *lua.LState) int {
var err error
defer func() {
Expand Down Expand Up @@ -189,9 +226,10 @@ func luathread(ls *lua.LState) int {
}
}

var tls = MakeLuaVM() // thread Lua state
var tls = lua.NewState() // thread Lua state
go func() {
defer tls.Close()
InitLuaVM(tls)

for key, val := range args {
tls.SetGlobal(key, val)
Expand Down Expand Up @@ -239,12 +277,11 @@ func WaitQuit() {
signal.Stop(sigterm)
}

// MakeLuaVM creates Lua state and performs initial registrations.
func MakeLuaVM() *lua.LState {
var ls = lua.NewState()

// preload modules
ls.PreloadModule("path", LoadPath)
// InitLuaVM performs initial registrations.
func InitLuaVM(ls *lua.LState) {
// set modules
RegPath(ls)
RegAtom(ls)
ls.PreloadModule("http", gluahttp.NewHttpModule(&http.Client{}).Loader)
ls.PreloadModule("json", json.Loader)
ls.PreloadModule("crypto", crypto.Loader)
Expand All @@ -270,16 +307,18 @@ func MakeLuaVM() *lua.LState {
ls.SetGlobal("hex2bin", ls.NewFunction(luahex2bin))
ls.SetGlobal("milli2time", ls.NewFunction(luamilli2time))
ls.SetGlobal("time2milli", ls.NewFunction(luatime2milli))
ls.SetGlobal("sec2dur", ls.NewFunction(luasec2dur))
ls.SetGlobal("after", ls.NewFunction(luaafter))
ls.SetGlobal("tick", ls.NewFunction(luatick))
ls.SetGlobal("sleep", ls.NewFunction(luasleep))
ls.SetGlobal("thread", ls.NewFunction(luathread))

return ls
}

// RunLuaVM runs specified Lua-script with Lua Bot API.
func RunLuaVM(fpath string) (err error) {
var ls = MakeLuaVM()
var ls = lua.NewState()
defer ls.Close()
InitLuaVM(ls)

var scrdir = path.Dir(util.ToSlash(fpath))
ls.SetGlobal("scrdir", lua.LString(scrdir))
Expand Down
Loading

0 comments on commit 7ca8174

Please sign in to comment.