From 9d104db0d69ebe9c047d7074421addd6eb3b3e72 Mon Sep 17 00:00:00 2001 From: Joey Hines Date: Sat, 8 May 2021 15:42:47 -0500 Subject: [PATCH] Refactor ahead of API addition + This commit started as an experiment in a GraphQL api. It didn't turn out well... --- .gitignore | 2 +- Cargo.lock | 899 +++++++++++++++++- Cargo.toml | 3 +- geoffrey_api/Cargo.toml | 15 + geoffrey_api/src/main.rs | 12 + geoffrey_db/src/database.rs | 57 +- geoffrey_db/src/lib.rs | 10 +- geoffrey_models/src/lib.rs | 4 +- geoffrey_models/src/models/attraction.rs | 40 - geoffrey_models/src/models/base.rs | 40 - geoffrey_models/src/models/farm.rs | 44 - geoffrey_models/src/models/item.rs | 20 + geoffrey_models/src/models/locations/farm.rs | 24 + .../src/models/locations/market.rs | 24 + geoffrey_models/src/models/locations/mod.rs | 73 ++ geoffrey_models/src/models/locations/shop.rs | 27 + geoffrey_models/src/models/locations/town.rs | 23 + geoffrey_models/src/models/market.rs | 44 - geoffrey_models/src/models/mod.rs | 82 +- geoffrey_models/src/models/shop.rs | 82 -- geoffrey_models/src/models/town.rs | 45 - 21 files changed, 1164 insertions(+), 406 deletions(-) create mode 100644 geoffrey_api/Cargo.toml create mode 100644 geoffrey_api/src/main.rs delete mode 100644 geoffrey_models/src/models/attraction.rs delete mode 100644 geoffrey_models/src/models/base.rs delete mode 100644 geoffrey_models/src/models/farm.rs create mode 100644 geoffrey_models/src/models/locations/farm.rs create mode 100644 geoffrey_models/src/models/locations/market.rs create mode 100644 geoffrey_models/src/models/locations/mod.rs create mode 100644 geoffrey_models/src/models/locations/shop.rs create mode 100644 geoffrey_models/src/models/locations/town.rs delete mode 100644 geoffrey_models/src/models/market.rs delete mode 100644 geoffrey_models/src/models/shop.rs delete mode 100644 geoffrey_models/src/models/town.rs diff --git a/.gitignore b/.gitignore index 7b881bc..150a35b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,3 @@ /target .idea -database/ +/database diff --git a/Cargo.lock b/Cargo.lock index c9e1162..19a5ff3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6,18 +6,49 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" +[[package]] +name = "base64" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "904dfeac50f3cdaba28fc6f57fdcddb75f49ed61346676a78c4ffe55877802fd" + [[package]] name = "bitflags" version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +[[package]] +name = "block-buffer" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" +dependencies = [ + "generic-array", +] + +[[package]] +name = "buf_redux" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b953a6887648bb07a535631f2bc00fbdb2a2216f135552cb3f534ed136b9c07f" +dependencies = [ + "memchr", + "safemem", +] + [[package]] name = "byteorder" version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ae44d1a3d5a19df61dd0c8beb138458ac2a53a7ac09eba97d55592540004306b" +[[package]] +name = "bytes" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b700ce4376041dcd0a327fd0097c41095743c4c8af8887265942faf1100bd040" + [[package]] name = "cfg-if" version = "1.0.0" @@ -38,6 +69,12 @@ dependencies = [ "winapi", ] +[[package]] +name = "cpuid-bool" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8aebca1129a03dc6dc2b127edd729435bbc4a37e1d5f4d7513165089ceb02634" + [[package]] name = "crc32fast" version = "1.2.1" @@ -71,6 +108,31 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "digest" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d3dd60d1080a57a05ab032377049e0591415d2b31afd7028356dbf3cc6dcb066" +dependencies = [ + "generic-array", +] + +[[package]] +name = "fnv" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" + +[[package]] +name = "form_urlencoded" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5fc25a87fa4fd2094bffb06925852034d90a17f0d1e05197d4956d3555752191" +dependencies = [ + "matches", + "percent-encoding", +] + [[package]] name = "fs2" version = "0.4.3" @@ -81,6 +143,68 @@ dependencies = [ "winapi", ] +[[package]] +name = "futures" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7f55667319111d593ba876406af7c409c0ebb44dc4be6132a783ccf163ea14c1" +dependencies = [ + "futures-channel", + "futures-core", + "futures-io", + "futures-sink", + "futures-task", + "futures-util", +] + +[[package]] +name = "futures-channel" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8c2dd2df839b57db9ab69c2c9d8f3e8c81984781937fe2807dc6dcf3b2ad2939" +dependencies = [ + "futures-core", + "futures-sink", +] + +[[package]] +name = "futures-core" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15496a72fabf0e62bdc3df11a59a3787429221dd0710ba8ef163d6f7a9112c94" + +[[package]] +name = "futures-io" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d71c2c65c57704c32f5241c1223167c2c3294fd34ac020c807ddbe6db287ba59" + +[[package]] +name = "futures-sink" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85754d98985841b7d4f5e8e6fbfa4a4ac847916893ec511a2917ccd8525b8bb3" + +[[package]] +name = "futures-task" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa189ef211c15ee602667a6fcfe1c1fd9e07d42250d2156382820fba33c9df80" + +[[package]] +name = "futures-util" +version = "0.3.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1812c7ab8aedf8d6f2701a43e1243acdbcc2b36ab26e2ad421eb99ac963d96d1" +dependencies = [ + "futures-core", + "futures-sink", + "futures-task", + "pin-project-lite", + "pin-utils", + "slab", +] + [[package]] name = "fxhash" version = "0.2.1" @@ -90,6 +214,28 @@ dependencies = [ "byteorder", ] +[[package]] +name = "generic-array" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817" +dependencies = [ + "typenum", + "version_check", +] + +[[package]] +name = "geoffrey_api" +version = "0.1.0" +dependencies = [ + "geoffrey_db", + "geoffrey_models", + "serde", + "serde_json", + "tokio", + "warp", +] + [[package]] name = "geoffrey_db" version = "0.1.0" @@ -111,6 +257,174 @@ dependencies = [ "serde_json", ] +[[package]] +name = "getrandom" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fc3cb4d91f53b50155bdcfd23f6a4c39ae1969c2ae85982b135750cccaf5fce" +dependencies = [ + "cfg-if", + "libc", + "wasi 0.9.0+wasi-snapshot-preview1", +] + +[[package]] +name = "getrandom" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8" +dependencies = [ + "cfg-if", + "libc", + "wasi 0.10.0+wasi-snapshot-preview1", +] + +[[package]] +name = "h2" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d832b01df74254fe364568d6ddc294443f61cbec82816b60904303af87efae78" +dependencies = [ + "bytes", + "fnv", + "futures-core", + "futures-sink", + "futures-util", + "http", + "indexmap", + "slab", + "tokio", + "tokio-util", + "tracing", +] + +[[package]] +name = "hashbrown" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d7afe4a420e3fe79967a00898cc1f4db7c8a49a9333a29f8a4bd76a253d5cd04" + +[[package]] +name = "headers" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0b7591fb62902706ae8e7aaff416b1b0fa2c0fd0878b46dc13baa3712d8a855" +dependencies = [ + "base64", + "bitflags", + "bytes", + "headers-core", + "http", + "mime", + "sha-1", + "time", +] + +[[package]] +name = "headers-core" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7f66481bfee273957b1f20485a4ff3362987f85b2c236580d81b4eb7a326429" +dependencies = [ + "http", +] + +[[package]] +name = "hermit-abi" +version = "0.1.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "322f4de77956e22ed0e5032c359a0f1273f1f7f0d79bfa3b8ffbc730d7fbcc5c" +dependencies = [ + "libc", +] + +[[package]] +name = "http" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7245cd7449cc792608c3c8a9eaf69bd4eabbabf802713748fd739c98b82f0747" +dependencies = [ + "bytes", + "fnv", + "itoa", +] + +[[package]] +name = "http-body" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2861bd27ee074e5ee891e8b539837a9430012e249d7f0ca2d795650f579c1994" +dependencies = [ + "bytes", + "http", +] + +[[package]] +name = "httparse" +version = "1.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "615caabe2c3160b313d52ccc905335f4ed5f10881dd63dc5699d47e90be85691" + +[[package]] +name = "httpdate" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "494b4d60369511e7dea41cf646832512a94e542f68bb9c49e54518e0f468eb47" + +[[package]] +name = "hyper" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e8e946c2b1349055e0b72ae281b238baf1a3ea7307c7e9f9d64673bdd9c26ac7" +dependencies = [ + "bytes", + "futures-channel", + "futures-core", + "futures-util", + "h2", + "http", + "http-body", + "httparse", + "httpdate", + "itoa", + "pin-project", + "socket2", + "tokio", + "tower-service", + "tracing", + "want", +] + +[[package]] +name = "idna" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89829a5d69c23d348314a7ac337fe39173b61149a9864deabd260983aed48c21" +dependencies = [ + "matches", + "unicode-bidi", + "unicode-normalization", +] + +[[package]] +name = "indexmap" +version = "1.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "824845a0bf897a9042383849b02c1bc219c2383772efcd5c6f9766fa4b81aef3" +dependencies = [ + "autocfg", + "hashbrown", +] + +[[package]] +name = "input_buffer" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f97967975f448f1a7ddb12b0bc41069d09ed6a1c161a92687e057325db35d413" +dependencies = [ + "bytes", +] + [[package]] name = "instant" version = "0.1.9" @@ -156,6 +470,18 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "matches" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" + +[[package]] +name = "memchr" +version = "2.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ee1c47aaa256ecabcaea351eae4a9b01ef39ed810004e298d2511ed284b1525" + [[package]] name = "memoffset" version = "0.6.1" @@ -165,6 +491,72 @@ dependencies = [ "autocfg", ] +[[package]] +name = "mime" +version = "0.3.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a60c7ce501c71e03a9c9c0d35b861413ae925bd979cc7a4e30d060069aaac8d" + +[[package]] +name = "mime_guess" +version = "2.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2684d4c2e97d99848d30b324b00c8fcc7e5c897b7cbb5819b09e7c90e8baf212" +dependencies = [ + "mime", + "unicase", +] + +[[package]] +name = "mio" +version = "0.7.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5dede4e2065b3842b8b0af444119f3aa331cc7cc2dd20388bfb0f5d5a38823a" +dependencies = [ + "libc", + "log", + "miow", + "ntapi", + "winapi", +] + +[[package]] +name = "miow" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a33c1b55807fbed163481b5ba66db4b2fa6cde694a5027be10fb724206c5897" +dependencies = [ + "socket2", + "winapi", +] + +[[package]] +name = "multipart" +version = "0.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d050aeedc89243f5347c3e237e3e13dc76fbe4ae3742a57b94dc14f69acf76d4" +dependencies = [ + "buf_redux", + "httparse", + "log", + "mime", + "mime_guess", + "quick-error", + "rand 0.7.3", + "safemem", + "tempfile", + "twoway", +] + +[[package]] +name = "ntapi" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f6bb902e437b6d86e03cce10a7e2af662292c5dfef23b65899ea3ac9354ad44" +dependencies = [ + "winapi", +] + [[package]] name = "num-integer" version = "0.1.44" @@ -184,6 +576,28 @@ dependencies = [ "autocfg", ] +[[package]] +name = "num_cpus" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" +dependencies = [ + "hermit-abi", + "libc", +] + +[[package]] +name = "once_cell" +version = "1.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3" + +[[package]] +name = "opaque-debug" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "624a8340c38c1b80fd549087862da4ba43e08858af025b236e509b6649fc13d5" + [[package]] name = "parking_lot" version = "0.11.1" @@ -209,6 +623,50 @@ dependencies = [ "winapi", ] +[[package]] +name = "percent-encoding" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e" + +[[package]] +name = "pin-project" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96fa8ebb90271c4477f144354485b8068bd8f6b78b428b01ba892ca26caf0b63" +dependencies = [ + "pin-project-internal", +] + +[[package]] +name = "pin-project-internal" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "758669ae3558c6f74bd2a18b41f7ac0b5a195aea6639d6a9b5e5d1ad5ba24c0b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pin-project-lite" +version = "0.2.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc0e1f259c92177c30a4c9d177246edd0a3568b25756a977d0632cf8fa37e905" + +[[package]] +name = "pin-utils" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184" + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + [[package]] name = "proc-macro2" version = "1.0.24" @@ -218,6 +676,12 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "quick-error" +version = "1.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" + [[package]] name = "quote" version = "1.0.9" @@ -227,6 +691,87 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rand" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" +dependencies = [ + "getrandom 0.1.16", + "libc", + "rand_chacha 0.2.2", + "rand_core 0.5.1", + "rand_hc 0.2.0", +] + +[[package]] +name = "rand" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0ef9e7e66b4468674bfcb0c81af8b7fa0bb154fa9f28eb840da5c447baeb8d7e" +dependencies = [ + "libc", + "rand_chacha 0.3.0", + "rand_core 0.6.2", + "rand_hc 0.3.0", +] + +[[package]] +name = "rand_chacha" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" +dependencies = [ + "ppv-lite86", + "rand_core 0.5.1", +] + +[[package]] +name = "rand_chacha" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e12735cf05c9e10bf21534da50a147b924d555dc7a547c42e6bb2d5b6017ae0d" +dependencies = [ + "ppv-lite86", + "rand_core 0.6.2", +] + +[[package]] +name = "rand_core" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" +dependencies = [ + "getrandom 0.1.16", +] + +[[package]] +name = "rand_core" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34cf66eb183df1c5876e2dcf6b13d57340741e8dc255b48e40a26de954d06ae7" +dependencies = [ + "getrandom 0.2.2", +] + +[[package]] +name = "rand_hc" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c" +dependencies = [ + "rand_core 0.5.1", +] + +[[package]] +name = "rand_hc" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3190ef7066a446f2e7f42e239d161e905420ccab01eb967c9eb27d21b2322a73" +dependencies = [ + "rand_core 0.6.2", +] + [[package]] name = "redox_syscall" version = "0.2.5" @@ -236,12 +781,33 @@ dependencies = [ "bitflags", ] +[[package]] +name = "remove_dir_all" +version = "0.5.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3acd125665422973a33ac9d3dd2df85edad0f4ae9b00dafb1a05e43a9f5ef8e7" +dependencies = [ + "winapi", +] + [[package]] name = "ryu" version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" +[[package]] +name = "safemem" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef703b7cb59335eae2eb93ceb664c0eb7ea6bf567079d843e09420219668e072" + +[[package]] +name = "scoped-tls" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea6a9290e3c9cf0f18145ef7ffa62d68ee0bf5fcd651017e586dc7fd5da448c2" + [[package]] name = "scopeguard" version = "1.1.0" @@ -250,18 +816,18 @@ checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd" [[package]] name = "serde" -version = "1.0.123" +version = "1.0.124" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "92d5161132722baa40d802cc70b15262b98258453e85e5d1d365c757c73869ae" +checksum = "bd761ff957cb2a45fbb9ab3da6512de9de55872866160b23c25f1a841e99d29f" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.123" +version = "1.0.124" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9391c295d64fc0abb2c556bad848f33cb8296276b1ad2677d1ae1ace4f258f31" +checksum = "1800f7693e94e186f5e25a28291ae1570da908aff7d97a095dec1e56ff99069b" dependencies = [ "proc-macro2", "quote", @@ -279,6 +845,46 @@ dependencies = [ "serde", ] +[[package]] +name = "serde_urlencoded" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edfa57a7f8d9c1d260a549e7224100f6c43d43f9103e06dd8b4095a9b2b43ce9" +dependencies = [ + "form_urlencoded", + "itoa", + "ryu", + "serde", +] + +[[package]] +name = "sha-1" +version = "0.9.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfebf75d25bd900fd1e7d11501efab59bc846dbc76196839663e6637bba9f25f" +dependencies = [ + "block-buffer", + "cfg-if", + "cpuid-bool", + "digest", + "opaque-debug", +] + +[[package]] +name = "signal-hook-registry" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16f1d0fef1604ba8f7a073c7e701f213e056707210e9020af4528e0101ce11a6" +dependencies = [ + "libc", +] + +[[package]] +name = "slab" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c111b5bd5695e56cffe5129854aa230b39c93a305372fdbb2668ca2394eea9f8" + [[package]] name = "sled" version = "0.34.6" @@ -301,6 +907,17 @@ version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fe0f37c9e8f3c5a4a66ad655a93c74daac4ad00c441533bf5c6e7990bb42604e" +[[package]] +name = "socket2" +version = "0.3.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "122e570113d28d773067fab24266b66753f6ea915758651696b6e35e49f88d6e" +dependencies = [ + "cfg-if", + "libc", + "winapi", +] + [[package]] name = "syn" version = "1.0.61" @@ -312,6 +929,20 @@ dependencies = [ "unicode-xid", ] +[[package]] +name = "tempfile" +version = "3.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dac1c663cfc93810f88aed9b8941d48cabf856a1b111c29a40439018d870eb22" +dependencies = [ + "cfg-if", + "libc", + "rand 0.8.3", + "redox_syscall", + "remove_dir_all", + "winapi", +] + [[package]] name = "time" version = "0.1.44" @@ -319,16 +950,274 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6db9e6914ab8b1ae1c260a4ae7a49b6c5611b40328a735b21862567685e73255" dependencies = [ "libc", - "wasi", + "wasi 0.10.0+wasi-snapshot-preview1", "winapi", ] +[[package]] +name = "tinyvec" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "317cca572a0e89c3ce0ca1f1bdc9369547fe318a683418e42ac8f59d14701023" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c" + +[[package]] +name = "tokio" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d56477f6ed99e10225f38f9f75f872f29b8b8bd8c0b946f63345bb144e9eeda" +dependencies = [ + "autocfg", + "bytes", + "libc", + "memchr", + "mio", + "num_cpus", + "once_cell", + "parking_lot", + "pin-project-lite", + "signal-hook-registry", + "tokio-macros", + "winapi", +] + +[[package]] +name = "tokio-macros" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "caf7b11a536f46a809a8a9f0bb4237020f70ecbf115b842360afb127ea2fda57" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tokio-stream" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c535f53c0cfa1acace62995a8994fc9cc1f12d202420da96ff306ee24d576469" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tokio-tungstenite" +version = "0.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e1a5f475f1b9d077ea1017ecbc60890fda8e54942d680ca0b1d2b47cfa2d861b" +dependencies = [ + "futures-util", + "log", + "pin-project", + "tokio", + "tungstenite", +] + +[[package]] +name = "tokio-util" +version = "0.6.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ec31e5cc6b46e653cf57762f36f71d5e6386391d88a72fd6db4508f8f676fb29" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "log", + "pin-project-lite", + "tokio", +] + +[[package]] +name = "tower-service" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "360dfd1d6d30e05fda32ace2c8c70e9c0a9da713275777f5a4dbb8a1893930c6" + +[[package]] +name = "tracing" +version = "0.1.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01ebdc2bb4498ab1ab5f5b73c5803825e60199229ccba0698170e3be0e7f959f" +dependencies = [ + "cfg-if", + "log", + "pin-project-lite", + "tracing-core", +] + +[[package]] +name = "tracing-core" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f50de3927f93d202783f4513cda820ab47ef17f624b03c096e86ef00c67e6b5f" +dependencies = [ + "lazy_static", +] + +[[package]] +name = "tracing-futures" +version = "0.2.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "97d095ae15e245a057c8e8451bab9b3ee1e1f68e9ba2b4fbc18d0ac5237835f2" +dependencies = [ + "pin-project", + "tracing", +] + +[[package]] +name = "try-lock" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" + +[[package]] +name = "tungstenite" +version = "0.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ada8297e8d70872fa9a551d93250a9f407beb9f37ef86494eb20012a2ff7c24" +dependencies = [ + "base64", + "byteorder", + "bytes", + "http", + "httparse", + "input_buffer", + "log", + "rand 0.8.3", + "sha-1", + "url", + "utf-8", +] + +[[package]] +name = "twoway" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59b11b2b5241ba34be09c3cc85a36e56e48f9888862e19cedf23336d35316ed1" +dependencies = [ + "memchr", +] + +[[package]] +name = "typenum" +version = "1.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "879f6906492a7cd215bfa4cf595b600146ccfac0c79bcbd1f3000162af5e8b06" + +[[package]] +name = "unicase" +version = "2.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50f37be617794602aabbeee0be4f259dc1778fabe05e2d67ee8f79326d5cb4f6" +dependencies = [ + "version_check", +] + +[[package]] +name = "unicode-bidi" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" +dependencies = [ + "matches", +] + +[[package]] +name = "unicode-normalization" +version = "0.1.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07fbfce1c8a97d547e8b5334978438d9d6ec8c20e38f56d4a4374d181493eaef" +dependencies = [ + "tinyvec", +] + [[package]] name = "unicode-xid" version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" +[[package]] +name = "url" +version = "2.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ccd964113622c8e9322cfac19eb1004a07e636c545f325da085d5cdde6f1f8b" +dependencies = [ + "form_urlencoded", + "idna", + "matches", + "percent-encoding", +] + +[[package]] +name = "utf-8" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05e42f7c18b8f902290b009cde6d651262f956c98bc51bca4cd1d511c9cd85c7" + +[[package]] +name = "version_check" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" + +[[package]] +name = "want" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ce8a968cb1cd110d136ff8b819a556d6fb6d919363c61534f6860c7eb172ba0" +dependencies = [ + "log", + "try-lock", +] + +[[package]] +name = "warp" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3dafd0aac2818a94a34df0df1100a7356c493d8ede4393875fd0b5c51bb6bc80" +dependencies = [ + "bytes", + "futures", + "headers", + "http", + "hyper", + "log", + "mime", + "mime_guess", + "multipart", + "percent-encoding", + "pin-project", + "scoped-tls", + "serde", + "serde_json", + "serde_urlencoded", + "tokio", + "tokio-stream", + "tokio-tungstenite", + "tokio-util", + "tower-service", + "tracing", + "tracing-futures", +] + +[[package]] +name = "wasi" +version = "0.9.0+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" + [[package]] name = "wasi" version = "0.10.0+wasi-snapshot-preview1" diff --git a/Cargo.toml b/Cargo.toml index 5d7f8dc..92b767e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,6 @@ [workspace] members = [ "geoffrey_models", - "geoffrey_db" + "geoffrey_db", + "geoffrey_api" ] \ No newline at end of file diff --git a/geoffrey_api/Cargo.toml b/geoffrey_api/Cargo.toml new file mode 100644 index 0000000..9026419 --- /dev/null +++ b/geoffrey_api/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "geoffrey_api" +version = "0.1.0" +authors = ["Joey Hines "] +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +tokio = { version = "1", features = ["full"] } +warp = "0.3" +serde = "1.0.124" +serde_json = "1.0.64" +geoffrey_models = { path = "../geoffrey_models" } +geoffrey_db = { path = "../geoffrey_db" } \ No newline at end of file diff --git a/geoffrey_api/src/main.rs b/geoffrey_api/src/main.rs new file mode 100644 index 0000000..8b6702a --- /dev/null +++ b/geoffrey_api/src/main.rs @@ -0,0 +1,12 @@ +use warp::Filter; + +#[tokio::main] +async fn main() { + // GET /hello/warp => 200 OK with body "Hello, warp!" + let hello = warp::path!("hello" / String) + .map(|name| format!("Hello, {}!", name)); + + warp::serve(hello) + .run(([127, 0, 0, 1], 3030)) + .await; +} \ No newline at end of file diff --git a/geoffrey_db/src/database.rs b/geoffrey_db/src/database.rs index 90b4c2c..f2acbfa 100644 --- a/geoffrey_db/src/database.rs +++ b/geoffrey_db/src/database.rs @@ -1,7 +1,8 @@ use crate::error::Result; use std::path::Path; use geoffrey_models::GeoffreyDatabaseModel; -use crate::u64_to_bytes; +use crate::{u64_from_bytes, u64_to_bytes}; +use sled::Iter; pub struct Database { db: sled::Db, @@ -51,6 +52,31 @@ impl Database { } } + + pub fn filter(&self, f: F) -> Result> where T: GeoffreyDatabaseModel, F: Fn(u64, &T) -> bool { + let tree = self.db.open_tree(T::tree())?; + + Ok(tree.iter().filter_map(|e| { + if let Ok((id, data)) = e { + let id = u64_from_bytes(&mut id.clone()); + let data = T::try_from_bytes(&data).unwrap(); + + if f(id, &data) { + Some(data) + } + else { + None + } + } + else { + None + } + }).collect()) + } + + pub fn tree_iter(&self) -> Result where T: GeoffreyDatabaseModel { + Ok(self.db.open_tree(T::tree()).map(|tree| tree.iter())?) + } } #[cfg(test)] @@ -59,12 +85,10 @@ mod tests { use std::path::Path; use geoffrey_models::models::player::{Player, UserID}; use lazy_static::lazy_static; - use geoffrey_models::models::shop::Shop; - use geoffrey_models::models::{Location, Position}; - + use geoffrey_models::GeoffreyDatabaseModel; lazy_static! { - static ref DB: Database = Database::new(Path::new("../database")).unwrap(); + static ref DB: Database = Database::new(Path::new("../locations")).unwrap(); } fn cleanup() { @@ -73,41 +97,26 @@ mod tests { #[test] fn test_insert() { - let player = Player::new("CoolZero123", UserID::DiscordUUID(0)); + let player = Player::new("CoolZero123", UserID::DiscordUUID(0u64)); let p2 = DB.insert::(player.clone()).unwrap(); - assert!(p2.id.is_some()); + assert!(p2.id().is_some()); assert_eq!(player.name, p2.name); cleanup(); } #[test] fn test_get() { - let player = Player::new("CoolZero123", UserID::DiscordUUID(0)); + let player = Player::new("CoolZero123", UserID::DiscordUUID(0u64)); let p2 = DB.insert::(player.clone()).unwrap(); - let p3 = DB.get::(p2.id.unwrap()).unwrap(); + let p3 = DB.get::(p2.id().unwrap()).unwrap(); assert!(p3.is_some()); assert_eq!(p3.unwrap().name, player.name); cleanup(); } - - #[test] - fn test_shop() { - let player = Player::new("CoolZero123", UserID::DiscordUUID(0)); - let player= DB.insert::(player.clone()).unwrap(); - let shop = Shop::new("cool shop123", Position::default(), player.id.unwrap(), None); - - let shop2 = DB.insert::(shop.clone()).unwrap(); - assert_eq!(shop.name(), shop2.name()); - - let shop3 = DB.get::(shop2.id.unwrap()).unwrap().unwrap(); - - assert_eq!(shop3.id, shop2.id); - cleanup(); - } } diff --git a/geoffrey_db/src/lib.rs b/geoffrey_db/src/lib.rs index be173f5..6a1b26f 100644 --- a/geoffrey_db/src/lib.rs +++ b/geoffrey_db/src/lib.rs @@ -1,5 +1,6 @@ #![allow(dead_code)] -use byteorder::{WriteBytesExt, BigEndian}; +use byteorder::{WriteBytesExt, BigEndian, ReadBytesExt}; +use std::io::Cursor; pub mod database; pub mod error; @@ -9,4 +10,9 @@ pub fn u64_to_bytes(n: u64) -> Vec { id_bytes.write_u64::(n).unwrap(); id_bytes -} \ No newline at end of file +} + +pub fn u64_from_bytes(bytes: &mut [u8]) -> u64 { + let mut cursor = Cursor::new(bytes); + cursor.read_u64::().unwrap() +} diff --git a/geoffrey_models/src/lib.rs b/geoffrey_models/src/lib.rs index 06d339f..3442075 100644 --- a/geoffrey_models/src/lib.rs +++ b/geoffrey_models/src/lib.rs @@ -1,9 +1,11 @@ #![allow(dead_code)] -use serde::{Serialize}; +use serde::Serialize; use serde::de::DeserializeOwned; pub mod models; +const DB_VERSION: u64 = 1; + pub trait GeoffreyDatabaseModel: Serialize + DeserializeOwned { fn id(&self) -> Option; fn set_id(&mut self, id: u64); diff --git a/geoffrey_models/src/models/attraction.rs b/geoffrey_models/src/models/attraction.rs deleted file mode 100644 index de1e270..0000000 --- a/geoffrey_models/src/models/attraction.rs +++ /dev/null @@ -1,40 +0,0 @@ -use serde::{Serialize, Deserialize}; -use crate::models::{LocationData, Location, Position, Tunnel}; -use crate::GeoffreyDatabaseModel; - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct Attraction { - pub id: Option, - pub location_data: LocationData -} - -impl GeoffreyDatabaseModel for Attraction { - fn id(&self) -> Option { - self.id - } - - fn set_id(&mut self, id: u64) { - self.id = Some(id) - } - - fn tree() -> String { - "attraction".to_string() - } -} - -impl Location for Attraction { - fn new(name: &str, position: Position, owner: u64, tunnel: Option) -> Self { - Self { - id: None, - location_data: LocationData::new(name, position, owner, tunnel) - } - } - - fn mut_location_data(&mut self) -> &mut LocationData { - &mut self.location_data - } - - fn location_data(&self) -> &LocationData { - &self.location_data - } -} diff --git a/geoffrey_models/src/models/base.rs b/geoffrey_models/src/models/base.rs deleted file mode 100644 index b6e07f1..0000000 --- a/geoffrey_models/src/models/base.rs +++ /dev/null @@ -1,40 +0,0 @@ -use serde::{Serialize, Deserialize}; -use crate::models::{LocationData, Location, Position, Tunnel}; -use crate::GeoffreyDatabaseModel; - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct Base { - pub id: Option, - pub location_data: LocationData -} - -impl GeoffreyDatabaseModel for Base { - fn id(&self) -> Option { - self.id - } - - fn set_id(&mut self, id: u64) { - self.id = Some(id) - } - - fn tree() -> String { - "base".to_string() - } -} - -impl Location for Base { - fn new(name: &str, position: Position, owner: u64, tunnel: Option) -> Self { - Self { - id: None, - location_data: LocationData::new(name, position, owner, tunnel) - } - } - - fn mut_location_data(&mut self) -> &mut LocationData { - &mut self.location_data - } - - fn location_data(&self) -> &LocationData { - &self.location_data - } -} diff --git a/geoffrey_models/src/models/farm.rs b/geoffrey_models/src/models/farm.rs deleted file mode 100644 index 9c65213..0000000 --- a/geoffrey_models/src/models/farm.rs +++ /dev/null @@ -1,44 +0,0 @@ -use serde::{Serialize, Deserialize}; -use crate::models::{LocationData, Location, Position, Tunnel}; -use crate::models::item::Item; -use crate::GeoffreyDatabaseModel; -use std::collections::HashSet; - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct Farm { - pub id: Option, - pub location_data: LocationData, - pub items_produced: HashSet -} - -impl GeoffreyDatabaseModel for Farm { - fn id(&self) -> Option { - self.id - } - - fn set_id(&mut self, id: u64) { - self.id = Some(id) - } - - fn tree() -> String { - "farm".to_string() - } -} - -impl Location for Farm { - fn new(name: &str, position: Position, owner: u64, tunnel: Option) -> Self { - Self { - id: None, - location_data: LocationData::new(name, position, owner, tunnel), - items_produced: HashSet::new(), - } - } - - fn mut_location_data(&mut self) -> &mut LocationData { - &mut self.location_data - } - - fn location_data(&self) -> &LocationData { - &self.location_data - } -} diff --git a/geoffrey_models/src/models/item.rs b/geoffrey_models/src/models/item.rs index 4ce2e26..ccd2d1d 100644 --- a/geoffrey_models/src/models/item.rs +++ b/geoffrey_models/src/models/item.rs @@ -1,3 +1,4 @@ +use chrono::{DateTime, Utc}; use serde::{Deserialize, Serialize}; #[derive(Serialize, Deserialize, Debug, Clone, Hash, Eq, PartialEq)] @@ -13,3 +14,22 @@ impl Item { } } +#[derive(Serialize, Deserialize, Debug, Clone, Hash, Eq, PartialEq)] +pub struct ItemListing { + pub item: Item, + pub price: u32, + pub count_per_price: u32, + pub restocked_time: DateTime +} + +impl ItemListing { + fn new(item: &str, price: u32, count_per_price: u32, restocked_time: DateTime) -> Self { + Self { + item: Item {name: item.to_string()}, + price, + count_per_price, + restocked_time, + } + } +} + diff --git a/geoffrey_models/src/models/locations/farm.rs b/geoffrey_models/src/models/locations/farm.rs new file mode 100644 index 0000000..acb5005 --- /dev/null +++ b/geoffrey_models/src/models/locations/farm.rs @@ -0,0 +1,24 @@ +use serde::{Serialize, Deserialize}; +use crate::models::item::Item; +use crate::GeoffreyDatabaseModel; +use std::collections::HashSet; + +#[derive(Serialize, Deserialize, Debug, Clone, Default)] +pub struct FarmData { + id: Option, + pub items_produced: HashSet +} + +impl GeoffreyDatabaseModel for FarmData { + fn id(&self) -> Option { + self.id + } + + fn set_id(&mut self, id: u64) { + self.id = Some(id) + } + + fn tree() -> String { + "farm".to_string() + } +} diff --git a/geoffrey_models/src/models/locations/market.rs b/geoffrey_models/src/models/locations/market.rs new file mode 100644 index 0000000..e58f9fa --- /dev/null +++ b/geoffrey_models/src/models/locations/market.rs @@ -0,0 +1,24 @@ +use serde::{Serialize, Deserialize}; +use crate::GeoffreyDatabaseModel; +use std::collections::HashSet; + +#[derive(Serialize, Deserialize, Debug, Clone, Default)] +pub struct Market { + pub id: Option, + pub shops: HashSet +} + +impl GeoffreyDatabaseModel for Market { + fn id(&self) -> Option { + self.id + } + + fn set_id(&mut self, id: u64) { + self.id = Some(id) + } + + fn tree() -> String { + "market".to_string() + } +} + diff --git a/geoffrey_models/src/models/locations/mod.rs b/geoffrey_models/src/models/locations/mod.rs new file mode 100644 index 0000000..1bf54fb --- /dev/null +++ b/geoffrey_models/src/models/locations/mod.rs @@ -0,0 +1,73 @@ +use serde::{Serialize, Deserialize}; +use std::collections::HashSet; + +use crate::GeoffreyDatabaseModel; +use crate::models::{Position, Tunnel}; + +pub mod farm; +pub mod market; +pub mod shop; +pub mod town; + + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub enum LocationType { + Base, + Shop(u64), + Attraction, + Town(u64), + Farm(u64), + Market(u64), +} + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct Location { + id: Option, + pub name: String, + pub position: Position, + owners: HashSet, + pub tunnel: Option, + loc_type: LocationType +} + +impl Location { + pub fn new (name: &str, position: Position, owner: u64, tunnel: Option, loc_type: LocationType) -> Self { + let mut owners = HashSet::new(); + owners.insert(owner); + + Self { + id: None, + name: name.to_string(), + position, + owners, + tunnel, + loc_type + } + } + + fn owners(&self) -> Vec { + self.owners.iter().cloned().collect() + } + + fn add_owner(&mut self, owner: u64) { + self.owners.insert(owner); + } + + fn remove_owner(&mut self, owner: u64) { + self.owners.remove(&owner); + } +} + +impl GeoffreyDatabaseModel for Location { + fn id(&self) -> Option { + self.id + } + + fn set_id(&mut self, id: u64) { + self.id = Some(id) + } + + fn tree() -> String { + "location".to_string() + } +} diff --git a/geoffrey_models/src/models/locations/shop.rs b/geoffrey_models/src/models/locations/shop.rs new file mode 100644 index 0000000..e2a3eb3 --- /dev/null +++ b/geoffrey_models/src/models/locations/shop.rs @@ -0,0 +1,27 @@ +use std::collections::HashSet; + +use serde::{Deserialize, Serialize}; + +use crate::GeoffreyDatabaseModel; +use crate::models::item::ItemListing; + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct Shop { + id: Option, + pub item_listings: HashSet, +} + +impl GeoffreyDatabaseModel for Shop { + fn id(&self) -> Option { + self.id + } + + fn set_id(&mut self, id: u64) { + self.id = Some(id); + } + + fn tree() -> String { + "shop".to_string() + } +} + diff --git a/geoffrey_models/src/models/locations/town.rs b/geoffrey_models/src/models/locations/town.rs new file mode 100644 index 0000000..deb4cbd --- /dev/null +++ b/geoffrey_models/src/models/locations/town.rs @@ -0,0 +1,23 @@ +use serde::{Deserialize, Serialize}; +use crate::GeoffreyDatabaseModel; +use std::collections::HashSet; + +#[derive(Serialize, Deserialize, Debug, Clone, Default)] +pub struct Town { + id: Option, + pub residents: HashSet +} + +impl GeoffreyDatabaseModel for Town { + fn id(&self) -> Option { + self.id + } + + fn set_id(&mut self, id: u64) { + self.id = Some(id) + } + + fn tree() -> String { + "town".to_string() + } +} diff --git a/geoffrey_models/src/models/market.rs b/geoffrey_models/src/models/market.rs deleted file mode 100644 index a376230..0000000 --- a/geoffrey_models/src/models/market.rs +++ /dev/null @@ -1,44 +0,0 @@ -use serde::{Serialize, Deserialize}; -use crate::models::{LocationData, Location, Position, Tunnel}; -use crate::GeoffreyDatabaseModel; -use std::collections::HashSet; - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct Market { - pub id: Option, - pub location_data: LocationData, - pub shops: HashSet -} - -impl GeoffreyDatabaseModel for Market { - fn id(&self) -> Option { - self.id - } - - fn set_id(&mut self, id: u64) { - self.id = Some(id) - } - - fn tree() -> String { - "farm".to_string() - } -} - -impl Location for Market { - fn new(name: &str, position: Position, owner: u64, tunnel: Option) -> Self { - Self { - id: None, - location_data: LocationData::new(name, position, owner, tunnel), - shops: HashSet::new() - } - } - - fn mut_location_data(&mut self) -> &mut LocationData { - &mut self.location_data - } - - fn location_data(&self) -> &LocationData { - &self.location_data - } -} - diff --git a/geoffrey_models/src/models/mod.rs b/geoffrey_models/src/models/mod.rs index 07fd72a..b6f2403 100644 --- a/geoffrey_models/src/models/mod.rs +++ b/geoffrey_models/src/models/mod.rs @@ -1,17 +1,10 @@ use serde::{Deserialize, Serialize}; -use serde::de::DeserializeOwned; -use std::collections::HashSet; pub mod player; -pub mod shop; pub mod item; -pub mod town; -pub mod farm; -pub mod market; pub mod token; pub mod meta; -mod base; -pub mod attraction; +pub mod locations; #[derive(Serialize, Deserialize, Debug, Copy, Clone)] pub enum Dimension { @@ -36,13 +29,13 @@ pub enum Direction { #[derive(Default, Serialize, Deserialize, Debug, Copy, Clone)] pub struct Position { - pub x: i64, - pub y: i64, + pub x: i32, + pub y: i32, pub dimension: Dimension, } impl Position { - pub fn new(x: i64, y: i64, dimension: Dimension) -> Self { + pub fn new(x: i32, y: i32, dimension: Dimension) -> Self { Self { x, y, @@ -54,71 +47,6 @@ impl Position { #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Tunnel { direction: Direction, - number: i64 -} - -#[derive(Serialize, Deserialize, Debug, Clone, Default)] -pub struct LocationData { - name: String, - position: Position, - owners: HashSet, - tunnel: Option, -} - - -impl LocationData { - pub fn new (name: &str, position: Position, owner: u64, tunnel: Option) -> Self { - let mut owners = HashSet::new(); - owners.insert(owner); - - Self { - name: name.to_string(), - position, - owners, - tunnel, - } - } -} - -pub trait Location: Serialize + DeserializeOwned + Clone { - fn new (name: &str, position: Position, owner: u64, tunnel: Option) -> Self; - fn mut_location_data(&mut self) -> &mut LocationData; - fn location_data(&self) -> &LocationData; - - fn name(&self) -> String { - self.location_data().name.clone() - } - - fn owners(&self) -> Vec { - self.location_data().owners.iter().cloned().collect() - } - - fn position(&self) -> Position { - self.location_data().position.clone() - } - - fn tunnel(&self) -> Option { - self.location_data().tunnel.clone() - } - - fn add_owner(&mut self, owner: u64) { - self.mut_location_data().owners.insert(owner); - } - - fn remove_owner(&mut self, owner: u64) { - self.mut_location_data().owners.remove(&owner); - } - - fn set_position(&mut self, position: Position) { - self.mut_location_data().position = position; - } - - fn set_tunnel(&mut self, tunnel: Tunnel) { - self.mut_location_data().tunnel = Some(tunnel); - } - - fn set_name(&mut self, name: &str) { - self.mut_location_data().name = name.to_string(); - } + number: i32 } diff --git a/geoffrey_models/src/models/shop.rs b/geoffrey_models/src/models/shop.rs deleted file mode 100644 index ef9876d..0000000 --- a/geoffrey_models/src/models/shop.rs +++ /dev/null @@ -1,82 +0,0 @@ -use serde::{Deserialize, Serialize}; -use chrono::{DateTime, Utc}; - -use crate::models::item::Item; -use std::collections::HashSet; -use crate::models::{LocationData, Location, Tunnel, Position}; -use crate::GeoffreyDatabaseModel; - -#[derive(Serialize, Deserialize, Debug, Clone, Hash, Eq, PartialEq)] -pub struct ItemListing { - pub item: Item, - pub price: u64, - pub count_per_price: u64, - pub restocked_time: DateTime -} - -impl ItemListing { - fn new(item: &str, price: u64, count_per_price: u64, restocked_time: DateTime) -> Self { - Self { - item: Item {name: item.to_string()}, - price, - count_per_price, - restocked_time, - } - } -} - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct Shop { - pub id: Option, - pub item_listings: HashSet, - pub location_data: LocationData -} - -impl Location for Shop { - fn new(name: &str, position: Position, owner: u64, tunnel: Option) -> Self { - Self { - id: None, - item_listings: Default::default(), - location_data: LocationData::new(name, position, owner, tunnel) - } - } - - fn mut_location_data(&mut self) -> &mut LocationData { - &mut self.location_data - } - - fn location_data(&self) -> &LocationData { - &self.location_data - } -} - -impl GeoffreyDatabaseModel for Shop { - fn id(&self) -> Option { - self.id - } - - fn set_id(&mut self, id: u64) { - self.id = Some(id); - } - - fn tree() -> String { - "shop".to_string() - } -} - -#[cfg(test)] -mod tests { - use crate::models::{Position, Dimension, LocationData, Location}; - use crate::models::player::{Player, UserID}; - use crate::models::shop::Shop; - - fn create_shop() -> Shop { - let player = Player::new("CoolZero123", UserID::DiscordUUID(0)); - let shop = Shop::new("Cool Shop 123", Position::default(), player, None); - } - - #[test] - fn test_new_shop() { - } -} - diff --git a/geoffrey_models/src/models/town.rs b/geoffrey_models/src/models/town.rs deleted file mode 100644 index 475afe0..0000000 --- a/geoffrey_models/src/models/town.rs +++ /dev/null @@ -1,45 +0,0 @@ -use serde::{Deserialize, Serialize}; -use crate::models::{LocationData, Location, Position, Tunnel}; -use crate::models::player::Player; -use crate::GeoffreyDatabaseModel; -use std::collections::HashSet; - -#[derive(Serialize, Deserialize, Debug, Clone)] -pub struct Town { - pub id: Option, - pub location_data: LocationData, - pub residents: HashSet -} - -impl GeoffreyDatabaseModel for Town { - fn id(&self) -> Option { - self.id - } - - fn set_id(&mut self, id: u64) { - self.id = Some(id) - } - - fn tree() -> String { - "town".to_string() - } -} - -impl Location for Town { - fn new(name: &str, position: Position, owner: u64, tunnel: Option) -> Self { - Self { - id: None, - location_data: LocationData::new(name, position, owner, tunnel), - residents: HashSet::new(), - } - } - - fn mut_location_data(&mut self) -> &mut LocationData { - &mut self.location_data - } - - fn location_data(&self) -> &LocationData { - &self.location_data - } -} -