Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/spa'
Browse files Browse the repository at this point in the history
  • Loading branch information
yeaktas committed Apr 15, 2024
2 parents b200ed7 + a6b7e3b commit f9e6354
Show file tree
Hide file tree
Showing 14 changed files with 1,855 additions and 1,815 deletions.
2 changes: 2 additions & 0 deletions indianpong/indianpong/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = environ.get("DEBUG", default=True)

BASE_URL = environ.get("BASE_URL", default="http://localhost:8000")

ALLOWED_HOSTS = ['indianpong.com','indianpong.onrender.com', 'http://127.0.0.1:8000', 'localhost', '127.0.0.1']#environ.get("ALLOWED_HOSTS", default="").split(" ")

CSRF_TRUSTED_ORIGINS = [
Expand Down
13 changes: 4 additions & 9 deletions indianpong/pong/consumer_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,20 +51,17 @@ async def receive(self, text_data):
# if accept it create game object and send link in form: /remote-game/invite/game_id to both
# send message to room group that user accepted the game make it in client side
accepted = data["accepted"]
group_name = f"{accepted}_{accepter}"
accepted = await UserProfile.objects.aget(username=accepted)
accepter = await UserProfile.objects.aget(username=self.user.username)
group_name = f"{accepted}-{accepter}"
# create game object
game = await Game.objects.acreate(group_name=group_name, player1=accepted, player2=accepter)
message = f"/remote-game/invite/{game.id}" #? Maybe do these in client side
m = await Message.objects.acreate(content=message, user=accepted, room_id=self.room_name) #?
await self.channel_layer.group_send(
self.room_group_name,
{
"type": "accept.game",
"message": message,
"game_id": game.id,
"user": accepted.username,
"created_date": m.get_short_date(), #? blocking?
}
)

Expand Down Expand Up @@ -131,15 +128,13 @@ async def invite_game(self, event):
}))

async def accept_game(self, event):
message = event["message"]
game_id = event["game_id"]
user = event["user"]
created_date = event["created_date"]
# Send message to WebSocket
await self.send(text_data=json.dumps({
"type": "accept.game",
"message": message,
"game_id": game_id,
"user": user,
"created_date": created_date,
}))

async def decline_game(self, event):
Expand Down
1,309 changes: 655 additions & 654 deletions indianpong/pong/consumer_pong.py

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions indianpong/pong/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ def getScore(self, username):
return self.player2.score

def getDuration(self):
if self.start_time == 0:
return 0
if self.end_time == 0:
self.end_time = time.time()
return self.end_time - self.start_time
Expand Down
3 changes: 2 additions & 1 deletion indianpong/pong/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from .utils import create_random_svg, get_upload_to
from indianpong.settings import EMAIL_HOST_USER, STATICFILES_DIRS
from django.utils import timezone
from django.conf import settings
import uuid
from datetime import timedelta
from .game import MAX_SCORE
Expand Down Expand Up @@ -365,7 +366,7 @@ def message_for_match(self, game):
except Room.DoesNotExist:
room = Room.objects.create(first_user=game.player1, second_user=game.player2)
# Create message for the room with game link
message = f"http://localhost:8000/remote-game/tournament/{game.id}"
message = f"{settings.BASE_URL}/remote-game/tournament/{game.id}"
Message.objects.create(user=game.player1, room=room, content=message)


Expand Down
250 changes: 126 additions & 124 deletions indianpong/pong/rps.py
Original file line number Diff line number Diff line change
@@ -1,124 +1,126 @@
from enum import Enum
import time

class Choices(Enum):
ROCK = 0
PAPER = 1
SCISSORS = 2

class Abilities(Enum):
LIKEACHEATER = 3,
GODOFTHINGS = 4

class RoundResult(Enum):
DRAW = 0
PLAYER1_WIN = 1
PLAYER2_WIN = 2

KV_CHOICES = {'rock': Choices.ROCK, 'paper': Choices.PAPER, 'scissors': Choices.SCISSORS, 'godthings': Abilities.GODOFTHINGS, 'cheater': Abilities.LIKEACHEATER}

class Shaker:
def __init__(self, username):
self.username = username
self.score = 0
self.choices = []

class RPS:
def __init__(self, player1, player2):
self.shaker1 = Shaker(player1)
self.shaker2 = Shaker(player2)
self.max_score = 3
self.group_name = f'rps_{player1}_{player2}'
self.start_time = 0
self.end_time = 0

def play(self, username, choice):
if (self.start_time == 0):
self.start_time = time.time()
if username == self.shaker1.username:
self.shaker1.choices.append(KV_CHOICES[choice])
elif username == self.shaker2.username:
self.shaker2.choices.append(KV_CHOICES[choice])

def ability_result(self, choice1, choice2):
ab1 = choice1 == Abilities.LIKEACHEATER or choice1 == Abilities.GODOFTHINGS
ab2 = choice2 == Abilities.LIKEACHEATER or choice2 == Abilities.GODOFTHINGS
if ab1 and ab2: #both played this it's draw
return 0
elif ab1 and choice1 == Abilities.LIKEACHEATER: #stole opponent score if greater than 0
if self.shaker2.score > 0:
self.shaker2.score -= 1
self.shaker1.score += 1
return 1
elif ab2 and choice2 == Abilities.LIKEACHEATER: #won round instantly
if self.shaker1.score > 0:
self.shaker1.score -= 1
self.shaker2.score += 1
return 2
elif ab1 and choice1 == Abilities.GODOFTHINGS:
self.shaker1.score += 1
return 1
elif ab2 and choice2 == Abilities.GODOFTHINGS:
self.shaker2.score += 1
return 2
else:
return 3


def round_result(self):
shaker1_choice = self.shaker1.choices.pop()
shaker2_choice = self.shaker2.choices.pop()
result = self.ability_result(shaker1_choice, shaker2_choice)
if result == 0:
return RoundResult.DRAW.name
elif result == 1:
return RoundResult.PLAYER1_WIN.name
elif result == 2:
return RoundResult.PLAYER2_WIN.name
result = (shaker1_choice.value - shaker2_choice.value) % 3
if result == 0:
return RoundResult.DRAW.name
elif result == 1:
self.shaker1.score += 1
return RoundResult.PLAYER1_WIN.name
else:
self.shaker2.score += 1
return RoundResult.PLAYER2_WIN.name

def check_is_over(self):
if self.shaker1.score == self.max_score or self.shaker2.score == self.max_score:
self.end_time = time.time()
return True
return False

def get_winner_loser(self):
if self.shaker1.score > self.shaker2.score:
return self.shaker1.username, self.shaker2.username
else:
return self.shaker2.username, self.shaker1.username

def otherPlayer(self, username):
if username == self.shaker1.username:
return self.shaker2.username
else:
return self.shaker1.username

def get_scores(self):
return self.shaker1.score, self.shaker2.score

def getDuration(self):
if self.end_time == 0:
self.end_time = time.time()
return self.end_time - self.start_time

def getWinnerLoserandScores(self):
if self.shaker1.score > self.shaker2.score:
return self.shaker1.username, self.shaker2.username, self.shaker1.score, self.shaker2.score
else:
return self.shaker2.username, self.shaker1.username, self.shaker2.score, self.shaker1.score

def both_played(self):
return len(self.shaker1.choices) == len(self.shaker2.choices) == 1

def getChoices(self):
return self.shaker1.choices[0].name, self.shaker2.choices[0].name
from enum import Enum
import time

class Choices(Enum):
ROCK = 0
PAPER = 1
SCISSORS = 2

class Abilities(Enum):
LIKEACHEATER = 3,
GODOFTHINGS = 4

class RoundResult(Enum):
DRAW = 0
PLAYER1_WIN = 1
PLAYER2_WIN = 2

KV_CHOICES = {'rock': Choices.ROCK, 'paper': Choices.PAPER, 'scissors': Choices.SCISSORS, 'godthings': Abilities.GODOFTHINGS, 'cheater': Abilities.LIKEACHEATER}

class Shaker:
def __init__(self, username):
self.username = username
self.score = 0
self.choices = []

class RPS:
def __init__(self, player1, player2):
self.shaker1 = Shaker(player1)
self.shaker2 = Shaker(player2)
self.max_score = 3
self.group_name = f'rps_{player1}_{player2}'
self.start_time = 0
self.end_time = 0

def play(self, username, choice):
if (self.start_time == 0):
self.start_time = time.time()
if username == self.shaker1.username:
self.shaker1.choices.append(KV_CHOICES[choice])
elif username == self.shaker2.username:
self.shaker2.choices.append(KV_CHOICES[choice])

def ability_result(self, choice1, choice2):
ab1 = choice1 == Abilities.LIKEACHEATER or choice1 == Abilities.GODOFTHINGS
ab2 = choice2 == Abilities.LIKEACHEATER or choice2 == Abilities.GODOFTHINGS
if ab1 and ab2: #both played this it's draw
return 0
elif ab1 and choice1 == Abilities.LIKEACHEATER: #stole opponent score if greater than 0
if self.shaker2.score > 0:
self.shaker2.score -= 1
self.shaker1.score += 1
return 1
elif ab2 and choice2 == Abilities.LIKEACHEATER: #won round instantly
if self.shaker1.score > 0:
self.shaker1.score -= 1
self.shaker2.score += 1
return 2
elif ab1 and choice1 == Abilities.GODOFTHINGS:
self.shaker1.score += 1
return 1
elif ab2 and choice2 == Abilities.GODOFTHINGS:
self.shaker2.score += 1
return 2
else:
return 3


def round_result(self):
shaker1_choice = self.shaker1.choices.pop()
shaker2_choice = self.shaker2.choices.pop()
result = self.ability_result(shaker1_choice, shaker2_choice)
if result == 0:
return RoundResult.DRAW.name
elif result == 1:
return RoundResult.PLAYER1_WIN.name
elif result == 2:
return RoundResult.PLAYER2_WIN.name
result = (shaker1_choice.value - shaker2_choice.value) % 3
if result == 0:
return RoundResult.DRAW.name
elif result == 1:
self.shaker1.score += 1
return RoundResult.PLAYER1_WIN.name
else:
self.shaker2.score += 1
return RoundResult.PLAYER2_WIN.name

def check_is_over(self):
if self.shaker1.score == self.max_score or self.shaker2.score == self.max_score:
self.end_time = time.time()
return True
return False

def get_winner_loser(self):
if self.shaker1.score > self.shaker2.score:
return self.shaker1.username, self.shaker2.username
else:
return self.shaker2.username, self.shaker1.username

def otherPlayer(self, username):
if username == self.shaker1.username:
return self.shaker2.username
else:
return self.shaker1.username

def get_scores(self):
return self.shaker1.score, self.shaker2.score

def getDuration(self):
if self.start_time == 0:
return 0
if self.end_time == 0:
self.end_time = time.time()
return self.end_time - self.start_time

def getWinnerLoserandScores(self):
if self.shaker1.score > self.shaker2.score:
return self.shaker1.username, self.shaker2.username, self.shaker1.score, self.shaker2.score
else:
return self.shaker2.username, self.shaker1.username, self.shaker2.score, self.shaker1.score

def both_played(self):
return len(self.shaker1.choices) == len(self.shaker2.choices) == 1

def getChoices(self):
return self.shaker1.choices[0].name, self.shaker2.choices[0].name
21 changes: 21 additions & 0 deletions indianpong/pong/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,27 @@ <h3>{{context.baseInfoSubHeaderText3}}</h3>
return cookieValue ? cookieValue.pop() : '';
}



function showToast(content, status, iconClass) {
const liveToast = document.getElementById('liveToast');
var toastContent = document.querySelector('#liveToast .fw-semibold');
var toastIcon = document.querySelector('.toast-body .i-class i');


toastIcon.className = iconClass;
liveToast.classList.remove('text-bg-danger');
liveToast.className = 'toast';
liveToast.classList.add(status);

toastContent.textContent = content;
const toast = new bootstrap.Toast(liveToast);
toast.show();
setTimeout(function() {
toast.hide();
}, 8000);
}

</script>
</body>
</html>
5 changes: 0 additions & 5 deletions indianpong/pong/templates/play-ai.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@
<input type="range" class="slider" id="volumeSlider" min="0" max="1" step="0.1" value="1">
</div>
</div>
<div>
<label for="reactionDelay" style="color: white;">{{context.aiGameReactionDelayText}}:</label>
<span id="delayValue" style="color: white;">200</span>
<input type="range" class="slider" id="reactionDelay" name="reactionDelay" min="1000" max="10000" value="2000" step="1000">
</div>
</div>
<div id="countdownContainer">

Expand Down
Loading

0 comments on commit f9e6354

Please sign in to comment.