Skip to content

Commit

Permalink
Add game title & mode tables (#15)
Browse files Browse the repository at this point in the history
Add game title & mode tables
  • Loading branch information
mayberryrfd authored Feb 18, 2018
1 parent 2bc63fe commit 723218c
Show file tree
Hide file tree
Showing 17 changed files with 126 additions and 27 deletions.
2 changes: 2 additions & 0 deletions migrations/2018-02-18-062321_fix_user_ratings_tablee/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table user_ratings drop column deviation;
alter table user_ratings drop column volatility;
4 changes: 4 additions & 0 deletions migrations/2018-02-18-062321_fix_user_ratings_tablee/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
alter table user_ratings alter column rating type numeric(6, 2);
alter table user_ratings alter column rating set default 1500.0;
alter table user_ratings add column deviation numeric(5, 2) not null default 350.0;
alter table user_ratings add column volatility numeric(3, 2) not null default 0.3;
1 change: 1 addition & 0 deletions migrations/2018-02-18-065230_add_game_title_table/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
drop table game_titles;
5 changes: 5 additions & 0 deletions migrations/2018-02-18-065230_add_game_title_table/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
create table game_titles (
game_title_id serial primary key,
game_name varchar not null,
unique(game_name)
);
1 change: 1 addition & 0 deletions migrations/2018-02-18-065740_add_game_mode_tablee/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
drop table game_modes;
6 changes: 6 additions & 0 deletions migrations/2018-02-18-065740_add_game_mode_tablee/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
create table game_modes (
game_mode_id serial primary key,
game_title_id integer references game_titles not null,
mode_name varchar not null,
unique(game_title_id, mode_name)
);
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table user_ratings drop column game_mode_id;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table user_ratings add column game_mode_id integer references game_modes not null;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table game_modes drop column team_size;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alter table game_modes add column team_size integer not null;
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
truncate game_titles restart identity cascade;
20 changes: 20 additions & 0 deletions migrations/2018-02-18-071905_add_default_games_and_modes/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
insert into game_titles (game_name) values
('Overwatch'),
('LawBreakers')
;

with ow_id as (
select game_title_id as id
from game_titles
where game_name = 'Overwatch'
)
insert into game_modes (game_title_id, mode_name, team_size) values
((select id from ow_id), 'Standard', 6),
((select id from ow_id), 'Capture The Flag', 6),
((select id from ow_id), '1v1', 1),
((select id from ow_id), '5v5', 5);

with lb_id as (select game_title_id as id from game_titles where game_name = 'LawBreakers')
insert into game_modes (game_title_id, mode_name, team_size) values
((select id from lb_id), 'Standard', 5),
((select id from lb_id), 'Team Deathmatch', 6);
32 changes: 12 additions & 20 deletions src/pugbot/db.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use diesel::{ Connection, RunQueryDsl, PgConnection };
use diesel::result::Error;
use diesel::insert_into;
use r2d2;
use r2d2_diesel::ConnectionManager;
Expand Down Expand Up @@ -46,26 +47,17 @@ pub fn create_user_and_ratings(
) -> Result<(), String> {

match insert_into(users).values(&IUsers::from(user)).get_result::<QUsers>(&*conn) {
Ok(user_record) => if let Ok(_ratings_record) = insert_into(user_ratings)
.values(&IUserRatings::from(user_record))
.get_result::<QUserRatings>(&*conn) {
Ok(())
} else {
Err("No user_ratings reckid created".to_string())
},
Err(e) => Err(format!("{:?}", e))
Ok(user_record) => match get_or_create_ratings(conn, user_record) {
Ok(_) => Ok(()),
e => Err(format!("{:?}", e))
},
e => Err(format!("{:?}", e))
}
}

// if let Ok(user_record) = insert_into(users).values(&IUsers::from(user)).get_result::<QUsers>(&*conn) {
// if let Ok(_ratings_record) = insert_into(user_ratings)
// .values(&IUserRatings::from(user_record))
// .get_result::<QUserRatings>(&*conn)
// {
// Ok(())
// } else {
// Err("Could not create user_ratings record".to_string())
// }
// } else {
// Err("Could not create users record".to_string())
// }
pub fn get_or_create_ratings(
conn: r2d2::PooledConnection<ConnectionManager<PgConnection>>,
user_record: QUsers
) -> Result<usize, Error> {
insert_into(user_ratings).values(&IUserRatings::from(user_record)).execute(&*conn)
}
4 changes: 3 additions & 1 deletion src/pugbot/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,9 @@ impl From<QUsers> for IUserRatings {
fn from(record: QUsers) -> IUserRatings {
IUserRatings {
user_id: record.user_id,
rating: BigDecimal::from_str("0.00").unwrap()
rating: None,
deviation: None,
volatility: None
}
}
}
21 changes: 21 additions & 0 deletions src/pugbot/schema.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,27 @@
table! {
game_modes (game_mode_id) {
game_mode_id -> Int4,
game_title_id -> Int4,
mode_name -> Varchar,
team_size -> Int4,
}
}

table! {
game_titles (game_title_id) {
game_title_id -> Int4,
game_name -> Varchar,
}
}

table! {
user_ratings (id) {
id -> Int4,
user_id -> Int4,
rating -> Numeric,
deviation -> Numeric,
volatility -> Numeric,
game_mode_id -> Int4,
}
}

Expand All @@ -16,3 +35,5 @@ table! {
}

joinable!(user_ratings -> users (user_id));
joinable!(game_modes -> game_titles (game_title_id));
joinable!(user_ratings -> game_modes (game_mode_id));
48 changes: 44 additions & 4 deletions src/pugbot/tables.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,28 @@
pub mod query {
use bigdecimal::BigDecimal;

#[primary_key(game_title_id)]
#[table_name="game_titles"]
#[derive(Debug, Queryable, Associations)]
pub struct GameTitles {
pub game_title_id: i32,
pub game_name: String
}

#[primary_key(game_mode_id)]
#[table_name="game_modes"]
#[belongs_to(GameTitles)]
#[derive(Debug, Queryable, Associations)]
pub struct GameModes {
pub game_mode_id: i32,
pub game_title_id: i32,
pub mode_name: String,
pub team_size: i32
}

#[primary_key(user_id)]
#[table_name="users"]
#[derive(Queryable, Associations)]
#[derive(Debug, Queryable, Associations)]
pub struct Users {
pub user_id: i32,
pub bot: bool,
Expand All @@ -12,19 +31,38 @@ pub mod query {
}

#[table_name = "user_ratings"]
#[derive(Queryable, Associations)]
#[derive(Debug, Queryable, Associations)]
#[belongs_to(Users)]
pub struct UserRatings {
pub id: i32,
pub user_id: i32,
pub rating: BigDecimal
pub rating: Option<BigDecimal>,
pub deviation: Option<BigDecimal>,
pub volatility: Option<BigDecimal>
}
}

pub mod insert {
use bigdecimal::BigDecimal;
use schema::*;

#[primary_key(game_title_id)]
#[table_name="game_titles"]
#[derive(Insertable)]
pub struct GameTitles {
pub game_name: String
}

#[primary_key(game_mode_id)]
#[table_name="game_modes"]
#[belongs_to(GameTitles)]
#[derive(Insertable)]
pub struct GameModes {
pub game_title_id: i32,
pub mode_name: String,
pub team_size: i32
}

#[table_name="users"]
#[derive(Insertable)]
pub struct Users {
Expand All @@ -38,6 +76,8 @@ pub mod insert {
#[belongs_to(Users)]
pub struct UserRatings {
pub user_id: i32,
pub rating: BigDecimal
pub rating: Option<BigDecimal>,
pub deviation: Option<BigDecimal>,
pub volatility: Option<BigDecimal>
}
}
4 changes: 2 additions & 2 deletions tests/test_commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ fn gen_test_user() -> User {
User {
id: UserId(210),
avatar: Some("abc".to_string()),
bot: true,
bot: false,
discriminator: 1432,
name: "test".to_string(),
name: "TestUser".to_string(),
}
}

Expand Down

0 comments on commit 723218c

Please sign in to comment.