Skip to content

Commit

Permalink
refactor: textables package
Browse files Browse the repository at this point in the history
  • Loading branch information
Gornak40 committed Mar 10, 2024
1 parent 87a79de commit 371cbe1
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 35 deletions.
28 changes: 25 additions & 3 deletions cmd/valeria/main.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
package main

import (
"errors"
"fmt"
"os"

"github.com/Gornak40/algolymp/config"
"github.com/Gornak40/algolymp/polygon"
"github.com/Gornak40/algolymp/polygon/valeria"
"github.com/Gornak40/algolymp/polygon/valeria/textables"
"github.com/akamensky/argparse"
"github.com/sirupsen/logrus"
)

var (
ErrUnknownTexTable = errors.New("unknown textable")
)

func main() {
parser := argparse.NewParser("valeria", "Build valuer + scorer using Polygon API.")
pID := parser.Int("i", "problem_id", &argparse.Options{
Expand All @@ -21,6 +27,18 @@ func main() {
Required: false,
Help: "Print valuer.cfg in stderr",
})
tableTyp := parser.Selector("t", "table-type", []string{
textables.UniversalTag,
}, &argparse.Options{
Required: false,
Default: textables.UniversalTag,
Help: "Tex table type",
})
cntVars := parser.Int("c", "count-depvars", &argparse.Options{
Required: false,
Default: 0,
Help: "Depvars count (useful for some textables)",
})
if err := parser.Parse(os.Args); err != nil {
logrus.WithError(err).Fatal("bad arguments")
}
Expand All @@ -29,9 +47,13 @@ func main() {
pClient := polygon.NewPolygon(&cfg.Polygon)
val := valeria.NewValeria(pClient)

table := valeria.UniversalTable{}
if err := val.InformaticsValuer(*pID, &table, *verbose); err != nil {
logrus.WithError(err).Fatal("failed get scoring")
table := textables.GetTexTable(*tableTyp, *cntVars)
if table == nil {
logrus.WithError(ErrUnknownTexTable).Fatal("failed to get textable")
}

if err := val.InformaticsValuer(*pID, table, *verbose); err != nil {
logrus.WithError(err).Fatal("failed to get scoring")
}

fmt.Println(table.String()) //nolint:forbidigo // Basic functionality.
Expand Down
28 changes: 28 additions & 0 deletions polygon/valeria/textables/table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package textables

import "github.com/sirupsen/logrus"

type GroupInfo struct {
Group string
Score int
Dependencies []string
}

type Table interface {
AddGroup0(info GroupInfo)
AddGroup(info GroupInfo)
AddLastGroup(info GroupInfo)

String() string
}

func GetTexTable(tableTyp string, cntVars int) Table { //nolint:ireturn
logrus.WithFields(logrus.Fields{"type": tableTyp, "vars": cntVars}).Info("select textable")

switch tableTyp {
case UniversalTag:
return &UniversalTable{}
default:
return nil
}
}
Original file line number Diff line number Diff line change
@@ -1,36 +1,40 @@
package valeria
package textables

import (
"fmt"
"strings"
)

const (
UniversalTag = "universal"
)

// Works both in HTML and PDF render.
type UniversalTable struct {
groups []string
}

var _ TexTable = &UniversalTable{}
var _ Table = &UniversalTable{}

func (t *UniversalTable) addGroupRow(info groupInfo, comment string) {
func (t *UniversalTable) addGroupRow(info GroupInfo, comment string) {
row := fmt.Sprintf("%s & %d & %s & %s \\\\ \\hline",
info.group,
info.score,
info.Group,
info.Score,
comment,
strings.Join(info.dependencies, ", "),
strings.Join(info.Dependencies, ", "),
)
t.groups = append(t.groups, row)
}

func (t *UniversalTable) addGroup0(info groupInfo) {
func (t *UniversalTable) AddGroup0(info GroupInfo) {
t.addGroupRow(info, "тесты из условия")
}

func (t *UniversalTable) addGroup(info groupInfo) {
func (t *UniversalTable) AddGroup(info GroupInfo) {
t.addGroupRow(info, "")
}

func (t *UniversalTable) addLastGroup(info groupInfo) {
func (t *UniversalTable) AddLastGroup(info GroupInfo) {
t.addGroupRow(info, "---")
}

Expand Down
33 changes: 10 additions & 23 deletions polygon/valeria/valeria.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"

"github.com/Gornak40/algolymp/polygon"
"github.com/Gornak40/algolymp/polygon/valeria/textables"
"github.com/sirupsen/logrus"
)

Expand All @@ -24,20 +25,6 @@ func NewValeria(client *polygon.Polygon) *Valeria {
}
}

type groupInfo struct {
group string
score int
dependencies []string
}

type TexTable interface {
addGroup0(info groupInfo)
addGroup(info groupInfo)
addLastGroup(info groupInfo)

String() string
}

type scoring struct {
score map[string]int
count map[string]int
Expand Down Expand Up @@ -94,25 +81,25 @@ func (s *scoring) buildValuer() string {
return strings.Join(res, "\n")
}

func (s *scoring) buildScoring(table TexTable) {
func (s *scoring) buildScoring(table textables.Table) {
for index, group := range s.groups {
info := groupInfo{
group: group,
score: s.score[group],
dependencies: s.dependencies[group],
info := textables.GroupInfo{
Group: group,
Score: s.score[group],
Dependencies: s.dependencies[group],
}
switch index {
case 0:
table.addGroup0(info)
table.AddGroup0(info)
case len(s.groups) - 1:
table.addLastGroup(info)
table.AddLastGroup(info)
default:
table.addGroup(info)
table.AddGroup(info)
}
}
}

func (v *Valeria) InformaticsValuer(pID int, table TexTable, verbose bool) error {
func (v *Valeria) InformaticsValuer(pID int, table textables.Table, verbose bool) error {
groups, err := v.client.GetGroups(pID)
if err != nil {
return err
Expand Down

0 comments on commit 371cbe1

Please sign in to comment.