The server for Open Course World
1package dyna
2
3import (
4 "context"
5 "smm2_gameserver/nex/datastore"
6
7 "github.com/jackc/pgx/v4"
8)
9
10func (s *DB) UpdateCourseInfoPlayStats(result PlayResult, uploaderPid datastore.Pid) error {
11 clears := 0
12 if result.Time.Valid {
13 clears = 1
14 }
15 _, err := s.Conn.Exec(context.Background(), `
16 INSERT INTO course (id, data_id, plays, tries, deaths, clears) VALUES (DEFAULT, $1, 1, $2, $3, $4)
17 ON CONFLICT (data_id) DO UPDATE SET plays = course.plays + 1, tries = course.tries + $2, deaths = course.deaths + $3, clears = course.clears + $4, clear_rate = (course.clears::float / COALESCE(NULLIF(course.tries,0), 1)::float)
18 `, result.DataId, result.Tries, result.Deaths, clears)
19 if err != nil {
20 return err
21 }
22
23 _, err = s.Conn.Exec(context.Background(), "UPDATE user_info SET maker_points = user_info.maker_points + 1 WHERE id = $1", uploaderPid)
24 return err
25}
26
27func (s *DB) UpdateCourseInfoRanking(dataId int64, isLike bool, uploaderPid datastore.Pid) error {
28 var like, boo int
29 if isLike {
30 like = 1
31 _, err := s.Conn.Exec(context.Background(), "UPDATE user_info SET maker_points = user_info.maker_points + 1, maker_likes = user_info.maker_likes + 1 WHERE id = $1", uploaderPid)
32 if err != nil {
33 return err
34 }
35 } else {
36 boo = 1
37 _, err := s.Conn.Exec(context.Background(), "UPDATE user_info SET maker_points = user_info.maker_points - 1 WHERE id = $1", uploaderPid)
38 if err != nil {
39 return err
40 }
41 }
42
43 _, err := s.Conn.Exec(context.Background(), `
44 INSERT INTO course (id, data_id, likes, boos) VALUES (DEFAULT, $1, $2, $3)
45 ON CONFLICT (data_id) DO UPDATE SET likes = course.likes + $2, boos = course.boos + $3, like_ratio = (course.likes::float / COALESCE(NULLIF(course.plays,0), 1)::float), boo_ratio = (course.boos::float / COALESCE(NULLIF(course.plays,0), 1)::float)
46 `, dataId, like, boo)
47 return err
48}
49
50func (s *DB) GetCourseInfoPlayStatsAndRatings(dataId int64) (int, int, int, int, int, int, error) {
51 row := s.Conn.QueryRow(context.Background(), `SELECT plays, tries, deaths, clears, likes, boos FROM course WHERE data_id = $1`, dataId)
52 var plays int
53 var tries int
54 var deaths int
55 var clears int
56 var likes int
57 var boos int
58 err := row.Scan(&plays, &tries, &deaths, &clears, &likes, &boos)
59 if err == pgx.ErrNoRows {
60 // Send back default info
61 plays = 0
62 tries = 0
63 deaths = 0
64 clears = 0
65 likes = 0
66 boos = 0
67 err = nil
68 }
69
70 return plays, tries, deaths, clears, likes, boos, err
71}
72
73func (s *DB) UpdateCourseTag(current_pid datastore.Pid, dataId int64, tag1 uint8, tag2 uint8) error {
74 _, err := s.Conn.Exec(context.Background(), "UPDATE course SET tag1 = $2, tag2 = $3 WHERE data_id = $1", dataId, tag1, tag2)
75 return err
76}