Skip to content

Commit

Permalink
Merge pull request #110 from JacobLinCool/feat/show-card
Browse files Browse the repository at this point in the history
Feat/show card
  • Loading branch information
JacobLinCool authored Jun 19, 2022
2 parents 6bfc0ac + c06c1ce commit 97ba350
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 13 deletions.
5 changes: 3 additions & 2 deletions src/core/cards.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "ai.h"

void died_player(Game* game, i32 me_id, i32 enemy_id) {
Player* me = game->players->data[me_id];
Player* enemy = game->players->data[enemy_id];
if (enemy->hp > 0) return;
respond_all(game, "status");
Expand Down Expand Up @@ -70,7 +71,8 @@ void died_player(Game* game, i32 me_id, i32 enemy_id) {
enemy->dead = true;
respond_all(game, "status");
respond_all_chat($(String.format("%s kill %s", game->players->data[me_id]->name, enemy->name)));
printf("Died player(%s) role is %s\n", enemy->name, role_name[enemy->role->type]);
printf("Died player (%s) role is %s, killed by %s (%s)\n", enemy->name,
role_name[enemy->role->type], me->name, role_name[me->role->type]);
respond_all_chat(
$(String.format("%s died, his role is %s", enemy->name, role_name[enemy->role->type])));
respond_all(game, "status");
Expand Down Expand Up @@ -138,7 +140,6 @@ void died_player(Game* game, i32 me_id, i32 enemy_id) {

respond_all(game, "status");
// Penalties and Rewards
Player* me = game->players->data[me_id];
DEBUG_PRINT("Penalties and Rewards.\n");
if (me_id == enemy_id) {
DEBUG_PRINT("I killed myself.\n");
Expand Down
1 change: 1 addition & 0 deletions src/core/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ void game_loop(Game *game) {
game->turn++;
}
game_win(game);
sleep(3);
pthread_exit(NULL);
}

Expand Down
4 changes: 3 additions & 1 deletion src/web/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,9 @@ static int event_handler(struct lws *instance, enum lws_callback_reasons reason,

while (client->msg_queue->size > 0) {
Message *msg = client->msg_queue->shift(client->msg_queue);
free_message(msg);
if (msg != NULL) {
free_message(msg);
}
}

cJSON *list = create_player_list();
Expand Down
30 changes: 22 additions & 8 deletions web/src/components/Field.vue
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,17 @@ function select_player() {
? 'bg-orange-300/20 border border-orange-300'
: 'bg-blue-300/20',
props.player?.dead ? 'filter brightness-50' : '',
choosing ? 'border-2 border-orange-500' : '',
]"
style="aspect-ratio: 16 / 9"
@click="
(event) => {
if (choosing) {
event.stopPropagation();
select_player();
}
}
"
>
<div
:class="['absolute right-0 w-2/3 flex p-1 justify-center', self ? 'bottom-0' : 'top-0']"
Expand All @@ -40,7 +49,12 @@ function select_player() {
self ? 'hover:-translate-y-5 z-10' : 'hover:translate-y-4',
]"
:style="{
'margin-right': ((props.player.hands?.length || 0) - 3) * -5.5 + '%',
'margin-right':
((props.player.hands?.length || 0) - 3 <= 6
? (props.player.hands?.length || 0) - 3
: 6) *
-5.5 +
'%',
left: ((props.player.hands?.length || 0) - 3) * -3 + '%',
}"
/>
Expand All @@ -53,36 +67,36 @@ function select_player() {
<Card
v-if="props.player.weapon"
:card="props.player.weapon"
class="h-full hover:z-20 max-h-60 flex-1"
class="h-full hover:z-20 max-h-45 flex-1"
></Card>
<Card
v-if="props.player.scope"
:card="props.player.scope"
class="h-full hover:z-20 max-h-60 flex-1"
class="h-full hover:z-20 max-h-45 flex-1"
></Card>
<Card
v-if="props.player.barrel"
:card="props.player.barrel"
class="h-full hover:z-20 max-h-60 flex-1"
class="h-full hover:z-20 max-h-45 flex-1"
></Card>
<Card
v-if="props.player.mustang"
:card="props.player.mustang"
class="h-full hover:z-20 max-h-60 flex-1"
class="h-full hover:z-20 max-h-45 flex-1"
></Card>
<Card
v-if="props.player.jail"
:card="props.player.jail"
class="h-full hover:z-20 max-h-60 flex-1"
class="h-full hover:z-20 max-h-45 flex-1"
></Card>
<Card
v-if="props.player.dynamite"
:card="props.player.dynamite"
class="h-full hover:z-20 max-h-60 flex-1"
class="h-full hover:z-20 max-h-45 flex-1"
></Card>
</ListTransition>
</div>
<Role @click="select_player" class="w-1/5 p-1" :type="props.player.role"></Role>
<Role @click="select_player" :class="['w-1/5 p-1']" :type="props.player.role"></Role>
<Character
@click="select_player"
:class="['w-1/5 p-1']"
Expand Down
41 changes: 39 additions & 2 deletions web/src/components/GameBoard.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
<script setup lang="ts">
import { game, send, name, selecting, requesting, choosing, timer } from "../composables/game";
import {
game,
send,
name,
selecting,
requesting,
choosing,
timer,
showing,
ramirezing,
} from "../composables/game";
import Field from "./Field.vue";
import { computed } from "vue";
import Card from "./Card.vue";
Expand Down Expand Up @@ -92,7 +102,7 @@ function skip_select_card() {
<div
class="absolute top-0 left-0 font-bold w-full h-full flex justify-center items-center text-white text-center text-3xl text-shadow-md"
>
{{ game.deck_size || "?" }}
{{ game.deck_size ?? "?" }}
</div>
</div>

Expand All @@ -102,6 +112,8 @@ function skip_select_card() {
:card="discard_top"
></Card>

<Card class="absolute w-24 right-[42%] top-[30%]" v-if="showing" :card="showing"></Card>

<div
v-if="requesting"
class="absolute bottom-0 right-0 m-8 w-24 h-10 bg-indigo-600/80 rounded-md flex justify-center items-center cursor-pointer text-white hover:bg-indigo-700 transition-all"
Expand All @@ -110,6 +122,31 @@ function skip_select_card() {
<span>Skip</span>
</div>

<div v-if="ramirezing" class="absolute bottom-0 right-0 m-8 flex">
<div
class="w-24 h-10 bg-indigo-500 rounded-l-md flex justify-center items-center cursor-pointer text-white hover:bg-indigo-700 transition-all"
@click="
() => {
send('yes_no', { 'y/n': 1 });
ramirezing = false;
}
"
>
YES
</div>
<div
class="w-24 h-10 bg-indigo-500 rounded-r-md flex justify-center items-center cursor-pointer text-white hover:bg-indigo-700 transition-all"
@click="
() => {
send('yes_no', { 'y/n': 0 });
ramirezing = false;
}
"
>
NO
</div>
</div>

<Fade>
<div
v-if="(selecting.length || requesting || choosing) && timer"
Expand Down
22 changes: 22 additions & 0 deletions web/src/composables/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ export const requesting = ref(false);
export const choosing = ref(false);
export const timer = ref(0);
export const winner = ref(-1);
export const showing = ref<Card | null>(null);
export const ramirezing = ref(false);
let timer_id: number;
let show_timer_id: number;

ws.addEventListener("open", () => {
state.value = 0;
Expand Down Expand Up @@ -88,6 +91,17 @@ ws.addEventListener("message", (event) => {
timer_start();
break;

case "show card":
Object.assign(game, message.payload.game);
show_card(message.payload.card);
break;

case "ramirez":
Object.assign(game, message.payload.game);
logs.push({ type: "chat", message: "Do you want to use Ramirez's skill?" });
ramirezing.value = true;
break;

case "end":
Object.assign(game, message.payload.game);
winner.value = message.payload.winner;
Expand Down Expand Up @@ -123,3 +137,11 @@ function timer_interval() {
clearInterval(timer_id);
}
}

export function show_card(card: Card) {
show_timer_id && clearInterval(show_timer_id);
show_timer_id = window.setInterval(() => {
showing.value = null;
}, 3_000);
showing.value = card;
}

0 comments on commit 97ba350

Please sign in to comment.