-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from HamoonZamiri/dev
introduce loot modules (chests and items)
- Loading branch information
Showing
10 changed files
with
296 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
backend/db/migrations/20241015224211_add_chests_and_items.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
-- +goose Up | ||
-- +goose StatementBegin | ||
-- Create the enum types first | ||
CREATE TYPE chest_type AS ENUM ('bronze', 'silver', 'gold'); | ||
CREATE TYPE item_status AS ENUM ('equipped', 'not_equipped'); | ||
CREATE TYPE chest_status AS ENUM ('opened', 'not_opened'); | ||
CREATE TYPE item_type AS ENUM ('common', 'rare', 'epic', 'legendary'); -- Assuming this is what `item_type` refers to. | ||
|
||
-- Create table `chest_items` | ||
CREATE TABLE chest_items ( | ||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | ||
image_url VARCHAR, | ||
title VARCHAR NOT NULL, | ||
rarity item_type NOT NULL, | ||
price INTEGER, | ||
created_at TIMESTAMP DEFAULT NOW(), | ||
updated_at TIMESTAMP DEFAULT NOW() | ||
); | ||
|
||
-- Create table `chests` | ||
CREATE TABLE chests ( | ||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | ||
type chest_type NOT NULL, | ||
description TEXT NOT NULL, | ||
price INTEGER NOT NULL, | ||
created_at TIMESTAMP DEFAULT NOW(), | ||
updated_at TIMESTAMP DEFAULT NOW() | ||
); | ||
|
||
-- Create table `chest_item_drop_rates` | ||
CREATE TABLE chest_item_drop_rates ( | ||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | ||
item_id UUID REFERENCES chest_items(id), | ||
chest_id UUID REFERENCES chests(id), | ||
drop_rate FLOAT NOT NULL, | ||
created_at TIMESTAMP DEFAULT NOW(), | ||
updated_at TIMESTAMP DEFAULT NOW() | ||
); | ||
|
||
-- Create table `user_items` | ||
CREATE TABLE user_items ( | ||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | ||
user_id UUID REFERENCES users(id), | ||
item_id UUID REFERENCES chest_items(id), | ||
status item_status DEFAULT 'not_equipped', | ||
created_at TIMESTAMP DEFAULT NOW(), | ||
updated_at TIMESTAMP DEFAULT NOW() | ||
); | ||
|
||
-- Create table `user_chests` | ||
CREATE TABLE user_chests ( | ||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | ||
user_id UUID REFERENCES users(id), | ||
chest_id UUID REFERENCES chests(id), | ||
quantity_owned INTEGER DEFAULT 0, | ||
created_at TIMESTAMP DEFAULT NOW(), | ||
updated_at TIMESTAMP DEFAULT NOW() | ||
); | ||
-- +goose StatementEnd | ||
-- +goose Down | ||
-- +goose StatementBegin | ||
-- Drop tables in reverse order of creation to respect foreign key constraints | ||
DROP TABLE IF EXISTS user_chests; | ||
DROP TABLE IF EXISTS user_items; | ||
DROP TABLE IF EXISTS chest_item_drop_rates; | ||
DROP TABLE IF EXISTS chests; | ||
DROP TABLE IF EXISTS chest_items; | ||
|
||
-- Drop enums | ||
DROP TYPE IF EXISTS chest_status; | ||
DROP TYPE IF EXISTS item_status; | ||
DROP TYPE IF EXISTS chest_type; | ||
DROP TYPE IF EXISTS item_type; | ||
-- +goose StatementEnd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package entities | ||
|
||
import ( | ||
"goalify/utils/options" | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
) | ||
|
||
type Chest struct { | ||
CreatedAt time.Time `db:"created_at" json:"created_at"` | ||
UpdatedAt time.Time `db:"updated_at" json:"updated_at"` | ||
Id uuid.UUID `db:"id" json:"id"` | ||
Type string `db:"type" json:"type"` // bronze | silver | gold | ||
Description string `db:"description" json:"description"` | ||
Price int `db:"price" json:"price"` | ||
} | ||
|
||
type ChestItem struct { | ||
CreatedAt time.Time `db:"created_at" json:"created_at"` | ||
UpdatedAt time.Time `db:"updated_at" json:"updated_at"` | ||
Id uuid.UUID `db:"id" json:"id"` | ||
ImageUrl string `db:"image_url" json:"image_url"` | ||
Rarity string `db:"rarity" json:"rarity"` // common | rare | epic | legendary | ||
Price options.Option[int] `db:"price" json:"price"` | ||
} | ||
|
||
type ChestItemDropRate struct { | ||
ItemId uuid.UUID `db:"item_id" json:"item_id"` | ||
ChestId uuid.UUID `db:"chest_id" json:"chest_id"` | ||
DropRate float32 `db:"drop_rate" json:"drop_rate"` | ||
} | ||
|
||
type UserChest struct { | ||
UserId uuid.UUID `db:"user_id" json:"user_id"` | ||
ChestId uuid.UUID `db:"chest_id" json:"chest_id"` | ||
QuantityOwned int `db:"quantity_owned" json:"quantity_owned"` | ||
} | ||
|
||
type UserItem struct { | ||
UserId uuid.UUID `db:"user_id" json:"user_id"` | ||
ItemId uuid.UUID `db:"item_id" json:"item_id"` | ||
Status string `db:"status" json:"status"` // equipped | unequipped | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package stores | ||
|
||
const ( | ||
CHEST_TABLE = "chests" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package stores | ||
|
||
import ( | ||
"fmt" | ||
"goalify/entities" | ||
|
||
"github.com/jmoiron/sqlx" | ||
) | ||
|
||
type LootStore interface { | ||
CreateChest(chestType, description string, price int) (*entities.Chest, error) | ||
} | ||
|
||
type lootStore struct { | ||
db *sqlx.DB | ||
} | ||
|
||
func NewChestStore(db *sqlx.DB) LootStore { | ||
return &lootStore{db: db} | ||
} | ||
|
||
func (s *lootStore) CreateChest(chestType, description string, price int) (*entities.Chest, error) { | ||
query := fmt.Sprintf(`INSERT INTO %s (type, description, price) VALUES ($1, $2, $3) RETURNING *`, CHEST_TABLE) | ||
|
||
var chest entities.Chest | ||
err := s.db.QueryRowx(query, chestType, description, price).StructScan(&chest) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &chest, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package stores | ||
|
||
import ( | ||
"context" | ||
"goalify/db" | ||
"goalify/testsetup" | ||
"log" | ||
"os" | ||
"testing" | ||
|
||
"github.com/jmoiron/sqlx" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/testcontainers/testcontainers-go/modules/postgres" | ||
) | ||
|
||
var ( | ||
dbConn *sqlx.DB | ||
cStore LootStore | ||
pgContainer *postgres.PostgresContainer | ||
) | ||
|
||
func setup(ctx context.Context) { | ||
var err error | ||
|
||
pgContainer, err = testsetup.GetPgContainer() | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
connStr, err := pgContainer.ConnectionString(ctx, "sslmode=disable") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
dbConn, err = db.NewWithConnString(connStr) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
cStore = NewChestStore(dbConn) | ||
} | ||
|
||
func TestMain(m *testing.M) { | ||
ctx := context.Background() | ||
|
||
setup(ctx) | ||
defer func() { | ||
if err := pgContainer.Terminate(ctx); err != nil { | ||
log.Fatalf("Failed to terminate container: %s", err) | ||
} | ||
}() | ||
|
||
code := m.Run() | ||
os.Exit(code) | ||
} | ||
|
||
func TestCreateChest(t *testing.T) { | ||
t.Parallel() | ||
|
||
chest, err := cStore.CreateChest("bronze", "bronze chest", 100) | ||
assert.Nil(t, err) | ||
assert.NotNil(t, chest) | ||
assert.Equal(t, 100, chest.Price) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.