Highscores in MongoDB - node.js

I've made a small HTML5 game with a highscores feature. At the end of every game, my node server stores name, score and userId (a number unique for each player) in a Mongo database, and then orders it by score and sends back the top 3.
So far it works, but I want to find a way so that each player can appear on the highscore table only once.
So if the scores would be:
Player A, 100
Player A, 99
Player A, 98
Player B, 97
Player B, 91
Player C, 96
then I would like to display:
Player A, 100
Player B, 97
Player C , 96
Is there a way to do this in Mongo? It's the first time I'm using it, and don't even now what this function would be called...

Related

I have three values and budget and i need to buy a product at the best price in python

The condition is as follows:
I have a budget of 500 EUR.
There are three bikes at different prices.
I need to buy a bike at the best price
If I set the budget to 0 then I get an else print, but if I change the budget to 1 then I get a successful if print even though my budget is less than the price of the bike.
Maybe you can help and explain what I'm doing wrong?
CODE:
budget = 500
bikeUnivega = 375
bikeKalkhoff = 449
bikeFocus = 462
bikeCatalogList = (bikeUnivega, bikeKalkhoff, bikeFocus)
lowestBikePrice = bikeCatalogList.index(min(bikeCatalogList))
if budget > lowestBikePrice:
print(f'I Will buy the bike for the lowest price of {bikeCatalogList[lowestBikePrice]} EUR.')
else:
print('I dont have enough money for the bike. I will save money until next year.')
I tried to find the information on google, but I was unsuccessful.
There are a couple things I've noticed in your code:
For starters, your list is using () like a tuple, instead of [].
Next is that lowestBikePrice is not returning the value stored in the variable, but its index.
So, if you were to get the min, it returns a 0 bc the lowest value variable is in position 0.
If you were to use a max instead, it would return a 2.
lastly your f string is hashing your list for an item lowestBikePrice which doesn't exist in the list.

Vehicle Routing Problem where distance constraint will depend on first visited node

I am trying to solve the vehicle routing problem for cabs to pick-up employees and drop them off at the office using OR-Tools.
One of the requirement is that no employee should spend more them 'x' km off the route, i.e. if x = 10 km and employee A lives 10 km away from the office, then the distance traveled by cab after picking up employee A should not be more than 20 km(10km of x + 10km of distance from A to office).
So, if for the best route, the cab needs to pick employee A then B, and then C, but the total distance is turning out to be 25km, then it is not allowed, if it is up to 20 it is fine.
So the max distance allowed for any cab to travel will depend on who the first pickup is. Is there any way to implement such a scenario using OR-tools??
In python, it should something like this:
x_pickup = manager.NodeToIndex(x_index)
x_drop = manager.NodeToIndex(y_index)
routing.solver().Add(
distance_dimension.CumulVar(x_pickup) + 10 <=
distance_dimension.CumulVar(y_drop))

How do I make a 'statistic over all played ' yahtzy games in Excel?

Scenario: Every week, my siblings and I play a few games of Yahtzy (and some other stuff) - it's a nice ritual we have. Over time we've wondered about the statistics of all our games, like "how many times have we each rolled a Yahtzy?" and "What do we get on average?". The results we would like to see is f.ex
"Adam, your average score is 243 and you get a yahtzy on 57% of all games".
Optional Game Description: Yahtzy is a game where you roll five dice for categories like "2 Pair" and "Yahtzy", where getting a Yahtzy is rolling five of a kind, and is rewarded with 50 points. All sums are then added up at the end, and the one with highest total is the winner.
Main problem: Iterating over all games.
Idea:
Sheet GAME is the standard yahtzy entry for the game, where you record the scores. (also, see picture)
Mike Adam
1's: 1 3
2's: 8 6
3's: 9 9
2 Pair: 22 18
Yahtzy: 50 50
Sheet ARCHIVE is a a record of all games played.
DATE MIKE_WIN ADAM_WIN MIKE_TOTAL ADAM_TOTAL MIKE_YAHTZY ADAM_YAHZY
07-07-20 TRUE FALSE 303 238 TRUE FALSE
12-07-20 FALSE TRUE 220 268 FALSE FALSE
Sheet STATS is a summary of relevant stats:
PLAYER MIKE ADAM
WINS 24 21
AVG SCORE 235 262
YAHTZY % 34% 54%
Question 1 ?: Is there a formula / function / macro that adds a row to ARCHIVE when each game is completed, and then clears GAME for the next game? (I can handle STATS myself, if ARCHIVE is working)
Question 2 ?: I add a copy of sheet GAME for each game played. Is there a formula / function / script that can loop through all sheets and either
add them to ARCHIVE as described above, or
lets me crunch the numbers directly in STATS ? Like:
MIKE AVG SCORE: =AVERAGE(EVERY_SHEET_CALLED_GAME!B7)
If you can think of a solution, please help me out. It would be EVEN BETTER if there was no VBA involved so I could run this on my Android smartphone.
Thanks for your time!
// CHR
PS: Assume the players are the same every time.
So I used countifs() based on simple data:
There are several functions available such as averageifs(), sumifs(), maxifs(), minifs()

How to compare multiple values from different columns in excel?

I have some difficulties with my IF formula.
I have the following columns that represent some subjects and the points that I managed to get for those subjects (F:H). In column I , I would like to compare my points with another table(M:N) that contains the minimum amount of points needed to pass the course .If my score is equal or greater then what the corresponding score is in column N, I would like to show PASS or FAIL in column I.
I Used the following formula but when comparing with M:N it keeps going down (outside the table) giving me in the end all the exams as passed.
=IF(AND(F2:$F$6=M2:$M$6)&H2:$H$6>=N2:$N6;"PASS";"FAIL")
F H I M N
(Subjects) (Points) (Subjects) (Points to Pass)
Mathematics 85 Mathematics 85
English 88 English 70
French 78 French 60
Chinese 56 Chinese 60
Physics 70 Sports 80
Sports 95 Physics 80
You could use:
=IF(H2>=VLOOKUP(F2,$M$2:$N$7,2,FALSE),"PASS","FAIL")
Results
Example formula should look like this:
=IF(H1>=INDEX($N$1:$N$6,MATCH(F1,$M$1:$M$6,0)),"PASS","FAIL")
Let your table layout housed in F1:N7 include header
In I2, formula copied down :
=IF(H2>=VLOOKUP(F2,M:N,2,0),"PASS","FAIL")

Linear Optimization with Categorical Constraints

I am trying to optimize a lineup of players according to the level of competition to play based on maximizing a single statistic(Points in my example below). For a given lineup, 2 players are matched up against a competition level. Here is an example of my DataFrame(This is just an example, my actual DataFrame has a lot more players than this):
Comp Level Points Minutes Played
Player A Elite 10.1 22
Player A Middle -5 22
Player A Low 7.2 22
Player B Elite 0.8 20
Player B Middle 5.6 20
Player B Low 2.2 20
Player C Elite -7.2 18
Player C Middle 3.3 21
Player C Low 6.6 23
Player D Elite -7.2 18
Player D Middle 3.3 21
Player D Low 6.6 23
Player E Elite -7.2 18
Player E Middle 3.3 21
Player E Low 6.6 23
Player F Elite -7.2 18
Player F Middle 3.3 21
Player F Low 6.6 23
The optimized lineup will maximize the total points of the entire lineup (A sum of points from each player). The constraints are:
1. You can't use the same player for more than one competition level (For example, if player A is paired against the elite level, they can't be played against the middle or low levels).
2. Each Lineup Pair has to have a minutes played value that is within 10% of each other (For Example, Player A Could be Paired with Player B at the Elite Level but not Player C).
I'm hoping my output will look something like this:
Line up:
Elite: Player A and B
Middle: Player C and Player D
Low: Player E and Player F
Team Total Points: X
I've started by creating my DataFrame and figured on using PuLP for this optimization. The part where I am struggling with is adding my objective function, and appropriate constraints to the problem.
This is my code thus far:
prob = LpProblem('LineUp Optimization', LpMaximize)
players = list(data['Name'])
# print('The Players under consideration are \n' + '-'*100)
# for x in players:
# print(x, end=',\n ')
costs = dict(zip(players, data['Points']))
comp_levels = dict(zip(players, data['Competition Level']))
Minutes = dict(zip(players, data['Minutes Played']))
ozs = dict(zip(players, data['OZS%']))
player_var = LpVariable.dicts('Players', players, 0, cat='Integer')
Any help/guidance on what my constraints would look like within this PuLP optimization would be greatly appreciated!
If I understood you're problem correctly you need to choose a pair of players for each of the Elite, Middle and lower competition levels.
There are a number of ways you could formulate this problem. The most obvious to me would be to have sets of binary variables which indicate wheter (1) or not (0) each player is chosen for each level of competition:
player_var = LpVariable.dicts('player_var', (players, comp_levels), cat='Binary')
Your objective is to maximise total points of the included players:
prob += lpSum([player_var[p, l]*points[p, l] for p in players for l in comp_levels])
You then need to put in constraints that two players should be chosen for each level:
for l in comp_levels:
prob += lpSum([player_var[p, l] for p in players]) == 2
And that a player can play in at most one competition level:
for p in players:
prob += lpSum([player_var[p, l] for l in comp_levels]) <= 1
And that your 10% rule on minutes played at the chosen level for the pairing is satisfied. This last one is kind of interesting. I'm sure there is a more efficient way of doing this but what I've done below should work... I think.
for l in comp_levels:
for p in players:
prob += minutes[p, l]*player_var[p,l] <= 1.1*lpSum([minutes[p,l]*player_ver[other_p,l] for other_p in players if other_p != p])
This creates a constraints for each competition level, and for each player, however if the player is not chosen to play at the level (player_var[p,l] == 0) then the constraints should have no effect - term on left-hand-side will be 0.
If the player is chosen (player_var[p,l] == 1) then the constaint ensures that the chosen player has no more than 10% more minutes than their oponent - note the right hand sand takes a sum over all other players but only 1 of these will be non-zero.
No check is carried out to ensure the chosen player has at least 90% of the minutes of their oponent - but that case is covered when this constraint is applied to their oponent.
I'm sure someone out there can suggest a much more efficient/elegant way of doing this...

Resources