From 342df3430218d5fd3be8ceca606330964b6f098b Mon Sep 17 00:00:00 2001 From: Jeffrey Armstrong Date: Mon, 29 Mar 2021 20:36:27 -0400 Subject: Added further player functions to db. Added contents for listing players via gemini. --- captain/db.f90 | 50 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) (limited to 'captain/db.f90') diff --git a/captain/db.f90 b/captain/db.f90 index 5fbd0b4..e191af7 100644 --- a/captain/db.f90 +++ b/captain/db.f90 @@ -1,7 +1,9 @@ module captain_db use sqlite implicit none - + + integer, parameter::PLAYER_NAME_LENGTH = 128 + character(1024)::database_file type(c_ptr)::db @@ -33,11 +35,20 @@ contains subroutine add_player_db(name, token) implicit none - character(*), intent(in)::name, token + character(*), intent(in)::name + character(*), intent(in), optional::token type(sqlite3_stmt)::stmt + + character(64)::my_token + + if(present(token)) then + my_token = token + else + my_token = "None" + end if if(stmt%prepare(db, "INSERT INTO players(name, token) VALUES(?, ?)") == SQLITE_OK) then - if(stmt%bind_text(1, name) == SQLITE_OK .and. stmt%bind_text(2, token) == SQLITE_OK) then + if(stmt%bind_text(1, name) == SQLITE_OK .and. stmt%bind_text(2, my_token) == SQLITE_OK) then call stmt%step_now() end if end if @@ -45,6 +56,37 @@ contains end subroutine add_player_db + subroutine update_player_token_db(name, token) + implicit none + + character(*), intent(in)::name + character(*), intent(in)::token + type(sqlite3_stmt)::stmt + + if(stmt%prepare(db, "UPDATE players SET token=? WHERE name=?") == SQLITE_OK) then + if(stmt%bind_text(2, name) == SQLITE_OK .and. stmt%bind_text(1, token) == SQLITE_OK) then + call stmt%step_now() + end if + end if + call stmt%finalize() + + end subroutine update_player_token_db + + subroutine remove_player_db(name) + implicit none + + character(*), intent(in)::name + type(sqlite3_stmt)::stmt + + if(stmt%prepare(db, "DELETE FROM players WHERE name=?") == SQLITE_OK) then + if(stmt%bind_text(1, name) == SQLITE_OK) then + call stmt%step_now() + end if + end if + call stmt%finalize() + + end subroutine remove_player_db + function get_player_count() implicit none @@ -65,7 +107,7 @@ contains implicit none type(sqlite3_stmt)::stmt - character(len=256), dimension(:), pointer::res + character(len=PLAYER_NAME_LENGTH), dimension(:), pointer::res integer::i,n n = get_player_count() -- cgit v1.2.3