Skip to content

Commit f41a22f

Browse files
committed
Fix bugs and enable app pushing
1 parent 781531a commit f41a22f

File tree

10 files changed

+87
-20
lines changed

10 files changed

+87
-20
lines changed

.github/workflows/build.yml

+7-7
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ jobs:
2727
name: fmcore/coreapi
2828
username: ${{ secrets.DOCKER_USERNAME }}
2929
password: ${{ secrets.DOCKER_PASSWORD }}
30-
# post-to-webhook:
31-
# needs: [docker-publish-latest]
32-
# runs-on: ubuntu-latest
33-
# steps:
34-
# - run: |
35-
# set +x
36-
# curl -XPOST -H 'X-Webhook-Auth: ${{ secrets.WEBHOOK_SECRET }}' -H "Content-type: application/json" -d '{"app": "flagbrew", "service": "coreapi"}' '${{ secrets.WEBHOOK_URL }}'
30+
post-to-webhook:
31+
needs: [docker-publish-latest]
32+
runs-on: ubuntu-latest
33+
steps:
34+
- run: |
35+
set +x
36+
curl -XPOST -H 'X-Webhook-Auth: ${{ secrets.WEBHOOK_SECRET }}' -H "Content-type: application/json" -d '{"app": "flagbrew", "service": "coreapi"}' '${{ secrets.WEBHOOK_URL }}'

http.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77
"time"
88

9+
"github.com/FlagBrew/CoreAPI/internal/handlers/generalhandler"
910
"github.com/FlagBrew/CoreAPI/internal/handlers/infohandler"
1011
"github.com/FlagBrew/CoreAPI/internal/handlers/legalityhandler"
1112
"github.com/go-chi/chi/v5"
@@ -73,9 +74,7 @@ func httpServer(ctx context.Context) *http.Server {
7374

7475
r.Route("/api/info", infohandler.NewHandler().Route)
7576
r.Route("/api/legality", legalityhandler.NewHandler().Route)
76-
r.Get("/ping", func(w http.ResponseWriter, r *http.Request) {
77-
w.Write([]byte("pong"))
78-
})
77+
r.Route("/api/general", generalhandler.NewHandler().Route)
7978

8079
return &http.Server{
8180
Addr: cli.Flags.HTTP.Addr,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package generalhandler
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"time"
8+
9+
"github.com/FlagBrew/CoreAPI/internal/models"
10+
"github.com/FlagBrew/CoreAPI/internal/utils"
11+
"github.com/go-chi/chi/v5"
12+
"github.com/lrstanley/chix"
13+
)
14+
15+
type Handler struct {
16+
}
17+
18+
func NewHandler() *Handler {
19+
return &Handler{}
20+
}
21+
22+
func (h *Handler) Route(r chi.Router) {
23+
r.Get("/ping", h.ping)
24+
}
25+
26+
func (h *Handler) ping(w http.ResponseWriter, r *http.Request) {
27+
start := time.Now()
28+
version, err := utils.RunCorePython("version", "potato", "", r.Context())
29+
30+
if err != nil {
31+
chix.Error(w, r, fmt.Errorf("something went wrong, please try again later"))
32+
return
33+
}
34+
35+
v := &models.ALMVersion{}
36+
if err := json.Unmarshal([]byte(version), &v); err != nil {
37+
chix.Error(w, r, fmt.Errorf("something went wrong, please try again later"))
38+
return
39+
}
40+
41+
chix.JSON(w, r, 200, chix.M{
42+
"alm_version": v.Version,
43+
"response_time": time.Since(start).Milliseconds(),
44+
})
45+
}

internal/models/misc.go

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package models
2+
3+
type ALMVersion struct {
4+
Version string `json:"alm_version"`
5+
}

internal/models/requests.go

+5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ type GetInfoRequest struct {
44
Generation string `json:"generation" form:"generation" query:"generation" validate:"omitempty,oneof=1 2 3 4 5 6 7 8 9 LGPE BDSP PLA"`
55
}
66

7+
type GetInfoRequestB64 struct {
8+
Generation string `json:"generation" form:"generation" query:"generation" validate:"omitempty,oneof=1 2 3 4 5 6 7 8 9 LGPE BDSP PLA"`
9+
Base64 string `json:"base64" form:"base64" query:"base64" validate:"required"`
10+
}
11+
712
type LegalityCheckRequest struct {
813
Generation string `json:"generation" form:"generation" query:"generation" validate:"omitempty,oneof=1 2 3 4 5 6 7 8 9 LGPE BDSP PLA"`
914
}

python/main.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# type: ignore ReportMissingImport
2+
13
import base64
24
import signal
35
from utils.load import *
@@ -8,14 +10,15 @@
810
from utils.sprites import Sprites
911
import json
1012
import sys
13+
from PKHeX.Core.AutoMod import ALMVersion
1114

1215
language = LanguageStrings()
1316
sprites = Sprites()
1417
moveTypes = MoveTypes()
1518

1619
def handleArgs():
1720
parser = argparse.ArgumentParser()
18-
parser.add_argument('--mode', type=str, required=True, choices=['legalize', 'report', 'info'], help='The function to perform')
21+
parser.add_argument('--mode', type=str, required=True, choices=['legalize', 'report', 'info', 'version'], help='The function to perform')
1922
parser.add_argument('--pkmn', type=str, required=True, help='The base64 encoded pokemon to perform the function on')
2023
parser.add_argument('--generation', type=str, required=False, help='The generation of the pokemon to perform the function on')
2124
return parser.parse_args()
@@ -55,6 +58,12 @@ def autoLegality(pkmn, generation):
5558
}))
5659
sys.stdout.write("\n")
5760
sys.stdout.flush()
61+
def version():
62+
sys.stdout.write(json.dumps({
63+
"alm_version": ALMVersion.CurrentVersion
64+
}))
65+
sys.stdout.write("\n")
66+
sys.stdout.flush()
5867

5968

6069
def getInfo(pkmn):
@@ -72,6 +81,10 @@ def handleTermination(signum, frame):
7281
signal.signal(signal.SIGINT, handleTermination)
7382
signal.signal(signal.SIGTERM, handleTermination)
7483

84+
if args.mode == 'version':
85+
version()
86+
sys.exit(0)
87+
7588
pkmn, error = get_pokemon_from_base64(args.pkmn, args.generation)
7689
if error is not None:
7790
sys.stderr.write(json.dumps(error, ensure_ascii=False))
@@ -88,7 +101,7 @@ def handleTermination(signum, frame):
88101
sys.stderr.write(json.dumps({"error": "something went wrong with legalizing your Pokemon"}))
89102
sys.stderr.write("\n")
90103
sys.stderr.flush()
91-
print(e)
104+
# print(e)
92105
sys.exit(1)
93106
elif args.mode == 'info':
94107
try:
@@ -97,5 +110,5 @@ def handleTermination(signum, frame):
97110
sys.stderr.write(json.dumps({"error": "something went wrong with getting info for your Pokemon"}))
98111
sys.stderr.write("\n")
99112
sys.stderr.flush()
100-
print(e)
113+
# print(e)
101114
sys.exit(1)

python/utils/legality.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def legalize(pkmn, generation):
4545

4646
legalized, err = helpers.get_pokemon_from_base64(result, generation)
4747
if err is not None:
48-
print(err, info.Generation)
48+
# print(err, info.Generation)
4949
return None
5050

5151
SimpleEdits.SetTrainerData(legalized, info)

python/utils/pkmn.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# type: ignore ReportMissingImport
22

33
import json
4-
from utils.helpers import LanguageStrings, MoveTypes
4+
from utils.helpers import LanguageStrings, MoveTypes, get_generation_from_version
55
from utils.sprites import Sprites
66
from utils.weridness import fix_weridness_for_strings
77
from utils.legality import legality_check
@@ -79,7 +79,7 @@ def __init__(self, pkmn, strings: LanguageStrings, moveTypes: MoveTypes, spriteH
7979

8080
self.ot_gender = GameInfo.GenderSymbolASCII[pkmn.OT_Gender]
8181
self.is_legal, self.illegal_reasons = legality_check(pkmn)
82-
self.generation = pkmn.Generation
82+
self.generation = get_generation_from_version(pkmn.Version)
8383
self.dex_number = pkmn.Species
8484
self.size = pkmn.SIZE_STORED
8585
self.item_num = pkmn.HeldItem

python/utils/sprites.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,14 @@ def generate_bindings(strings: LanguageStrings):
139139
lastIndex = data.get("last_index")
140140
# check the size
141141
if len(species_form) != data.get('last_size'):
142-
print(len(species_form), data.get('last_size'))
142+
# print(len(species_form), data.get('last_size'))
143143
q = input("The size of the species_form list is different than the lastSize in bindings.json, do you want to start from the beginning? (y/n): ")
144144
if q == "y":
145145
lastIndex = 0
146146
except FileNotFoundError:
147147
pass
148148

149-
print("There are {} forms to go through and you have gotten through {} of them, meaning {} more to go, good luck, you're doing this manually".format(len(species_form), lastIndex, len(species_form)- lastIndex))
149+
# print("There are {} forms to go through and you have gotten through {} of them, meaning {} more to go, good luck, you're doing this manually".format(len(species_form), lastIndex, len(species_form)- lastIndex))
150150
bindings = {}
151151
if lastIndex != 0:
152152
bindings = data.get('bindings')
@@ -204,7 +204,7 @@ def generate_bindings(strings: LanguageStrings):
204204
"last_index": species_form.index(s),
205205
"last_size": len(species_form)
206206
}, f, indent=4)
207-
print("Interrupted, saved bindings to bindings.json")
207+
# print("Interrupted, saved bindings to bindings.json")
208208

209209

210210

python/utils/weridness.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def fix_weridness_for_strings(input: str):
3636
string = split[-1]
3737
# replace the input with the new string
3838
input = input.replace(match, string)
39-
print(split, string, input)
39+
# print(split, string, input)
4040

4141

4242
# if there isn't anything we need to fix, just return the input

0 commit comments

Comments
 (0)