def start_reviewing(cls, workshop): """ Set the reviewers for all assessments in this workshop """ le_assessments = [a for a in list(workshop.assessment_set.filter(answered=True)) if not a.player.in_staff_group()] shuffle(le_assessments) participating_players = [a.player for a in le_assessments] if not participating_players: return pp_rotated = [participating_players[-1]] + participating_players[:-1] for i,a in enumerate(le_assessments): a.reviewers.clear() a.reviewers.add(pp_rotated[i]) # If there are more than two players, do this again if len(participating_players) > 2: pp_rotated = participating_players[-2:] + participating_players[:-2] for i,a in enumerate(le_assessments): a.reviewers.add(pp_rotated[i]) workshop.status = 1 # reviewing workshop.save() # send message to every player for player in participating_players: Message.send(None, player, _("Workshop to review!"), _("Hello, the reviewing stage for the latest workshop has begun."))
def create(self, request): attrs = self.flatten_dict(request.POST) sender = request.user.get_profile() if 'receiver' not in attrs.keys(): return {'success': False, 'error': 'Missing receiver'} try: if attrs['receiver'].isdigit(): receiver = Player.objects.get(pk=attrs['receiver']) else: receiver = Player.objects.get(user__username=attrs['receiver']) except Player.DoesNotExist: return {'success': False, 'error': 'Invalid receiver'} if 'text' not in attrs.keys(): return {'success': False, 'error': 'Missing text'} if 'subject' not in attrs.keys(): attrs['subject'] = '' try: reply_to = Message.objects.get(pk=attrs['reply_to']) except (KeyError, Message.DoesNotExist): reply_to = None Message.send(sender, receiver, attrs['subject'], attrs['text'], reply_to=reply_to) return {'success': True}
def earn_achievement(cls, player, modifier): result = player.magic.give_modifier(modifier) if result is not None: message = ugettext_noop('earned {artifact}') addActivity.send(sender=None, user_from=player, game=None, message=message, arguments=dict(artifact=result.artifact) ) Message.send(sender=None, receiver=player, subject="Achievement", text="You have just earned "+modifier) else: logging.debug('%s would have earned %s, but there was no artifact' % (player, modifier))
def score(self): if not self.challenge.SCORING: return for u in (self.challenge.user_to, self.challenge.user_from): u.percents = 100 if self.challenge.status == 'D': scoring.score(self.challenge.user_to.user, ChallengeGame, 'chall-draw', percents=self.challenge.user_to.percents) scoring.score(self.challenge.user_from.user, ChallengeGame, 'chall-draw', percents=self.challenge.user_from.percents) else: diff_race = self.challenge.user_won.user.race != self.challenge.user_lost.user.race diff_class = self.challenge.user_won.user.group != self.challenge.user_lost.user.group diff_race = 1 if diff_race else 0 diff_class = 1 if diff_class else 0 winner_points = self.challenge.user_won.user.points loser_points = self.challenge.user_lost.user.points # Check for charge if self.challenge.user_won.user.magic.has_modifier('challenge-affect-scoring-won'): self.challenge.user_won.percents += self.challenge.user_won.user.magic.modifier_percents('challenge-affect-scoring-won') - 100 # Check for weakness if self.challenge.user_won.user.magic.has_modifier('challenge-affect-scoring-lost'): self.challenge.user_won.percents += self.challenge.user_won.user.magic.modifier_percents('challenge-affect-scoring-lost') - 100 # Check for frenzy on the winning player if self.challenge.user_won.user.magic.has_modifier('challenge-affect-scoring'): self.challenge.user_won.percents += self.challenge.user_won.user.magic.modifier_percents('challenge-affect-scoring') - 100 if self.challenge.WARRANTY: # warranty not affected by percents scoring.score(self.challenge.user_won.user, ChallengeGame, 'chall-warranty-return', external_id=self.challenge.id) scoring.score(self.challenge.user_won.user, ChallengeGame, 'chall-won', external_id=self.challenge.id, percents=self.challenge.user_won.percents, points=self.challenge.user_won.score, points2=self.challenge.user_lost.score, different_race=diff_race, different_class=diff_class, winner_points=winner_points, loser_points=loser_points) # Check for frenzy on the losing player if self.challenge.user_lost.user.magic.has_modifier('challenge-affect-scoring'): self.challenge.user_lost.percents += self.challenge.user_lost.user.magic.modifier_percents('challenge-affect-scoring') - 100 # Check for spell evade if self.challenge.user_lost.user.magic.has_modifier('challenge-evade'): random.seed() if random.random() < 0.20: # He's lucky, no penalty, return warranty scoring.score(self.challenge.user_lost.user, ChallengeGame, 'chall-warranty-return', external_id=self.challenge.id) Message.send(sender=None, receiver=self.challenge.user_lost.user, subject="Challenge evaded", text="You have just evaded losing points in a challenge") scoring.score(self.challenge.user_lost.user, ChallengeGame, 'chall-lost', external_id=self.challenge.id, percents=self.challenge.user_lost.percents, points=self.challenge.user_lost.score, points2=self.challenge.user_lost.score)
def system_message_group(request, group): group = get_object_or_404(PlayerGroup, pk=group) if request.method == 'POST': text = request.POST['text'] for p in group.players.all(): Message.send(sender=None, receiver=p, subject="System message", text=text) messages.success(request, 'Message sent!') return render_to_response('cpanel/system_message_group.html', {'group': group}, context_instance=RequestContext(request))
def test_popularity_5_pm_3(self): Artifact.objects.create(group=Artifact.DEFAULT(), name='ach-popularity') user_to = self._get_player(100).get_extension(MessagingUser) for i in range(10): player = self._get_player(i).get_extension(MessagingUser) if i <= 3: timestamp = datetime.now() + timedelta(minutes=-1) a = Message.objects.create(timestamp=timestamp, sender=player,receiver=user_to,subject = "a",text = "b") else: timestamp = datetime.now() + timedelta(minutes=-35) a = Message.objects.create(timestamp=timestamp, sender=player,receiver=user_to,subject = "a",text = "b") Message.send(sender=player,receiver=user_to,subject="a",text="b") self.assertEqual(unique_users_pm(user_to,30),5) self.assertTrue(user_to.magic.has_modifier('ach-popularity'))
def create(request, to=None, reply_to=None): if to is not None: to = get_object_or_404(Player, pk=to) if reply_to is not None: reply_to = get_object_or_404(Message, pk=reply_to) subject = "Re: " + reply_to.subject if not reply_to.subject.startswith("Re: ") else reply_to.subject else: subject = "" if request.method == "POST": form = ComposeForm(request.POST) if form.is_valid(): m = Message.send( request.user.get_profile(), form.cleaned_data["to"], form.cleaned_data["subject"], form.cleaned_data["text"], reply_to=form.cleaned_data["reply_to"], ) return HttpResponseRedirect(reverse("wouso.interface.messaging.views.home")) # else: # print form, form.is_valid(), request.POST return render_to_response( "messaging/create.html", {"to": to, "reply_to": reply_to, "subject": subject}, context_instance=RequestContext(request), )
def create(request, to=None, reply_to=None): if to is not None: to = get_object_or_404(Player, pk=to) if reply_to is not None: reply_to = get_object_or_404(Message, pk=reply_to) subject = 'Re: ' + reply_to.subject if not reply_to.subject.startswith('Re: ') else reply_to.subject else: subject = '' error = '' if request.method == "POST": form = ComposeForm(request.POST) if form.is_valid(): m = Message.send(request.user.get_profile(), form.cleaned_data['to'], form.cleaned_data['subject'], form.cleaned_data['text'], reply_to=reply_to.id if reply_to else None, ) if m is None: return HttpResponseRedirect(reverse('messaging')) else: error = m #else: # print form, form.is_valid(), request.POST return render_to_response('messaging/create.html', {'error': error, 'to': to, 'reply_to': reply_to, 'subject': subject}, context_instance=RequestContext(request))
def header_footer(request): """ Generate header and footer bar contents. """ try: reverse('homepage') except NoReverseMatch: return {} #TODO ordering, using config header = [] try: for game in get_games(): h = game.get_header_link(request) if h: header.append((h, game.get_instance().name)) except Exception as e: logging.exception(e) # add also messages and magic link try: h = Message.get_header_link(request) if h: header.append((h, 'Message')) h = Bazaar.get_header_link(request) if h: header.append((h, 'Magic')) h = Chat.get_header_link(request) if h: header.append((h, 'Chat')) except Exception as e: logging.exception(e) footer = [] try: for game in get_games(): f = game.get_footer_link(request) if f: footer.append(f) except: pass # also add static pages footer.extend(get_static_pages()) for a in get_apps(): f = a.get_footer_link(request) if f: footer.append(a.get_footer_link(request)) # format header hids = lambda p: '<span id="head-%s"><a href="%s">%s</a>%s</span>' % (p[1].lower(), \ p[0]['link'], p[0]['text'], \ '<sup class="unread-count">%d</sup>' % p[0]['count'] if p[0].get('count', False) else '') header_html = " ".join(map(hids, header)) footer = " | ".join(footer) return {'header': header_html, 'heads': header, 'footer': footer}
def test_popularity_5_pm_3(self): Artifact.objects.create(group=None, name='ach-popularity') user_to = self._get_player(100).get_extension(MessagingUser) for i in range(10): player = self._get_player(i).get_extension(MessagingUser) if i <= 3: timestamp = datetime.now() + timedelta(minutes=-10) a = Message.objects.create(timestamp=timestamp, sender=player, receiver=user_to, subject="a", text="b") else: timestamp = datetime.now() + timedelta(minutes=-35) a = Message.objects.create(timestamp=timestamp, sender=player, receiver=user_to, subject="a", text="b") Message.send(sender=player, receiver=user_to, subject="a", text="b") self.assertEqual(unique_users_pm(user_to, 30), 5) self.assertTrue(user_to.magic.has_modifier('ach-popularity'))
def create(self, request): attrs = self.flatten_dict(request.POST) sender = request.user.get_profile() try: reply_to = Message.objects.get(pk=attrs['reply_to']) receiver = reply_to.sender except (KeyError, Message.DoesNotExist): reply_to = None receiver = None if not reply_to and 'receiver' not in attrs.keys(): return {'success': False, 'error': 'Missing receiver or reply_to'} if not receiver: try: if attrs['receiver'].isdigit(): receiver = Player.objects.get(pk=attrs['receiver']) else: receiver = Player.objects.get( user__username=attrs['receiver']) except Player.DoesNotExist: return {'success': False, 'error': 'Invalid receiver'} if 'text' not in attrs.keys(): return {'success': False, 'error': 'Missing text'} if 'subject' not in attrs.keys(): attrs['subject'] = '' Message.send(sender, receiver, attrs['subject'], attrs['text'], reply_to=reply_to) return {'success': True}
def played(self): """ Both players have played, save and score Notice the fact this is the only function where the scoring is affected """ """ Handle artifacts and spells """ for u in (self.user_to, self.user_from): # always lose, you mofo if u.user.has_modifier('challenge-always-lose'): u.score = -1 # affect bonuses if u.user.has_modifier('challenge-affect-scoring'): u.percents = u.user.modifier_percents('challenge-affect-scoring') else: u.percents = 100 if self.user_to.score > self.user_from.score: result = (self.user_to, self.user_from) elif self.user_from.score > self.user_to.score: result = (self.user_from, self.user_to) else: #draw game result = 'draw' if result == 'draw': self.status = 'D' scoring.score(self.user_to.user, ChallengeGame, 'chall-draw', percents=self.user_to.percents) scoring.score(self.user_from.user, ChallengeGame, 'chall-draw', percents=self.user_from.percents) # send activty signal action_msg = "chall-draw" signal_msg = ugettext_noop('draw result between {user_to} and {user_from}:\n{extra}') signals.addActivity.send(sender=None, user_from=self.user_to.user, \ user_to=self.user_from.user, \ message=signal_msg, \ arguments=dict(user_to=self.user_to, user_from=self.user_from, extra=self.extraInfo(self.user_from, self.user_to)),\ action=action_msg, \ game=ChallengeGame.get_instance()) else: self.status = 'P' self.user_won, self.user_lost = result self.winner = self.user_won.user diff_race = self.user_won.user.series != self.user_lost.user.series diff_class = self.user_won.user.proximate_group != self.user_lost.user.proximate_group diff_race = 1 if diff_race else 0 diff_class = 1 if diff_class else 0 winner_points = self.user_won.user.points loser_points = self.user_lost.user.points # warranty not affected by percents scoring.score(self.user_won.user, ChallengeGame, 'chall-warranty-return', external_id=self.id) scoring.score(self.user_won.user, ChallengeGame, 'chall-won', external_id=self.id, percents=self.user_won.percents, points=self.user_won.score, points2=self.user_lost.score, different_race=diff_race, different_class=diff_class, winner_points=winner_points, loser_points=loser_points, ) #Check for spell evade if self.user_lost.user.has_modifier('challenge-evade'): random.seed() if random.random() < 0.33: #He's lucky,no penalty,return warrany scoring.score(self.user_lost.user, ChallengeGame, 'chall-warranty-return', external_id=self.id) Message.send(sender=None, receiver=self.user_lost.user, subject="Challenge evaded", text="You have just evaded losing points in a challenge") scoring.score(self.user_lost.user, ChallengeGame, 'chall-lost', external_id=self.id, points=self.user_lost.score, points2=self.user_lost.score) # send activty signal signal_msg = ugettext_noop('won challenge with {user_lost}: {extra}') action_msg = "chall-won" signals.addActivity.send(sender=None, user_from=self.user_won.user, \ user_to=self.user_lost.user, \ message=signal_msg, arguments=dict(user_lost=self.user_lost, id=self.id, extra=self.extraInfo(self.user_won, self.user_lost)), \ action=action_msg, \ game=ChallengeGame.get_instance()) self.save()
def setUp(self): Message.disable_check()
def tearDown(self): Message.enable_check()
def score(self): if not self.challenge.SCORING: return for u in (self.challenge.user_to, self.challenge.user_from): # affect bonuses if u.user.magic.has_modifier('challenge-affect-scoring'): u.percents = u.user.magic.modifier_percents( 'challenge-affect-scoring') else: u.percents = 100 if self.challenge.status == 'D': scoring.score(self.challenge.user_to.user, ChallengeGame, 'chall-draw', percents=self.challenge.user_to.percents) scoring.score(self.challenge.user_from.user, ChallengeGame, 'chall-draw', percents=self.challenge.user_from.percents) else: diff_race = self.challenge.user_won.user.race != self.challenge.user_lost.user.race diff_class = self.challenge.user_won.user.group != self.challenge.user_lost.user.group diff_race = 1 if diff_race else 0 diff_class = 1 if diff_class else 0 winner_points = self.challenge.user_won.user.points loser_points = self.challenge.user_lost.user.points if self.challenge.user_won.user.magic.has_modifier( 'challenge-affect-scoring-won'): self.challenge.user_won.percents += self.challenge.user_won.user.magic.modifier_percents( 'challenge-affect-scoring-won') if self.challenge.WARRANTY: # warranty not affected by percents scoring.score(self.challenge.user_won.user, ChallengeGame, 'chall-warranty-return', external_id=self.challenge.id) scoring.score( self.challenge.user_won.user, ChallengeGame, 'chall-won', external_id=self.challenge.id, percents=self.challenge.user_won.percents, points=self.challenge.user_won.score, points2=self.challenge.user_lost.score, different_race=diff_race, different_class=diff_class, winner_points=winner_points, loser_points=loser_points, ) #Check for spell evade if self.challenge.user_lost.user.magic.has_modifier( 'challenge-evade'): random.seed() if random.random() < 0.33: #He's lucky,no penalty,return warranty scoring.score(self.challenge.user_lost.user, ChallengeGame, 'chall-warranty-return', external_id=self.challenge.id) Message.send( sender=None, receiver=self.challenge.user_lost.user, subject="Challenge evaded", text="You have just evaded losing points in a challenge" ) scoring.score(self.challenge.user_lost.user, ChallengeGame, 'chall-lost', external_id=self.challenge.id, points=self.challenge.user_lost.score, points2=self.challenge.user_lost.score)
def played(self): """ Both players have played, save and score Notice the fact this is the only function where the scoring is affected """ """ Handle artifacts and spells """ for u in (self.user_to, self.user_from): # always lose, you mofo if u.user.has_modifier('challenge-always-lose'): u.score = -1 # affect bonuses if u.user.has_modifier('challenge-affect-scoring'): u.percents = u.user.modifier_percents('challenge-affect-scoring') else: u.percents = 100 if self.user_to.score > self.user_from.score: result = (self.user_to, self.user_from) elif self.user_from.score > self.user_to.score: result = (self.user_from, self.user_to) else: #draw game result = 'draw' if result == 'draw': self.status = 'D' scoring.score(self.user_to.user, ChallengeGame, 'chall-draw', percents=self.user_to.percents) scoring.score(self.user_from.user, ChallengeGame, 'chall-draw', percents=self.user_from.percents) # send activty signal action_msg = "chall-draw" signal_msg = ugettext_noop('draw result between {user_to} and {user_from}:\n{extra}') signals.addActivity.send(sender=None, user_from=self.user_to.user, \ user_to=self.user_from.user, \ message=signal_msg, \ arguments=dict(user_to=self.user_to, user_from=self.user_from, extra=self.extraInfo(self.user_from, self.user_to)),\ action=action_msg, \ game=ChallengeGame.get_instance()) else: self.status = 'P' self.user_won, self.user_lost = result self.winner = self.user_won.user diff_race = self.user_won.user.race != self.user_lost.user.race diff_class = self.user_won.user.group != self.user_lost.user.group diff_race = 1 if diff_race else 0 diff_class = 1 if diff_class else 0 winner_points = self.user_won.user.points loser_points = self.user_lost.user.points # warranty not affected by percents scoring.score(self.user_won.user, ChallengeGame, 'chall-warranty-return', external_id=self.id) if self.user_won.user.has_modifier('challenge-affect-scoring-won'): self.user_won.percents += self.user_won.user.modifier_percents('challenge-affect-scoring-won') scoring.score(self.user_won.user, ChallengeGame, 'chall-won', external_id=self.id, percents=self.user_won.percents, points=self.user_won.score, points2=self.user_lost.score, different_race=diff_race, different_class=diff_class, winner_points=winner_points, loser_points=loser_points, ) #Check for spell evade if self.user_lost.user.has_modifier('challenge-evade'): random.seed() if random.random() < 0.33: #He's lucky,no penalty,return warrany scoring.score(self.user_lost.user, ChallengeGame, 'chall-warranty-return', external_id=self.id) Message.send(sender=None, receiver=self.user_lost.user, subject="Challenge evaded", text="You have just evaded losing points in a challenge") scoring.score(self.user_lost.user, ChallengeGame, 'chall-lost', external_id=self.id, points=self.user_lost.score, points2=self.user_lost.score) # send activty signal signal_msg = ugettext_noop('won challenge with {user_lost}: {extra}') action_msg = "chall-won" signals.addActivity.send(sender=None, user_from=self.user_won.user, \ user_to=self.user_lost.user, \ message=signal_msg, arguments=dict(user_lost=self.user_lost, id=self.id, extra=self.extraInfo(self.user_won, self.user_lost)), \ action=action_msg, \ game=ChallengeGame.get_instance()) self.save()
def header_footer(request): """ Generate header and footer bar contents. """ #TODO ordering, using config header = [] try: for game in get_games(): h = game.get_header_link(request) if h: header.append((h, game.get_instance().name)) except Exception as e: logging.exception(e) # add also messages and magic link try: h = Message.get_header_link(request) if h: header.append((h, 'Message')) h = Bazaar.get_header_link(request) if h: header.append((h, 'Magic')) h = Chat.get_header_link(request) if h: header.append((h, 'Chat')) except Exception as e: logging.exception(e) footer = [] try: for game in get_games(): f = game.get_footer_link(request) if f: footer.append(f) except: pass # also add stats link try: f = stats_link(request) if f: footer.append(f) except: pass # also add static pages for sp in get_static_pages(): footer.append(sp.html_link()) # qporposal if not Qproposal.disabled(): footer.append(Qproposal.get_footer_link(request)) # format header hids = lambda p: '<span id="head-%s"><a href="%s">%s</a>%s</span>' % (p[1].lower(), \ p[0]['link'], p[0]['text'], \ '<sup class="unread-count">%d</sup>' % p[0]['count'] if p[0].get('count', False) else '') header_html = " ".join(map(hids, header)) footer = " | ".join(footer) return {'header': header_html, 'heads': header, 'footer': footer}