it works!

This commit is contained in:
randomuser 2021-05-24 22:51:46 -05:00
parent c15182cfac
commit 4167163510
3 changed files with 68 additions and 22 deletions

55
main.py
View File

@ -1,16 +1,49 @@
import requests
import json
import time
from mastosnake import Mastosnake as mastosnake
from snake import Snake as snake
host = 'tilde.zone/'
prefix = 'api/v1/statuses'
postend = 'api/v1/statuses'
getend = 'api/v1/polls/'
token = '5mvTdpbj6lF55WRjky7IgOj-BNpH5MkBqSvaOeHLowM'
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)
print(r.text)
def main():
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()

View File

@ -1,29 +1,41 @@
class Mastosnake:
def __init__(self, tok, game):
self.tok = tok
def __init__(self, game):
self.game = game
def returnStatus(self):
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]):
if (i, j) in self.game.snake:
if (i, j) == self.game.snake:
statusstring += "X"
if (i, j) == self.game.snake[0]:
statusstring += ''
else:
statusstring += "x"
elif (i, j) == self.apple:
statusstring += "a"
statusstring += ''
elif (i, j) == self.game.apple:
statusstring += '🍎'
else:
statusstring += " "
statusstring += "\n"
statusstring += ''
statusstring += '\n'
statusstring += '\n'
statusstring += self.game.gamestate
statusstring += ' | score: '
statusstring += str(len(self.game.snake))
dataskel = {
'status': statusstring,
'poll': {
'options': [],
'expires_in': 60,
'expires_in': 100000,
'multiple': False
}
}
for _, _, i in self.game.moveset():
dataskel['poll']['options'].append(i)
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])

View File

@ -50,6 +50,7 @@ class Snake:
self.snake[0][1] + i[1])
if considered in self.snake:
self.gamestate = "gameover"
print("gameover!")
if considered in self.nodir():
self.snake.insert(0, considered)
if self.apple != considered: