Skip to content
This repository has been archived by the owner on Oct 14, 2019. It is now read-only.

Commit

Permalink
Merge pull request #380 from JPadley18/master
Browse files Browse the repository at this point in the history
Added Rank attribute to Website
  • Loading branch information
thecsw committed May 10, 2019
2 parents 5a0d59d + 9fb0e7c commit 54c671f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
3 changes: 2 additions & 1 deletion api/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ All investments invested in the specified post. The post string is sanitized wit
],
"firm": 0,
"firm_role": "",
"networth": 11000
"networth": 11000,
"rank": 1
}
```

Expand Down
22 changes: 22 additions & 0 deletions api/investor/investor.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type investor struct {
Firm int `json:"firm"`
Firm_role string `json:"firm_role"`
NetWorth int64 `json:"networth"`
Rank int64 `json:"rank"`
}

type investment struct {
Expand Down Expand Up @@ -95,6 +96,27 @@ func Investor() func(w http.ResponseWriter, r *http.Request) {
var active_coins int64
err = conn.QueryRow(query_net).Scan(&active_coins)
temp.NetWorth = temp.Balance + active_coins

// Calculate the investor's rank
query_rank := fmt.Sprintf(`
SELECT position FROM (
SELECT ROW_NUMBER() OVER (ORDER BY networth DESC) AS position, name, networth FROM (
SELECT
Investors.name, SUM(COALESCE(Investments.amount, 0)) + Investors.balance AS networth
FROM Investors
LEFT OUTER JOIN (SELECT * FROM Investments WHERE done = 0)
AS Investments ON Investments.name = Investors.name
GROUP BY Investors.id
ORDER BY networth DESC
)sub
)sub1 WHERE name = '%s';
`, temp.Name)

var rank int64
err = conn.QueryRow(query_rank).Scan(&rank)

temp.Rank = rank

result, _ := json.Marshal(temp)
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "%s", string(result))
Expand Down
7 changes: 5 additions & 2 deletions docs/js/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,14 +167,16 @@ let overview = (function(){
netWorth: undefined,
balance: undefined,
investmentsActive: undefined,
goneBroke: undefined
goneBroke: undefined,
rank: undefined
}
function init(data){
counters = {
netWorth: new CountUp("net-worth", 0, 0),
balance: new CountUp("balance", 0, 0),
completedInvestments: new CountUp("completed-investments", 0, 0),
goneBroke: new CountUp("gone-broke", 0, 0)
goneBroke: new CountUp("gone-broke", 0, 0),
rank: new CountUp("rank", 0, 0)
}
if(data)update(data);
}
Expand All @@ -183,6 +185,7 @@ let overview = (function(){
counters.balance.update(data.balance)
counters.completedInvestments.update(data.completed)
counters.goneBroke.update(data.broke)
counters.rank.update(data.rank)
}
return {
init:init,
Expand Down
1 change: 1 addition & 0 deletions docs/user.html
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ <h5 class="orange-text text-lighten-1"><a id="username" target="_blank" href=""
<p class="overview-text">
net worth: <span id="net-worth">---</span> <span></span><br>
balance: <span id="balance">---</span> <span></span><br>
rank: <span id="rank">---</span><br>
gone broke: <span id="gone-broke">--</span> <span id="fucking-plural-s">times</span><br>
completed investments: <span id="completed-investments">--</span><br>
firm: <a href="" id ="user-firm">--</a><br>
Expand Down

0 comments on commit 54c671f

Please sign in to comment.