~muirrum/hkgi

ba291184816e591692e89af7d9b95a1e1b6eed45 — Cara Salter 6 months ago 1eeae10 master
feat: Initial work on game tick manager (WIP)
4 files changed, 51 insertions(+), 2 deletions(-)

M internal/game/game.go
M internal/models/models.go
M main.go
M migrations/01_create_stead_table.up.sql
M internal/game/game.go => internal/game/game.go +46 -0
@@ 4,8 4,10 @@ import (
	"errors"
	"math"
	"math/rand"
	"time"

	"git.sr.ht/~muirrum/hkgi/database"
	"git.sr.ht/~muirrum/hkgi/internal/models"
)

func NewPlant(username string, plant_kind string) error {


@@ 116,3 118,47 @@ func MegaboxDrop() map[string]interface{} {
		"powder_t3": Choose[int]([]int{0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 3}),
	}
}

const SECONDS_PER_TICK = 0.5

func RunTick() error {
	db := database.DB

	for {
		var users []models.Stead
		err := db.Select(&users, "SELECT * FROM stead")
		if err != nil {
			return err
		}

		for _, u := range users {
			for k, v := range u.Ephemeral_statuses {
				status := v.(map[string]interface{})
				tt_expire := status["tt_expire"].(string)
				time_expire, _ := time.Parse(time.UnixDate, tt_expire)
				if time_expire.Before(time.Now()) {
					u.Ephemeral_statuses[k] = nil
				}
			}

			var plants []models.Plant
			err = db.Select(&plants, "SELECT * FROM plant WHERE stead_owner=$1", u.Id)
			if err != nil {
				return err
			}

			tx, err := db.Begin()
			if err != nil {
				return err
			}
			for _, p := range plants {
				if p.Kind == "dirt" {
					continue
				}
			}

		}
	}

	return nil
}

M internal/models/models.go => internal/models/models.go +1 -1
@@ 26,7 26,7 @@ type Stead struct {
	Username           string
	Password           string
	Inventory          map[string]interface{}
	ephemeral_statuses []string
	Ephemeral_statuses map[string]interface{}
}

type PlantKind string

M main.go => main.go +3 -0
@@ 6,6 6,7 @@ import (

	"git.sr.ht/~muirrum/hkgi/database"
	"git.sr.ht/~muirrum/hkgi/internal"
	"git.sr.ht/~muirrum/hkgi/internal/game"
	"git.sr.ht/~muirrum/hkgi/internal/state"
	"github.com/gofiber/fiber/v2"
	"github.com/joho/godotenv"


@@ 45,5 46,7 @@ func main() {
		return c.JSON(&fiber.Map{"data": "Hello from Fiber!"})
	})

	go game.RunTick()

	app.Listen(":6000")
}

M migrations/01_create_stead_table.up.sql => migrations/01_create_stead_table.up.sql +1 -1
@@ 3,7 3,7 @@ CREATE TABLE Stead (
    username TEXT NOT NULL UNIQUE,
    password TEXT NOT NULL,
    inventory JSONB,
    ephemeral_statuses TEXT[]
    ephemeral_statuses JSONB
);