This repository has been archived by the owner on Oct 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
201 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package controller | ||
|
||
import ( | ||
"log" | ||
|
||
"github.com/robyzzz/csl-backend/config" | ||
"github.com/robyzzz/csl-backend/model" | ||
"github.com/robyzzz/csl-backend/utils" | ||
"github.com/solovev/steam_go" | ||
) | ||
|
||
func CreateSteamUser(user *steam_go.PlayerSummaries) error { | ||
return model.CreateSteamUser(utils.PlayerSummariesToSteamUser(user)) | ||
} | ||
|
||
func UpdateSteamUser(steamID string) error { | ||
updatedUser, err := steam_go.GetPlayerSummaries(steamID, config.STEAM_API_KEY) | ||
if err != nil { | ||
log.Printf("Failed to get PlayerSummaries @UpdateSteamUser: %s", err.Error()) | ||
return err | ||
} | ||
|
||
return model.UpdateSteamUser(utils.PlayerSummariesToSteamUser(updatedUser)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package model | ||
|
||
import ( | ||
"log" | ||
) | ||
|
||
type SteamUser struct { | ||
ID uint64 `json:"id"` | ||
SteamID string `json:"steamid"` | ||
PersonaName string `json:"personaname"` | ||
LastLogOff int `json:"lastlogoff"` | ||
ProfileUrl string `json:"profileurl"` | ||
Avatar string `json:"avatar"` | ||
AvatarMedium string `json:"avatarmedium"` | ||
AvatarFull string `json:"avatarfull"` | ||
RealName string `json:"realname"` | ||
PrimaryClanID string `json:"primaryclanid"` | ||
TimeCreated int `json:"timecreated"` | ||
LocCountryCode string `json:"loccountrycode"` | ||
GameID int `json:"gameid"` | ||
} | ||
|
||
func CreateSteamUser(user SteamUser) error { | ||
userExists, err := DoesSteamUserExist(user.SteamID) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if userExists { | ||
return nil | ||
} | ||
|
||
query := `INSERT INTO steam_user(steamid, personaname, lastlogoff, profileurl, avatar, avatarmedium, avatarfull, realname, primaryclanid, timecreated, loccountrycode, gameid) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12);` | ||
|
||
_, err = db.Exec(query, user.SteamID, user.PersonaName, user.LastLogOff, user.ProfileUrl, user.Avatar, user.AvatarMedium, user.AvatarFull, user.RealName, user.PrimaryClanID, user.TimeCreated, user.LocCountryCode, user.GameID) | ||
|
||
if err != nil { | ||
log.Printf("Error creating steam user @CreateSteamUser: %s\n", err.Error()) | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func UpdateSteamUser(user SteamUser) error { | ||
query := `UPDATE steam_user SET personaname = $1::text, lastlogoff = $2, profileurl = $3::text, avatar = $4::text, avatarmedium = $5::text, avatarfull = $6::text, realname = $7::text, primaryclanid = $8::text, timecreated = $9, loccountrycode = $10::text, gameid = $11 WHERE steamid=$12::text ;` | ||
|
||
_, err = db.Exec(query, user.PersonaName, user.LastLogOff, user.ProfileUrl, user.Avatar, user.AvatarMedium, user.AvatarFull, user.RealName, user.PrimaryClanID, user.TimeCreated, user.LocCountryCode, user.GameID, user.SteamID) | ||
|
||
if err != nil { | ||
log.Printf("Error updating steam user @UpdateSteamUser: %s\n", err.Error()) | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func DoesSteamUserExist(steamid string) (bool, error) { | ||
var count int | ||
err := db.QueryRow("SELECT COUNT(*) FROM steam_user WHERE steamid = '" + steamid + "';").Scan(&count) | ||
if err != nil { | ||
log.Printf("Error querying steam user @DoesSteamUserExist: %s\n", err.Error()) | ||
return false, err | ||
} | ||
|
||
if count == 0 { | ||
return false, nil | ||
} else { | ||
return true, nil | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package utils | ||
|
||
import ( | ||
"github.com/robyzzz/csl-backend/model" | ||
"github.com/solovev/steam_go" | ||
) | ||
|
||
// Converts PlayerSummaries to SteamUser | ||
func PlayerSummariesToSteamUser(user *steam_go.PlayerSummaries) model.SteamUser { | ||
return model.SteamUser{ | ||
ID: 0, | ||
SteamID: user.SteamId, | ||
PersonaName: user.PersonaName, | ||
LastLogOff: user.LastLogOff, | ||
ProfileUrl: user.ProfileUrl, | ||
Avatar: user.Avatar, | ||
AvatarMedium: user.AvatarMedium, | ||
AvatarFull: user.AvatarFull, | ||
RealName: user.RealName, | ||
PrimaryClanID: user.PrimaryClanId, | ||
TimeCreated: user.TimeCreated, | ||
LocCountryCode: user.LocCountryCode, | ||
GameID: user.GameId, | ||
} | ||
} | ||
|
||
type PlayerSumm steam_go.PlayerSummaries | ||
|
||
func (user *PlayerSumm) ToSteamUser() model.SteamUser { | ||
return model.SteamUser{ | ||
ID: 0, | ||
SteamID: user.SteamId, | ||
PersonaName: user.PersonaName, | ||
LastLogOff: user.LastLogOff, | ||
ProfileUrl: user.ProfileUrl, | ||
Avatar: user.Avatar, | ||
AvatarMedium: user.AvatarMedium, | ||
AvatarFull: user.AvatarFull, | ||
RealName: user.RealName, | ||
PrimaryClanID: user.PrimaryClanId, | ||
TimeCreated: user.TimeCreated, | ||
LocCountryCode: user.LocCountryCode, | ||
GameID: user.GameId, | ||
} | ||
} |