aboutsummaryrefslogtreecommitdiff
path: root/go/draftsim/client
diff options
context:
space:
mode:
Diffstat (limited to 'go/draftsim/client')
-rw-r--r--go/draftsim/client/main.go200
1 files changed, 200 insertions, 0 deletions
diff --git a/go/draftsim/client/main.go b/go/draftsim/client/main.go
new file mode 100644
index 00000000..d62ce544
--- /dev/null
+++ b/go/draftsim/client/main.go
@@ -0,0 +1,200 @@
+package main
+
+import (
+ "fmt"
+ "log"
+ "net/http"
+ "strings"
+
+ "github.com/hajimehoshi/ebiten/v2"
+
+ "muhq.space/muhqs-game/go/activities"
+ "muhq.space/muhqs-game/go/draftsim/common"
+ "muhq.space/muhqs-game/go/font"
+ "muhq.space/muhqs-game/go/game"
+ "muhq.space/muhqs-game/go/ui"
+ "muhq.space/muhqs-game/go/utils"
+)
+
+const (
+ // SERVER_URL = "draft.muhq.space"
+ SERVER_URL = "localhost:8080"
+)
+
+type app struct {
+ width, height int
+}
+
+func (a *app) Layout(int, int) (int, int) {
+ return a.width, a.height
+}
+
+func (a *app) Update() error {
+ return activities.CurActivity().Update()
+}
+
+func (a *app) Draw(screen *ebiten.Image) {
+ activities.CurActivity().Draw(screen)
+}
+
+type startActivity struct {
+ ui.Collection
+}
+
+func (a *startActivity) Layout(int, int) (int, int) {
+ return a.Collection.Layout()
+}
+
+func (a *startActivity) Update() error {
+ if err := ui.Update(); err != nil {
+ return err
+ }
+ return a.Collection.Update()
+}
+
+func genHandleNotification(wdth, hght int, ctrl game.PlayerControl) func(game.PlayerNotification) {
+ return func(n game.PlayerNotification) {
+ if n.Notification != game.DraftPickPrompt {
+ log.Fatalf("Unexpexted notification: %s\n", n)
+ }
+ // Pop the Session
+ activities.PopActivity()
+ activities.PushActivity(activities.StartNewRemoteDraft(wdth, hght, ctrl, n))
+ }
+}
+
+func newStartActivity(app *app) *startActivity {
+ a := &startActivity{ui.Collection{Width: app.width, Height: app.height}}
+
+ nameInput := ui.NewTextInput(
+ a.Width/2-150,
+ 50,
+ 300,
+ 40,
+ "name")
+ a.AddWidget(nameInput)
+
+ descInput := ui.NewTextInput(
+ a.Width/2-150,
+ 100,
+ 300,
+ 40,
+ "3x[2;8]")
+ a.AddWidget(descInput)
+
+ setInput := ui.NewTextInput(
+ a.Width/2-150,
+ 150,
+ 300,
+ 40,
+ "base,magic,equipments")
+ a.AddWidget(setInput)
+
+ aiNInput := ui.NewNumberChoice(
+ (a.Width-ui.NUMBER_CHOICE_WIDTH_MIN)/2,
+ 200,
+ 1,
+ nil,
+ )
+ a.AddWidget(aiNInput)
+
+ a.AddWidget(ui.NewSimpleButton(
+ a.Width/2-100,
+ 250,
+ 200,
+ 40,
+ "local",
+ func(*ui.SimpleButton) {
+ d, err := activities.StartNewLocalDraft(
+ a.Width,
+ a.Height,
+ nameInput.Text(),
+ descInput.TextOrLabel(),
+ setInput.TextOrLabel(),
+ aiNInput.GetChoosen(),
+ )
+
+ if err == nil {
+ activities.PushActivity(d)
+ return
+ }
+
+ // TODO: hint errors
+ }))
+
+ sessionInput := ui.NewTextInput(
+ a.Width/2-150,
+ 300,
+ 300,
+ 40,
+ "session")
+ a.AddWidget(sessionInput)
+
+ a.AddWidget(ui.NewSimpleButton(
+ a.Width/2-100,
+ 350,
+ 200,
+ 40,
+ "connect",
+ func(*ui.SimpleButton) {
+ playerName := nameInput.Text()
+ player := game.NewDraftPlayer(playerName)
+ session := sessionInput.Text()
+ joinArgs := common.JoinParams{
+ Table: session,
+ Name: playerName,
+ Desc: fmt.Sprintf("%s:%s", descInput.TextOrLabel(), setInput.TextOrLabel()),
+ NPlayers: aiNInput.GetChoosen() + 1,
+ }
+
+ joinUrl := fmt.Sprintf("ws://%s/join", SERVER_URL)
+ conn, err := utils.WsDial(joinUrl)
+ if err != nil {
+ log.Fatalf("dial failed: %s\n", err)
+ }
+
+ err = utils.WsJsonSend(conn, joinArgs)
+ if err != nil {
+ log.Fatalf("sending joinArgs failed: %s\n", err)
+ }
+
+ var joinInfo common.JoinInfo
+ err = utils.WsJsonRecv(conn, &joinInfo)
+ if err != nil {
+ log.Fatalf("receiving joinInfo failed: %s\n", err)
+ }
+
+ ctrl := game.NewRWPlayerControl(player, conn)
+ startFunc := func() {
+ startUrl := fmt.Sprintf("https://%s/start", SERVER_URL)
+ r, err := http.Post(startUrl, "text/plain", strings.NewReader(session))
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer r.Body.Close()
+ }
+
+ s := activities.NewSession(
+ a.Width, a.Height,
+ fmt.Sprintf("Draft session %s", session),
+ ctrl,
+ startFunc,
+ genHandleNotification(a.Width, a.Height, ctrl))
+
+ for _, p := range joinInfo.Players {
+ s.AddPlayer(p)
+ }
+
+ activities.PushActivity(s)
+ }))
+ return a
+}
+
+func main() {
+ app := &app{width: 800, height: 1200}
+ font.InitFont()
+ activities.PushActivity(newStartActivity(app))
+ if err := ebiten.RunGame(app); err != nil {
+ log.Fatalln(err)
+ }
+}