Skip to content

Commit

Permalink
Optimizations!
Browse files Browse the repository at this point in the history
 - Removed errors chan with a func passer
 - Updated the handler of tweets to run regardless (it bails if not active)
 - Simpl-ier Cmdline
 - Version num update 2.2 => 2.21
  • Loading branch information
iDigitalFlame committed Oct 5, 2020
1 parent 31afd55 commit 724e399
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 142 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ bash build.sh
The Scoreboard can be configured by command line options, though it's preferred to use a config file instead. (Below).

```text
Scorebot Scoreboard v2.2
Scorebot Scoreboard v2.21
Leaving any of the required Twitter options empty in command
line or config will result in Twitter functionality being disabled.
Expand Down
2 changes: 1 addition & 1 deletion html/public/script/scoreboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// You should have received a copy of the GNU Affero General Public License
// along with this program.If not, see <https://www.gnu.org/licenses/>.
//
// Scoreboard v2.2
// Scoreboard v2.21
// 2020 iDigitalFlame
//
// Javascript Main File
Expand Down
2 changes: 1 addition & 1 deletion html/public/style/scoreboard.css
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Scoreboard v2.2
Scoreboard v2.21
2020 iDigitalFlame
CSS Main File
Expand Down
2 changes: 1 addition & 1 deletion html/template/home.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Scoreboard v2.2
Scoreboard v2.21
2020 iDigitalFlame
Games Template List Page
Expand Down
2 changes: 1 addition & 1 deletion html/template/scoreboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
Scoreboard v2.2
Scoreboard v2.21
2020 iDigitalFlame
Scoreboard HTML Template Page
Expand Down
7 changes: 4 additions & 3 deletions scoreboard/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ import (

func main() {
s, err := scoreboard.Cmdline()
if err == flag.ErrHelp {
os.Exit(2)
}

if err != nil {
if err == flag.ErrHelp {
os.Exit(2)
}
os.Stderr.WriteString("Error during startup: " + err.Error() + "!\n")
os.Exit(1)
}
Expand Down
81 changes: 67 additions & 14 deletions scoreboard/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,53 @@ const defaults = `{
"timeout": 10,
"scorebot": "http://scorebot"
}
`
const usage = `Scorebot Scoreboard v2.21
Leaving any of the required Twitter options empty in command
line or config will result in Twitter functionality being disabled.
Required Twitter options: 'Consumer Key and Secret', 'Access Key and Secret',
'Twitter Keywords' and 'Twitter Language'.
Usage of scoreboard:
-c <file> Scorebot configuration file path.
-d Print default configuration and exit.
-sbe <url> Scorebot core address or URL (Required without "-c").
-assets <dir> Scoreboard secondary assets override URL.
-dir <directory> Scoreboard HTML override directory path.
-log <file> Scoreboard log file path.
-log-level <number [0-5]> Scoreboard logging level (Default 2).
-tick <seconds> Scorebot poll tate, in seconds (Default 5).
-timeout <seconds> Scoreboard request timeout, in seconds (Default 10).
-bind <socket> Address and port to listen on (Default "0.0.0.0:8080").
-cert <file> Path to TLS certificate file.
-key <file> Path to TLS key file.
-tw-ck <key> Twitter Consumer API key.
-tw-cs <secret> Twitter Consumer API secret.
-tw-ak <key> Twitter Access API key.
-tw-as <secret> Twitter Access API secret.
-tw-keywords <list> Twitter search keywords (Comma separated)
-tw-lang <list> Twitter search language (Comma separated)
-tw-expire <seconds> Tweet display time, in seconds (Default 45).
-tw-block-words <list> Twitter blocked words (Comma separated).
-tw-block-user <list> Twitter blocked Usernames (Comma separated).
-tw-only-users <list> Twitter whitelisted Usernames (Comma separated).
Copyright (C) 2020 iDigitalFlame
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
`

type log struct {
Expand All @@ -76,9 +123,7 @@ type tweets struct {
Expire int `json:"expire"`
Credentials creds `json:"auth"`
}

// Config is a struct that is used to store the configuration options for the scoreboard.
type Config struct {
type config struct {
Log log `json:"log,omitempty"`
Key string `json:"key,omitempty"`
Cert string `json:"cert,omitempty"`
Expand Down Expand Up @@ -110,21 +155,29 @@ func split(s string) []string {
}
return o
}
func (c *Config) verify() error {
func (e errval) Error() string {
if e.e == nil {
return e.s
}
return e.s + ": " + e.e.Error()
}
func (e errval) Unwrap() error {
return e.e
}
func (c *config) verify() error {
if c.Tick <= 0 {
return &errorval{s: "tick " + strconv.Itoa(c.Tick) + " cannot be less than or equal to zero"}
return &errval{s: "tick " + strconv.Itoa(c.Tick) + " cannot be less than or equal to zero"}
}
if c.Timeout <= 0 {
return &errorval{s: "timeout " + strconv.Itoa(c.Timeout) + " cannot be less than or equal to zero"}
return &errval{s: "timeout " + strconv.Itoa(c.Timeout) + " cannot be less than or equal to zero"}
}
if c.Log.Level < int(logx.Trace) || c.Log.Level > int(logx.Fatal) {
return &errorval{s: "log level " + strconv.Itoa(c.Tick) + " must be between zero and five"}
return &errval{s: "log level " + strconv.Itoa(c.Tick) + " must be between zero and five"}
}
if len(c.Listen) == 0 {
c.Listen = "0.0.0.0:8080"
}
c.twitter = true
if len(c.Twitter.Filter.Language) == 0 || len(c.Twitter.Filter.Keywords) == 0 {
if c.twitter = true; len(c.Twitter.Filter.Language) == 0 || len(c.Twitter.Filter.Keywords) == 0 {
c.twitter = false
}
if len(c.Twitter.Credentials.AccessKey) == 0 || len(c.Twitter.Credentials.AccessSecret) == 0 {
Expand All @@ -134,7 +187,7 @@ func (c *Config) verify() error {
c.twitter = false
}
if c.twitter && c.Twitter.Expire <= 0 {
return &errorval{s: "tweet expire time " + strconv.Itoa(c.Timeout) + " cannot be less than or equal to zero"}
return &errval{s: "tweet expire time " + strconv.Itoa(c.Timeout) + " cannot be less than or equal to zero"}
}
return nil
}
Expand All @@ -145,7 +198,7 @@ func (c *Config) verify() error {
// this means that the defaults are being printed and to bail out with a success status.
func Cmdline() (*Scoreboard, error) {
var (
c Config
c config
d bool
args = flag.NewFlagSet("Scorebot Scoreboard", flag.ExitOnError)
twbWords, twoUsers string
Expand All @@ -155,7 +208,6 @@ func Cmdline() (*Scoreboard, error) {
os.Stdout.WriteString(usage)
os.Exit(2)
}

args.StringVar(&s, "c", "", "scoreboard config file path.")
args.BoolVar(&d, "d", false, "Print default configuration and exit.")
args.StringVar(&c.Scorebot, "sbe", "", "Scorebot core address or URL (Required without -c).")
Expand All @@ -178,6 +230,7 @@ func Cmdline() (*Scoreboard, error) {
args.StringVar(&twbWords, "tw-block-words", "", "Twitter blocked words (Comma separated).")
args.StringVar(&twbUsers, "tw-block-user", "", "Twitter blocked Usernames (Comma separated).")
args.StringVar(&twoUsers, "tw-only-users", "", "Twitter whitelisted Usernames (Comma separated).")

if err := args.Parse(os.Args[1:]); err != nil {
os.Stdout.WriteString(usage)
return nil, flag.ErrHelp
Expand All @@ -196,10 +249,10 @@ func Cmdline() (*Scoreboard, error) {
if len(s) > 0 {
b, err := ioutil.ReadFile(s)
if err != nil {
return nil, &errorval{s: `cannot read file "` + s + `"`, e: err}
return nil, &errval{s: `cannot read file "` + s + `"`, e: err}
}
if err := json.Unmarshal(b, &c); err != nil {
return nil, &errorval{s: `cannot parse JSON from file "` + s + `"`, e: err}
return nil, &errval{s: `cannot parse JSON from file "` + s + `"`, e: err}
}
}
return c.New()
Expand Down
6 changes: 3 additions & 3 deletions scoreboard/game/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,9 +393,9 @@ func (s *subscription) update(x context.Context, m *Manager) {
}
}

// Twitter creates and returns the Twitter channel. This channel can be used to submit Tweets
// to be sent to the scoreboard.
func (m *Manager) Twitter(t time.Duration) chan *twitter.Tweet {
// Twitter creates and returns the Twitter channel. This channel can be used to submit Tweets to
// be sent to the scoreboard.
func (m *Manager) Twitter(t time.Duration) chan<- *twitter.Tweet {
m.twitter = &tweets{new: make(chan *twitter.Tweet), timeout: t}
return m.twitter.new
}
Expand Down
Loading

0 comments on commit 724e399

Please sign in to comment.