repl.it
@pyelias/

farming game database

Python

No description

fork
loading
Files
  • main.py
  • poetry.lock
  • pyproject.toml
  • requirements.txt
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import pymongo, flask, os

client = pymongo.MongoClient(f"mongodb+srv://{os.getenv('USERNAME')}:{os.getenv('PASSWORD')}@cluster0-hwxl5.mongodb.net/test?retryWrites=true&w=majority")
scores = client.scores

app = flask.Flask(__name__)

@app.route('/lead')
def leaders():
  newdata = []
  data = list(scores.leaders.find({}))
  for d in data:
    del d['_id']
    newdata.append(d)
  return str(newdata)

@app.route('/avg')
def average():
  data = scores.avg.find_one({'key': 'AVG'})
  return str(data['score']/data['players'])

@app.route('/add', methods=['GET'])
def add(): 
  nscores = []
  def getscore(p):
    return p[0]
  for player in list(scores.leaders.find({})):
    nscores.append((player['s'], player['n']))
  nscores.sort(key=getscore)
  nscores.reverse()
  u = flask.request.args.get('u')
  s = float(flask.request.args.get('s'))
  l = int(flask.request.args.get('l'))
  if l==1:
    scores.leaders.insert_one({'n': u, 's': s})
    scores.leaders.delete_one({'s': nscores[4][0]})
  data = scores.avg.find_one({'key': 'AVG'})
  scores.avg.delete_one({'key': 'AVG'})
  scores.avg.insert_one({'key': 'AVG', 'score': data['score']+s, 'players':data['players']+1})
  return 'SUCCESS!'

app.run(host='0.0.0.0', port=6506)