Compare commits
3 Commits
Author | SHA1 | Date | |
---|---|---|---|
196056099b | |||
d701d1604e | |||
dd3a079d62 |
3
.gitignore
vendored
3
.gitignore
vendored
@ -8,9 +8,6 @@
|
|||||||
*.so
|
*.so
|
||||||
*.dylib
|
*.dylib
|
||||||
|
|
||||||
# Normal binary, `go build`
|
|
||||||
watbot
|
|
||||||
|
|
||||||
# Test binary, build with `go test -c`
|
# Test binary, build with `go test -c`
|
||||||
*.test
|
*.test
|
||||||
|
|
||||||
|
6
DOCKERFILE
Normal file
6
DOCKERFILE
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
FROM golang
|
||||||
|
RUN mkdir /app
|
||||||
|
ADD . /app
|
||||||
|
WORKDIR /app
|
||||||
|
RUN go build /app
|
||||||
|
CMD [ "/app/watbot"]
|
22
README.md
22
README.md
@ -1,5 +1,23 @@
|
|||||||
# watbot
|
# watbot
|
||||||
|
|
||||||
watbot
|
|
||||||
|
|
||||||
ported watbot from wuselfuzz
|
ported watbot from wuselfuzz
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
### Prerequisites
|
||||||
|
|
||||||
|
* podman
|
||||||
|
* git
|
||||||
|
|
||||||
|
|
||||||
|
### Setup
|
||||||
|
|
||||||
|
* `git clone <repo>`
|
||||||
|
* `cd <repo>`
|
||||||
|
* `podman build -f DOCKERFILE`
|
||||||
|
* `podman run <container-id>
|
||||||
|
|
||||||
|
Note: You might wanna detach at the end there.
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,27 +0,0 @@
|
|||||||
watbot:
|
|
||||||
database: wat.db # wat.db (in the working directory) is the default
|
|
||||||
server:
|
|
||||||
host: irc.casa # mandatory, no default
|
|
||||||
port: 6697
|
|
||||||
tls_verify: true
|
|
||||||
name: watest
|
|
||||||
nick: watest # nick is name by default
|
|
||||||
user: watest # user is nick by default
|
|
||||||
bots: # optional, no default
|
|
||||||
games: # mapping of bot names to games
|
|
||||||
katyusha:
|
|
||||||
- jeopardy # currently jeopardy is the only integrated game
|
|
||||||
hosts: # hostmasks considered as valid bots
|
|
||||||
- bot.example.com
|
|
||||||
admins: # optional, no default
|
|
||||||
hosts:
|
|
||||||
- admin.example.com
|
|
||||||
ignores: # optional, no default
|
|
||||||
hosts:
|
|
||||||
- annoying.example.com
|
|
||||||
channels: # optional, no default
|
|
||||||
join:
|
|
||||||
- crantest # channels without a prefix character will be prefixed with "#"
|
|
||||||
permitted:
|
|
||||||
- '#lucy'
|
|
||||||
|
|
6
go.mod
6
go.mod
@ -3,12 +3,8 @@ module git.circuitco.de/self/watbot
|
|||||||
go 1.15
|
go 1.15
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/creasty/defaults v1.8.0
|
|
||||||
github.com/ergochat/irc-go v0.4.0
|
|
||||||
github.com/go-irc/irc v2.1.0+incompatible
|
github.com/go-irc/irc v2.1.0+incompatible
|
||||||
github.com/stretchr/testify v1.9.0 // indirect
|
github.com/namsral/flag v1.7.4-pre
|
||||||
gopkg.in/yaml.v2 v2.4.0 // indirect
|
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
|
||||||
gorm.io/driver/sqlite v1.1.4
|
gorm.io/driver/sqlite v1.1.4
|
||||||
gorm.io/gorm v1.20.11
|
gorm.io/gorm v1.20.11
|
||||||
)
|
)
|
||||||
|
28
go.sum
28
go.sum
@ -1,10 +1,4 @@
|
|||||||
github.com/creasty/defaults v1.8.0 h1:z27FJxCAa0JKt3utc0sCImAEb+spPucmKoOdLHvHYKk=
|
github.com/go-irc/irc v1.3.0 h1:IMD+d/+EzY51ecMLOz73r/NXTZrEp8khrePxRCvX71M=
|
||||||
github.com/creasty/defaults v1.8.0/go.mod h1:iGzKe6pbEHnpMPtfDXZEr0NVxWnPTjb1bbDy08fPzYM=
|
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
|
||||||
github.com/ergochat/irc-go v0.4.0 h1:0YibCKfAAtwxQdNjLQd9xpIEPisLcJ45f8FNsMHAuZc=
|
|
||||||
github.com/ergochat/irc-go v0.4.0/go.mod h1:2vi7KNpIPWnReB5hmLpl92eMywQvuIeIIGdt/FQCph0=
|
|
||||||
github.com/go-irc/irc v2.1.0+incompatible h1:pg7pMVq5OYQbqTxceByD/EN8VIsba7DtKn49rsCnG8Y=
|
github.com/go-irc/irc v2.1.0+incompatible h1:pg7pMVq5OYQbqTxceByD/EN8VIsba7DtKn49rsCnG8Y=
|
||||||
github.com/go-irc/irc v2.1.0+incompatible/go.mod h1:jJILTRy8s/qOvusiKifAEfhQMVwft1ZwQaVJnnzmyX4=
|
github.com/go-irc/irc v2.1.0+incompatible/go.mod h1:jJILTRy8s/qOvusiKifAEfhQMVwft1ZwQaVJnnzmyX4=
|
||||||
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
|
||||||
@ -13,24 +7,8 @@ github.com/jinzhu/now v1.1.1 h1:g39TucaRWyV3dwDO++eEc6qf8TVIQ/Da48WmqjZ3i7E=
|
|||||||
github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
github.com/jinzhu/now v1.1.1/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
|
||||||
github.com/mattn/go-sqlite3 v1.14.5 h1:1IdxlwTNazvbKJQSxoJ5/9ECbEeaTTyeU7sEAZ5KKTQ=
|
github.com/mattn/go-sqlite3 v1.14.5 h1:1IdxlwTNazvbKJQSxoJ5/9ECbEeaTTyeU7sEAZ5KKTQ=
|
||||||
github.com/mattn/go-sqlite3 v1.14.5/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
|
github.com/mattn/go-sqlite3 v1.14.5/go.mod h1:WVKg1VTActs4Qso6iwGbiFih2UIHo0ENGwNd0Lj+XmI=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/namsral/flag v1.7.4-pre h1:b2ScHhoCUkbsq0d2C15Mv+VU8bl8hAXV8arnWiOHNZs=
|
||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/namsral/flag v1.7.4-pre/go.mod h1:OXldTctbM6SWH1K899kPZcf65KxJiD7MsceFUpB5yDo=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
|
||||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
|
||||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
|
||||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
|
||||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
|
||||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
|
||||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
|
||||||
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
|
||||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
|
||||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
|
||||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
||||||
gorm.io/driver/sqlite v1.1.4 h1:PDzwYE+sI6De2+mxAneV9Xs11+ZyKV6oxD3wDGkaNvM=
|
gorm.io/driver/sqlite v1.1.4 h1:PDzwYE+sI6De2+mxAneV9Xs11+ZyKV6oxD3wDGkaNvM=
|
||||||
gorm.io/driver/sqlite v1.1.4/go.mod h1:mJCeTFr7+crvS+TRnWc5Z3UvwxUN1BGBLMrf5LA9DYw=
|
gorm.io/driver/sqlite v1.1.4/go.mod h1:mJCeTFr7+crvS+TRnWc5Z3UvwxUN1BGBLMrf5LA9DYw=
|
||||||
gorm.io/gorm v1.20.7/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
|
gorm.io/gorm v1.20.7/go.mod h1:0HFTzE/SqkGTzK6TlDPPQbAYCluiVvhzoA1+aVyzenw=
|
||||||
|
125
main.go
125
main.go
@ -1,121 +1,40 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import "fmt"
|
||||||
"crypto/tls"
|
import "crypto/tls"
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"log"
|
|
||||||
"os"
|
|
||||||
|
|
||||||
"flag"
|
import "github.com/go-irc/irc"
|
||||||
"github.com/go-irc/irc"
|
import "github.com/namsral/flag"
|
||||||
|
|
||||||
"git.circuitco.de/self/watbot/wat"
|
import "git.circuitco.de/self/watbot/wat"
|
||||||
"github.com/creasty/defaults"
|
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Config struct {
|
|
||||||
Watbot watConfig `yaml:"watbot"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type watConfig struct {
|
|
||||||
Database string `default:"wat.db" yaml:"database"`
|
|
||||||
Nick string `yaml:"nick"`
|
|
||||||
Pass string `yaml:"pass"`
|
|
||||||
User string `yaml:"user"`
|
|
||||||
Name string `yaml:"name"`
|
|
||||||
Bots struct {
|
|
||||||
Hosts []string `yaml:"hosts"`
|
|
||||||
Games wat.BotGameConfig `yaml:"games"`
|
|
||||||
} `yaml:"bots"`
|
|
||||||
Admins struct {
|
|
||||||
Hosts []string `yaml:"hosts"`
|
|
||||||
} `yaml:"admins"`
|
|
||||||
Channels struct {
|
|
||||||
Join []string `yaml:"join"`
|
|
||||||
Permitted []string `yaml:"permitted"`
|
|
||||||
} `yaml:"channels"`
|
|
||||||
Ignores struct {
|
|
||||||
Hosts []string `yaml:"hosts"`
|
|
||||||
} `yaml:"ignores"`
|
|
||||||
Server struct {
|
|
||||||
Host string `yaml:"host"`
|
|
||||||
Port int `default:"6697" yaml:"port"`
|
|
||||||
TlsVerify bool `default:"true" yaml:"tls_verify"`
|
|
||||||
} `yaml:"server"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func readConfig(configPath string) (*watConfig, error) {
|
|
||||||
allConfig := Config{}
|
|
||||||
|
|
||||||
buffer, err := os.ReadFile(configPath)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("Could not read configuration file: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = yaml.Unmarshal(buffer, &allConfig)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("Could not parse configuration file: %s", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
config := &allConfig.Watbot
|
|
||||||
defaults.Set(config)
|
|
||||||
|
|
||||||
if config.Server.Host == "" {
|
|
||||||
return nil, errors.New("Shall I play wattery to know where to connect to?")
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.Name != "" && config.Nick == "" {
|
|
||||||
config.Nick = config.Name
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.Nick != "" && config.User == "" {
|
|
||||||
config.User = config.Nick
|
|
||||||
}
|
|
||||||
|
|
||||||
if config.Name == "" || config.Nick == "" || config.User == "" {
|
|
||||||
return nil, errors.New("Don't know who I am.")
|
|
||||||
}
|
|
||||||
|
|
||||||
return config, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
var configPathArg string
|
pass := flag.String("pass", "", "password")
|
||||||
flag.StringVar(&configPathArg, "config", "config.yaml", "Path to configuration file")
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
log.Println("Starting with configuration:", configPathArg)
|
fmt.Printf("PASS len %d\n", len(*pass))
|
||||||
|
config := irc.ClientConfig{
|
||||||
config, err := readConfig(configPathArg)
|
Nick: "watbot",
|
||||||
if err != nil {
|
Pass: *pass,
|
||||||
log.Fatalln(err)
|
User: "watbot",
|
||||||
os.Exit(1)
|
Name: "watbot",
|
||||||
}
|
|
||||||
|
|
||||||
ircConfig := irc.ClientConfig{
|
|
||||||
Nick: config.Nick,
|
|
||||||
Pass: config.Pass,
|
|
||||||
User: config.User,
|
|
||||||
Name: config.Name,
|
|
||||||
}
|
}
|
||||||
watConfig := wat.WatConfig{
|
watConfig := wat.WatConfig{
|
||||||
DatabasePath: config.Database,
|
PermittedChannels: []string{
|
||||||
AutoJoinChannels: config.Channels.Join,
|
"#lucy",
|
||||||
PermittedChannels: config.Channels.Permitted,
|
"#sweden",
|
||||||
IgnoredHosts: config.Ignores.Hosts,
|
},
|
||||||
AdminHosts: config.Admins.Hosts,
|
IgnoredHosts: []string{
|
||||||
BotHosts: config.Bots.Hosts,
|
"tripsit/user/creatonez",
|
||||||
BotGames: config.Bots.Games,
|
},
|
||||||
}
|
}
|
||||||
tcpConf := &tls.Config{
|
tcpConf := &tls.Config{
|
||||||
InsecureSkipVerify: !config.Server.TlsVerify,
|
InsecureSkipVerify: false,
|
||||||
}
|
}
|
||||||
conn, err := tls.Dial("tcp", fmt.Sprintf("%s:%d", config.Server.Host, config.Server.Port), tcpConf)
|
conn, err := tls.Dial("tcp", "irc.casa:6697", tcpConf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println("err " + err.Error())
|
fmt.Println("err " + err.Error())
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
wwat := wat.NewWatBot(&ircConfig, &watConfig, conn)
|
wwat := wat.NewWatBot(&config, &watConfig, conn)
|
||||||
wwat.Run()
|
wwat.Run()
|
||||||
}
|
}
|
||||||
|
@ -1,15 +0,0 @@
|
|||||||
#!/usr/bin/python
|
|
||||||
|
|
||||||
# LICENSE: DO WAT YOU WANT WITH THIS!
|
|
||||||
|
|
||||||
|
|
||||||
from watbot_config import WatbotConfig
|
|
||||||
from watbot_db import WatbotDB
|
|
||||||
from watbot_game import WatbotGame
|
|
||||||
from watbot_irc import WatbotIRC
|
|
||||||
|
|
||||||
db = WatbotDB(WatbotConfig)
|
|
||||||
game = WatbotGame(WatbotConfig, db)
|
|
||||||
irc = WatbotIRC(WatbotConfig, game)
|
|
||||||
|
|
||||||
irc.main_loop()
|
|
@ -1,19 +0,0 @@
|
|||||||
#!/usr/bin/python
|
|
||||||
|
|
||||||
from watbot_config import WatbotConfig
|
|
||||||
from watbot_db import WatbotDB
|
|
||||||
from watbot_game import WatbotGame
|
|
||||||
from watbot_console import WatbotConsole
|
|
||||||
|
|
||||||
import sys
|
|
||||||
|
|
||||||
if len(sys.argv) > 1:
|
|
||||||
|
|
||||||
db = WatbotDB(WatbotConfig)
|
|
||||||
game = WatbotGame(WatbotConfig, db)
|
|
||||||
con = WatbotConsole(WatbotConfig, game, sys.argv[1])
|
|
||||||
|
|
||||||
con.main_loop()
|
|
||||||
else:
|
|
||||||
print "usage: " + sys.argv[0] + " <nickname>"
|
|
||||||
|
|
@ -1,13 +0,0 @@
|
|||||||
#!/usr/bin/python
|
|
||||||
|
|
||||||
config = {
|
|
||||||
"prefix": "abc",
|
|
||||||
"currency": "abccoin",
|
|
||||||
"bot_nick": "abcbot",
|
|
||||||
"channel": "#abc",
|
|
||||||
"server": "irc.abc.example.com",
|
|
||||||
"port": 6667,
|
|
||||||
"ssl": False,
|
|
||||||
"nickserv": False,
|
|
||||||
"password": ""
|
|
||||||
}
|
|
@ -1,143 +0,0 @@
|
|||||||
#!/usr/bin/python
|
|
||||||
|
|
||||||
import sqlite3
|
|
||||||
import time
|
|
||||||
|
|
||||||
|
|
||||||
class WatbotDB:
|
|
||||||
"""watbot database abstraction"""
|
|
||||||
|
|
||||||
def __init__(self, config):
|
|
||||||
self.config = config
|
|
||||||
self.currency = config["currency"]
|
|
||||||
self.conn = sqlite3.connect(self.currency + ".db")
|
|
||||||
self.c = self.conn.cursor()
|
|
||||||
|
|
||||||
self.c.execute("create table if not exists players ("
|
|
||||||
" nickname varchar(256),"
|
|
||||||
" watting integer,"
|
|
||||||
" anarchy integer, "
|
|
||||||
" trickery integer, "
|
|
||||||
" coins integer, "
|
|
||||||
" health integer, "
|
|
||||||
" last_mine integer, "
|
|
||||||
" last_rest integer, "
|
|
||||||
" primary key(nickname)"
|
|
||||||
")"
|
|
||||||
)
|
|
||||||
|
|
||||||
self.c.execute("create table if not exists ledger ("
|
|
||||||
" nickname varchar(256),"
|
|
||||||
" timestamp integer,"
|
|
||||||
" balance integer,"
|
|
||||||
" log text,"
|
|
||||||
" foreign key(nickname) references players(nickname)"
|
|
||||||
")"
|
|
||||||
)
|
|
||||||
|
|
||||||
self.c.execute("create table if not exists items ("
|
|
||||||
" itemname varchar(256),"
|
|
||||||
" nickname varchar(256),"
|
|
||||||
" price integer,"
|
|
||||||
" primary key(itemname),"
|
|
||||||
" foreign key(nickname) references players(nickname)"
|
|
||||||
")"
|
|
||||||
)
|
|
||||||
|
|
||||||
self.c.execute("create table if not exists inventory ("
|
|
||||||
" nickname varchar(256),"
|
|
||||||
" itemname varchar(256),"
|
|
||||||
" count integer,"
|
|
||||||
" foreign key(nickname) references players(nickname),"
|
|
||||||
" foreign key(itemname) references items(itemname)"
|
|
||||||
")"
|
|
||||||
)
|
|
||||||
|
|
||||||
self.conn.commit()
|
|
||||||
|
|
||||||
def get_account(self,nick):
|
|
||||||
self.c.execute("select watting, anarchy, trickery, coins, last_mine, health, last_rest from players where nickname=?", (nick.lower(),))
|
|
||||||
data = self.c.fetchone()
|
|
||||||
|
|
||||||
if data is None:
|
|
||||||
earlier = time.time() - ( 24 * 3600 + 1)
|
|
||||||
self.c.execute("insert into players(nickname, watting, anarchy, trickery, coins, last_mine, health, last_rest) values(?, 0, 0, 0, 0, ?, 100, ?)", (nick.lower(), earlier, earlier))
|
|
||||||
self.c.execute("insert into ledger(nickname, timestamp, balance, log) values(?, ?, ?, ?)", (nick.lower(), earlier, 0, "created"))
|
|
||||||
self.conn.commit()
|
|
||||||
data = (0, 0, 0, 0, earlier, 100, earlier)
|
|
||||||
|
|
||||||
return data
|
|
||||||
|
|
||||||
def update_account(self, nick, watting, anarchy, trickery, coins, last_mine, health, last_rest, log):
|
|
||||||
now = time.time()
|
|
||||||
self.c.execute("update players set watting=?, anarchy=?, trickery=?, coins=?, last_mine=?, health=?, last_rest=? where nickname=?", (watting, anarchy, trickery, coins, last_mine, health, last_rest, nick.lower()))
|
|
||||||
self.c.execute("insert into ledger(nickname, timestamp, balance, log) values(?, ?, ?, ?)", (nick.lower(), now, coins, log))
|
|
||||||
if not log is None:
|
|
||||||
print "log: " + log
|
|
||||||
self.conn.commit()
|
|
||||||
|
|
||||||
def close(self):
|
|
||||||
self.conn.close()
|
|
||||||
|
|
||||||
def topten(self):
|
|
||||||
out = ""
|
|
||||||
self.c.execute("select nickname, coins from players order by coins desc limit 10")
|
|
||||||
while True:
|
|
||||||
d = self.c.fetchone()
|
|
||||||
if d == None:
|
|
||||||
break
|
|
||||||
out += d[0] + "(" + str(d[1]) + ") "
|
|
||||||
|
|
||||||
return out
|
|
||||||
|
|
||||||
def items(self):
|
|
||||||
out = ""
|
|
||||||
self.c.execute("select itemname, nickname, price from items order by itemname")
|
|
||||||
while True:
|
|
||||||
d = self.c.fetchone()
|
|
||||||
if d == None:
|
|
||||||
break
|
|
||||||
out += d[0] + "(" + d[1] + ", " + str(d[2]) + ") "
|
|
||||||
|
|
||||||
return out
|
|
||||||
|
|
||||||
def invent_item(self, itemname, nickname, price):
|
|
||||||
try:
|
|
||||||
self.c.execute("insert into items(itemname, nickname, price) values(?, ?, ?)", (itemname.lower(), nickname.lower(), price))
|
|
||||||
self.conn.commit()
|
|
||||||
return True
|
|
||||||
except:
|
|
||||||
return False
|
|
||||||
|
|
||||||
def inventory(self, nickname):
|
|
||||||
out = ""
|
|
||||||
self.c.execute("select itemname, count from inventory where nickname = ? order by itemname", (nickname.lower(),))
|
|
||||||
while True:
|
|
||||||
d = self.c.fetchone()
|
|
||||||
if d == None:
|
|
||||||
break
|
|
||||||
out += d[0] + "(" + str(d[1]) + ") "
|
|
||||||
|
|
||||||
return out
|
|
||||||
|
|
||||||
def get_item(self, itemname):
|
|
||||||
self.c.execute("select itemname, nickname, price from items where itemname = ?", (itemname.lower(),))
|
|
||||||
return self.c.fetchone()
|
|
||||||
|
|
||||||
def get_item_count(self, nickname, itemname):
|
|
||||||
self.c.execute("select count from inventory where nickname = ? and itemname = ?", (nickname.lower(), itemname.lower()))
|
|
||||||
d = self.c.fetchone()
|
|
||||||
if d == None:
|
|
||||||
return 0
|
|
||||||
else:
|
|
||||||
return d[0]
|
|
||||||
|
|
||||||
def set_item_count(self, nickname, itemname, count):
|
|
||||||
if count == 0:
|
|
||||||
self.c.execute("delete from inventory where nickname = ? and itemname = ?", (nickname.lower(), itemname.lower()))
|
|
||||||
else:
|
|
||||||
if self.get_item_count(nickname, itemname) != 0:
|
|
||||||
self.c.execute("update inventory set count=? where nickname = ? and itemname = ?", (count, nickname.lower(), itemname.lower()))
|
|
||||||
else:
|
|
||||||
self.c.execute("insert into inventory(nickname, itemname, count) values(?, ?, ?)", (nickname.lower(), itemname.lower(), count))
|
|
||||||
self.conn.commit()
|
|
@ -1,440 +0,0 @@
|
|||||||
#!/usr/bin/python
|
|
||||||
|
|
||||||
import time
|
|
||||||
import random
|
|
||||||
import math
|
|
||||||
|
|
||||||
from watbot_player import WatbotPlayer
|
|
||||||
|
|
||||||
class WatbotGame:
|
|
||||||
"""Class containing the game logic"""
|
|
||||||
|
|
||||||
def __init__(self, config, db):
|
|
||||||
random.seed()
|
|
||||||
|
|
||||||
self.config = config
|
|
||||||
self.db = db
|
|
||||||
|
|
||||||
self.bot_nick = config["bot_nick"]
|
|
||||||
self.bot_player = WatbotPlayer(self.db, self.bot_nick)
|
|
||||||
|
|
||||||
self.quiet = False
|
|
||||||
|
|
||||||
self.help_text = (
|
|
||||||
"balance [<nickname>], "
|
|
||||||
"watch [<nickname>], "
|
|
||||||
"inventory [<nickname>], "
|
|
||||||
"topten, "
|
|
||||||
"items, "
|
|
||||||
"mine, "
|
|
||||||
"transfer <nickname> <amount>, "
|
|
||||||
"roll <amount>, "
|
|
||||||
"steal <nickname> <amount>, "
|
|
||||||
"frame <nickname> <amount>, "
|
|
||||||
"punch <nickname>, "
|
|
||||||
"give <nickname> <count> <itemname>, "
|
|
||||||
"invent <itemname> <price>, "
|
|
||||||
"create <itemname> <count>, "
|
|
||||||
|
|
||||||
)
|
|
||||||
|
|
||||||
self.rules_text = ("A new account is created with 5 hours time credit. "
|
|
||||||
"Mining exchanges time credit for coins: "
|
|
||||||
"1h - 10h: 1 coin/h; >10h: 10 coin; >1 day: 50 coin; >1 month: 1000 coin.")
|
|
||||||
|
|
||||||
def do_command(self, nick, command, args):
|
|
||||||
try:
|
|
||||||
player = WatbotPlayer(self.db, nick)
|
|
||||||
self.now = time.time()
|
|
||||||
|
|
||||||
if command == "wat":
|
|
||||||
out = self.mega_wat(player)
|
|
||||||
|
|
||||||
elif command == "speak" and (player.nick == "Burrito" or player.nick == "wuselfuzz"):
|
|
||||||
self.quiet = False
|
|
||||||
out = "wat!"
|
|
||||||
|
|
||||||
elif command == "shutup" and (player.nick == "Burrito" or player.nick == "wuselfuzz"):
|
|
||||||
self.quiet = True
|
|
||||||
out = "wat."
|
|
||||||
|
|
||||||
elif self.quiet:
|
|
||||||
out = None
|
|
||||||
|
|
||||||
elif command == "help":
|
|
||||||
out = self.help_text
|
|
||||||
|
|
||||||
elif command == "rules":
|
|
||||||
out = self.rules_text
|
|
||||||
|
|
||||||
elif command == "topten":
|
|
||||||
out = self.db.topten()
|
|
||||||
|
|
||||||
elif command == "items":
|
|
||||||
out = "temporarily disabled, because bug!"
|
|
||||||
#out = self.db.items()
|
|
||||||
|
|
||||||
elif command == "inventory":
|
|
||||||
if len(args) > 0:
|
|
||||||
out = self.db.inventory(args[0])
|
|
||||||
else:
|
|
||||||
out = self.db.inventory(player.nick)
|
|
||||||
|
|
||||||
elif command == "watch":
|
|
||||||
if len(args) > 0:
|
|
||||||
out = self.watch(self.get_target_player(player, args[0]))
|
|
||||||
else:
|
|
||||||
out = self.watch(player)
|
|
||||||
|
|
||||||
elif command == "balance":
|
|
||||||
if len(args) > 0:
|
|
||||||
out = self.balance(self.get_target_player(player, args[0]))
|
|
||||||
else:
|
|
||||||
out = self.balance(player)
|
|
||||||
|
|
||||||
elif command == "mine":
|
|
||||||
out = self.mine(player)
|
|
||||||
|
|
||||||
elif command == "transfer":
|
|
||||||
if len(args) < 2:
|
|
||||||
out = "transfer <target> <amount>"
|
|
||||||
else:
|
|
||||||
out = self.transfer(player, self.get_target_player(player, args[0]), int(args[1]))
|
|
||||||
|
|
||||||
elif player.health <= 0:
|
|
||||||
out = "You cannot do that while unconscious!"
|
|
||||||
|
|
||||||
# ----- commands that require consciousness below -----
|
|
||||||
|
|
||||||
elif command == "steal":
|
|
||||||
if len(args) < 2:
|
|
||||||
out = "steal <target> <amount> - rolls a d6. If <3, you steal <target> <amount> coins. Otherwise, you pay a <amount> * 2 fine to "+ self.bot_nick + "."
|
|
||||||
else:
|
|
||||||
out = self.steal(player, self.get_target_player(player, args[0]), int(args[1]))
|
|
||||||
|
|
||||||
elif command == "frame":
|
|
||||||
if len(args) < 2:
|
|
||||||
out = "frame <target> <amount> - rolls a d6. If <3, you make <target> pay a fine of <amount> coins to " + self.bot_nick + ". Otherwise, you pay a ceil(<amount>/2) to <target and floor(<amount>/2) to " + self.bot_nick + " as fines."
|
|
||||||
else:
|
|
||||||
out = self.frame(player, self.get_target_player(player, args[0]), int(args[1]))
|
|
||||||
|
|
||||||
elif command == "punch":
|
|
||||||
if len(args) < 1:
|
|
||||||
out = "punch <target>"
|
|
||||||
else:
|
|
||||||
out = self.punch(player, self.get_target_player(player, args[0]))
|
|
||||||
|
|
||||||
elif command == "roll":
|
|
||||||
if len(args) < 1:
|
|
||||||
out = "roll <amount> - rolls a d100 against watcoinbot. result<50 wins <amount>, result >=50 loses <amount>"
|
|
||||||
else:
|
|
||||||
out = self.roll(player, int(args[0]))
|
|
||||||
|
|
||||||
elif command == "invent":
|
|
||||||
if len(args) < 2:
|
|
||||||
out = "invent <itemname> <price> - invent an item called <itemname> which can be bought for <price>. An invention costs 100 coins."
|
|
||||||
else:
|
|
||||||
out = self.invent(player, args[0], int(args[1]))
|
|
||||||
|
|
||||||
elif command == "create":
|
|
||||||
if len(args) < 2:
|
|
||||||
out = "create <itemname> <count> - create <count> <itemname>s. You have to pay the price and must be the inventor of the item!"
|
|
||||||
else:
|
|
||||||
out = self.create(player, args[0], int(args[1]))
|
|
||||||
|
|
||||||
elif command == "give":
|
|
||||||
if len(args) < 3:
|
|
||||||
out = "give <target> <count> <itemname>"
|
|
||||||
else:
|
|
||||||
out = self.give(player, args[0], int(args[1]), args[2])
|
|
||||||
|
|
||||||
else:
|
|
||||||
out = None
|
|
||||||
|
|
||||||
return out
|
|
||||||
|
|
||||||
except:
|
|
||||||
return "wat?"
|
|
||||||
|
|
||||||
def get_target_player(self, player, target_nick):
|
|
||||||
if target_nick == player.nick:
|
|
||||||
return player
|
|
||||||
elif target_nick == self.bot_nick:
|
|
||||||
return self.bot_player
|
|
||||||
else:
|
|
||||||
return WatbotPlayer(self.db, target_nick)
|
|
||||||
|
|
||||||
def watch(self, player):
|
|
||||||
out = (
|
|
||||||
"Watting: " + str(player.watting) + "(" + str(player.watting_exp) + ") / " +
|
|
||||||
"Anarchy: " + str(player.anarchy) + "(" + str(player.anarchy_exp) + ") / " +
|
|
||||||
"Trickery: " + str(player.trickery) + "(" + str(player.trickery_exp) + ") " +
|
|
||||||
"Coins: " + str(player.coins) + " " +
|
|
||||||
"Health: " + str(player.health)
|
|
||||||
)
|
|
||||||
return out
|
|
||||||
|
|
||||||
def balance(self, player):
|
|
||||||
out = player.nick + "'s watcoin balance is " + str(player.coins) + ". Mining time credit: " + self.dhms(int(self.now - player.last_mine)) + " seconds."
|
|
||||||
return out
|
|
||||||
|
|
||||||
def mine(self, player):
|
|
||||||
delta = self.now - player.last_mine
|
|
||||||
|
|
||||||
if delta < 3600:
|
|
||||||
return "wat? not so soon again!"
|
|
||||||
|
|
||||||
if delta < 36000:
|
|
||||||
mined_coins = int(delta / 3600)
|
|
||||||
elif delta < 86400:
|
|
||||||
mined_coins = 10
|
|
||||||
elif delta < 2592000:
|
|
||||||
mined_coins = 50
|
|
||||||
else:
|
|
||||||
mined_coins = 1000
|
|
||||||
|
|
||||||
player.coins += mined_coins
|
|
||||||
player.last_mine = self.now
|
|
||||||
|
|
||||||
out = player.nick + " mined " + str(mined_coins) + " coins for " + self.dhms(int(delta)) + " seconds and now has " + str(player.coins) + " watcoins."
|
|
||||||
|
|
||||||
player.update(out)
|
|
||||||
return out
|
|
||||||
|
|
||||||
|
|
||||||
def transfer(self, player, target_player, amount):
|
|
||||||
if amount < 0:
|
|
||||||
return "wat? you thief!"
|
|
||||||
|
|
||||||
if player.coins < amount:
|
|
||||||
return "wat? you poor fuck don't have enough!"
|
|
||||||
|
|
||||||
player.coins -= amount
|
|
||||||
target_player.coins += amount
|
|
||||||
|
|
||||||
if amount != 1:
|
|
||||||
out = player.nick + " sent " + target_player.nick + " " +str(amount) + " watcoins."
|
|
||||||
else:
|
|
||||||
out = player.nick + " sent " + target_player.nick + " a watcoin."
|
|
||||||
|
|
||||||
player.update(out)
|
|
||||||
target_player.update(out)
|
|
||||||
|
|
||||||
return out
|
|
||||||
|
|
||||||
def mega_wat(self, player):
|
|
||||||
|
|
||||||
mega_number = random.randint(1,1000000)
|
|
||||||
kilo_number = random.randint(1,1000)
|
|
||||||
|
|
||||||
print "mega_wat(" + player.nick + ") mega_number == " + str(mega_number) + ", kilo_number == " + str(kilo_number)
|
|
||||||
|
|
||||||
out = None
|
|
||||||
|
|
||||||
if mega_number == 23:
|
|
||||||
player.coins += 1000000
|
|
||||||
out = "OMGWATWATWAT!!!! " + player.nick + " has won the MegaWat lottery and gains 1000000 watcoins!"
|
|
||||||
|
|
||||||
if kilo_number == 5:
|
|
||||||
player.coins += 1000
|
|
||||||
out = "OMGWAT! " + player.nick + " has won the KiloWat lottery and gains 1000 watcoins!"
|
|
||||||
|
|
||||||
player.watting_exp += 1
|
|
||||||
player.update(out)
|
|
||||||
|
|
||||||
return out
|
|
||||||
|
|
||||||
def roll(self, player, amount):
|
|
||||||
if amount < 0:
|
|
||||||
return "wat? nonono!"
|
|
||||||
|
|
||||||
if player.coins < amount:
|
|
||||||
return "wat? you broke, go away!"
|
|
||||||
|
|
||||||
if self.bot_player.coins < amount:
|
|
||||||
bot_mining_delta = self.now - self.bot_player.last_mine
|
|
||||||
if bot_mining_delta > 86400:
|
|
||||||
return self.bot_nick + " doesn't have enough coins for this, but " + self.bot_nick + " can mine! " + self.mine(self.bot_player) + self.roll(player, amount)
|
|
||||||
else:
|
|
||||||
return "wat? " + self.bot_nick + " only has " + str(bot_player.coins) + " wtc left. Try again later or beg someone to fund the bot. " + self.bot_nick + " will mine in " + str(self.dhms(int(86400 - bot_mining_delta))) + "."
|
|
||||||
|
|
||||||
number = random.randint(1, 100)
|
|
||||||
|
|
||||||
if number < 50:
|
|
||||||
player.coins += amount
|
|
||||||
player.trickery_exp += 1
|
|
||||||
self.bot_player.coins -= amount
|
|
||||||
out = player.nick + " rolls a d100 (<50 wins): " + str(number) + ". You win! Your new balance is " + str(player.coins) + "."
|
|
||||||
else:
|
|
||||||
player.coins -= amount
|
|
||||||
self.bot_player.coins += amount
|
|
||||||
out = player.nick + " rolls a d100 (<50 wins): " + str(number) + ". You lose. Your new balance is " + str(player.coins) + "."
|
|
||||||
|
|
||||||
player.update(out)
|
|
||||||
self.bot_player.update(out)
|
|
||||||
return out
|
|
||||||
|
|
||||||
def invent(self, player, itemname, price):
|
|
||||||
if price <= 0:
|
|
||||||
return "wat? nonono!"
|
|
||||||
|
|
||||||
invent_cost = 100 - player.watting
|
|
||||||
|
|
||||||
if player.coins < invent_cost:
|
|
||||||
return "wat? inventions cost you " + str(invent_cost) + " coins, but you're poor!"
|
|
||||||
|
|
||||||
if self.db.invent_item(itemname, player.nick, price):
|
|
||||||
player.coins -= invent_cost
|
|
||||||
self.bot_player.coins += invent_cost
|
|
||||||
out = player.nick + " invented " + itemname + " (" + str(price) + ")."
|
|
||||||
player.update(out)
|
|
||||||
self.bot_player.update(out)
|
|
||||||
else:
|
|
||||||
out = "wat?" + itemname + " already invented!"
|
|
||||||
|
|
||||||
return out
|
|
||||||
|
|
||||||
def create(self, player, itemname, count):
|
|
||||||
if count <= 0:
|
|
||||||
return "wat? nonono!"
|
|
||||||
|
|
||||||
(itemname, inventor_nick, price) = self.db.get_item(itemname)
|
|
||||||
|
|
||||||
if player.nick.lower() != inventor_nick:
|
|
||||||
return "wat? that's not your invention!"
|
|
||||||
|
|
||||||
if count * price > player.coins:
|
|
||||||
return "wat? you need more money for that!"
|
|
||||||
|
|
||||||
player.coins -= count * price
|
|
||||||
original_count = self.db.get_item_count(player.nick, itemname)
|
|
||||||
self.db.set_item_count(player.nick, itemname, original_count + count)
|
|
||||||
|
|
||||||
out = player.nick + " created " + str(count) + " " + itemname + "."
|
|
||||||
player.update(out)
|
|
||||||
return out
|
|
||||||
|
|
||||||
def give(self, player, target_nick, count, itemname):
|
|
||||||
player_item_count = self.db.get_item_count(player.nick, itemname)
|
|
||||||
if player_item_count < count:
|
|
||||||
return "wat? you don't have that many!"
|
|
||||||
|
|
||||||
self.db.set_item_count(player.nick, itemname, player_item_count - count)
|
|
||||||
|
|
||||||
target_item_count = self.db.get_item_count(target_nick, itemname)
|
|
||||||
self.db.set_item_count(target_nick, itemname, target_item_count + count)
|
|
||||||
|
|
||||||
return player.nick + " gave " + target_nick + " " + str(count) + " " + itemname
|
|
||||||
|
|
||||||
def steal(self, player, target_player, amount):
|
|
||||||
if amount < 0:
|
|
||||||
return "wat? nonono!"
|
|
||||||
|
|
||||||
if player.coins < amount * 2:
|
|
||||||
return "wat? you don't have enough to pay the possible fine!"
|
|
||||||
|
|
||||||
if target_player.coins < amount:
|
|
||||||
return "wat? " + target_player.nick + " is a poor fuck and doesn't have " + str(amount) + " coins."
|
|
||||||
|
|
||||||
number = random.randint(1,6)
|
|
||||||
|
|
||||||
if number <3:
|
|
||||||
out = player.nick + " rolls a d6 to steal " + str(amount) + " watcoins from " + target_player.nick + ": " + str(number) + " (<3 wins). You win! Sneaky bastard!"
|
|
||||||
player.coins += amount
|
|
||||||
player.anarchy_exp += 1
|
|
||||||
target_player.coins -= amount
|
|
||||||
player.update(out)
|
|
||||||
target_player.update(out)
|
|
||||||
else:
|
|
||||||
out = player.nick + " rolls a d6 to steal " + str(amount) + " watcoins from " + target_player.nick + ": " + str(number) + " (<3 wins). You were caught and pay " + str(2 * amount) + " coins to " + self.bot_nick + "."
|
|
||||||
player.coins -= 2 * amount
|
|
||||||
self.bot_player.coins += 2 * amount
|
|
||||||
player.update(out)
|
|
||||||
self.bot_player.update(out)
|
|
||||||
|
|
||||||
return out
|
|
||||||
|
|
||||||
def frame(self, player, target_player, amount):
|
|
||||||
if amount < 0:
|
|
||||||
return "wat? nonono!"
|
|
||||||
|
|
||||||
if player.coins < amount:
|
|
||||||
return "wat? you don't have enough to pay the possible fine!"
|
|
||||||
|
|
||||||
if target_player.coins < amount:
|
|
||||||
return "wat? " + target_player.nick + " is a poor fuck and doesn't have " + str(amount) + " coins."
|
|
||||||
|
|
||||||
number = random.randint(1,6)
|
|
||||||
|
|
||||||
if number <3:
|
|
||||||
out = player.nick + " rolls a d6 to frame " + str(amount) + " watcoins from " + target_player.nick + ": " + str(number) + " (<3 wins). You win! " + target_player.nick + " pays " + str(amount) + " to " + self.bot_nick + "."
|
|
||||||
|
|
||||||
player.anarchy_exp += 1
|
|
||||||
target_player.coins -= amount
|
|
||||||
self.bot_player.coins += amount
|
|
||||||
player.update(out)
|
|
||||||
target_player.update(out)
|
|
||||||
self.bot_player.update(out)
|
|
||||||
else:
|
|
||||||
target_amount = int(math.ceil(float(amount)/2))
|
|
||||||
bot_amount = int(math.floor(float(amount)/2))
|
|
||||||
out = player.nick + " rolls a d6 to frame " + str(amount) + " watcoins from " + target_player.nick + ": " + str(number) + " (<3 wins). You were caught and pay " + str(target_amount) + " coins to " + target_player.nick + " and " + str(bot_amount) + " coins to " + self.bot_nick + "."
|
|
||||||
|
|
||||||
player.coins -= amount
|
|
||||||
target_player.coins += target_amount
|
|
||||||
self.bot_player.coins += bot_amount
|
|
||||||
|
|
||||||
player.update(out)
|
|
||||||
target_player.update(out)
|
|
||||||
self.bot_player.update(out)
|
|
||||||
|
|
||||||
return out
|
|
||||||
|
|
||||||
def punch(self, player, target_player):
|
|
||||||
|
|
||||||
number = random.randint(1, 6)
|
|
||||||
dmg = random.randint(1, 6)
|
|
||||||
|
|
||||||
if number <3:
|
|
||||||
dmg += player.anarchy
|
|
||||||
out = player.nick + " rolls a d6 to punch " + target_player.nick + ": " + str(number) +". " + player.nick + " hits for " + str(dmg) + " points of damage."
|
|
||||||
target_player.health -= dmg
|
|
||||||
if target_player.health <= 0:
|
|
||||||
out += " " + target_player.nick + " falls unconscious."
|
|
||||||
target_player.update(out)
|
|
||||||
else:
|
|
||||||
dmg += target_player.anarchy
|
|
||||||
out = player.nick + " rolls a d6 to punch " + target_player.nick + ": " + str(number) +". " + player.nick + " misses. " + target_player.nick + " punches back for " + str(dmg) + " points of damage."
|
|
||||||
player.health -= dmg
|
|
||||||
if player.health <= 0:
|
|
||||||
out += " " + player.nick + " falls unconscious."
|
|
||||||
player.update(out)
|
|
||||||
|
|
||||||
return out
|
|
||||||
|
|
||||||
def dhms(self, seconds):
|
|
||||||
days = int(seconds / 86400)
|
|
||||||
seconds -= days * 86400
|
|
||||||
hours = int(seconds / 3600)
|
|
||||||
seconds -= hours * 3600
|
|
||||||
minutes = int(seconds / 60)
|
|
||||||
seconds -= minutes * 60
|
|
||||||
|
|
||||||
out = ""
|
|
||||||
show = False
|
|
||||||
|
|
||||||
if days > 0:
|
|
||||||
out += str(days) + "d "
|
|
||||||
show = True
|
|
||||||
|
|
||||||
if hours > 0 or show:
|
|
||||||
out += str(hours) + "h "
|
|
||||||
show = True
|
|
||||||
|
|
||||||
if minutes > 0 or show:
|
|
||||||
out += str(minutes) + "m "
|
|
||||||
show = True
|
|
||||||
|
|
||||||
out += str(seconds) + "s"
|
|
||||||
return out
|
|
||||||
|
|
@ -1,102 +0,0 @@
|
|||||||
#!/usr/bin/python
|
|
||||||
|
|
||||||
import irc.client
|
|
||||||
import ssl
|
|
||||||
|
|
||||||
class WatbotIRC:
|
|
||||||
"""irc frontend class"""
|
|
||||||
|
|
||||||
def __init__(self, config, game):
|
|
||||||
|
|
||||||
self.config = config
|
|
||||||
self.game = game
|
|
||||||
|
|
||||||
self.client = irc.client.IRC()
|
|
||||||
server = self.client.server()
|
|
||||||
|
|
||||||
if not config["ssl"]:
|
|
||||||
server.connect(
|
|
||||||
config["server"],
|
|
||||||
config["port"],
|
|
||||||
config["bot_nick"],
|
|
||||||
username = config["bot_nick"],
|
|
||||||
ircname = (config["prefix"] + " help"),
|
|
||||||
)
|
|
||||||
else:
|
|
||||||
ssl_factory = irc.connection.Factory(wrapper=ssl.wrap_socket)
|
|
||||||
server.connect(
|
|
||||||
config["server"],
|
|
||||||
config["port"],
|
|
||||||
config["bot_nick"],
|
|
||||||
username = config["bot_nick"],
|
|
||||||
ircname = (config["prefix"] + " help"),
|
|
||||||
connect_factory = ssl_factory
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
self.client.add_global_handler("welcome", self.on_connect)
|
|
||||||
self.client.add_global_handler("privmsg", self.on_msg)
|
|
||||||
self.client.add_global_handler("pubmsg", self.on_msg)
|
|
||||||
self.client.add_global_handler("disconnect", self.on_disconnect)
|
|
||||||
|
|
||||||
|
|
||||||
def main_loop(self):
|
|
||||||
self.client.process_forever()
|
|
||||||
|
|
||||||
def on_connect(self, connection, event):
|
|
||||||
if self.config["nickserv"]:
|
|
||||||
connection.privmsg("nickserv", "identify " + self.config["password"])
|
|
||||||
|
|
||||||
connection.join(self.config["channel"])
|
|
||||||
|
|
||||||
def on_msg(self, connection, event):
|
|
||||||
for a in event.arguments:
|
|
||||||
self.do_command(connection, event.source, event.target, a)
|
|
||||||
|
|
||||||
def do_command(self, connection, source, target, commandline):
|
|
||||||
|
|
||||||
cl_list = commandline.strip().split(" ", 2)
|
|
||||||
|
|
||||||
if len(cl_list) > 0:
|
|
||||||
if cl_list[0].lower() == "wat":
|
|
||||||
cl_list = [ self.config["prefix"], "wat" ]
|
|
||||||
|
|
||||||
if len(cl_list) < 2:
|
|
||||||
return
|
|
||||||
|
|
||||||
if cl_list[0].lower() != self.config["prefix"]:
|
|
||||||
return
|
|
||||||
|
|
||||||
command = cl_list[1].lower()
|
|
||||||
if len(cl_list) > 2:
|
|
||||||
args_list = cl_list[2].split(" ")
|
|
||||||
else:
|
|
||||||
args_list = []
|
|
||||||
|
|
||||||
if self.config["nickserv"]:
|
|
||||||
# The @ must be there, irc standard
|
|
||||||
host = source.split("@")[1]
|
|
||||||
host_split = host.split("/")
|
|
||||||
if len(host_split) < 3:
|
|
||||||
return
|
|
||||||
|
|
||||||
# Only allow nickserved users
|
|
||||||
if host_split[0] != "tripsit":
|
|
||||||
return
|
|
||||||
|
|
||||||
#nick = host_split[2]
|
|
||||||
nick = source.split("!", 1)[0]
|
|
||||||
|
|
||||||
output = self.game.do_command(nick, command, args_list)
|
|
||||||
|
|
||||||
|
|
||||||
if target == self.config["bot_nick"]:
|
|
||||||
target = nick
|
|
||||||
|
|
||||||
if not output is None:
|
|
||||||
connection.privmsg(target, output)
|
|
||||||
|
|
||||||
def on_disconnect(self, connection, event):
|
|
||||||
raise SystemExit()
|
|
||||||
|
|
||||||
|
|
@ -1,46 +0,0 @@
|
|||||||
#!/usr/bin/python
|
|
||||||
|
|
||||||
import time
|
|
||||||
|
|
||||||
class WatbotPlayer:
|
|
||||||
"""class representing a player account"""
|
|
||||||
|
|
||||||
def __init__(self, db, nick):
|
|
||||||
self.nick = nick
|
|
||||||
self.db = db
|
|
||||||
|
|
||||||
(
|
|
||||||
self.watting_exp,
|
|
||||||
self.anarchy_exp,
|
|
||||||
self.trickery_exp,
|
|
||||||
self.coins,
|
|
||||||
self.last_mine,
|
|
||||||
self.health,
|
|
||||||
self.last_rest
|
|
||||||
) = db.get_account(nick)
|
|
||||||
|
|
||||||
self.watting = self.get_level(self.watting_exp)
|
|
||||||
self.anarchy = self.get_level(self.anarchy_exp)
|
|
||||||
self.trickery = self.get_level(self.trickery_exp)
|
|
||||||
|
|
||||||
now = time.time()
|
|
||||||
delta = now - self.last_rest
|
|
||||||
if delta > 60:
|
|
||||||
self.health += int(delta/60)
|
|
||||||
if self.health > 10:
|
|
||||||
self.health = 10
|
|
||||||
|
|
||||||
self.last_rest += int(delta/60) * 60
|
|
||||||
|
|
||||||
def get_level(self, exp):
|
|
||||||
if exp < 100:
|
|
||||||
level = int(exp/10)
|
|
||||||
elif exp < 900:
|
|
||||||
level = 10 + int(exp/100)
|
|
||||||
else:
|
|
||||||
level = 99
|
|
||||||
|
|
||||||
return level
|
|
||||||
|
|
||||||
def update(self, log):
|
|
||||||
self.db.update_account(self.nick, self.watting_exp, self.anarchy_exp, self.trickery_exp, self.coins, self.last_mine, self.health, self.last_rest, log)
|
|
35
wat/bot.go
35
wat/bot.go
@ -6,7 +6,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/go-irc/irc"
|
"github.com/go-irc/irc"
|
||||||
"github.com/ergochat/irc-go/ircfmt"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type WatBot struct {
|
type WatBot struct {
|
||||||
@ -14,26 +13,19 @@ type WatBot struct {
|
|||||||
conn *tls.Conn
|
conn *tls.Conn
|
||||||
c *WatConfig
|
c *WatConfig
|
||||||
game *WatGame
|
game *WatGame
|
||||||
integration *WatIntegration
|
|
||||||
Db *WatDb
|
Db *WatDb
|
||||||
Nick string
|
Nick string
|
||||||
}
|
}
|
||||||
|
|
||||||
type WatConfig struct {
|
type WatConfig struct {
|
||||||
DatabasePath string
|
|
||||||
BotHosts []string
|
|
||||||
BotGames BotGameConfig
|
|
||||||
AdminHosts []string
|
|
||||||
IgnoredHosts []string
|
|
||||||
AutoJoinChannels []string
|
|
||||||
PermittedChannels []string
|
PermittedChannels []string
|
||||||
|
IgnoredHosts []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWatBot(config *irc.ClientConfig, watConfig *WatConfig, serverConn *tls.Conn) *WatBot {
|
func NewWatBot(config *irc.ClientConfig, watConfig *WatConfig, serverConn *tls.Conn) *WatBot {
|
||||||
wat := WatBot{conn: serverConn, Nick: config.Nick, c: watConfig}
|
wat := WatBot{conn: serverConn, Nick: config.Nick, c: watConfig}
|
||||||
wat.Db = NewWatDb(watConfig.DatabasePath)
|
wat.Db = NewWatDb()
|
||||||
wat.game = NewWatGame(&wat, wat.Db)
|
wat.game = NewWatGame(&wat, wat.Db)
|
||||||
wat.integration = NewWatIntegration(&wat, wat.Db, &WatIntegrationConfig{BotHosts: watConfig.BotHosts, BotGames: watConfig.BotGames})
|
|
||||||
config.Handler = irc.HandlerFunc(wat.HandleIrcMsg)
|
config.Handler = irc.HandlerFunc(wat.HandleIrcMsg)
|
||||||
wat.client = irc.NewClient(wat.conn, *config)
|
wat.client = irc.NewClient(wat.conn, *config)
|
||||||
return &wat
|
return &wat
|
||||||
@ -43,29 +35,17 @@ func CleanNick(nick string) string {
|
|||||||
return string(nick[0]) + "\u200c" + nick[1:]
|
return string(nick[0]) + "\u200c" + nick[1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
func PrefixChannel(channel string) string {
|
|
||||||
// there could theoretically be other channel prefixes ..
|
|
||||||
if channel[0] != '#' && channel[0] != '!' {
|
|
||||||
channel = "#" + channel
|
|
||||||
}
|
|
||||||
return channel
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *WatBot) HandleIrcMsg(c *irc.Client, m *irc.Message) {
|
func (w *WatBot) HandleIrcMsg(c *irc.Client, m *irc.Message) {
|
||||||
switch cmd := m.Command; cmd {
|
switch cmd := m.Command; cmd {
|
||||||
case "PING":
|
case "PING":
|
||||||
w.write("PONG", m.Params[0])
|
w.write("PONG", m.Params[0])
|
||||||
case "PRIVMSG":
|
case "PRIVMSG":
|
||||||
w.Msg(m)
|
w.Msg(m)
|
||||||
case "001":
|
|
||||||
for _, channel := range w.c.AutoJoinChannels {
|
|
||||||
w.write("JOIN", PrefixChannel(channel))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WatBot) Admin(m *irc.Message) bool {
|
func (w *WatBot) Admin(m *irc.Message) bool {
|
||||||
return w.Allowed(m.Prefix.Host, w.c.AdminHosts)
|
return m.Prefix.Host == "mph.monster"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (w *WatBot) Allowed(c string, r []string) bool {
|
func (w *WatBot) Allowed(c string, r []string) bool {
|
||||||
@ -87,7 +67,7 @@ func (w *WatBot) CanRespond(m *irc.Message) bool {
|
|||||||
// if !strings.Contains(m.Prefix.Host, "") {
|
// if !strings.Contains(m.Prefix.Host, "") {
|
||||||
// return false
|
// return false
|
||||||
// }
|
// }
|
||||||
if !w.Allowed(PrefixChannel(m.Params[0]), w.c.PermittedChannels) {
|
if !w.Allowed(m.Params[0], w.c.PermittedChannels) {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
@ -106,7 +86,7 @@ func (w *WatBot) Msg(m *irc.Message) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// fieldsfunc allows you to obtain rune separated fields/args
|
// fieldsfunc allows you to obtain rune separated fields/args
|
||||||
args := strings.FieldsFunc(ircfmt.Strip(m.Params[1]), func(c rune) bool { return c == ' ' })
|
args := strings.FieldsFunc(m.Params[1], func(c rune) bool { return c == ' ' })
|
||||||
|
|
||||||
if len(args) == 0 {
|
if len(args) == 0 {
|
||||||
return
|
return
|
||||||
@ -129,11 +109,6 @@ func (w *WatBot) Msg(m *irc.Message) {
|
|||||||
args = args[1:]
|
args = args[1:]
|
||||||
}
|
}
|
||||||
|
|
||||||
// integration with games in other bots
|
|
||||||
if w.integration.HandleIntegration(m, args) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// check if command char (or something weird) is present
|
// check if command char (or something weird) is present
|
||||||
if args[0] != "wat" && args[0][0] != '#' {
|
if args[0] != "wat" && args[0][0] != '#' {
|
||||||
return
|
return
|
||||||
|
10
wat/db.go
10
wat/db.go
@ -52,10 +52,10 @@ type WatDb struct {
|
|||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewWatDb(dbpath string) *WatDb {
|
func NewWatDb() *WatDb {
|
||||||
w := WatDb{}
|
w := WatDb{}
|
||||||
var err error
|
var err error
|
||||||
w.db, err = gorm.Open(sqlite.Open(dbpath), &gorm.Config{})
|
w.db, err = gorm.Open(sqlite.Open("wat.db"), &gorm.Config{})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -65,12 +65,8 @@ func NewWatDb(dbpath string) *WatDb {
|
|||||||
|
|
||||||
func (w *WatDb) User(nick, host string, create bool) Player {
|
func (w *WatDb) User(nick, host string, create bool) Player {
|
||||||
var player Player
|
var player Player
|
||||||
query := "nick = ?"
|
|
||||||
if host != "" {
|
|
||||||
query = query + " or host = ?"
|
|
||||||
}
|
|
||||||
// Try and get a user
|
// Try and get a user
|
||||||
if err := w.db.First(&player, query, nick, host).Error; err != nil && create {
|
if err := w.db.First(&player, "nick = ? or host = ?", nick, host).Error; err != nil && create {
|
||||||
fmt.Printf("Creating user: %s\n", err.Error())
|
fmt.Printf("Creating user: %s\n", err.Error())
|
||||||
// No user, make another
|
// No user, make another
|
||||||
player.Nick = nick
|
player.Nick = nick
|
||||||
|
111
wat/game.go
111
wat/game.go
@ -28,7 +28,7 @@ var unconscious = "wat, your hands fumble and fail you. try resting, weakling."
|
|||||||
|
|
||||||
func NewWatGame(bot *WatBot, db *WatDb) *WatGame {
|
func NewWatGame(bot *WatBot, db *WatDb) *WatGame {
|
||||||
g := WatGame{bot, db, Player{}, nil, nil, nil, nil, map[string]int{}}
|
g := WatGame{bot, db, Player{}, nil, nil, nil, nil, map[string]int{}}
|
||||||
g.me = g.db.User(bot.Nick, "", true)
|
g.me = g.db.User(bot.Nick, "amia8t89xfp8y.liberta.casa", true)
|
||||||
g.commands = map[string](func(*Player, []string) string){
|
g.commands = map[string](func(*Player, []string) string){
|
||||||
//"wat": g.megaWat,
|
//"wat": g.megaWat,
|
||||||
"steroid": g.Steroid,
|
"steroid": g.Steroid,
|
||||||
@ -121,17 +121,9 @@ func (g *WatGame) help() string {
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *WatGame) RandInt(maxx int64) (uint64, error) {
|
func (g *WatGame) RandInt(max int64) uint64 {
|
||||||
bi := big.NewInt(maxx)
|
i, _ := rand.Int(rand.Reader, big.NewInt(max))
|
||||||
// prevent panic of rand.Int on big numbers
|
return i.Uint64()
|
||||||
if bi.BitLen() < 2 {
|
|
||||||
return 0, fmt.Errorf("overflow, got %d", bi)
|
|
||||||
}
|
|
||||||
i, err := rand.Int(rand.Reader, bi)
|
|
||||||
if err != nil {
|
|
||||||
return 0, err
|
|
||||||
}
|
|
||||||
return i.Uint64(), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *WatGame) Heal(player *Player, fields []string) string {
|
func (g *WatGame) Heal(player *Player, fields []string) string {
|
||||||
@ -174,11 +166,8 @@ func (g *WatGame) Dice(player *Player, fields []string) string {
|
|||||||
roll = i
|
roll = i
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
answer, err := g.RandInt(int64(roll))
|
answer := g.RandInt(int64(roll)) + 1
|
||||||
if e := handleError(err); e != "" {
|
return fmt.Sprintf("1d%d - %d", roll, answer)
|
||||||
return e
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("1d%d - %d", roll + 1, answer)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type PositiveError struct{}
|
type PositiveError struct{}
|
||||||
@ -217,20 +206,12 @@ func (g *WatGame) Roll(player *Player, fields []string) string {
|
|||||||
}
|
}
|
||||||
lotteryNum := int64(-1)
|
lotteryNum := int64(-1)
|
||||||
if dieSize > 100 {
|
if dieSize > 100 {
|
||||||
lotteryNumRand, randErr := g.RandInt(dieSize)
|
lotteryNum = int64(g.RandInt(dieSize)) + 1
|
||||||
if e := handleError(randErr); e != "" {
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
lotteryNum = int64(lotteryNumRand) + 1
|
|
||||||
}
|
}
|
||||||
if amount > player.Coins {
|
if amount > player.Coins {
|
||||||
return "wat? brokeass"
|
return "wat? brokeass"
|
||||||
}
|
}
|
||||||
nRand, randErr := g.RandInt(dieSize)
|
n := int64(g.RandInt(dieSize)) + 1
|
||||||
if e := handleError(randErr); e != "" {
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
n := int64(nRand) + 1
|
|
||||||
ret := fmt.Sprintf("%s rolls the %d sided die... %d! ", player.Nick, dieSize, n)
|
ret := fmt.Sprintf("%s rolls the %d sided die... %d! ", player.Nick, dieSize, n)
|
||||||
if n == lotteryNum {
|
if n == lotteryNum {
|
||||||
player.Coins += player.Coins
|
player.Coins += player.Coins
|
||||||
@ -274,18 +255,11 @@ func (g *WatGame) Punch(player *Player, fields []string) string {
|
|||||||
if !target.Conscious() {
|
if !target.Conscious() {
|
||||||
return "wat? you're punching someone who is already unconscious. u crazy?"
|
return "wat? you're punching someone who is already unconscious. u crazy?"
|
||||||
}
|
}
|
||||||
chance, randErr := g.RandInt(6)
|
chance := g.RandInt(6) + 1
|
||||||
if e := handleError(randErr); e != "" {
|
dmg := g.RandInt(6) + 1
|
||||||
return e
|
|
||||||
}
|
|
||||||
dmg, randErr := g.RandInt(6)
|
|
||||||
if e := handleError(randErr); e != "" {
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
dmg = dmg + 1
|
|
||||||
ret := fmt.Sprintf("%s rolls a d6... %s ", player.Nick, player.Nick)
|
ret := fmt.Sprintf("%s rolls a d6... %s ", player.Nick, player.Nick)
|
||||||
dmg += uint64(player.Level(player.Anarchy))
|
dmg += uint64(player.Level(player.Anarchy))
|
||||||
if chance + 1 > 3 {
|
if chance > 3 {
|
||||||
ret += fmt.Sprintf("hits %s for %d points of damage! ", target.Nick, dmg)
|
ret += fmt.Sprintf("hits %s for %d points of damage! ", target.Nick, dmg)
|
||||||
target.Health -= int64(dmg)
|
target.Health -= int64(dmg)
|
||||||
g.db.Update(target)
|
g.db.Update(target)
|
||||||
@ -326,11 +300,7 @@ func (g *WatGame) Frame(player *Player, fields []string) string {
|
|||||||
if target.Coins < amount {
|
if target.Coins < amount {
|
||||||
return fmt.Sprintf("wat? %s is too poor for this.", target.Nick)
|
return fmt.Sprintf("wat? %s is too poor for this.", target.Nick)
|
||||||
}
|
}
|
||||||
n, randErr := g.RandInt(6)
|
n := g.RandInt(6) + 1
|
||||||
if e := handleError(randErr); e != "" {
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
n = n + 1
|
|
||||||
ret := fmt.Sprintf("%s rolls a d6 to frame %s with %d %s: It's a %d! (<3 wins). ", player.Nick, target.Nick, amount, currency, n)
|
ret := fmt.Sprintf("%s rolls a d6 to frame %s with %d %s: It's a %d! (<3 wins). ", player.Nick, target.Nick, amount, currency, n)
|
||||||
if n < 3 {
|
if n < 3 {
|
||||||
ret += fmt.Sprintf("You frame %s for a minor crime. They pay me %d.", target.Nick, amount)
|
ret += fmt.Sprintf("You frame %s for a minor crime. They pay me %d.", target.Nick, amount)
|
||||||
@ -365,11 +335,7 @@ func (g *WatGame) Steal(player *Player, fields []string) string {
|
|||||||
if target.Coins < amount {
|
if target.Coins < amount {
|
||||||
return fmt.Sprintf("wat? %s is poor and doesn't have that much to steal. (%d %s)", target.Nick, target.Coins, currency)
|
return fmt.Sprintf("wat? %s is poor and doesn't have that much to steal. (%d %s)", target.Nick, target.Coins, currency)
|
||||||
}
|
}
|
||||||
n, randErr := g.RandInt(6)
|
n := g.RandInt(6) + 1
|
||||||
if e := handleError(randErr); e != "" {
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
n = n + 1
|
|
||||||
ret := fmt.Sprintf("%s is trying to steal %d %s from %s... ", player.Nick, amount, currency, target.Nick)
|
ret := fmt.Sprintf("%s is trying to steal %d %s from %s... ", player.Nick, amount, currency, target.Nick)
|
||||||
if n < 3 {
|
if n < 3 {
|
||||||
ret += "You did it! Sneaky bastard!"
|
ret += "You did it! Sneaky bastard!"
|
||||||
@ -414,11 +380,7 @@ func (g *WatGame) Leech(player *Player, fields []string) string {
|
|||||||
if err != "" {
|
if err != "" {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
r, randErr := g.RandInt(10)
|
r := g.RandInt(10) + 1
|
||||||
if e := handleError(randErr); e != "" {
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
r = r + 1
|
|
||||||
reply := fmt.Sprintf("You muster your wealth and feed it to %s. ", g.bot.Nick)
|
reply := fmt.Sprintf("You muster your wealth and feed it to %s. ", g.bot.Nick)
|
||||||
hpDown := amount / divisor
|
hpDown := amount / divisor
|
||||||
player.Coins -= amount
|
player.Coins -= amount
|
||||||
@ -446,11 +408,7 @@ func (g *WatGame) Rest(player *Player, fields []string) string {
|
|||||||
} else if delta < minRest {
|
} else if delta < minRest {
|
||||||
ret = fmt.Sprintf("wat were you thinking, sleeping at a time like this (%d until next rest)", minRest-delta)
|
ret = fmt.Sprintf("wat were you thinking, sleeping at a time like this (%d until next rest)", minRest-delta)
|
||||||
} else {
|
} else {
|
||||||
value, randErr := g.RandInt(10)
|
value := g.RandInt(10) + 1
|
||||||
if e := handleError(randErr); e != "" {
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
value = value + 1
|
|
||||||
if player.Health < -5 {
|
if player.Health < -5 {
|
||||||
player.Health = 1
|
player.Health = 1
|
||||||
ret = fmt.Sprintf("wow ur beat up. i pity u, ur health is now 1.")
|
ret = fmt.Sprintf("wow ur beat up. i pity u, ur health is now 1.")
|
||||||
@ -477,15 +435,8 @@ func (g *WatGame) Bench(player *Player, fields []string) string {
|
|||||||
if !g.CanAct(player, Action_Lift, minTime) {
|
if !g.CanAct(player, Action_Lift, minTime) {
|
||||||
return "you're tired. no more lifting for now."
|
return "you're tired. no more lifting for now."
|
||||||
}
|
}
|
||||||
weight, randErr := g.RandInt(370)
|
weight := g.RandInt(370) + 50
|
||||||
if e := handleError(randErr); e != "" {
|
reps := g.RandInt(10)
|
||||||
return e
|
|
||||||
}
|
|
||||||
weight = weight + 50
|
|
||||||
reps, randErr := g.RandInt(10)
|
|
||||||
if e := handleError(randErr); e != "" {
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
value := int64(0)
|
value := int64(0)
|
||||||
reply := fmt.Sprintf("%s benches %dwatts for %d reps, ", player.Nick, weight, reps)
|
reply := fmt.Sprintf("%s benches %dwatts for %d reps, ", player.Nick, weight, reps)
|
||||||
if weight < 150 {
|
if weight < 150 {
|
||||||
@ -501,10 +452,7 @@ func (g *WatGame) Bench(player *Player, fields []string) string {
|
|||||||
}
|
}
|
||||||
if g.roid[player.Nick] != 0 {
|
if g.roid[player.Nick] != 0 {
|
||||||
delete(g.roid, player.Nick)
|
delete(g.roid, player.Nick)
|
||||||
success, randErr := g.RandInt(2)
|
success := g.RandInt(2)
|
||||||
if e := handleError(randErr); e != "" {
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
if success != 0 {
|
if success != 0 {
|
||||||
player.Health = 0
|
player.Health = 0
|
||||||
player.Anarchy -= 10
|
player.Anarchy -= 10
|
||||||
@ -527,10 +475,7 @@ func (g *WatGame) Riot(player *Player, fields []string) string {
|
|||||||
if !g.CanAct(player, Action_Riot, int64((48 * time.Hour).Seconds())) {
|
if !g.CanAct(player, Action_Riot, int64((48 * time.Hour).Seconds())) {
|
||||||
return "Planning a riot takes time and the right circumstances. Be prepared. (nothing happens)"
|
return "Planning a riot takes time and the right circumstances. Be prepared. (nothing happens)"
|
||||||
}
|
}
|
||||||
r, randErr := g.RandInt(100)
|
r := g.RandInt(100)
|
||||||
if e := handleError(randErr); e != "" {
|
|
||||||
return e
|
|
||||||
}
|
|
||||||
reply := ""
|
reply := ""
|
||||||
if r > 40 {
|
if r > 40 {
|
||||||
player.Anarchy += 3
|
player.Anarchy += 3
|
||||||
@ -676,21 +621,9 @@ func PrintTwo(nick string, value uint64) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *WatGame) megaWat(player *Player, _ []string) string {
|
func (g *WatGame) megaWat(player *Player, _ []string) string {
|
||||||
mega, randErr := g.RandInt(1000000)
|
mega := g.RandInt(1000000) + 1
|
||||||
if e := handleError(randErr); e != "" {
|
kilo := g.RandInt(1000) + 1
|
||||||
return e + " mega fail"
|
ten := g.RandInt(100) + 1
|
||||||
}
|
|
||||||
mega = mega + 1
|
|
||||||
kilo, randErr := g.RandInt(1000)
|
|
||||||
if e := handleError(randErr); e != "" {
|
|
||||||
return e + " kilo fail"
|
|
||||||
}
|
|
||||||
kilo = kilo + 1
|
|
||||||
ten, randErr := g.RandInt(100)
|
|
||||||
if e := handleError(randErr); e != "" {
|
|
||||||
return e + " ten fail"
|
|
||||||
}
|
|
||||||
ten = ten + 1
|
|
||||||
reply := ""
|
reply := ""
|
||||||
if mega == 23 {
|
if mega == 23 {
|
||||||
player.Coins += 1000000
|
player.Coins += 1000000
|
||||||
|
@ -1,126 +0,0 @@
|
|||||||
package wat
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/go-irc/irc"
|
|
||||||
)
|
|
||||||
|
|
||||||
type BotGameConfig map[string][]string
|
|
||||||
|
|
||||||
type WatIntegrationConfig struct {
|
|
||||||
BotHosts []string
|
|
||||||
BotGames BotGameConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
type WatIntegration struct {
|
|
||||||
bot *WatBot
|
|
||||||
db *WatDb
|
|
||||||
c *WatIntegrationConfig
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewWatIntegration(bot *WatBot, db *WatDb, c *WatIntegrationConfig) *WatIntegration {
|
|
||||||
return &WatIntegration{bot, db, c}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *WatIntegration) Bot(m *irc.Message) (bool, []string) {
|
|
||||||
isBot := w.bot.Allowed(m.Prefix.Host, w.c.BotHosts)
|
|
||||||
var games []string
|
|
||||||
if isBot {
|
|
||||||
for b, g := range w.c.BotGames {
|
|
||||||
if b == m.Prefix.Name {
|
|
||||||
games = g
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return isBot, games
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *WatIntegration) HandleIntegration(m *irc.Message, msgargs []string) bool {
|
|
||||||
isBot, games := w.Bot(m)
|
|
||||||
if isBot {
|
|
||||||
// handles a message "Top finishers: (nick1: 1300) (nick2: 1200)" from an authorized Jeopardy game bot
|
|
||||||
if msgargs[0] == "Top" && msgargs[1] == "finishers:" && w.bot.Allowed("jeopardy", games) {
|
|
||||||
w.Jeopardy(m, msgargs)
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// not an authorized bot or no integration matched the given message
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
func (w *WatIntegration) Jeopardy(m *irc.Message, msgargs []string) {
|
|
||||||
// hey, I avoided regex!
|
|
||||||
// 1. Starts parsing an array of message arguments containing "Top finishers: (nick1: 1000) (nick2: 2000)", where
|
|
||||||
// the "($nick: $value)" pairs can contain arbitrary nicknames + integer values and can repeat one to any amount of times
|
|
||||||
// 2. Join the array on spaces to a string, but skip the first two elements to remove "Top" and "finishers:"
|
|
||||||
// 3. Replace ") (" in the string with ";" - the semicolon is chosen as a temporary delimiter because it does not conflict with any other characters in the message
|
|
||||||
// 4. Replace ": " in the string with ":"
|
|
||||||
// 5. Replace "(" in the string with "" (relevant for the first nick/value pair)
|
|
||||||
// 6. Replace ")" in the string with "" (relevant for the last nick/value pair)
|
|
||||||
// 7. Now, we have a string like "nick1:1000;nick2:2000" - split it back into an array on ";"
|
|
||||||
// 8. The result is an array like "[nick1:1000, nick2:2000]"
|
|
||||||
finisherPrizes := strings.Split(strings.Replace(strings.Replace(strings.Replace(strings.Replace(strings.Join(msgargs[2:], " "), ") (", ";", -1), ": ", ":", -1), "(", "", 1), ")", "", 1), ";")
|
|
||||||
fmt.Printf("Processing Jeopardy: %s\n", finisherPrizes)
|
|
||||||
var msg string
|
|
||||||
var many bool
|
|
||||||
fiprcount := len(finisherPrizes)
|
|
||||||
cashoutcount := 0
|
|
||||||
// only a single winner
|
|
||||||
if fiprcount == 1 {
|
|
||||||
msg = "smartass %s :) gave u %d"
|
|
||||||
many = false
|
|
||||||
// multiple winners
|
|
||||||
} else if fiprcount > 1 {
|
|
||||||
msg = "gang of smartasses :) gave %s %d"
|
|
||||||
many = true
|
|
||||||
// no winners (should never get here)
|
|
||||||
} else {
|
|
||||||
fmt.Printf("Empty finishers, aborting Jeopardy processing")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
// iterate over the "$nick:$value" string elements
|
|
||||||
for _, pair := range finisherPrizes {
|
|
||||||
// turn the string element into an array, where the first entry is the nickname, and the second the value
|
|
||||||
nameCoinPair := strings.Split(pair, ":")
|
|
||||||
coins, err := strconv.ParseUint(nameCoinPair[1], 10, 64)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Invalid coins, cannot process pair for cashout: %s.\n", nameCoinPair)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
name := nameCoinPair[0]
|
|
||||||
// Jeopardy prizes are quite a lot of $$$, make it a bit more sane
|
|
||||||
coins = coins / 40
|
|
||||||
if coins == 0 {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
cashoutcount += 1
|
|
||||||
// name = we assume the Jeopardy player name to match a Watbot player name
|
|
||||||
// host = we could use some WHO logic to find the host, but assuming nickname lookup to be sufficient here
|
|
||||||
// create = based on the above, maybe rather not create Watbot players based on only a nick?
|
|
||||||
// but it expects someone to have played with Watbot before to be eligible for Jeopardy cashout ..
|
|
||||||
player := w.db.User(strings.ToLower(name), "", false)
|
|
||||||
if player.Nick == "" {
|
|
||||||
fmt.Printf("Player %s does not exist in Watbot, skipping cashout.\n", name)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
// fill previous format placeholders
|
|
||||||
msg = fmt.Sprintf(msg, player.Nick, coins)
|
|
||||||
if many {
|
|
||||||
// append additional ones for filling in the next loop iteration
|
|
||||||
msg = msg + ", %s %d"
|
|
||||||
}
|
|
||||||
player.Coins += coins
|
|
||||||
w.db.Update(player)
|
|
||||||
}
|
|
||||||
if many {
|
|
||||||
// remove format placeholders from last loop iteration
|
|
||||||
msg = strings.Replace(msg, ", %s %d", ".", 1)
|
|
||||||
}
|
|
||||||
if cashoutcount > 0 {
|
|
||||||
w.bot.reply(m, msg)
|
|
||||||
}
|
|
||||||
}
|
|
22
wat/utils.go
22
wat/utils.go
@ -1,22 +0,0 @@
|
|||||||
package wat
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"runtime"
|
|
||||||
)
|
|
||||||
|
|
||||||
func handleError(err error) string {
|
|
||||||
if err != nil {
|
|
||||||
pc, _, _, ok := runtime.Caller(1)
|
|
||||||
details := runtime.FuncForPC(pc)
|
|
||||||
var cFun string
|
|
||||||
if ok && details != nil {
|
|
||||||
cFun = details.Name()
|
|
||||||
} else {
|
|
||||||
cFun = "???"
|
|
||||||
}
|
|
||||||
fmt.Printf("caught error in %s: %v\n", cFun, err)
|
|
||||||
return "u wat"
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user