Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Postgres Conversion #27

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
Cargo.lock
.env
/config/content.json
*.swp
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ edition = "2018"
serde = { version = "1.0.105", features = ["derive"] }
serde_json = "1.0.49"
rand = "0.7.3"
rusoto_core = "0.46.0"
rusoto_dynamodb = "0.46.0"
uuid = { version = "0.8.1", features = ["v4", "serde"] }
futures = { version = "0.3.4", features = ["compat"] }
regex = "1.3.6"
Expand All @@ -23,8 +21,10 @@ derive_more = "0.99"
dotenv = "0.15"
reqwest = { version = "0.11.1", features = ["json"] }
tokio = "1.0"
time = {version="0.2", features=["serde", "std"]}

mongodb = {version = "1.0", optional = true}
sqlx = {version = "0.5", features=["postgres", "macros", "uuid", "time", "runtime-tokio-rustls", "migrate"]}
barrel = "0.6"

[[bin]]
name = "yank_config"
Expand All @@ -35,4 +35,4 @@ name = "hcor"

[features]
actix = ["actix-web"]
mongo = ["mongodb"]

53 changes: 53 additions & 0 deletions migrations/20210305203340_hacksteaders.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
-- Add migration script here
CREATE TABLE IF NOT EXISTS crafts (
id uuid PRIMARY KEY,
until_finish float,
total_cycles float,
destroys_plant boolean,
makes text
);

CREATE TYPE seed_grower AS (
id text,
generations numeric
);

CREATE TABLE IF NOT EXISTS plants (
id uuid PRIMARY KEY,
xp numeric,
until_yield float,
craft references crafts(id),
pedigree []seed_grower,
archetype_handle text,
on_market boolean
);

CREATE TABLE IF NOT EXISTS tiles (
id uuid PRIMARY KEY,
acquired timestamp,
plant references plants(id),
steader text
);

CREATE TABLE IF NOT EXISTS profiles (
joined timestamp,
last_active timestamp,
last_farm timestamp,
id text,
xp numeric
);


CREATE TABLE IF NOT EXISTS hacksteaders (
user_id text,
profile references profiles(id)
);

CREATE TABLE IF NOT EXISTS tiles_steaders (
steader_id text references hacksteaders(user_id),
tile_id uuid references tiles(id)
);

CREATE TABLE IF NOT EXISTS possess_steaders (
steader_id text references hacksteaders(user_id),

2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ impl Config {
}

// I should _really_ use a different version of this for PlantArchetypes and PossessionArchetypes ...
pub type ArchetypeHandle = usize;
pub type ArchetypeHandle = u32;

lazy_static::lazy_static! {
pub static ref CONFIG: Config = {
Expand Down
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#![recursion_limit = "256"]
#![feature(try_trait)]
#![feature(trivial_bounds)]
#![warn(missing_docs)]

use humantime::{format_rfc3339, parse_rfc3339};
use rusoto_dynamodb::{AttributeValue, DynamoDb, DynamoDbClient};
use std::collections::HashMap;
use std::fmt;
use std::time::SystemTime;
Expand All @@ -28,6 +28,8 @@ pub use category::{Category, CategoryError};
pub use config::CONFIG;
pub use possess::{Possessed, Possession};

use sqlx::postgres::PgConnection;

pub const TABLE_NAME: &'static str = "hackagotchi";
pub type Item = HashMap<String, AttributeValue>;

Expand Down Expand Up @@ -130,7 +132,8 @@ impl Profile {
.increase_xp(&mut self.xp, amt)
}

pub async fn fetch_all(db: &DynamoDbClient) -> Result<Vec<Profile>, String> {
pub async fn fetch_all(conn: &PgConnection) -> Result<Vec<Profile>, String> {
let result = sqlx::query!("SELECT * FROM hacksteaders").fetch_all(&mut conn).await?;
let query = db
.query(rusoto_dynamodb::QueryInput {
table_name: TABLE_NAME.to_string(),
Expand Down
27 changes: 15 additions & 12 deletions src/models.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use crate::config::{ArchetypeHandle, PlantArchetype};
use crate::*;
use serde::Serialize;
use std::time::SystemTime;
use time::PrimitiveDateTime;
use sqlx::types::Type;
use sqlx::FromRow;

#[derive(Clone, Debug, Serialize)]

#[derive(Clone, Debug, Serialize, FromRow)]
pub struct Hacksteader {
pub user_id: String,
pub profile: Profile,
Expand All @@ -12,35 +15,35 @@ pub struct Hacksteader {
pub gotchis: Vec<Possessed<possess::Gotchi>>,
}

#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Clone, Serialize, FromRow)]
pub struct Tile {
pub acquired: SystemTime,
pub acquired: PrimitiveDateTime,
pub plant: Option<Plant>,
pub id: uuid::Uuid,
pub steader: String,
}

#[derive(Clone, Debug, Serialize)]
#[derive(Clone, Debug, Serialize, FromRow)]
pub struct Profile {
/// Indicates when this Hacksteader first joined the elite community.
pub joined: SystemTime,
pub last_active: SystemTime,
pub last_farm: SystemTime,
pub joined: PrimitiveDateTime,
pub last_active: PrimitiveDateTime,
pub last_farm: PrimitiveDateTime,
/// This is not an uuid::Uuid because it's actually the steader id of the person who owns this Profile
pub id: String,
pub xp: u64,
pub xp: u32,
}

#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Clone, Serialize, FromRow)]
pub struct Plant {
pub xp: u64,
pub xp: u32,
pub until_yield: f32,
pub craft: Option<Craft>,
pub pedigree: Vec<possess::seed::SeedGrower>,
pub archetype_handle: ArchetypeHandle,
}

#[derive(Debug, Clone, Serialize)]
#[derive(Debug, Clone, Serialize, FromRow)]
pub struct Craft {
pub until_finish: f32,
pub total_cycles: f32,
Expand Down
35 changes: 4 additions & 31 deletions src/possess/seed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::{config, AttributeParseError, Item, CONFIG};
use config::{ArchetypeHandle, ArchetypeKind};
use rusoto_dynamodb::AttributeValue;
use serde::{Deserialize, Serialize};
use sqlx::types::Type as PgType;

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
pub struct Seed {
Expand All @@ -17,13 +18,13 @@ impl Possessable for Seed {
PossessionKind::Seed(self)
}
}
#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq, PgType)]
pub struct SeedGrower {
pub id: String,
pub generations: u64,
pub generations: u32,
}
impl SeedGrower {
pub fn new(id: String, generations: u64) -> Self {
pub fn new(id: String, generations: u32) -> Self {
SeedGrower { id, generations }
}

Expand All @@ -49,34 +50,6 @@ impl SeedGrower {
})
}
}
impl Into<AttributeValue> for SeedGrower {
fn into(self) -> AttributeValue {
AttributeValue {
m: Some(
[
(
"id".to_string(),
AttributeValue {
s: Some(self.id.clone()),
..Default::default()
},
),
(
"generations".to_string(),
AttributeValue {
n: Some(self.generations.to_string()),
..Default::default()
},
),
]
.iter()
.cloned()
.collect(),
),
..Default::default()
}
}
}

impl std::ops::Deref for Seed {
type Target = config::SeedArchetype;
Expand Down