Skip to content

Commit

Permalink
Merge pull request #49 from thejackal360/why-kiwis
Browse files Browse the repository at this point in the history
Why kiwis
  • Loading branch information
thejackal360 authored Dec 8, 2023
2 parents 97dd965 + efabe87 commit 87c2a17
Show file tree
Hide file tree
Showing 19 changed files with 199 additions and 142 deletions.
2 changes: 1 addition & 1 deletion src/elena/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env python3

from .elena import eFlask, Module, Game
from .elena import eFlask, Module, Game, Point
40 changes: 36 additions & 4 deletions src/elena/elena.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# Imports

from enum import Enum
from flask import jsonify, Flask, Response, request, render_template
from functools import partial
from itertools import chain
Expand All @@ -10,6 +11,7 @@
import json
import random
import sys
from typing import Dict

# https://github.com/pallets/flask/blob/main/src/flask/scaffold.py#L36
import typing as t
Expand Down Expand Up @@ -57,6 +59,30 @@ def dict_stitching(list_of_dicts):
# Core classes


class Point(Enum):
"""
Enumeration representing different types of points
"""

KIWI = 1
STRAWBERRY = 2
CHERRY = 3


"""
A dictionary mapping Point enum values to corresponding emoji representations.
This dictionary is used to associate each Point type with its corresponding
Unicode emoji representation. It allows for easy conversion between Point
types and the emojis used to visually represent them.
"""
PointToEmoji: Dict[Point, str] = {
Point.KIWI: "🥝",
Point.STRAWBERRY: "🍓",
Point.CHERRY: "🍒",
}


class eFlask(Flask):
"""
Class used to instantiate app objects.
Expand All @@ -66,25 +92,28 @@ def __init__(
self,
module_list,
domain,
kiwi_cost_to_play_game=5,
point_cost_to_play_game=5,
ask_for_game_every_N_rounds=3,
bouncing_text=True,
point=Point.KIWI,
**kwargs,
):
"""
Initializer.
@param module_list: list of module objects for trivia game
@param domain: website's domain
@param kiwi_cost_to_play_game: how many kiwis does it cost to play a game? default value is 5
@param point_cost_to_play_game: how many points does it cost to play a game? default value is 5
@param ask_for_game_every_N_rounds: minimum number of rounds between games; default value is 3
@param bouncing_text: controls whether textboxes are animated to bounce up and down; default value is True
"""
super().__init__(**kwargs)
self.module_list = module_list
self.domain = domain
self.kiwi_cost_to_play_game = kiwi_cost_to_play_game
self.point_cost_to_play_game = point_cost_to_play_game
self.ask_for_game_every_N_rounds = ask_for_game_every_N_rounds
self.bouncing_text = bouncing_text
self.point = point
self.point_text = PointToEmoji[self.point]
self.http_post_ptype_to_fn = dict_stitching(
[m.http_post_ptype_to_fn for m in self.module_list]
)
Expand Down Expand Up @@ -157,6 +186,7 @@ def gen_elenajs(self):
elena_template.render(
module_name=m.fn_module_name,
module_name_in_quotes='"' + m.fn_module_name + '"',
point_text=self.point_text,
)
)

Expand All @@ -167,8 +197,9 @@ def gen_elena_top_js(self):
env = Environment(loader=FileSystemLoader("."))
template = env.get_template("templates/elena_top.js")
rendered_content = template.render(
kiwi_cost_to_play_game=self.kiwi_cost_to_play_game,
point_cost_to_play_game=self.point_cost_to_play_game,
ask_for_game_every_N_rounds=self.ask_for_game_every_N_rounds,
point_text=self.point_text,
)
with open("static/js/elena_top.js", "w") as f:
f.write(rendered_content)
Expand Down Expand Up @@ -246,6 +277,7 @@ def handle_GET(self):
module_js_globals=module_js_globals,
module_subjs=module_subjs,
pick_mod=pick_mod,
point_text=self.point_text,
)


Expand Down
2 changes: 1 addition & 1 deletion src/elena/templates/elena.css
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ body {
cursor: pointer;
}

.kiwibar {
.pointbar {
overflow: hidden;
background-color: grey;
position: fixed;
Expand Down
35 changes: 20 additions & 15 deletions src/elena/templates/elena.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Check whether chat topic is valid given (a) number of kiwis,
* Check whether chat topic is valid given (a) number of points,
* or trivia points earned by the user for correct answers, and (b)
* the number of rounds since the last game.
* @param {BigInt} _t - topic index; limited by NumTopics constant
* in generated *_globals.js file in static/js/games/
*/
function {{ module_name }}_acceptable_topic(_t) {
if (((kiwicount_var >= kiwi_cost_to_play_game) &&
if (((pointcount_var >= point_cost_to_play_game) &&
(rounds_since_last_game >= ask_for_game_every_N_rounds) &&
(_t != {{ module_name }}TriviaTopic)) ||
(_t == {{ module_name }}TriviaTopic)) {
Expand All @@ -17,7 +17,7 @@ function {{ module_name }}_acceptable_topic(_t) {
}

/**
* Check whether chat topic is valid given (a) number of kiwis,
* Check whether chat topic is valid given (a) number of points,
* or trivia points earned by the user for correct answers, and (b)
* the number of rounds since the last game.
* @param {BigInt} _t - topic index; limited by NumTopics constant
Expand Down Expand Up @@ -56,23 +56,28 @@ function {{ module_name }}_convo(val) {
// Race condition: No youHold and no more chelsea_msgs
// Need to hold until you generate topic content.
chelsea_msgs.push("Good to meet you, " + val);
chelsea_msgs.push("Answer trivia questions to earn {{ point_text }}s.");
if ({{ module_name }}NumTopics > 1) {
chelsea_msgs.push("Pay {{ point_text }}s to play mini games.");
}
chelsea_msgs.push("Let's get started!");
topic = {{ module_name }}_randint_topics();
{{ module_name }}_convo(val);
} else if (topic == {{ module_name }}TriviaTopic) {
if (question_asked) {
let kiwi_inc = false;
let point_inc = false;
if (scrub_str(ans_txt) == scrub_str(val)) {
chelsea_msgs.push("That is correct!");
inc_kiwi_count();
kiwi_inc = true;
inc_point_count();
point_inc = true;
} else {
chelsea_msgs.push("Incorrect! The answer is: " +
ans_txt
);
}
question_asked = false;
topic = {{ module_name }}_randint_topics();
if (!kiwi_inc) {
if (!point_inc) {
{{ module_name }}_convo(val);
}
} else {
Expand Down Expand Up @@ -138,18 +143,18 @@ function {{ module_name }}_start_game() {
else bub.innerHTML += ".";
}
}, 1000);
var kiwi_inc_interval = window.setInterval(function() {
var point_inc_interval = window.setInterval(function() {
if ((chelsea_msgs.length == 0) &&
(kiwi_inc_waits > 0)) {
kiwicount_var += kiwi_inc_waits;
kiwi_inc_waits = 0;
$("#kiwicount").fadeOut(100).fadeIn(100).fadeOut(100).
(point_inc_waits > 0)) {
pointcount_var += point_inc_waits;
point_inc_waits = 0;
$("#pointcount").fadeOut(100).fadeIn(100).fadeOut(100).
fadeIn(100).fadeOut(100).
fadeIn(100).fadeOut(100).
fadeIn(100).fadeOut(100).
html(kiwicount_var).fadeIn(100);
document.getElementById("kiwicount").innerHTML =
kiwicount_var;
html(pointcount_var).fadeIn(100);
document.getElementById("pointcount").innerHTML =
pointcount_var;
youHold = false;
{{ module_name }}_convo(null);
newChelseaBubble();
Expand Down
36 changes: 18 additions & 18 deletions src/elena/templates/elena_top.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ var question_sel = -1;
var rounds_since_last_game = 0;

var ans_txt = "";
var kiwi_inc_waits = 0;
var kiwicount_var = 0;
var point_inc_waits = 0;
var pointcount_var = 0;
var herready = false;
var already_hit = false;
var chelsea_msgs = ["Hi! I'm Chelsea. What's your name?"];
var convo_counter = 0;

const kiwi_cost_to_play_game = {{ kiwi_cost_to_play_game }};
const point_cost_to_play_game = {{ point_cost_to_play_game }};
const ask_for_game_every_N_rounds = {{ ask_for_game_every_N_rounds }};

/**
Expand Down Expand Up @@ -224,36 +224,36 @@ function start() {
*/

/**
* Pay kiwi_cost_to_play_game kiwis to play a game.
* Pay point_cost_to_play_game points to play a game.
*/
function pay_to_play() {
kiwicount_var -= kiwi_cost_to_play_game;
document.getElementById("kiwicount").innerHTML = kiwicount_var;
pointcount_var -= point_cost_to_play_game;
document.getElementById("pointcount").innerHTML = pointcount_var;
}

/**
* Increment someone's score by i kiwis, either through answering a
* Increment someone's score by i points, either through answering a
* trivia question or through a bonus round.
* @param {BigInt} i - number of kiwis by which to increment the user's
* @param {BigInt} i - number of points by which to increment the user's
* score.
*/
function inc_kiwi_count(i = 1) {
function inc_point_count(i = 1) {
// Wait for chelsea msg to be posted.
youHold = true;
if (i == 1) {
var kiwi_convo_idx = randint(3);
if (kiwi_convo_idx == 0) {
chelsea_msgs.push("Here's a kiwi. One less for me.");
} else if (kiwi_convo_idx == 1) {
chelsea_msgs.push("Have a kiwi.");
var point_convo_idx = randint(3);
if (point_convo_idx == 0) {
chelsea_msgs.push("Here's a {{ point_text }}. One less for me.");
} else if (point_convo_idx == 1) {
chelsea_msgs.push("Have a {{ point_text }}.");
} else {
chelsea_msgs.push("I shall award you with one kiwi.");
chelsea_msgs.push("I shall award you with one {{ point_text }}.");
}
} else if (i > 0) {
chelsea_msgs.push("Congrats! Here's a " +
i.toString() + " kiwi bonus!");
i.toString() + " {{ point_text }} bonus!");
}
kiwi_inc_waits += i;
point_inc_waits += i;
}

/**
Expand Down Expand Up @@ -301,7 +301,7 @@ function back_button_click() {
$("#chat").animate({opacity: 0}, {duration: 3000}).promise().done(function() {
$("#chat").hide();
document.getElementById("final_score").innerHTML =
"<br/>Final score: " + kiwicount_var.toString() + "&#129373;";
"<br/>Final score: " + pointcount_var.toString() + "{{ point_text }}";
$("#final_score").animate({opacity: 1}, {duration: 3000}).promise().done(function() {
$("#final_score").animate({opacity: 0}, {duration: 3000}).promise().done(function() {
document.location.reload();
Expand Down
4 changes: 2 additions & 2 deletions src/elena/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@
</div>
</div>

<div id="kbar" class="kiwibar">
<div id="kbar" class="pointbar">
<span>
<!--- https://stackoverflow.com/questions/8523028/left-center-and-right-aligned-text-on-the-same-line -->
<div class="back-arrow-bubble" style="float: left;">
<img width="32" class="back-arrow-icon" tabindex="0" onkeydown="handleEnterSpaceClick(event)" onclick="back_button_click();" alt="Arrow full left - Picol.org, CC BY 3.0 &lt;https://creativecommons.org/licenses/by/3.0&gt;, via Wikimedia Commons" src="https://upload.wikimedia.org/wikipedia/commons/thumb/9/97/Arrow_full_left.svg/32px-Arrow_full_left.svg.png"></img>
</div>
<div style="margin: 0 auto; width:10%;">&#129373;<span id="kiwicount">0</span></div>
<div style="margin: 0 auto; width:10%;">{{ point_text | safe }}<span id="pointcount">0</span></div>
</span>
</div>

Expand Down
2 changes: 1 addition & 1 deletion src/example/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from flask import jsonify, request, Response, send_from_directory
from PIL import Image
from static.ml.stylize import stylize
from elena import eFlask, Module, Game
from elena import eFlask, Module, Game, Point

# Constants

Expand Down
2 changes: 1 addition & 1 deletion src/example/static/css/elena.css
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ body {
cursor: pointer;
}

.kiwibar {
.pointbar {
overflow: hidden;
background-color: grey;
position: fixed;
Expand Down
36 changes: 18 additions & 18 deletions src/example/static/js/elena_top.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ var question_sel = -1;
var rounds_since_last_game = 0;

var ans_txt = "";
var kiwi_inc_waits = 0;
var kiwicount_var = 0;
var point_inc_waits = 0;
var pointcount_var = 0;
var herready = false;
var already_hit = false;
var chelsea_msgs = ["Hi! I'm Chelsea. What's your name?"];
var convo_counter = 0;

const kiwi_cost_to_play_game = 5;
const point_cost_to_play_game = 5;
const ask_for_game_every_N_rounds = 3;

/**
Expand Down Expand Up @@ -224,36 +224,36 @@ function start() {
*/

/**
* Pay kiwi_cost_to_play_game kiwis to play a game.
* Pay point_cost_to_play_game points to play a game.
*/
function pay_to_play() {
kiwicount_var -= kiwi_cost_to_play_game;
document.getElementById("kiwicount").innerHTML = kiwicount_var;
pointcount_var -= point_cost_to_play_game;
document.getElementById("pointcount").innerHTML = pointcount_var;
}

/**
* Increment someone's score by i kiwis, either through answering a
* Increment someone's score by i points, either through answering a
* trivia question or through a bonus round.
* @param {BigInt} i - number of kiwis by which to increment the user's
* @param {BigInt} i - number of points by which to increment the user's
* score.
*/
function inc_kiwi_count(i = 1) {
function inc_point_count(i = 1) {
// Wait for chelsea msg to be posted.
youHold = true;
if (i == 1) {
var kiwi_convo_idx = randint(3);
if (kiwi_convo_idx == 0) {
chelsea_msgs.push("Here's a kiwi. One less for me.");
} else if (kiwi_convo_idx == 1) {
chelsea_msgs.push("Have a kiwi.");
var point_convo_idx = randint(3);
if (point_convo_idx == 0) {
chelsea_msgs.push("Here's a &#127826;. One less for me.");
} else if (point_convo_idx == 1) {
chelsea_msgs.push("Have a &#127826;.");
} else {
chelsea_msgs.push("I shall award you with one kiwi.");
chelsea_msgs.push("I shall award you with one &#127826;.");
}
} else if (i > 0) {
chelsea_msgs.push("Congrats! Here's a " +
i.toString() + " kiwi bonus!");
i.toString() + " &#127826; bonus!");
}
kiwi_inc_waits += i;
point_inc_waits += i;
}

/**
Expand Down Expand Up @@ -301,7 +301,7 @@ function back_button_click() {
$("#chat").animate({opacity: 0}, {duration: 3000}).promise().done(function() {
$("#chat").hide();
document.getElementById("final_score").innerHTML =
"<br/>Final score: " + kiwicount_var.toString() + "&#129373;";
"<br/>Final score: " + pointcount_var.toString() + "&#127826;";
$("#final_score").animate({opacity: 1}, {duration: 3000}).promise().done(function() {
$("#final_score").animate({opacity: 0}, {duration: 3000}).promise().done(function() {
document.location.reload();
Expand Down
Loading

0 comments on commit 87c2a17

Please sign in to comment.