Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit e3a62a8

Browse files
committed
implement gateway.HandleDeath and gateway.HandleEnd
1 parent efc59ff commit e3a62a8

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

‎http/gateway/handler.go‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package gateway
22

33
import (
44
"fmt"
5+
"github.com/guregu/null"
56
"math/rand"
67
"sync"
78
"time"
@@ -192,6 +193,7 @@ func handleClose(c *WebSocketConnection) {
192193
// TODO: optimize this
193194
r := room.FindLobbyByWebsocketID(c.ID)
194195
if r != nil {
196+
// TODO: Store travelled distance in DB if player is not a guest
195197
r.RemovePlayer(r.GetPlayerIndexByWebSocketID(c.ID))
196198
}
197199
}
@@ -236,6 +238,10 @@ func (c *WebSocketConnection) HandleAntiCheatFlags(r *room.Room, flags int) bool
236238
return false
237239
}
238240

241+
// sadly we have to define these functions in this package
242+
// because go doesn't like import cycles
243+
// (room functions that interact with anything websocket related)
244+
239245
func BroadcastMessage(r *room.Room, d AnyMessage) {
240246
for _, p := range r.Players {
241247
conn, ok := connections[p.ID]
@@ -284,3 +290,71 @@ func StartEliminationRoom(r *room.Room) {
284290
},
285291
})
286292
}
293+
294+
func HandleDeath(r *room.Room, loser *player.Player, winner *player.Player) {
295+
placement := len(r.Players) - 1
296+
297+
coinGain := room.GetRewardForPlacement(room.CoinRewardType, placement)
298+
brGain := room.GetRewardForPlacement(room.BRRewardType, placement)
299+
300+
err := loser.Update(brGain, coinGain, room.DefaultXPGain)
301+
if err != nil {
302+
fmt.Println(err)
303+
}
304+
305+
conn, ok := connections[loser.ID]
306+
if !ok {
307+
return
308+
}
309+
310+
// TODO: loser is kicked instead of shown the result screen
311+
conn.Send(AnyMessage {
312+
Op: OpClose,
313+
T: PlayerKickEvent,
314+
Data: map[string]interface{} {
315+
"type": EliminationKick,
316+
"result": brGain,
317+
"coinChange": coinGain,
318+
"noms": loser.Noms,
319+
"message": "You were nommed by " + winner.Username,
320+
},
321+
})
322+
323+
handleClose(conn)
324+
HandleEnd(r)
325+
}
326+
327+
func HandleEnd(r *room.Room) {
328+
if !r.IsSingle() || r.State != room.IngameState {
329+
return
330+
}
331+
332+
winner := r.Players[0]
333+
brGain := room.GetRewardForPlacement(room.BRRewardType, 0)
334+
coinGain := room.GetRewardForPlacement(room.CoinRewardType, 0)
335+
winner.Update(brGain, coinGain, room.WinXPGain)
336+
337+
conn, ok := connections[winner.ID]
338+
if !ok {
339+
return
340+
}
341+
342+
// TODO: winner is kicked without a reason
343+
// instead, don't use OpClose and send it as event instead
344+
conn.Send(AnyMessage{
345+
Op: OpClose,
346+
T: PlayerKickEvent,
347+
Data: map[string]interface{} {
348+
"type": WinKick,
349+
"result": brGain,
350+
"noms": winner.Noms,
351+
"message": null.NewString("", false),
352+
},
353+
})
354+
355+
handleClose(conn)
356+
357+
// TODO: delete room and create new room
358+
// or alternatively, create a reset function on room struct
359+
// that sets the state to waiting and other prep stuff
360+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /