it works!
This commit is contained in:
parent
c15182cfac
commit
4167163510
55
main.py
55
main.py
|
@ -1,16 +1,49 @@
|
||||||
import requests
|
import requests
|
||||||
|
import json
|
||||||
|
import time
|
||||||
|
from mastosnake import Mastosnake as mastosnake
|
||||||
|
from snake import Snake as snake
|
||||||
|
|
||||||
host = 'tilde.zone/'
|
host = 'tilde.zone/'
|
||||||
prefix = 'api/v1/statuses'
|
postend = 'api/v1/statuses'
|
||||||
|
getend = 'api/v1/polls/'
|
||||||
token = '5mvTdpbj6lF55WRjky7IgOj-BNpH5MkBqSvaOeHLowM'
|
token = '5mvTdpbj6lF55WRjky7IgOj-BNpH5MkBqSvaOeHLowM'
|
||||||
auth = {'Authorization': 'Bearer ' + token }
|
auth = {'Authorization': 'Bearer ' + token }
|
||||||
params = {
|
|
||||||
'status': 'testing from python',
|
|
||||||
'poll': {
|
|
||||||
'options': ['a', 'b', 'c', 'd'],
|
|
||||||
'expires_in': 86400,
|
|
||||||
'multiple': False
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
r = requests.post("https://" + host + prefix, json=params, headers=auth)
|
def main():
|
||||||
print(r.text)
|
game = snake()
|
||||||
|
mastogame = mastosnake(game)
|
||||||
|
while True:
|
||||||
|
data = mastogame.returnStatus()
|
||||||
|
r = requests.post("https://" + host + postend, json=data, headers=auth)
|
||||||
|
print("posting game status")
|
||||||
|
|
||||||
|
if mastogame.game.gamestate == "gameover":
|
||||||
|
print("gameover")
|
||||||
|
time.sleep(10)
|
||||||
|
game = snake()
|
||||||
|
mastogame = mastosnake(game)
|
||||||
|
continue
|
||||||
|
|
||||||
|
time.sleep(120)
|
||||||
|
|
||||||
|
poll_id = json.loads(r.text)['poll']['id']
|
||||||
|
r = requests.get("https://" + host + getend + poll_id)
|
||||||
|
mastogame.updateState(json.loads(r.text))
|
||||||
|
print("game state has been updated")
|
||||||
|
|
||||||
|
time.sleep(10)
|
||||||
|
print("posting new game status")
|
||||||
|
|
||||||
|
|
||||||
|
#req = requests.post("https://" + host + prefix, json=params, headers=auth)
|
||||||
|
#print(req.text)
|
||||||
|
#print("https://" + host + prefix2 + json.loads(req.text)['id'])
|
||||||
|
#r = requests.get("https://" + host + prefix2 + json.loads(req.text)['poll']['id'])
|
||||||
|
#print(req.text)
|
||||||
|
#r = requests.get("https://" + host + prefix2 + '42003')
|
||||||
|
#print(r.text)
|
||||||
|
#mastosnake.updateState(json.loads(r.text))
|
||||||
|
#print(mastosnake.game.render())
|
||||||
|
|
||||||
|
main()
|
||||||
|
|
|
@ -1,29 +1,41 @@
|
||||||
class Mastosnake:
|
class Mastosnake:
|
||||||
def __init__(self, tok, game):
|
def __init__(self, game):
|
||||||
self.tok = tok
|
|
||||||
self.game = game
|
self.game = game
|
||||||
def returnStatus(self):
|
def returnStatus(self):
|
||||||
statusstring = ""
|
statusstring = ""
|
||||||
for j in range(self.game.boarddimensions[0] - 1, -1 -1):
|
for j in range(self.game.boarddimensions[0] - 1, -1, -1):
|
||||||
for i in range(0, self.game.boarddimensions[1]):
|
for i in range(0, self.game.boarddimensions[1]):
|
||||||
if (i, j) in self.game.snake:
|
if (i, j) in self.game.snake:
|
||||||
if (i, j) == self.game.snake:
|
if (i, j) == self.game.snake[0]:
|
||||||
statusstring += "X"
|
statusstring += '⬛'
|
||||||
else:
|
else:
|
||||||
statusstring += "x"
|
statusstring += '◾'
|
||||||
elif (i, j) == self.apple:
|
elif (i, j) == self.game.apple:
|
||||||
statusstring += "a"
|
statusstring += '🍎'
|
||||||
else:
|
else:
|
||||||
statusstring += " "
|
statusstring += '⬜'
|
||||||
statusstring += "\n"
|
statusstring += '\n'
|
||||||
|
statusstring += '\n'
|
||||||
|
statusstring += self.game.gamestate
|
||||||
|
statusstring += ' | score: '
|
||||||
|
statusstring += str(len(self.game.snake))
|
||||||
dataskel = {
|
dataskel = {
|
||||||
'status': statusstring,
|
'status': statusstring,
|
||||||
'poll': {
|
'poll': {
|
||||||
'options': [],
|
'options': [],
|
||||||
'expires_in': 60,
|
'expires_in': 100000,
|
||||||
'multiple': False
|
'multiple': False
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for _, _, i in self.game.moveset():
|
for _, _, i in self.game.moveset():
|
||||||
dataskel['poll']['options'].append(i)
|
dataskel['poll']['options'].append(i)
|
||||||
return dataskel
|
return dataskel
|
||||||
|
def updateState(self, data):
|
||||||
|
tmp = []
|
||||||
|
for i in data['options']:
|
||||||
|
tmp.append((i['title'], i['votes_count']))
|
||||||
|
tmp2 = (0, -1)
|
||||||
|
for i in tmp:
|
||||||
|
if i[1] > tmp2[1]:
|
||||||
|
tmp2 = i
|
||||||
|
self.game.move(tmp2[0])
|
||||||
|
|
1
snake.py
1
snake.py
|
@ -50,6 +50,7 @@ class Snake:
|
||||||
self.snake[0][1] + i[1])
|
self.snake[0][1] + i[1])
|
||||||
if considered in self.snake:
|
if considered in self.snake:
|
||||||
self.gamestate = "gameover"
|
self.gamestate = "gameover"
|
||||||
|
print("gameover!")
|
||||||
if considered in self.nodir():
|
if considered in self.nodir():
|
||||||
self.snake.insert(0, considered)
|
self.snake.insert(0, considered)
|
||||||
if self.apple != considered:
|
if self.apple != considered:
|
||||||
|
|
Loading…
Reference in New Issue