add /points/<username>

This commit is contained in:
Nico Schottelius 2019-08-26 22:16:23 +02:00
parent c165b433c2
commit 044801328d
6 changed files with 93 additions and 56 deletions

1
.gitignore vendored
View File

@ -1 +1,2 @@
*.db
default.etcd/

21
README-20190826.md Normal file
View File

@ -0,0 +1,21 @@
## How to setup
### Prerequisites
* Start etcd somewhere
* Start ungleich-otp somewhere
### Install pythor requirements
```
pipenv install
```
### Run the game server
```
ETCD_HOST=... UNGLEICH_OTP_HOST=... pipenv run python server.py
```
## How to play

View File

@ -58,5 +58,63 @@ api.add_resource(Employees_Name, '/employees/<employee_id>') # Route_3
# def get_ip_address():
# args = self.require_args("network", "user")
# # Needs to be fixed with ungleich-otp
# username=args['user']
# if request.method == 'GET':
# return Response("""
# This is an easy level - just register any /64 network
# that you fully control. After submission the game server will generate
# a random IPv6 address in this network.
# """)
# client = etcd.Client(port=2379)
# try:
# data = client.read("/ungleichgame/v1/{}/network".format(username))
# # FIXME: differentiate keynotfound and other errors
# except Exception as e:
# return Response(status=400, response="Cannot read your network, try registering first (error: {})".format(e))
# return Response("data={}".format(data.value))
# address = get_random_ip(data.value)
# # FIXME: catch errors
# client.write("/ungleichgame/v1/{}/address".format(username), address)
# return Response("Your IPv6 address for this game is {}. Make it pingable and post to /level/1/result".format(address))
# @app.route("/level/2", methods=['GET', 'POST']) # post for username
# def pingme():
# parser = reqparse.RequestParser()
# parser.add_argument('user', required=True)
# args = parser.parse_args()
# # Needs to be fixed with ungleich-otp
# username=args['user']
# if request.method == 'GET':
# return Response("""
# Proof that you can really control the network that you submitted:
# - Setup the IPv6 address to be ping6 able globally
# - POST to this address when it is configured
# """)
# if request.method == 'POST':
# try:
# data = client.read("/ungleichgame/v1/{}/address".format(username), address)
# except Exception as e:
# return Response(status=400,
# response="""
# You need to register a network before trying to be reachable.
# Please go back to Level 1 for registering your network.
# """)
# return Response("something good")
if __name__ == '__main__':
app.run(port='5002')

View File

@ -11,7 +11,7 @@ class Challenge(object):
points = 0
provides = []
requires = []
description = None
description = "This description should be overriden"
dependencies_provided_by = {}
def __init__(self, wrapper):

View File

@ -7,6 +7,7 @@ import datetime
import inspect
from ungleich_game_db import *
from challenge import Challenge
from flask import Flask, abort, request, Response
from flask_restful import reqparse
@ -102,6 +103,7 @@ class Game(object):
self.app.add_url_rule('/', 'index', self.index)
self.app.add_url_rule('/points', 'points', self.points)
self.app.add_url_rule('/points/<username>', 'points', self.points)
self.app.add_url_rule('/register', 'register', self.register, methods=['POST'])
# etcd paths are below here
@ -193,12 +195,20 @@ For more information visit
https://code.ungleich.ch/nico/ungleich-game
""".format(points)
def points(self):
def points(self, username=None):
point_list = self.get_points()
res = []
if not point_list:
return Response("No winners yet!")
if username:
userpoints = 0
if username in point_list:
userpoints = point_list[username]
return "{} has {} points".format(username, userpoints)
for k, v in point_list.items():
res.append("{} has {} points".format(k, v))
@ -221,60 +231,6 @@ Point list (aka high score)
return "Registered at: {}\n".format(value)
# def get_ip_address():
# args = self.require_args("network", "user")
# # Needs to be fixed with ungleich-otp
# username=args['user']
# if request.method == 'GET':
# return Response("""
# This is an easy level - just register any /64 network
# that you fully control. After submission the game server will generate
# a random IPv6 address in this network.
# """)
# client = etcd.Client(port=2379)
# try:
# data = client.read("/ungleichgame/v1/{}/network".format(username))
# # FIXME: differentiate keynotfound and other errors
# except Exception as e:
# return Response(status=400, response="Cannot read your network, try registering first (error: {})".format(e))
# return Response("data={}".format(data.value))
# address = get_random_ip(data.value)
# # FIXME: catch errors
# client.write("/ungleichgame/v1/{}/address".format(username), address)
# return Response("Your IPv6 address for this game is {}. Make it pingable and post to /level/1/result".format(address))
# @app.route("/level/2", methods=['GET', 'POST']) # post for username
# def pingme():
# parser = reqparse.RequestParser()
# parser.add_argument('user', required=True)
# args = parser.parse_args()
# # Needs to be fixed with ungleich-otp
# username=args['user']
# if request.method == 'GET':
# return Response("""
# Proof that you can really control the network that you submitted:
# - Setup the IPv6 address to be ping6 able globally
# - POST to this address when it is configured
# """)
# if request.method == 'POST':
# try:
# data = client.read("/ungleichgame/v1/{}/address".format(username), address)
# except Exception as e:
# return Response(status=400,
# response="""
# You need to register a network before trying to be reachable.
# Please go back to Level 1 for registering your network.
# """)
# return Response("something good")
if __name__ == '__main__':

View File

@ -1,4 +1,5 @@
import etcd
from flask import abort
class etcdWrapper(object):
""" Generalises some etcd actions """