From 044801328d8f9a8e30b5e7cb0d6612e18ada67d9 Mon Sep 17 00:00:00 2001 From: Nico Schottelius Date: Mon, 26 Aug 2019 22:16:23 +0200 Subject: [PATCH] add /points/ --- .gitignore | 1 + README-20190826.md | 21 +++++++++++++++ archive/server.py | 58 +++++++++++++++++++++++++++++++++++++++ challenge.py | 2 +- server.py | 66 ++++++++------------------------------------- ungleich_game_db.py | 1 + 6 files changed, 93 insertions(+), 56 deletions(-) create mode 100644 README-20190826.md diff --git a/.gitignore b/.gitignore index 98e6ef6..4679ce0 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.db +default.etcd/ diff --git a/README-20190826.md b/README-20190826.md new file mode 100644 index 0000000..4bfb3b7 --- /dev/null +++ b/README-20190826.md @@ -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 diff --git a/archive/server.py b/archive/server.py index 7fcfaff..9fc63fc 100644 --- a/archive/server.py +++ b/archive/server.py @@ -58,5 +58,63 @@ api.add_resource(Employees_Name, '/employees/') # 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') diff --git a/challenge.py b/challenge.py index 3e8c48f..72ec29b 100644 --- a/challenge.py +++ b/challenge.py @@ -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): diff --git a/server.py b/server.py index 9683723..648e267 100644 --- a/server.py +++ b/server.py @@ -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/', '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__': diff --git a/ungleich_game_db.py b/ungleich_game_db.py index 4c64e60..01e77ae 100644 --- a/ungleich_game_db.py +++ b/ungleich_game_db.py @@ -1,4 +1,5 @@ import etcd +from flask import abort class etcdWrapper(object): """ Generalises some etcd actions """