@ -18,18 +18,20 @@
package cache
package cache
import (
import (
"fmt"
"log"
"time"
"time"
)
)
func AddAccountSession ( uuid [ ] byte , token [ ] byte ) bool {
func AddAccountSession ( uuid [ ] byte , token [ ] byte ) bool {
rdb. Do ( "SELECT" , sessionDB )
key := fmt . Sprintf ( "session:%s" , token )
err := rdb . Set ( string ( token ) , string ( uuid ) , 7 * 24 * time . Hour ) . Err ( )
err := rdb . Set ( key , string ( uuid ) , 24 * time . Hour ) . Err ( )
return err == nil
return err == nil
}
}
func FetchUsernameBySessionToken ( token [ ] byte ) ( string , bool ) {
func FetchUsernameBySessionToken ( token [ ] byte ) ( string , bool ) {
rdb. Do ( "SELECT" , sessionDB )
key := fmt . Sprintf ( "session:%s" , token )
username , err := rdb . Get ( string ( token ) ) . Result ( )
username , err := rdb . Get ( key ) . Result ( )
if err != nil {
if err != nil {
return "" , false
return "" , false
}
}
@ -37,40 +39,31 @@ func FetchUsernameBySessionToken(token []byte) (string, bool) {
return username , true
return username , true
}
}
func updateActivePlayers ( uuid [ ] byte ) bool {
rdb . Do ( "SELECT" , activePlayersDB )
err := rdb . Set ( string ( uuid ) , 1 , 0 ) . Err ( )
if err != nil {
return false
}
err = rdb . Expire ( string ( uuid ) , 5 * time . Minute ) . Err ( )
return err == nil
}
func UpdateAccountLastActivity ( uuid [ ] byte ) bool {
func UpdateAccountLastActivity ( uuid [ ] byte ) bool {
rdb. Do ( "SELECT" , accountsDB )
key := fmt . Sprintf ( "account:%s" , uuid )
err := rdb . HSet ( string ( uuid ) , "lastActivity" , time . Now ( ) . Format ( "2006-01-02 15:04:05" ) ) . Err ( )
err := rdb . HSet ( key , "lastActivity" , time . Now ( ) . Format ( "2006-01-02 15:04:05" ) ) . Err ( )
if err != nil {
if err != nil {
return false
return false
}
}
updateActivePlayers ( uuid )
err = rdb . Expire ( key , 5 * time . Minute ) . Err ( )
return err == nil
return err == nil
}
}
// FIXME
func UpdateAccountStats ( uuid [ ] byte , battles , classicSessionsPlayed int ) bool {
func UpdateAccountStats ( uuid [ ] byte , battles , classicSessionsPlayed int ) bool {
rdb. Do ( "SELECT" , accountsDB )
key := fmt . Sprintf ( "account:%s" , uuid )
err := rdb . HIncrBy ( string ( uuid ) , "battles" , int64 ( battles ) ) . Err ( )
err := rdb . HIncrBy ( key , "battles" , int64 ( battles ) ) . Err ( )
if err != nil {
if err != nil {
return false
return false
}
}
err = rdb . HIncrBy ( string ( uuid ) , "classicSessionsPlayed" , int64 ( classicSessionsPlayed ) ) . Err ( )
err = rdb . HIncrBy ( key , "classicSessionsPlayed" , int64 ( classicSessionsPlayed ) ) . Err ( )
return err == nil
return err == nil
}
}
func FetchTrainerIds ( uuid [ ] byte ) ( int , int , bool ) {
func FetchTrainerIds ( uuid [ ] byte ) ( int , int , bool ) {
rdb . Do ( "SELECT" , accountsDB )
log . Println ( "FetchTrainerIds" , uuid )
vals , err := rdb . HMGet ( string ( uuid ) , "trainerId" , "secretId" ) . Result ( )
key := fmt . Sprintf ( "account:%s" , uuid )
vals , err := rdb . HMGet ( key , "trainerId" , "secretId" ) . Result ( )
if err == nil && len ( vals ) == 2 && vals [ 0 ] != nil && vals [ 1 ] != nil {
if err == nil && len ( vals ) == 2 && vals [ 0 ] != nil && vals [ 1 ] != nil {
trainerId , ok1 := vals [ 0 ] . ( int )
trainerId , ok1 := vals [ 0 ] . ( int )
secretId , ok2 := vals [ 1 ] . ( int )
secretId , ok2 := vals [ 1 ] . ( int )
@ -83,42 +76,53 @@ func FetchTrainerIds(uuid []byte) (int, int, bool) {
}
}
func UpdateTrainerIds ( trainerId , secretId int , uuid [ ] byte ) bool {
func UpdateTrainerIds ( trainerId , secretId int , uuid [ ] byte ) bool {
rdb. Do ( "SELECT" , accountsDB )
key := fmt . Sprintf ( "account:%s" , uuid )
err := rdb . HMSet ( string ( uuid ) , map [ string ] interface { } {
err := rdb . HMSet ( key , map [ string ] interface { } {
"trainerId" : trainerId ,
"trainerId" : trainerId ,
"secretId" : secretId ,
"secretId" : secretId ,
} ) . Err ( )
} ) . Err ( )
if err != nil {
return false
}
err = rdb . Expire ( key , 5 * time . Minute ) . Err ( )
return err == nil
return err == nil
}
}
func IsActiveSession ( uuid [ ] byte , sessionId string ) ( bool , bool ) {
func IsActiveSession ( uuid [ ] byte , sessionId string ) ( bool , bool ) {
rdb . Do ( "SELECT" , activeClientSessionsDB )
key := fmt . Sprintf ( "active_sessions:%s" , uuid )
id , err := rdb . Get ( string ( uuid ) ) . Result ( )
id , err := rdb . Get ( key ) . Result ( )
return id == sessionId , err == nil
}
func UpdateActiveSession ( uuid [ ] byte , sessionId string ) bool {
key := fmt . Sprintf ( "active_sessions:%s" , uuid )
err := rdb . Set ( key , sessionId , 0 ) . Err ( )
if err != nil {
return false
}
err = rdb . Expire ( key , 5 * time . Minute ) . Err ( )
if err != nil {
if err != nil {
return false , false
return false
}
}
return id == sessionId , true
err = rdb . SAdd ( "active_players" , uuid ) . Err ( )
if err != nil {
return false
}
}
err = rdb . Expire ( "active_players" , 5 * time . Minute ) . Err ( )
func UpdateActiveSession ( uuid [ ] byte , sessionId string ) bool {
rdb . Do ( "SELECT" , activeClientSessionsDB )
err := rdb . Set ( string ( uuid ) , sessionId , 0 ) . Err ( )
return err == nil
return err == nil
}
}
func FetchUUIDFromToken ( token [ ] byte ) ( [ ] byte , bool ) {
func FetchUUIDFromToken ( token [ ] byte ) ( [ ] byte , bool ) {
rdb . Do ( "SELECT" , sessionDB )
key := fmt . Sprintf ( "session:%s" , token )
uuid , err := rdb . Get ( string ( token ) ) . Bytes ( )
uuid , err := rdb . Get ( key ) . Bytes ( )
if err != nil {
return uuid , err == nil
return nil , false
}
return uuid , true
}
}
func RemoveSessionFromToken ( token [ ] byte ) bool {
func RemoveSessionFromToken ( token [ ] byte ) bool {
rdb. Do ( "SELECT" , sessionDB )
key := fmt . Sprintf ( "session:%s" , token )
err := rdb . Del ( string ( token ) ) . Err ( )
err := rdb . Del ( key ) . Err ( )
return err == nil
return err == nil
}
}